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 thread *td = curthread;
119 struct proc *p = td->td_proc;
120 struct ucred *newcred, *oldcred;
121 l_gid16_t linux_gidset[NGROUPS];
126 if (ldebug(setgroups16))
127 kprintf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
130 ngrp = args->gidsetsize;
131 oldcred = td->td_ucred;
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.
139 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0)
142 if ((u_int)ngrp >= NGROUPS)
146 newcred = crdup(oldcred);
148 error = copyin((caddr_t)args->gidset, linux_gidset,
149 ngrp * sizeof(l_gid16_t));
155 newcred->cr_ngroups = ngrp + 1;
157 bsd_gidset = newcred->cr_groups;
160 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
164 newcred->cr_ngroups = 1;
168 oldcred = p->p_ucred; /* deal with threads race */
169 p->p_ucred = newcred;
181 sys_linux_getgroups16(struct linux_getgroups16_args *args)
183 struct proc *p = curproc;
185 l_gid16_t linux_gidset[NGROUPS];
187 int bsd_gidsetsz, ngrp, error;
190 if (ldebug(getgroups16))
191 kprintf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
195 bsd_gidset = cred->cr_groups;
196 bsd_gidsetsz = cred->cr_ngroups - 1;
199 * cr_groups[0] holds egid. Returning the whole set
200 * here will cause a duplicate. Exclude cr_groups[0]
204 if ((ngrp = args->gidsetsize) == 0) {
205 args->sysmsg_result = bsd_gidsetsz;
209 if ((u_int)ngrp < (u_int)bsd_gidsetsz)
213 while (ngrp < bsd_gidsetsz) {
214 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
218 error = copyout(linux_gidset, (caddr_t)args->gidset,
219 ngrp * sizeof(l_gid16_t));
223 args->sysmsg_result = ngrp;
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
234 * linux_getgid16() - MP SAFE
235 * linux_getuid16() - MP SAFE
242 sys_linux_getgid16(struct linux_getgid16_args *args)
244 struct proc *p = curproc;
246 args->sysmsg_result = p->p_ucred->cr_rgid;
254 sys_linux_getuid16(struct linux_getuid16_args *args)
256 struct proc *p = curproc;
258 args->sysmsg_result = p->p_ucred->cr_ruid;
266 sys_linux_getegid16(struct linux_getegid16_args *args)
268 struct getegid_args bsd;
271 bsd.sysmsg_result = 0;
273 error = sys_getegid(&bsd);
274 args->sysmsg_result = bsd.sysmsg_result;
282 sys_linux_geteuid16(struct linux_geteuid16_args *args)
284 struct geteuid_args bsd;
287 bsd.sysmsg_result = 0;
289 error = sys_geteuid(&bsd);
290 args->sysmsg_result = bsd.sysmsg_result;
298 sys_linux_setgid16(struct linux_setgid16_args *args)
300 struct setgid_args bsd;
304 bsd.sysmsg_result = 0;
306 error = sys_setgid(&bsd);
307 args->sysmsg_result = bsd.sysmsg_result;
315 sys_linux_setuid16(struct linux_setuid16_args *args)
317 struct setuid_args bsd;
321 bsd.sysmsg_result = 0;
323 error = sys_setuid(&bsd);
324 args->sysmsg_result = bsd.sysmsg_result;
332 sys_linux_setregid16(struct linux_setregid16_args *args)
334 struct setregid_args bsd;
337 bsd.rgid = CAST_NOCHG(args->rgid);
338 bsd.egid = CAST_NOCHG(args->egid);
339 bsd.sysmsg_result = 0;
341 error = sys_setregid(&bsd);
342 args->sysmsg_result = bsd.sysmsg_result;
350 sys_linux_setreuid16(struct linux_setreuid16_args *args)
352 struct setreuid_args bsd;
355 bsd.ruid = CAST_NOCHG(args->ruid);
356 bsd.euid = CAST_NOCHG(args->euid);
357 bsd.sysmsg_result = 0;
359 error = sys_setreuid(&bsd);
360 args->sysmsg_result = bsd.sysmsg_result;
368 sys_linux_setresgid16(struct linux_setresgid16_args *args)
370 struct setresgid_args bsd;
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;
378 error = sys_setresgid(&bsd);
379 args->sysmsg_result = bsd.sysmsg_result;
387 sys_linux_setresuid16(struct linux_setresuid16_args *args)
389 struct setresuid_args bsd;
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;
397 error = sys_setresuid(&bsd);
398 args->sysmsg_result = bsd.sysmsg_result;