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 $
29 #include "opt_compat.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/sysproto.h>
36 #include <machine/../linux/linux.h>
37 #include <machine/../linux/linux_proto.h>
38 #include <compat/linux/linux_util.h>
45 #define CAST_NOCHG(x) (x == 0xFFFF) ? -1 : x;
48 linux_chown16(struct proc *p, struct linux_chown16_args *args)
50 struct chown_args bsd;
54 CHECKALTEXIST(p, &sg, args->path);
58 printf(ARGS(chown16, "%s, %d, %d"), args->path, args->uid,
62 bsd.path = args->path;
63 bsd.uid = CAST_NOCHG(args->uid);
64 bsd.gid = CAST_NOCHG(args->gid);
65 return (chown(p, &bsd));
69 linux_lchown16(struct proc *p, struct linux_lchown16_args *args)
71 struct lchown_args bsd;
75 CHECKALTEXIST(p, &sg, args->path);
79 printf(ARGS(lchown16, "%s, %d, %d"), args->path, args->uid,
83 bsd.path = args->path;
84 bsd.uid = CAST_NOCHG(args->uid);
85 bsd.gid = CAST_NOCHG(args->gid);
86 return (lchown(p, &bsd));
90 linux_setgroups16(struct proc *p, struct linux_setgroups16_args *args)
92 struct ucred *newcred, *oldcred;
93 l_gid16_t linux_gidset[NGROUPS];
98 if (ldebug(setgroups16))
99 printf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
102 ngrp = args->gidsetsize;
103 oldcred = p->p_ucred;
106 * cr_groups[0] holds egid. Setting the whole set from
107 * the supplied set will cause egid to be changed too.
108 * Keep cr_groups[0] unchanged to prevent that.
111 if ((error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
117 newcred = crdup(oldcred);
119 error = copyin((caddr_t)args->gidset, linux_gidset,
120 ngrp * sizeof(l_gid16_t));
124 newcred->cr_ngroups = ngrp + 1;
126 bsd_gidset = newcred->cr_groups;
129 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
134 newcred->cr_ngroups = 1;
137 p->p_ucred = newcred;
143 linux_getgroups16(struct proc *p, struct linux_getgroups16_args *args)
146 l_gid16_t linux_gidset[NGROUPS];
148 int bsd_gidsetsz, ngrp, error;
151 if (ldebug(getgroups16))
152 printf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
156 bsd_gidset = cred->cr_groups;
157 bsd_gidsetsz = cred->cr_ngroups - 1;
160 * cr_groups[0] holds egid. Returning the whole set
161 * here will cause a duplicate. Exclude cr_groups[0]
165 if ((ngrp = args->gidsetsize) == 0) {
166 p->p_retval[0] = bsd_gidsetsz;
170 if (ngrp < bsd_gidsetsz)
174 while (ngrp < bsd_gidsetsz) {
175 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
179 error = copyout(linux_gidset, (caddr_t)args->gidset,
180 ngrp * sizeof(l_gid16_t));
184 p->p_retval[0] = ngrp;
189 * The FreeBSD native getgid(2) and getuid(2) also modify p->p_retval[1]
190 * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that
191 * are assumed to be preserved. The following lightweight syscalls fixes
192 * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in
195 * linux_getgid16() - MP SAFE
196 * linux_getuid16() - MP SAFE
200 linux_getgid16(struct proc *p, struct linux_getgid16_args *args)
203 p->p_retval[0] = p->p_cred->p_rgid;
208 linux_getuid16(struct proc *p, struct linux_getuid16_args *args)
211 p->p_retval[0] = p->p_cred->p_ruid;
216 linux_getegid16(struct proc *p, struct linux_getegid16_args *args)
218 struct getegid_args bsd;
220 return (getegid(p, &bsd));
224 linux_geteuid16(struct proc *p, struct linux_geteuid16_args *args)
226 struct geteuid_args bsd;
228 return (geteuid(p, &bsd));
232 linux_setgid16(struct proc *p, struct linux_setgid16_args *args)
234 struct setgid_args bsd;
237 return (setgid(p, &bsd));
241 linux_setuid16(struct proc *p, struct linux_setuid16_args *args)
243 struct setuid_args bsd;
246 return (setuid(p, &bsd));
250 linux_setregid16(struct proc *p, struct linux_setregid16_args *args)
252 struct setregid_args bsd;
254 bsd.rgid = CAST_NOCHG(args->rgid);
255 bsd.egid = CAST_NOCHG(args->egid);
256 return (setregid(p, &bsd));
260 linux_setreuid16(struct proc *p, struct linux_setreuid16_args *args)
262 struct setreuid_args bsd;
264 bsd.ruid = CAST_NOCHG(args->ruid);
265 bsd.euid = CAST_NOCHG(args->euid);
266 return (setreuid(p, &bsd));
270 linux_setresgid16(struct proc *p, struct linux_setresgid16_args *args)
272 struct setresgid_args bsd;
274 bsd.rgid = CAST_NOCHG(args->rgid);
275 bsd.egid = CAST_NOCHG(args->egid);
276 bsd.sgid = CAST_NOCHG(args->sgid);
277 return (setresgid(p, &bsd));
281 linux_setresuid16(struct proc *p, struct linux_setresuid16_args *args)
283 struct setresuid_args bsd;
285 bsd.ruid = CAST_NOCHG(args->ruid);
286 bsd.euid = CAST_NOCHG(args->euid);
287 bsd.suid = CAST_NOCHG(args->suid);
288 return (setresuid(p, &bsd));