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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_compat.h"
32 #include <sys/fcntl.h>
33 #include <sys/param.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
39 #include <sys/syscallsubr.h>
40 #include <sys/sysproto.h>
41 #include <sys/systm.h>
44 #include <machine/../linux32/linux.h>
45 #include <machine/../linux32/linux32_proto.h>
47 #include <machine/../linux/linux.h>
48 #include <machine/../linux/linux_proto.h>
51 #include <compat/linux/linux_util.h>
58 #define CAST_NOCHG(x) ((x == 0xFFFF) ? -1 : x)
61 linux_chown16(struct thread *td, struct linux_chown16_args *args)
66 LCONVPATHEXIST(td, args->path, &path);
70 printf(ARGS(chown16, "%s, %d, %d"), path, args->uid, args->gid);
72 error = kern_chown(td, path, UIO_SYSSPACE, CAST_NOCHG(args->uid),
73 CAST_NOCHG(args->gid));
79 linux_lchown16(struct thread *td, struct linux_lchown16_args *args)
84 LCONVPATHEXIST(td, args->path, &path);
88 printf(ARGS(lchown16, "%s, %d, %d"), path, args->uid,
91 error = kern_lchown(td, path, UIO_SYSSPACE, CAST_NOCHG(args->uid),
92 CAST_NOCHG(args->gid));
98 linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args)
100 struct ucred *newcred, *oldcred;
101 l_gid16_t *linux_gidset;
107 if (ldebug(setgroups16))
108 printf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
111 ngrp = args->gidsetsize;
112 if (ngrp < 0 || ngrp >= ngroups_max + 1)
114 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
115 error = copyin(args->gidset, linux_gidset, ngrp * sizeof(l_gid16_t));
121 oldcred = crcopysafe(p, newcred);
124 * cr_groups[0] holds egid. Setting the whole set from
125 * the supplied set will cause egid to be changed too.
126 * Keep cr_groups[0] unchanged to prevent that.
129 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
136 newcred->cr_ngroups = ngrp + 1;
138 bsd_gidset = newcred->cr_groups;
141 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
146 newcred->cr_ngroups = 1;
148 setsugid(td->td_proc);
149 p->p_ucred = newcred;
154 free(linux_gidset, M_TEMP);
159 linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args)
162 l_gid16_t *linux_gidset;
164 int bsd_gidsetsz, ngrp, error;
167 if (ldebug(getgroups16))
168 printf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
172 bsd_gidset = cred->cr_groups;
173 bsd_gidsetsz = cred->cr_ngroups - 1;
176 * cr_groups[0] holds egid. Returning the whole set
177 * here will cause a duplicate. Exclude cr_groups[0]
181 if ((ngrp = args->gidsetsize) == 0) {
182 td->td_retval[0] = bsd_gidsetsz;
186 if (ngrp < bsd_gidsetsz)
190 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
192 while (ngrp < bsd_gidsetsz) {
193 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
197 error = copyout(linux_gidset, args->gidset, ngrp * sizeof(l_gid16_t));
198 free(linux_gidset, M_TEMP);
202 td->td_retval[0] = ngrp;
207 * The FreeBSD native getgid(2) and getuid(2) also modify td->td_retval[1]
208 * when COMPAT_43 is defined. This clobbers registers that are assumed to
209 * be preserved. The following lightweight syscalls fixes this. See also
210 * linux_getpid(2), linux_getgid(2) and linux_getuid(2) in linux_misc.c
212 * linux_getgid16() - MP SAFE
213 * linux_getuid16() - MP SAFE
217 linux_getgid16(struct thread *td, struct linux_getgid16_args *args)
220 td->td_retval[0] = td->td_ucred->cr_rgid;
225 linux_getuid16(struct thread *td, struct linux_getuid16_args *args)
228 td->td_retval[0] = td->td_ucred->cr_ruid;
233 linux_getegid16(struct thread *td, struct linux_getegid16_args *args)
235 struct getegid_args bsd;
237 return (getegid(td, &bsd));
241 linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args)
243 struct geteuid_args bsd;
245 return (geteuid(td, &bsd));
249 linux_setgid16(struct thread *td, struct linux_setgid16_args *args)
251 struct setgid_args bsd;
254 return (setgid(td, &bsd));
258 linux_setuid16(struct thread *td, struct linux_setuid16_args *args)
260 struct setuid_args bsd;
263 return (setuid(td, &bsd));
267 linux_setregid16(struct thread *td, struct linux_setregid16_args *args)
269 struct setregid_args bsd;
271 bsd.rgid = CAST_NOCHG(args->rgid);
272 bsd.egid = CAST_NOCHG(args->egid);
273 return (setregid(td, &bsd));
277 linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args)
279 struct setreuid_args bsd;
281 bsd.ruid = CAST_NOCHG(args->ruid);
282 bsd.euid = CAST_NOCHG(args->euid);
283 return (setreuid(td, &bsd));
287 linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args)
289 struct setresgid_args bsd;
291 bsd.rgid = CAST_NOCHG(args->rgid);
292 bsd.egid = CAST_NOCHG(args->egid);
293 bsd.sgid = CAST_NOCHG(args->sgid);
294 return (setresgid(td, &bsd));
298 linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args)
300 struct setresuid_args bsd;
302 bsd.ruid = CAST_NOCHG(args->ruid);
303 bsd.euid = CAST_NOCHG(args->euid);
304 bsd.suid = CAST_NOCHG(args->suid);
305 return (setresuid(td, &bsd));