Merge branch 'vendor/BIND' into bind_vendor2
[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_CRED_SETGROUPS, 0)) != 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                         crfree(newcred);
137                         return (error);
138                 }
139
140                 newcred->cr_ngroups = ngrp + 1;
141
142                 bsd_gidset = newcred->cr_groups;
143                 ngrp--;
144                 while (ngrp >= 0) {
145                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
146                         ngrp--;
147                 }
148         } else {
149                 newcred->cr_ngroups = 1;
150         }
151
152         setsugid();
153         p->p_ucred = newcred;
154         crfree(oldcred);
155         return (0);
156 }
157
158 int
159 sys_linux_getgroups16(struct linux_getgroups16_args *args)
160 {
161         struct proc *p = curproc;
162         struct ucred *cred;
163         l_gid16_t linux_gidset[NGROUPS];
164         gid_t *bsd_gidset;
165         int bsd_gidsetsz, ngrp, error;
166
167 #ifdef DEBUG
168         if (ldebug(getgroups16))
169                 kprintf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
170 #endif
171
172         cred = p->p_ucred;
173         bsd_gidset = cred->cr_groups;
174         bsd_gidsetsz = cred->cr_ngroups - 1;
175
176         /*
177          * cr_groups[0] holds egid. Returning the whole set
178          * here will cause a duplicate. Exclude cr_groups[0]
179          * to prevent that.
180          */
181
182         if ((ngrp = args->gidsetsize) == 0) {
183                 args->sysmsg_result = bsd_gidsetsz;
184                 return (0);
185         }
186
187         if (ngrp < bsd_gidsetsz)
188                 return (EINVAL);
189
190         ngrp = 0;
191         while (ngrp < bsd_gidsetsz) {
192                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
193                 ngrp++;
194         }
195
196         error = copyout(linux_gidset, (caddr_t)args->gidset,
197             ngrp * sizeof(l_gid16_t));
198         if (error)
199                 return (error);
200
201         args->sysmsg_result = ngrp;
202         return (0);
203 }
204
205 /*
206  * The FreeBSD native getgid(2) and getuid(2) also modify p->p_retval[1]
207  * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that
208  * are assumed to be preserved. The following lightweight syscalls fixes
209  * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in
210  * linux_misc.c
211  *
212  * linux_getgid16() - MP SAFE
213  * linux_getuid16() - MP SAFE
214  */
215
216 int
217 sys_linux_getgid16(struct linux_getgid16_args *args)
218 {
219         struct proc *p = curproc;
220
221         args->sysmsg_result = p->p_ucred->cr_rgid;
222         return (0);
223 }
224
225 int
226 sys_linux_getuid16(struct linux_getuid16_args *args)
227 {
228         struct proc *p = curproc;
229
230         args->sysmsg_result = p->p_ucred->cr_ruid;
231         return (0);
232 }
233
234 int
235 sys_linux_getegid16(struct linux_getegid16_args *args)
236 {
237         struct getegid_args bsd;
238         int error;
239
240         bsd.sysmsg_result = 0;
241
242         error = sys_getegid(&bsd);
243         args->sysmsg_result = bsd.sysmsg_result;
244         return(error);
245 }
246
247 int
248 sys_linux_geteuid16(struct linux_geteuid16_args *args)
249 {
250         struct geteuid_args bsd;
251         int error;
252
253         bsd.sysmsg_result = 0;
254
255         error = sys_geteuid(&bsd);
256         args->sysmsg_result = bsd.sysmsg_result;
257         return(error);
258 }
259
260 int
261 sys_linux_setgid16(struct linux_setgid16_args *args)
262 {
263         struct setgid_args bsd;
264         int error;
265
266         bsd.gid = args->gid;
267         bsd.sysmsg_result = 0;
268
269         error = sys_setgid(&bsd);
270         args->sysmsg_result = bsd.sysmsg_result;
271         return(error);
272 }
273
274 int
275 sys_linux_setuid16(struct linux_setuid16_args *args)
276 {
277         struct setuid_args bsd;
278         int error;
279
280         bsd.uid = args->uid;
281         bsd.sysmsg_result = 0;
282
283         error = sys_setuid(&bsd);
284         args->sysmsg_result = bsd.sysmsg_result;
285         return(error);
286 }
287
288 int
289 sys_linux_setregid16(struct linux_setregid16_args *args)
290 {
291         struct setregid_args bsd;
292         int error;
293
294         bsd.rgid = CAST_NOCHG(args->rgid);
295         bsd.egid = CAST_NOCHG(args->egid);
296         bsd.sysmsg_result = 0;
297
298         error = sys_setregid(&bsd);
299         args->sysmsg_result = bsd.sysmsg_result;
300         return(error);
301 }
302
303 int
304 sys_linux_setreuid16(struct linux_setreuid16_args *args)
305 {
306         struct setreuid_args bsd;
307         int error;
308
309         bsd.ruid = CAST_NOCHG(args->ruid);
310         bsd.euid = CAST_NOCHG(args->euid);
311         bsd.sysmsg_result = 0;
312
313         error = sys_setreuid(&bsd);
314         args->sysmsg_result = bsd.sysmsg_result;
315         return(error);
316 }
317
318 int
319 sys_linux_setresgid16(struct linux_setresgid16_args *args)
320 {
321         struct setresgid_args bsd;
322         int error;
323
324         bsd.rgid = CAST_NOCHG(args->rgid);
325         bsd.egid = CAST_NOCHG(args->egid);
326         bsd.sgid = CAST_NOCHG(args->sgid);
327         bsd.sysmsg_result = 0;
328
329         error = sys_setresgid(&bsd);
330         args->sysmsg_result = bsd.sysmsg_result;
331         return(error);
332 }
333
334 int
335 sys_linux_setresuid16(struct linux_setresuid16_args *args)
336 {
337         struct setresuid_args bsd;
338         int error;
339
340         bsd.ruid = CAST_NOCHG(args->ruid);
341         bsd.euid = CAST_NOCHG(args->euid);
342         bsd.suid = CAST_NOCHG(args->suid);
343         bsd.sysmsg_result = 0;
344
345         error = sys_setresuid(&bsd);
346         args->sysmsg_result = bsd.sysmsg_result;
347         return(error);
348 }
349