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)
53 sys_linux_chown16(struct linux_chown16_args *args)
55 struct nlookupdata nd;
59 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
64 kprintf(ARGS(chown16, "%s, %d, %d"), path, args->uid,
67 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
69 error = kern_chown(&nd, CAST_NOCHG(args->uid),
70 CAST_NOCHG(args->gid));
73 linux_free_path(&path);
78 sys_linux_lchown16(struct linux_lchown16_args *args)
80 struct nlookupdata nd;
84 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
89 kprintf(ARGS(lchown16, "%s, %d, %d"), path, args->uid,
92 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
94 error = kern_chown(&nd, CAST_NOCHG(args->uid),
95 CAST_NOCHG(args->gid));
98 linux_free_path(&path);
103 sys_linux_setgroups16(struct linux_setgroups16_args *args)
105 struct proc *p = curproc;
106 struct ucred *newcred, *oldcred;
107 l_gid16_t linux_gidset[NGROUPS];
112 if (ldebug(setgroups16))
113 kprintf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
116 ngrp = args->gidsetsize;
117 oldcred = p->p_ucred;
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.
125 if ((error = priv_check_cred(oldcred, PRIV_ROOT, PRISON_ROOT)) != 0)
131 newcred = crdup(oldcred);
133 error = copyin((caddr_t)args->gidset, linux_gidset,
134 ngrp * sizeof(l_gid16_t));
138 newcred->cr_ngroups = ngrp + 1;
140 bsd_gidset = newcred->cr_groups;
143 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
148 newcred->cr_ngroups = 1;
151 p->p_ucred = newcred;
157 sys_linux_getgroups16(struct linux_getgroups16_args *args)
159 struct proc *p = curproc;
161 l_gid16_t linux_gidset[NGROUPS];
163 int bsd_gidsetsz, ngrp, error;
166 if (ldebug(getgroups16))
167 kprintf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
171 bsd_gidset = cred->cr_groups;
172 bsd_gidsetsz = cred->cr_ngroups - 1;
175 * cr_groups[0] holds egid. Returning the whole set
176 * here will cause a duplicate. Exclude cr_groups[0]
180 if ((ngrp = args->gidsetsize) == 0) {
181 args->sysmsg_result = bsd_gidsetsz;
185 if (ngrp < bsd_gidsetsz)
189 while (ngrp < bsd_gidsetsz) {
190 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
194 error = copyout(linux_gidset, (caddr_t)args->gidset,
195 ngrp * sizeof(l_gid16_t));
199 args->sysmsg_result = ngrp;
204 * The FreeBSD native getgid(2) and getuid(2) also modify p->p_retval[1]
205 * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that
206 * are assumed to be preserved. The following lightweight syscalls fixes
207 * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in
210 * linux_getgid16() - MP SAFE
211 * linux_getuid16() - MP SAFE
215 sys_linux_getgid16(struct linux_getgid16_args *args)
217 struct proc *p = curproc;
219 args->sysmsg_result = p->p_ucred->cr_rgid;
224 sys_linux_getuid16(struct linux_getuid16_args *args)
226 struct proc *p = curproc;
228 args->sysmsg_result = p->p_ucred->cr_ruid;
233 sys_linux_getegid16(struct linux_getegid16_args *args)
235 struct getegid_args bsd;
238 bsd.sysmsg_result = 0;
240 error = sys_getegid(&bsd);
241 args->sysmsg_result = bsd.sysmsg_result;
246 sys_linux_geteuid16(struct linux_geteuid16_args *args)
248 struct geteuid_args bsd;
251 bsd.sysmsg_result = 0;
253 error = sys_geteuid(&bsd);
254 args->sysmsg_result = bsd.sysmsg_result;
259 sys_linux_setgid16(struct linux_setgid16_args *args)
261 struct setgid_args bsd;
265 bsd.sysmsg_result = 0;
267 error = sys_setgid(&bsd);
268 args->sysmsg_result = bsd.sysmsg_result;
273 sys_linux_setuid16(struct linux_setuid16_args *args)
275 struct setuid_args bsd;
279 bsd.sysmsg_result = 0;
281 error = sys_setuid(&bsd);
282 args->sysmsg_result = bsd.sysmsg_result;
287 sys_linux_setregid16(struct linux_setregid16_args *args)
289 struct setregid_args bsd;
292 bsd.rgid = CAST_NOCHG(args->rgid);
293 bsd.egid = CAST_NOCHG(args->egid);
294 bsd.sysmsg_result = 0;
296 error = sys_setregid(&bsd);
297 args->sysmsg_result = bsd.sysmsg_result;
302 sys_linux_setreuid16(struct linux_setreuid16_args *args)
304 struct setreuid_args bsd;
307 bsd.ruid = CAST_NOCHG(args->ruid);
308 bsd.euid = CAST_NOCHG(args->euid);
309 bsd.sysmsg_result = 0;
311 error = sys_setreuid(&bsd);
312 args->sysmsg_result = bsd.sysmsg_result;
317 sys_linux_setresgid16(struct linux_setresgid16_args *args)
319 struct setresgid_args bsd;
322 bsd.rgid = CAST_NOCHG(args->rgid);
323 bsd.egid = CAST_NOCHG(args->egid);
324 bsd.sgid = CAST_NOCHG(args->sgid);
325 bsd.sysmsg_result = 0;
327 error = sys_setresgid(&bsd);
328 args->sysmsg_result = bsd.sysmsg_result;
333 sys_linux_setresuid16(struct linux_setresuid16_args *args)
335 struct setresuid_args bsd;
338 bsd.ruid = CAST_NOCHG(args->ruid);
339 bsd.euid = CAST_NOCHG(args->euid);
340 bsd.suid = CAST_NOCHG(args->suid);
341 bsd.sysmsg_result = 0;
343 error = sys_setresuid(&bsd);
344 args->sysmsg_result = bsd.sysmsg_result;