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