2 * Copyright (c) 2001 The FreeBSD Project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.9 2003/11/13 04:04:42 daver Exp $
30 #include "opt_compat.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kern_syscall.h>
35 #include <sys/namei.h>
37 #include <sys/sysproto.h>
38 #include <sys/thread.h>
40 #include <arch_linux/linux.h>
41 #include <arch_linux/linux_proto.h>
42 #include "linux_util.h"
49 #define CAST_NOCHG(x) ((x == 0xFFFF) ? -1 : x)
52 linux_chown16(struct linux_chown16_args *args)
54 struct thread *td = curthread;
59 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
64 printf(ARGS(chown16, "%s, %d, %d"), path, args->uid,
67 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_SYSSPACE, path, td);
69 error = kern_chown(&nd, CAST_NOCHG(args->uid), CAST_NOCHG(args->gid));
71 linux_free_path(&path);
76 linux_lchown16(struct linux_lchown16_args *args)
78 struct thread *td = curthread;
83 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
88 printf(ARGS(lchown16, "%s, %d, %d"), path, args->uid,
91 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_SYSSPACE, path, td);
93 error = kern_chown(&nd, CAST_NOCHG(args->uid), CAST_NOCHG(args->gid));
95 linux_free_path(&path);
100 linux_setgroups16(struct linux_setgroups16_args *args)
102 struct proc *p = curproc;
103 struct ucred *newcred, *oldcred;
104 l_gid16_t linux_gidset[NGROUPS];
109 if (ldebug(setgroups16))
110 printf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
113 ngrp = args->gidsetsize;
114 oldcred = p->p_ucred;
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.
122 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
128 newcred = crdup(oldcred);
130 error = copyin((caddr_t)args->gidset, linux_gidset,
131 ngrp * sizeof(l_gid16_t));
135 newcred->cr_ngroups = ngrp + 1;
137 bsd_gidset = newcred->cr_groups;
140 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
145 newcred->cr_ngroups = 1;
148 p->p_ucred = newcred;
154 linux_getgroups16(struct linux_getgroups16_args *args)
156 struct proc *p = curproc;
158 l_gid16_t linux_gidset[NGROUPS];
160 int bsd_gidsetsz, ngrp, error;
163 if (ldebug(getgroups16))
164 printf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
168 bsd_gidset = cred->cr_groups;
169 bsd_gidsetsz = cred->cr_ngroups - 1;
172 * cr_groups[0] holds egid. Returning the whole set
173 * here will cause a duplicate. Exclude cr_groups[0]
177 if ((ngrp = args->gidsetsize) == 0) {
178 args->sysmsg_result = bsd_gidsetsz;
182 if (ngrp < bsd_gidsetsz)
186 while (ngrp < bsd_gidsetsz) {
187 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
191 error = copyout(linux_gidset, (caddr_t)args->gidset,
192 ngrp * sizeof(l_gid16_t));
196 args->sysmsg_result = ngrp;
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
207 * linux_getgid16() - MP SAFE
208 * linux_getuid16() - MP SAFE
212 linux_getgid16(struct linux_getgid16_args *args)
214 struct proc *p = curproc;
216 args->sysmsg_result = p->p_ucred->cr_rgid;
221 linux_getuid16(struct linux_getuid16_args *args)
223 struct proc *p = curproc;
225 args->sysmsg_result = p->p_ucred->cr_ruid;
230 linux_getegid16(struct linux_getegid16_args *args)
232 struct getegid_args bsd;
235 bsd.sysmsg_result = 0;
237 error = getegid(&bsd);
238 args->sysmsg_result = bsd.sysmsg_result;
243 linux_geteuid16(struct linux_geteuid16_args *args)
245 struct geteuid_args bsd;
248 bsd.sysmsg_result = 0;
250 error = geteuid(&bsd);
251 args->sysmsg_result = bsd.sysmsg_result;
256 linux_setgid16(struct linux_setgid16_args *args)
258 struct setgid_args bsd;
262 bsd.sysmsg_result = 0;
264 error = setgid(&bsd);
265 args->sysmsg_result = bsd.sysmsg_result;
270 linux_setuid16(struct linux_setuid16_args *args)
272 struct setuid_args bsd;
276 bsd.sysmsg_result = 0;
278 error = setuid(&bsd);
279 args->sysmsg_result = bsd.sysmsg_result;
284 linux_setregid16(struct linux_setregid16_args *args)
286 struct setregid_args bsd;
289 bsd.rgid = CAST_NOCHG(args->rgid);
290 bsd.egid = CAST_NOCHG(args->egid);
291 bsd.sysmsg_result = 0;
293 error = setregid(&bsd);
294 args->sysmsg_result = bsd.sysmsg_result;
299 linux_setreuid16(struct linux_setreuid16_args *args)
301 struct setreuid_args bsd;
304 bsd.ruid = CAST_NOCHG(args->ruid);
305 bsd.euid = CAST_NOCHG(args->euid);
306 bsd.sysmsg_result = 0;
308 error = setreuid(&bsd);
309 args->sysmsg_result = bsd.sysmsg_result;
314 linux_setresgid16(struct linux_setresgid16_args *args)
316 struct setresgid_args bsd;
319 bsd.rgid = CAST_NOCHG(args->rgid);
320 bsd.egid = CAST_NOCHG(args->egid);
321 bsd.sgid = CAST_NOCHG(args->sgid);
322 bsd.sysmsg_result = 0;
324 error = setresgid(&bsd);
325 args->sysmsg_result = bsd.sysmsg_result;
330 linux_setresuid16(struct linux_setresuid16_args *args)
332 struct setresuid_args bsd;
335 bsd.ruid = CAST_NOCHG(args->ruid);
336 bsd.euid = CAST_NOCHG(args->euid);
337 bsd.suid = CAST_NOCHG(args->suid);
338 bsd.sysmsg_result = 0;
340 error = setresuid(&bsd);
341 args->sysmsg_result = bsd.sysmsg_result;