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.12 2006/12/23 00:27:02 swildner 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/nlookup.h>
38 #include <sys/sysproto.h>
39 #include <sys/thread.h>
41 #include <arch_linux/linux.h>
42 #include <arch_linux/linux_proto.h>
43 #include "linux_util.h"
50 #define CAST_NOCHG(x) ((x == 0xFFFF) ? -1 : x)
56 sys_linux_chown16(struct linux_chown16_args *args)
58 struct nlookupdata nd;
62 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
67 kprintf(ARGS(chown16, "%s, %d, %d"), path, args->uid,
71 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
73 error = kern_chown(&nd, CAST_NOCHG(args->uid),
74 CAST_NOCHG(args->gid));
78 linux_free_path(&path);
86 sys_linux_lchown16(struct linux_lchown16_args *args)
88 struct nlookupdata nd;
92 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
97 kprintf(ARGS(lchown16, "%s, %d, %d"), path, args->uid,
101 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
103 error = kern_chown(&nd, CAST_NOCHG(args->uid),
104 CAST_NOCHG(args->gid));
108 linux_free_path(&path);
116 sys_linux_setgroups16(struct linux_setgroups16_args *args)
118 struct proc *p = curproc;
119 struct ucred *newcred, *oldcred;
120 l_gid16_t linux_gidset[NGROUPS];
125 if (ldebug(setgroups16))
126 kprintf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
129 ngrp = args->gidsetsize;
130 oldcred = p->p_ucred;
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.
138 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0)
141 if ((u_int)ngrp >= NGROUPS)
145 newcred = crdup(oldcred);
147 error = copyin((caddr_t)args->gidset, linux_gidset,
148 ngrp * sizeof(l_gid16_t));
154 newcred->cr_ngroups = ngrp + 1;
156 bsd_gidset = newcred->cr_groups;
159 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
163 newcred->cr_ngroups = 1;
167 p->p_ucred = newcred;
179 sys_linux_getgroups16(struct linux_getgroups16_args *args)
181 struct proc *p = curproc;
183 l_gid16_t linux_gidset[NGROUPS];
185 int bsd_gidsetsz, ngrp, error;
188 if (ldebug(getgroups16))
189 kprintf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
193 bsd_gidset = cred->cr_groups;
194 bsd_gidsetsz = cred->cr_ngroups - 1;
197 * cr_groups[0] holds egid. Returning the whole set
198 * here will cause a duplicate. Exclude cr_groups[0]
202 if ((ngrp = args->gidsetsize) == 0) {
203 args->sysmsg_result = bsd_gidsetsz;
207 if ((u_int)ngrp < (u_int)bsd_gidsetsz)
211 while (ngrp < bsd_gidsetsz) {
212 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
216 error = copyout(linux_gidset, (caddr_t)args->gidset,
217 ngrp * sizeof(l_gid16_t));
221 args->sysmsg_result = ngrp;
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
232 * linux_getgid16() - MP SAFE
233 * linux_getuid16() - MP SAFE
240 sys_linux_getgid16(struct linux_getgid16_args *args)
242 struct proc *p = curproc;
244 args->sysmsg_result = p->p_ucred->cr_rgid;
252 sys_linux_getuid16(struct linux_getuid16_args *args)
254 struct proc *p = curproc;
256 args->sysmsg_result = p->p_ucred->cr_ruid;
264 sys_linux_getegid16(struct linux_getegid16_args *args)
266 struct getegid_args bsd;
269 bsd.sysmsg_result = 0;
271 error = sys_getegid(&bsd);
272 args->sysmsg_result = bsd.sysmsg_result;
280 sys_linux_geteuid16(struct linux_geteuid16_args *args)
282 struct geteuid_args bsd;
285 bsd.sysmsg_result = 0;
287 error = sys_geteuid(&bsd);
288 args->sysmsg_result = bsd.sysmsg_result;
296 sys_linux_setgid16(struct linux_setgid16_args *args)
298 struct setgid_args bsd;
302 bsd.sysmsg_result = 0;
304 error = sys_setgid(&bsd);
305 args->sysmsg_result = bsd.sysmsg_result;
313 sys_linux_setuid16(struct linux_setuid16_args *args)
315 struct setuid_args bsd;
319 bsd.sysmsg_result = 0;
321 error = sys_setuid(&bsd);
322 args->sysmsg_result = bsd.sysmsg_result;
330 sys_linux_setregid16(struct linux_setregid16_args *args)
332 struct setregid_args bsd;
335 bsd.rgid = CAST_NOCHG(args->rgid);
336 bsd.egid = CAST_NOCHG(args->egid);
337 bsd.sysmsg_result = 0;
339 error = sys_setregid(&bsd);
340 args->sysmsg_result = bsd.sysmsg_result;
348 sys_linux_setreuid16(struct linux_setreuid16_args *args)
350 struct setreuid_args bsd;
353 bsd.ruid = CAST_NOCHG(args->ruid);
354 bsd.euid = CAST_NOCHG(args->euid);
355 bsd.sysmsg_result = 0;
357 error = sys_setreuid(&bsd);
358 args->sysmsg_result = bsd.sysmsg_result;
366 sys_linux_setresgid16(struct linux_setresgid16_args *args)
368 struct setresgid_args bsd;
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;
376 error = sys_setresgid(&bsd);
377 args->sysmsg_result = bsd.sysmsg_result;
385 sys_linux_setresuid16(struct linux_setresuid16_args *args)
387 struct setresuid_args bsd;
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;
395 error = sys_setresuid(&bsd);
396 args->sysmsg_result = bsd.sysmsg_result;