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.4 2003/06/25 03:55:44 dillon Exp $
30 #include "opt_compat.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/sysproto.h>
37 #include <machine/../linux/linux.h>
38 #include <machine/../linux/linux_proto.h>
39 #include <compat/linux/linux_util.h>
46 #define CAST_NOCHG(x) (x == 0xFFFF) ? -1 : x;
49 linux_chown16(struct linux_chown16_args *args)
51 struct chown_args bsd;
55 CHECKALTEXIST(&sg, args->path);
59 printf(ARGS(chown16, "%s, %d, %d"), args->path, args->uid,
63 bsd.path = args->path;
64 bsd.uid = CAST_NOCHG(args->uid);
65 bsd.gid = CAST_NOCHG(args->gid);
70 linux_lchown16(struct linux_lchown16_args *args)
72 struct lchown_args bsd;
76 CHECKALTEXIST(&sg, args->path);
80 printf(ARGS(lchown16, "%s, %d, %d"), args->path, args->uid,
84 bsd.path = args->path;
85 bsd.uid = CAST_NOCHG(args->uid);
86 bsd.gid = CAST_NOCHG(args->gid);
87 return (lchown(&bsd));
91 linux_setgroups16(struct linux_setgroups16_args *args)
93 struct proc *p = curproc;
94 struct ucred *newcred, *oldcred;
95 l_gid16_t linux_gidset[NGROUPS];
100 if (ldebug(setgroups16))
101 printf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
104 ngrp = args->gidsetsize;
105 oldcred = p->p_ucred;
108 * cr_groups[0] holds egid. Setting the whole set from
109 * the supplied set will cause egid to be changed too.
110 * Keep cr_groups[0] unchanged to prevent that.
113 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
119 newcred = crdup(oldcred);
121 error = copyin((caddr_t)args->gidset, linux_gidset,
122 ngrp * sizeof(l_gid16_t));
126 newcred->cr_ngroups = ngrp + 1;
128 bsd_gidset = newcred->cr_groups;
131 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
136 newcred->cr_ngroups = 1;
139 p->p_ucred = newcred;
145 linux_getgroups16(struct linux_getgroups16_args *args)
147 struct proc *p = curproc;
149 l_gid16_t linux_gidset[NGROUPS];
151 int bsd_gidsetsz, ngrp, error;
154 if (ldebug(getgroups16))
155 printf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
159 bsd_gidset = cred->cr_groups;
160 bsd_gidsetsz = cred->cr_ngroups - 1;
163 * cr_groups[0] holds egid. Returning the whole set
164 * here will cause a duplicate. Exclude cr_groups[0]
168 if ((ngrp = args->gidsetsize) == 0) {
169 p->p_retval[0] = bsd_gidsetsz;
173 if (ngrp < bsd_gidsetsz)
177 while (ngrp < bsd_gidsetsz) {
178 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
182 error = copyout(linux_gidset, (caddr_t)args->gidset,
183 ngrp * sizeof(l_gid16_t));
187 p->p_retval[0] = ngrp;
192 * The FreeBSD native getgid(2) and getuid(2) also modify p->p_retval[1]
193 * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that
194 * are assumed to be preserved. The following lightweight syscalls fixes
195 * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in
198 * linux_getgid16() - MP SAFE
199 * linux_getuid16() - MP SAFE
203 linux_getgid16(struct linux_getgid16_args *args)
205 struct proc *p = curproc;
207 p->p_retval[0] = p->p_ucred->cr_rgid;
212 linux_getuid16(struct linux_getuid16_args *args)
214 struct proc *p = curproc;
216 p->p_retval[0] = p->p_ucred->cr_ruid;
221 linux_getegid16(struct linux_getegid16_args *args)
223 struct getegid_args bsd;
225 return (getegid(&bsd));
229 linux_geteuid16(struct linux_geteuid16_args *args)
231 struct geteuid_args bsd;
233 return (geteuid(&bsd));
237 linux_setgid16(struct linux_setgid16_args *args)
239 struct setgid_args bsd;
242 return (setgid(&bsd));
246 linux_setuid16(struct linux_setuid16_args *args)
248 struct setuid_args bsd;
251 return (setuid(&bsd));
255 linux_setregid16(struct linux_setregid16_args *args)
257 struct setregid_args bsd;
259 bsd.rgid = CAST_NOCHG(args->rgid);
260 bsd.egid = CAST_NOCHG(args->egid);
261 return (setregid(&bsd));
265 linux_setreuid16(struct linux_setreuid16_args *args)
267 struct setreuid_args bsd;
269 bsd.ruid = CAST_NOCHG(args->ruid);
270 bsd.euid = CAST_NOCHG(args->euid);
271 return (setreuid(&bsd));
275 linux_setresgid16(struct linux_setresgid16_args *args)
277 struct setresgid_args bsd;
279 bsd.rgid = CAST_NOCHG(args->rgid);
280 bsd.egid = CAST_NOCHG(args->egid);
281 bsd.sgid = CAST_NOCHG(args->sgid);
282 return (setresgid(&bsd));
286 linux_setresuid16(struct linux_setresuid16_args *args)
288 struct setresuid_args bsd;
290 bsd.ruid = CAST_NOCHG(args->ruid);
291 bsd.euid = CAST_NOCHG(args->euid);
292 bsd.suid = CAST_NOCHG(args->suid);
293 return (setresuid(&bsd));