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