direct sysargs to /dev/null for the emualtion system call sets to avoid a
[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.10 2004/11/12 00:09:18 dillon 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/sysproto.h>
38 #include <sys/thread.h>
39
40 #include <arch_linux/linux.h>
41 #include <arch_linux/linux_proto.h>
42 #include "linux_util.h"
43
44 DUMMY(setfsuid16);
45 DUMMY(setfsgid16);
46 DUMMY(getresuid16);
47 DUMMY(getresgid16);
48
49 #define CAST_NOCHG(x)   ((x == 0xFFFF) ? -1 : x)
50
51 int
52 linux_chown16(struct linux_chown16_args *args)
53 {
54         struct nlookupdata nd;
55         char *path;
56         int error;
57
58         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
59         if (error)
60                 return (error);
61 #ifdef DEBUG
62         if (ldebug(chown16))
63                 printf(ARGS(chown16, "%s, %d, %d"), path, args->uid,
64                     args->gid);
65 #endif
66         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
67         if (error == 0) {
68                 error = kern_chown(&nd, CAST_NOCHG(args->uid),
69                                     CAST_NOCHG(args->gid));
70         }
71         nlookup_done(&nd);
72         linux_free_path(&path);
73         return(error);
74 }
75
76 int
77 linux_lchown16(struct linux_lchown16_args *args)
78 {
79         struct nlookupdata nd;
80         char *path;
81         int error;
82
83         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
84         if (error)
85                 return (error);
86 #ifdef DEBUG
87         if (ldebug(lchown16))
88                 printf(ARGS(lchown16, "%s, %d, %d"), path, args->uid,
89                     args->gid);
90 #endif
91         error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
92         if (error == 0) {
93                 error = kern_chown(&nd, CAST_NOCHG(args->uid),
94                                     CAST_NOCHG(args->gid));
95         }
96         nlookup_done(&nd);
97         linux_free_path(&path);
98         return(error);
99 }
100
101 int
102 linux_setgroups16(struct linux_setgroups16_args *args)
103 {
104         struct proc *p = curproc;
105         struct ucred *newcred, *oldcred;
106         l_gid16_t linux_gidset[NGROUPS];
107         gid_t *bsd_gidset;
108         int ngrp, error;
109
110 #ifdef DEBUG
111         if (ldebug(setgroups16))
112                 printf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
113 #endif
114
115         ngrp = args->gidsetsize;
116         oldcred = p->p_ucred;
117
118         /*
119          * cr_groups[0] holds egid. Setting the whole set from
120          * the supplied set will cause egid to be changed too.
121          * Keep cr_groups[0] unchanged to prevent that.
122          */
123
124         if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
125                 return (error);
126
127         if (ngrp >= NGROUPS)
128                 return (EINVAL);
129
130         newcred = crdup(oldcred);
131         if (ngrp > 0) {
132                 error = copyin((caddr_t)args->gidset, linux_gidset,
133                     ngrp * sizeof(l_gid16_t));
134                 if (error)
135                         return (error);
136
137                 newcred->cr_ngroups = ngrp + 1;
138
139                 bsd_gidset = newcred->cr_groups;
140                 ngrp--;
141                 while (ngrp >= 0) {
142                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
143                         ngrp--;
144                 }
145         }
146         else
147                 newcred->cr_ngroups = 1;
148
149         setsugid();
150         p->p_ucred = newcred;
151         crfree(oldcred);
152         return (0);
153 }
154
155 int
156 linux_getgroups16(struct linux_getgroups16_args *args)
157 {
158         struct proc *p = curproc;
159         struct ucred *cred;
160         l_gid16_t linux_gidset[NGROUPS];
161         gid_t *bsd_gidset;
162         int bsd_gidsetsz, ngrp, error;
163
164 #ifdef DEBUG
165         if (ldebug(getgroups16))
166                 printf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
167 #endif
168
169         cred = p->p_ucred;
170         bsd_gidset = cred->cr_groups;
171         bsd_gidsetsz = cred->cr_ngroups - 1;
172
173         /*
174          * cr_groups[0] holds egid. Returning the whole set
175          * here will cause a duplicate. Exclude cr_groups[0]
176          * to prevent that.
177          */
178
179         if ((ngrp = args->gidsetsize) == 0) {
180                 args->sysmsg_result = bsd_gidsetsz;
181                 return (0);
182         }
183
184         if (ngrp < bsd_gidsetsz)
185                 return (EINVAL);
186
187         ngrp = 0;
188         while (ngrp < bsd_gidsetsz) {
189                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
190                 ngrp++;
191         }
192
193         error = copyout(linux_gidset, (caddr_t)args->gidset,
194             ngrp * sizeof(l_gid16_t));
195         if (error)
196                 return (error);
197
198         args->sysmsg_result = ngrp;
199         return (0);
200 }
201
202 /*
203  * The FreeBSD native getgid(2) and getuid(2) also modify p->p_retval[1]
204  * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that
205  * are assumed to be preserved. The following lightweight syscalls fixes
206  * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in
207  * linux_misc.c
208  *
209  * linux_getgid16() - MP SAFE
210  * linux_getuid16() - MP SAFE
211  */
212
213 int
214 linux_getgid16(struct linux_getgid16_args *args)
215 {
216         struct proc *p = curproc;
217
218         args->sysmsg_result = p->p_ucred->cr_rgid;
219         return (0);
220 }
221
222 int
223 linux_getuid16(struct linux_getuid16_args *args)
224 {
225         struct proc *p = curproc;
226
227         args->sysmsg_result = p->p_ucred->cr_ruid;
228         return (0);
229 }
230
231 int
232 linux_getegid16(struct linux_getegid16_args *args)
233 {
234         struct getegid_args bsd;
235         int error;
236
237         bsd.sysmsg_result = 0;
238
239         error = getegid(&bsd);
240         args->sysmsg_result = bsd.sysmsg_result;
241         return(error);
242 }
243
244 int
245 linux_geteuid16(struct linux_geteuid16_args *args)
246 {
247         struct geteuid_args bsd;
248         int error;
249
250         bsd.sysmsg_result = 0;
251
252         error = geteuid(&bsd);
253         args->sysmsg_result = bsd.sysmsg_result;
254         return(error);
255 }
256
257 int
258 linux_setgid16(struct linux_setgid16_args *args)
259 {
260         struct setgid_args bsd;
261         int error;
262
263         bsd.gid = args->gid;
264         bsd.sysmsg_result = 0;
265
266         error = setgid(&bsd);
267         args->sysmsg_result = bsd.sysmsg_result;
268         return(error);
269 }
270
271 int
272 linux_setuid16(struct linux_setuid16_args *args)
273 {
274         struct setuid_args bsd;
275         int error;
276
277         bsd.uid = args->uid;
278         bsd.sysmsg_result = 0;
279
280         error = setuid(&bsd);
281         args->sysmsg_result = bsd.sysmsg_result;
282         return(error);
283 }
284
285 int
286 linux_setregid16(struct linux_setregid16_args *args)
287 {
288         struct setregid_args bsd;
289         int error;
290
291         bsd.rgid = CAST_NOCHG(args->rgid);
292         bsd.egid = CAST_NOCHG(args->egid);
293         bsd.sysmsg_result = 0;
294
295         error = setregid(&bsd);
296         args->sysmsg_result = bsd.sysmsg_result;
297         return(error);
298 }
299
300 int
301 linux_setreuid16(struct linux_setreuid16_args *args)
302 {
303         struct setreuid_args bsd;
304         int error;
305
306         bsd.ruid = CAST_NOCHG(args->ruid);
307         bsd.euid = CAST_NOCHG(args->euid);
308         bsd.sysmsg_result = 0;
309
310         error = setreuid(&bsd);
311         args->sysmsg_result = bsd.sysmsg_result;
312         return(error);
313 }
314
315 int
316 linux_setresgid16(struct linux_setresgid16_args *args)
317 {
318         struct setresgid_args bsd;
319         int error;
320
321         bsd.rgid = CAST_NOCHG(args->rgid);
322         bsd.egid = CAST_NOCHG(args->egid);
323         bsd.sgid = CAST_NOCHG(args->sgid);
324         bsd.sysmsg_result = 0;
325
326         error = setresgid(&bsd);
327         args->sysmsg_result = bsd.sysmsg_result;
328         return(error);
329 }
330
331 int
332 linux_setresuid16(struct linux_setresuid16_args *args)
333 {
334         struct setresuid_args bsd;
335         int error;
336
337         bsd.ruid = CAST_NOCHG(args->ruid);
338         bsd.euid = CAST_NOCHG(args->euid);
339         bsd.suid = CAST_NOCHG(args->suid);
340         bsd.sysmsg_result = 0;
341
342         error = setresuid(&bsd);
343         args->sysmsg_result = bsd.sysmsg_result;
344         return(error);
345 }
346