Merge branch 'vendor/GDTOA'
[dragonfly.git] / sys / emulation / linux / linux_uid16.c
1 /*-
2  * Copyright (c) 2001  The FreeBSD Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/compat/linux/linux_uid16.c,v 1.4.2.1 2001/10/21 03:57:35 marcel Exp $
27  * $DragonFly: src/sys/emulation/linux/linux_uid16.c,v 1.12 2006/12/23 00:27:02 swildner Exp $
28  */
29
30 #include "opt_compat.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kern_syscall.h>
35 #include <sys/nlookup.h>
36 #include <sys/proc.h>
37 #include <sys/priv.h>
38 #include <sys/sysproto.h>
39 #include <sys/thread.h>
40
41 #include <arch_linux/linux.h>
42 #include <arch_linux/linux_proto.h>
43 #include "linux_util.h"
44
45 DUMMY(setfsuid16);
46 DUMMY(setfsgid16);
47 DUMMY(getresuid16);
48 DUMMY(getresgid16);
49
50 #define CAST_NOCHG(x)   ((x == 0xFFFF) ? -1 : x)
51
52 int
53 sys_linux_chown16(struct linux_chown16_args *args)
54 {
55         struct nlookupdata nd;
56         char *path;
57         int error;
58
59         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
60         if (error)
61                 return (error);
62 #ifdef DEBUG
63         if (ldebug(chown16))
64                 kprintf(ARGS(chown16, "%s, %d, %d"), path, args->uid,
65                     args->gid);
66 #endif
67         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
68         if (error == 0) {
69                 error = kern_chown(&nd, CAST_NOCHG(args->uid),
70                                     CAST_NOCHG(args->gid));
71         }
72         nlookup_done(&nd);
73         linux_free_path(&path);
74         return(error);
75 }
76
77 int
78 sys_linux_lchown16(struct linux_lchown16_args *args)
79 {
80         struct nlookupdata nd;
81         char *path;
82         int error;
83
84         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
85         if (error)
86                 return (error);
87 #ifdef DEBUG
88         if (ldebug(lchown16))
89                 kprintf(ARGS(lchown16, "%s, %d, %d"), path, args->uid,
90                     args->gid);
91 #endif
92         error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
93         if (error == 0) {
94                 error = kern_chown(&nd, CAST_NOCHG(args->uid),
95                                     CAST_NOCHG(args->gid));
96         }
97         nlookup_done(&nd);
98         linux_free_path(&path);
99         return(error);
100 }
101
102 int
103 sys_linux_setgroups16(struct linux_setgroups16_args *args)
104 {
105         struct proc *p = curproc;
106         struct ucred *newcred, *oldcred;
107         l_gid16_t linux_gidset[NGROUPS];
108         gid_t *bsd_gidset;
109         int ngrp, error;
110
111 #ifdef DEBUG
112         if (ldebug(setgroups16))
113                 kprintf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
114 #endif
115
116         ngrp = args->gidsetsize;
117         oldcred = p->p_ucred;
118
119         /*
120          * cr_groups[0] holds egid. Setting the whole set from
121          * the supplied set will cause egid to be changed too.
122          * Keep cr_groups[0] unchanged to prevent that.
123          */
124
125         if ((error = priv_check_cred(oldcred, PRIV_ROOT, PRISON_ROOT)) != 0)
126                 return (error);
127
128         if (ngrp >= NGROUPS)
129                 return (EINVAL);
130
131         newcred = crdup(oldcred);
132         if (ngrp > 0) {
133                 error = copyin((caddr_t)args->gidset, linux_gidset,
134                     ngrp * sizeof(l_gid16_t));
135                 if (error)
136                         return (error);
137
138                 newcred->cr_ngroups = ngrp + 1;
139
140                 bsd_gidset = newcred->cr_groups;
141                 ngrp--;
142                 while (ngrp >= 0) {
143                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
144                         ngrp--;
145                 }
146         }
147         else
148                 newcred->cr_ngroups = 1;
149
150         setsugid();
151         p->p_ucred = newcred;
152         crfree(oldcred);
153         return (0);
154 }
155
156 int
157 sys_linux_getgroups16(struct linux_getgroups16_args *args)
158 {
159         struct proc *p = curproc;
160         struct ucred *cred;
161         l_gid16_t linux_gidset[NGROUPS];
162         gid_t *bsd_gidset;
163         int bsd_gidsetsz, ngrp, error;
164
165 #ifdef DEBUG
166         if (ldebug(getgroups16))
167                 kprintf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
168 #endif
169
170         cred = p->p_ucred;
171         bsd_gidset = cred->cr_groups;
172         bsd_gidsetsz = cred->cr_ngroups - 1;
173
174         /*
175          * cr_groups[0] holds egid. Returning the whole set
176          * here will cause a duplicate. Exclude cr_groups[0]
177          * to prevent that.
178          */
179
180         if ((ngrp = args->gidsetsize) == 0) {
181                 args->sysmsg_result = bsd_gidsetsz;
182                 return (0);
183         }
184
185         if (ngrp < bsd_gidsetsz)
186                 return (EINVAL);
187
188         ngrp = 0;
189         while (ngrp < bsd_gidsetsz) {
190                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
191                 ngrp++;
192         }
193
194         error = copyout(linux_gidset, (caddr_t)args->gidset,
195             ngrp * sizeof(l_gid16_t));
196         if (error)
197                 return (error);
198
199         args->sysmsg_result = ngrp;
200         return (0);
201 }
202
203 /*
204  * The FreeBSD native getgid(2) and getuid(2) also modify p->p_retval[1]
205  * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that
206  * are assumed to be preserved. The following lightweight syscalls fixes
207  * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in
208  * linux_misc.c
209  *
210  * linux_getgid16() - MP SAFE
211  * linux_getuid16() - MP SAFE
212  */
213
214 int
215 sys_linux_getgid16(struct linux_getgid16_args *args)
216 {
217         struct proc *p = curproc;
218
219         args->sysmsg_result = p->p_ucred->cr_rgid;
220         return (0);
221 }
222
223 int
224 sys_linux_getuid16(struct linux_getuid16_args *args)
225 {
226         struct proc *p = curproc;
227
228         args->sysmsg_result = p->p_ucred->cr_ruid;
229         return (0);
230 }
231
232 int
233 sys_linux_getegid16(struct linux_getegid16_args *args)
234 {
235         struct getegid_args bsd;
236         int error;
237
238         bsd.sysmsg_result = 0;
239
240         error = sys_getegid(&bsd);
241         args->sysmsg_result = bsd.sysmsg_result;
242         return(error);
243 }
244
245 int
246 sys_linux_geteuid16(struct linux_geteuid16_args *args)
247 {
248         struct geteuid_args bsd;
249         int error;
250
251         bsd.sysmsg_result = 0;
252
253         error = sys_geteuid(&bsd);
254         args->sysmsg_result = bsd.sysmsg_result;
255         return(error);
256 }
257
258 int
259 sys_linux_setgid16(struct linux_setgid16_args *args)
260 {
261         struct setgid_args bsd;
262         int error;
263
264         bsd.gid = args->gid;
265         bsd.sysmsg_result = 0;
266
267         error = sys_setgid(&bsd);
268         args->sysmsg_result = bsd.sysmsg_result;
269         return(error);
270 }
271
272 int
273 sys_linux_setuid16(struct linux_setuid16_args *args)
274 {
275         struct setuid_args bsd;
276         int error;
277
278         bsd.uid = args->uid;
279         bsd.sysmsg_result = 0;
280
281         error = sys_setuid(&bsd);
282         args->sysmsg_result = bsd.sysmsg_result;
283         return(error);
284 }
285
286 int
287 sys_linux_setregid16(struct linux_setregid16_args *args)
288 {
289         struct setregid_args bsd;
290         int error;
291
292         bsd.rgid = CAST_NOCHG(args->rgid);
293         bsd.egid = CAST_NOCHG(args->egid);
294         bsd.sysmsg_result = 0;
295
296         error = sys_setregid(&bsd);
297         args->sysmsg_result = bsd.sysmsg_result;
298         return(error);
299 }
300
301 int
302 sys_linux_setreuid16(struct linux_setreuid16_args *args)
303 {
304         struct setreuid_args bsd;
305         int error;
306
307         bsd.ruid = CAST_NOCHG(args->ruid);
308         bsd.euid = CAST_NOCHG(args->euid);
309         bsd.sysmsg_result = 0;
310
311         error = sys_setreuid(&bsd);
312         args->sysmsg_result = bsd.sysmsg_result;
313         return(error);
314 }
315
316 int
317 sys_linux_setresgid16(struct linux_setresgid16_args *args)
318 {
319         struct setresgid_args bsd;
320         int error;
321
322         bsd.rgid = CAST_NOCHG(args->rgid);
323         bsd.egid = CAST_NOCHG(args->egid);
324         bsd.sgid = CAST_NOCHG(args->sgid);
325         bsd.sysmsg_result = 0;
326
327         error = sys_setresgid(&bsd);
328         args->sysmsg_result = bsd.sysmsg_result;
329         return(error);
330 }
331
332 int
333 sys_linux_setresuid16(struct linux_setresuid16_args *args)
334 {
335         struct setresuid_args bsd;
336         int error;
337
338         bsd.ruid = CAST_NOCHG(args->ruid);
339         bsd.euid = CAST_NOCHG(args->euid);
340         bsd.suid = CAST_NOCHG(args->suid);
341         bsd.sysmsg_result = 0;
342
343         error = sys_setresuid(&bsd);
344         args->sysmsg_result = bsd.sysmsg_result;
345         return(error);
346 }
347