2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 The FreeBSD Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_compat.h"
34 #include <sys/fcntl.h>
35 #include <sys/param.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysproto.h>
45 #include <sys/systm.h>
48 #include <machine/../linux32/linux.h>
49 #include <machine/../linux32/linux32_proto.h>
51 #include <machine/../linux/linux.h>
52 #include <machine/../linux/linux_proto.h>
55 #include <compat/linux/linux_dtrace.h>
56 #include <compat/linux/linux_util.h>
59 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
62 * DTrace probes in this module.
64 LIN_SDT_PROBE_DEFINE1(uid16, linux_chown16, conv_path, "char *");
65 LIN_SDT_PROBE_DEFINE1(uid16, linux_lchown16, conv_path, "char *");
66 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, copyin_error, "int");
67 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, priv_check_cred_error, "int");
68 LIN_SDT_PROBE_DEFINE1(uid16, linux_getgroups16, copyout_error, "int");
75 #define CAST_NOCHG(x) ((x == 0xFFFF) ? -1 : x)
78 linux_chown16(struct thread *td, struct linux_chown16_args *args)
83 if (!LUSECONVPATH(td) && !SDT_PROBES_ENABLED()) {
84 error = kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
85 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), 0);
87 LCONVPATHEXIST(td, args->path, &path);
89 * The DTrace probes have to be after the LCONVPATHEXIST, as
90 * LCONVPATHEXIST may return on its own and we do not want to
91 * have a stray entry without the corresponding return.
93 LIN_SDT_PROBE1(uid16, linux_chown16, conv_path, path);
95 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE,
96 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), 0);
103 linux_lchown16(struct thread *td, struct linux_lchown16_args *args)
108 if (!LUSECONVPATH(td) && !SDT_PROBES_ENABLED()) {
109 error = kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
110 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), AT_SYMLINK_NOFOLLOW);
112 LCONVPATHEXIST(td, args->path, &path);
115 * The DTrace probes have to be after the LCONVPATHEXIST, as
116 * LCONVPATHEXIST may return on its own and we do not want to
117 * have a stray entry without the corresponding return.
119 LIN_SDT_PROBE1(uid16, linux_lchown16, conv_path, path);
121 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE,
122 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), AT_SYMLINK_NOFOLLOW);
129 linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args)
131 struct ucred *newcred, *oldcred;
132 l_gid16_t *linux_gidset;
137 ngrp = args->gidsetsize;
138 if (ngrp < 0 || ngrp >= ngroups_max + 1)
140 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
141 error = copyin(args->gidset, linux_gidset, ngrp * sizeof(l_gid16_t));
143 LIN_SDT_PROBE1(uid16, linux_setgroups16, copyin_error, error);
144 free(linux_gidset, M_LINUX);
150 oldcred = crcopysafe(p, newcred);
153 * cr_groups[0] holds egid. Setting the whole set from
154 * the supplied set will cause egid to be changed too.
155 * Keep cr_groups[0] unchanged to prevent that.
158 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
162 LIN_SDT_PROBE1(uid16, linux_setgroups16, priv_check_cred_error,
168 newcred->cr_ngroups = ngrp + 1;
170 bsd_gidset = newcred->cr_groups;
173 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
178 newcred->cr_ngroups = 1;
180 setsugid(td->td_proc);
181 proc_set_cred(p, newcred);
186 free(linux_gidset, M_LINUX);
192 linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args)
195 l_gid16_t *linux_gidset;
197 int bsd_gidsetsz, ngrp, error;
200 bsd_gidset = cred->cr_groups;
201 bsd_gidsetsz = cred->cr_ngroups - 1;
204 * cr_groups[0] holds egid. Returning the whole set
205 * here will cause a duplicate. Exclude cr_groups[0]
209 if ((ngrp = args->gidsetsize) == 0) {
210 td->td_retval[0] = bsd_gidsetsz;
214 if (ngrp < bsd_gidsetsz)
218 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
220 while (ngrp < bsd_gidsetsz) {
221 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
225 error = copyout(linux_gidset, args->gidset, ngrp * sizeof(l_gid16_t));
226 free(linux_gidset, M_LINUX);
228 LIN_SDT_PROBE1(uid16, linux_getgroups16, copyout_error, error);
232 td->td_retval[0] = ngrp;
238 linux_getgid16(struct thread *td, struct linux_getgid16_args *args)
241 td->td_retval[0] = td->td_ucred->cr_rgid;
247 linux_getuid16(struct thread *td, struct linux_getuid16_args *args)
250 td->td_retval[0] = td->td_ucred->cr_ruid;
256 linux_getegid16(struct thread *td, struct linux_getegid16_args *args)
258 struct getegid_args bsd;
261 error = sys_getegid(td, &bsd);
267 linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args)
269 struct geteuid_args bsd;
272 error = sys_geteuid(td, &bsd);
278 linux_setgid16(struct thread *td, struct linux_setgid16_args *args)
280 struct setgid_args bsd;
284 error = sys_setgid(td, &bsd);
290 linux_setuid16(struct thread *td, struct linux_setuid16_args *args)
292 struct setuid_args bsd;
296 error = sys_setuid(td, &bsd);
302 linux_setregid16(struct thread *td, struct linux_setregid16_args *args)
304 struct setregid_args bsd;
307 bsd.rgid = CAST_NOCHG(args->rgid);
308 bsd.egid = CAST_NOCHG(args->egid);
309 error = sys_setregid(td, &bsd);
315 linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args)
317 struct setreuid_args bsd;
320 bsd.ruid = CAST_NOCHG(args->ruid);
321 bsd.euid = CAST_NOCHG(args->euid);
322 error = sys_setreuid(td, &bsd);
328 linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args)
330 struct setresgid_args bsd;
333 bsd.rgid = CAST_NOCHG(args->rgid);
334 bsd.egid = CAST_NOCHG(args->egid);
335 bsd.sgid = CAST_NOCHG(args->sgid);
336 error = sys_setresgid(td, &bsd);
342 linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args)
344 struct setresuid_args bsd;
347 bsd.ruid = CAST_NOCHG(args->ruid);
348 bsd.euid = CAST_NOCHG(args->euid);
349 bsd.suid = CAST_NOCHG(args->suid);
350 error = sys_setresuid(td, &bsd);