c01106b5c42091506cbd40be56ac2b92658876db
[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 /*
53  * MPALMOSTSAFE
54  */
55 int
56 sys_linux_chown16(struct linux_chown16_args *args)
57 {
58         struct nlookupdata nd;
59         char *path;
60         int error;
61
62         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
63         if (error)
64                 return (error);
65 #ifdef DEBUG
66         if (ldebug(chown16))
67                 kprintf(ARGS(chown16, "%s, %d, %d"), path, args->uid,
68                     args->gid);
69 #endif
70         get_mplock();
71         error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
72         if (error == 0) {
73                 error = kern_chown(&nd, CAST_NOCHG(args->uid),
74                                     CAST_NOCHG(args->gid));
75         }
76         nlookup_done(&nd);
77         rel_mplock();
78         linux_free_path(&path);
79         return(error);
80 }
81
82 /*
83  * MPALMOSTSAFE
84  */
85 int
86 sys_linux_lchown16(struct linux_lchown16_args *args)
87 {
88         struct nlookupdata nd;
89         char *path;
90         int error;
91
92         error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
93         if (error)
94                 return (error);
95 #ifdef DEBUG
96         if (ldebug(lchown16))
97                 kprintf(ARGS(lchown16, "%s, %d, %d"), path, args->uid,
98                     args->gid);
99 #endif
100         get_mplock();
101         error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
102         if (error == 0) {
103                 error = kern_chown(&nd, CAST_NOCHG(args->uid),
104                                     CAST_NOCHG(args->gid));
105         }
106         nlookup_done(&nd);
107         rel_mplock();
108         linux_free_path(&path);
109         return(error);
110 }
111
112 /*
113  * MPALMOSTSAFE
114  */
115 int
116 sys_linux_setgroups16(struct linux_setgroups16_args *args)
117 {
118         struct proc *p = curproc;
119         struct ucred *newcred, *oldcred;
120         l_gid16_t linux_gidset[NGROUPS];
121         gid_t *bsd_gidset;
122         int ngrp, error;
123
124 #ifdef DEBUG
125         if (ldebug(setgroups16))
126                 kprintf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
127 #endif
128
129         ngrp = args->gidsetsize;
130         oldcred = p->p_ucred;
131
132         /*
133          * cr_groups[0] holds egid. Setting the whole set from
134          * the supplied set will cause egid to be changed too.
135          * Keep cr_groups[0] unchanged to prevent that.
136          */
137
138         if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0)
139                 return (error);
140
141         if ((u_int)ngrp >= NGROUPS)
142                 return (EINVAL);
143
144         get_mplock();
145         newcred = crdup(oldcred);
146         if (ngrp > 0) {
147                 error = copyin((caddr_t)args->gidset, linux_gidset,
148                                ngrp * sizeof(l_gid16_t));
149                 if (error) {
150                         crfree(newcred);
151                         goto done;
152                 }
153
154                 newcred->cr_ngroups = ngrp + 1;
155
156                 bsd_gidset = newcred->cr_groups;
157                 ngrp--;
158                 while (ngrp >= 0) {
159                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
160                         ngrp--;
161                 }
162         } else {
163                 newcred->cr_ngroups = 1;
164         }
165
166         setsugid();
167         p->p_ucred = newcred;
168         crfree(oldcred);
169         error = 0;
170 done:
171         rel_mplock();
172         return (error);
173 }
174
175 /*
176  * MPSAFE
177  */
178 int
179 sys_linux_getgroups16(struct linux_getgroups16_args *args)
180 {
181         struct proc *p = curproc;
182         struct ucred *cred;
183         l_gid16_t linux_gidset[NGROUPS];
184         gid_t *bsd_gidset;
185         int bsd_gidsetsz, ngrp, error;
186
187 #ifdef DEBUG
188         if (ldebug(getgroups16))
189                 kprintf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
190 #endif
191
192         cred = p->p_ucred;
193         bsd_gidset = cred->cr_groups;
194         bsd_gidsetsz = cred->cr_ngroups - 1;
195
196         /*
197          * cr_groups[0] holds egid. Returning the whole set
198          * here will cause a duplicate. Exclude cr_groups[0]
199          * to prevent that.
200          */
201
202         if ((ngrp = args->gidsetsize) == 0) {
203                 args->sysmsg_result = bsd_gidsetsz;
204                 return (0);
205         }
206
207         if ((u_int)ngrp < (u_int)bsd_gidsetsz)
208                 return (EINVAL);
209
210         ngrp = 0;
211         while (ngrp < bsd_gidsetsz) {
212                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
213                 ngrp++;
214         }
215
216         error = copyout(linux_gidset, (caddr_t)args->gidset,
217                         ngrp * sizeof(l_gid16_t));
218         if (error)
219                 return (error);
220
221         args->sysmsg_result = ngrp;
222         return (0);
223 }
224
225 /*
226  * The FreeBSD native getgid(2) and getuid(2) also modify p->p_retval[1]
227  * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that
228  * are assumed to be preserved. The following lightweight syscalls fixes
229  * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in
230  * linux_misc.c
231  *
232  * linux_getgid16() - MP SAFE
233  * linux_getuid16() - MP SAFE
234  */
235
236 /*
237  * MPSAFE
238  */
239 int
240 sys_linux_getgid16(struct linux_getgid16_args *args)
241 {
242         struct proc *p = curproc;
243
244         args->sysmsg_result = p->p_ucred->cr_rgid;
245         return (0);
246 }
247
248 /*
249  * MPSAFE
250  */
251 int
252 sys_linux_getuid16(struct linux_getuid16_args *args)
253 {
254         struct proc *p = curproc;
255
256         args->sysmsg_result = p->p_ucred->cr_ruid;
257         return (0);
258 }
259
260 /*
261  * MPSAFE
262  */
263 int
264 sys_linux_getegid16(struct linux_getegid16_args *args)
265 {
266         struct getegid_args bsd;
267         int error;
268
269         bsd.sysmsg_result = 0;
270
271         error = sys_getegid(&bsd);
272         args->sysmsg_result = bsd.sysmsg_result;
273         return(error);
274 }
275
276 /*
277  * MPSAFE
278  */
279 int
280 sys_linux_geteuid16(struct linux_geteuid16_args *args)
281 {
282         struct geteuid_args bsd;
283         int error;
284
285         bsd.sysmsg_result = 0;
286
287         error = sys_geteuid(&bsd);
288         args->sysmsg_result = bsd.sysmsg_result;
289         return(error);
290 }
291
292 /*
293  * MPSAFE
294  */
295 int
296 sys_linux_setgid16(struct linux_setgid16_args *args)
297 {
298         struct setgid_args bsd;
299         int error;
300
301         bsd.gid = args->gid;
302         bsd.sysmsg_result = 0;
303
304         error = sys_setgid(&bsd);
305         args->sysmsg_result = bsd.sysmsg_result;
306         return(error);
307 }
308
309 /*
310  * MPSAFE
311  */
312 int
313 sys_linux_setuid16(struct linux_setuid16_args *args)
314 {
315         struct setuid_args bsd;
316         int error;
317
318         bsd.uid = args->uid;
319         bsd.sysmsg_result = 0;
320
321         error = sys_setuid(&bsd);
322         args->sysmsg_result = bsd.sysmsg_result;
323         return(error);
324 }
325
326 /*
327  * MPSAFE
328  */
329 int
330 sys_linux_setregid16(struct linux_setregid16_args *args)
331 {
332         struct setregid_args bsd;
333         int error;
334
335         bsd.rgid = CAST_NOCHG(args->rgid);
336         bsd.egid = CAST_NOCHG(args->egid);
337         bsd.sysmsg_result = 0;
338
339         error = sys_setregid(&bsd);
340         args->sysmsg_result = bsd.sysmsg_result;
341         return(error);
342 }
343
344 /*
345  * MPSAFE
346  */
347 int
348 sys_linux_setreuid16(struct linux_setreuid16_args *args)
349 {
350         struct setreuid_args bsd;
351         int error;
352
353         bsd.ruid = CAST_NOCHG(args->ruid);
354         bsd.euid = CAST_NOCHG(args->euid);
355         bsd.sysmsg_result = 0;
356
357         error = sys_setreuid(&bsd);
358         args->sysmsg_result = bsd.sysmsg_result;
359         return(error);
360 }
361
362 /*
363  * MPSAFE
364  */
365 int
366 sys_linux_setresgid16(struct linux_setresgid16_args *args)
367 {
368         struct setresgid_args bsd;
369         int error;
370
371         bsd.rgid = CAST_NOCHG(args->rgid);
372         bsd.egid = CAST_NOCHG(args->egid);
373         bsd.sgid = CAST_NOCHG(args->sgid);
374         bsd.sysmsg_result = 0;
375
376         error = sys_setresgid(&bsd);
377         args->sysmsg_result = bsd.sysmsg_result;
378         return(error);
379 }
380
381 /*
382  * MPSAFE
383  */
384 int
385 sys_linux_setresuid16(struct linux_setresuid16_args *args)
386 {
387         struct setresuid_args bsd;
388         int error;
389
390         bsd.ruid = CAST_NOCHG(args->ruid);
391         bsd.euid = CAST_NOCHG(args->euid);
392         bsd.suid = CAST_NOCHG(args->suid);
393         bsd.sysmsg_result = 0;
394
395         error = sys_setresuid(&bsd);
396         args->sysmsg_result = bsd.sysmsg_result;
397         return(error);
398 }
399