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_DEFINE3(uid16, linux_chown16, entry, "char *", "l_uid16_t",
66 LIN_SDT_PROBE_DEFINE1(uid16, linux_chown16, conv_path, "char *");
67 LIN_SDT_PROBE_DEFINE1(uid16, linux_chown16, return, "int");
68 LIN_SDT_PROBE_DEFINE3(uid16, linux_lchown16, entry, "char *", "l_uid16_t",
70 LIN_SDT_PROBE_DEFINE1(uid16, linux_lchown16, conv_path, "char *");
71 LIN_SDT_PROBE_DEFINE1(uid16, linux_lchown16, return, "int");
72 LIN_SDT_PROBE_DEFINE2(uid16, linux_setgroups16, entry, "l_uint", "l_gid16_t *");
73 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, copyin_error, "int");
74 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, priv_check_cred_error, "int");
75 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, return, "int");
76 LIN_SDT_PROBE_DEFINE2(uid16, linux_getgroups16, entry, "l_uint", "l_gid16_t *");
77 LIN_SDT_PROBE_DEFINE1(uid16, linux_getgroups16, copyout_error, "int");
78 LIN_SDT_PROBE_DEFINE1(uid16, linux_getgroups16, return, "int");
79 LIN_SDT_PROBE_DEFINE0(uid16, linux_getgid16, entry);
80 LIN_SDT_PROBE_DEFINE1(uid16, linux_getgid16, return, "int");
81 LIN_SDT_PROBE_DEFINE0(uid16, linux_getuid16, entry);
82 LIN_SDT_PROBE_DEFINE1(uid16, linux_getuid16, return, "int");
83 LIN_SDT_PROBE_DEFINE0(uid16, linux_getegid16, entry);
84 LIN_SDT_PROBE_DEFINE1(uid16, linux_getegid16, return, "int");
85 LIN_SDT_PROBE_DEFINE0(uid16, linux_geteuid16, entry);
86 LIN_SDT_PROBE_DEFINE1(uid16, linux_geteuid16, return, "int");
87 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgid16, entry, "l_gid16_t");
88 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgid16, return, "int");
89 LIN_SDT_PROBE_DEFINE1(uid16, linux_setuid16, entry, "l_uid16_t");
90 LIN_SDT_PROBE_DEFINE1(uid16, linux_setuid16, return, "int");
91 LIN_SDT_PROBE_DEFINE2(uid16, linux_setregid16, entry, "l_gid16_t", "l_gid16_t");
92 LIN_SDT_PROBE_DEFINE1(uid16, linux_setregid16, return, "int");
93 LIN_SDT_PROBE_DEFINE2(uid16, linux_setreuid16, entry, "l_uid16_t", "l_uid16_t");
94 LIN_SDT_PROBE_DEFINE1(uid16, linux_setreuid16, return, "int");
95 LIN_SDT_PROBE_DEFINE3(uid16, linux_setresgid16, entry, "l_gid16_t", "l_gid16_t",
97 LIN_SDT_PROBE_DEFINE1(uid16, linux_setresgid16, return, "int");
98 LIN_SDT_PROBE_DEFINE3(uid16, linux_setresuid16, entry, "l_uid16_t", "l_uid16_t",
100 LIN_SDT_PROBE_DEFINE1(uid16, linux_setresuid16, return, "int");
107 #define CAST_NOCHG(x) ((x == 0xFFFF) ? -1 : x)
110 linux_chown16(struct thread *td, struct linux_chown16_args *args)
115 LCONVPATHEXIST(td, args->path, &path);
118 * The DTrace probes have to be after the LCONVPATHEXIST, as
119 * LCONVPATHEXIST may return on its own and we do not want to
120 * have a stray entry without the corresponding return.
122 LIN_SDT_PROBE3(uid16, linux_chown16, entry, args->path, args->uid,
124 LIN_SDT_PROBE1(uid16, linux_chown16, conv_path, path);
126 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE,
127 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), 0);
130 LIN_SDT_PROBE1(uid16, linux_chown16, return, error);
135 linux_lchown16(struct thread *td, struct linux_lchown16_args *args)
140 LCONVPATHEXIST(td, args->path, &path);
143 * The DTrace probes have to be after the LCONVPATHEXIST, as
144 * LCONVPATHEXIST may return on its own and we do not want to
145 * have a stray entry without the corresponding return.
147 LIN_SDT_PROBE3(uid16, linux_lchown16, entry, args->path, args->uid,
149 LIN_SDT_PROBE1(uid16, linux_lchown16, conv_path, path);
151 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE,
152 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), AT_SYMLINK_NOFOLLOW);
155 LIN_SDT_PROBE1(uid16, linux_lchown16, return, error);
160 linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args)
162 struct ucred *newcred, *oldcred;
163 l_gid16_t *linux_gidset;
168 LIN_SDT_PROBE2(uid16, linux_setgroups16, entry, args->gidsetsize,
171 ngrp = args->gidsetsize;
172 if (ngrp < 0 || ngrp >= ngroups_max + 1) {
173 LIN_SDT_PROBE1(uid16, linux_setgroups16, return, EINVAL);
176 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
177 error = copyin(args->gidset, linux_gidset, ngrp * sizeof(l_gid16_t));
179 LIN_SDT_PROBE1(uid16, linux_setgroups16, copyin_error, error);
180 LIN_SDT_PROBE1(uid16, linux_setgroups16, return, error);
181 free(linux_gidset, M_LINUX);
187 oldcred = crcopysafe(p, newcred);
190 * cr_groups[0] holds egid. Setting the whole set from
191 * the supplied set will cause egid to be changed too.
192 * Keep cr_groups[0] unchanged to prevent that.
195 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
199 LIN_SDT_PROBE1(uid16, linux_setgroups16, priv_check_cred_error,
205 newcred->cr_ngroups = ngrp + 1;
207 bsd_gidset = newcred->cr_groups;
210 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
215 newcred->cr_ngroups = 1;
217 setsugid(td->td_proc);
218 proc_set_cred(p, newcred);
223 free(linux_gidset, M_LINUX);
225 LIN_SDT_PROBE1(uid16, linux_setgroups16, return, error);
230 linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args)
233 l_gid16_t *linux_gidset;
235 int bsd_gidsetsz, ngrp, error;
237 LIN_SDT_PROBE2(uid16, linux_getgroups16, entry, args->gidsetsize,
241 bsd_gidset = cred->cr_groups;
242 bsd_gidsetsz = cred->cr_ngroups - 1;
245 * cr_groups[0] holds egid. Returning the whole set
246 * here will cause a duplicate. Exclude cr_groups[0]
250 if ((ngrp = args->gidsetsize) == 0) {
251 td->td_retval[0] = bsd_gidsetsz;
253 LIN_SDT_PROBE1(uid16, linux_getgroups16, return, 0);
257 if (ngrp < bsd_gidsetsz) {
258 LIN_SDT_PROBE1(uid16, linux_getgroups16, return, EINVAL);
263 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
265 while (ngrp < bsd_gidsetsz) {
266 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
270 error = copyout(linux_gidset, args->gidset, ngrp * sizeof(l_gid16_t));
271 free(linux_gidset, M_LINUX);
273 LIN_SDT_PROBE1(uid16, linux_getgroups16, copyout_error, error);
274 LIN_SDT_PROBE1(uid16, linux_getgroups16, return, error);
278 td->td_retval[0] = ngrp;
280 LIN_SDT_PROBE1(uid16, linux_getgroups16, return, 0);
285 linux_getgid16(struct thread *td, struct linux_getgid16_args *args)
288 LIN_SDT_PROBE0(uid16, linux_getgid16, entry);
290 td->td_retval[0] = td->td_ucred->cr_rgid;
292 LIN_SDT_PROBE1(uid16, linux_getgid16, return, 0);
297 linux_getuid16(struct thread *td, struct linux_getuid16_args *args)
300 LIN_SDT_PROBE0(uid16, linux_getuid16, entry);
302 td->td_retval[0] = td->td_ucred->cr_ruid;
304 LIN_SDT_PROBE1(uid16, linux_getuid16, return, 0);
309 linux_getegid16(struct thread *td, struct linux_getegid16_args *args)
311 struct getegid_args bsd;
314 LIN_SDT_PROBE0(uid16, linux_getegid16, entry);
316 error = sys_getegid(td, &bsd);
318 LIN_SDT_PROBE1(uid16, linux_getegid16, return, error);
323 linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args)
325 struct geteuid_args bsd;
328 LIN_SDT_PROBE0(uid16, linux_geteuid16, entry);
330 error = sys_geteuid(td, &bsd);
332 LIN_SDT_PROBE1(uid16, linux_geteuid16, return, error);
337 linux_setgid16(struct thread *td, struct linux_setgid16_args *args)
339 struct setgid_args bsd;
342 LIN_SDT_PROBE1(uid16, linux_setgid16, entry, args->gid);
345 error = sys_setgid(td, &bsd);
347 LIN_SDT_PROBE1(uid16, linux_setgid16, return, error);
352 linux_setuid16(struct thread *td, struct linux_setuid16_args *args)
354 struct setuid_args bsd;
357 LIN_SDT_PROBE1(uid16, linux_setuid16, entry, args->uid);
360 error = sys_setuid(td, &bsd);
362 LIN_SDT_PROBE1(uid16, linux_setuid16, return, error);
367 linux_setregid16(struct thread *td, struct linux_setregid16_args *args)
369 struct setregid_args bsd;
372 LIN_SDT_PROBE2(uid16, linux_setregid16, entry, args->rgid, args->egid);
374 bsd.rgid = CAST_NOCHG(args->rgid);
375 bsd.egid = CAST_NOCHG(args->egid);
376 error = sys_setregid(td, &bsd);
378 LIN_SDT_PROBE1(uid16, linux_setregid16, return, error);
383 linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args)
385 struct setreuid_args bsd;
388 LIN_SDT_PROBE2(uid16, linux_setreuid16, entry, args->ruid, args->euid);
390 bsd.ruid = CAST_NOCHG(args->ruid);
391 bsd.euid = CAST_NOCHG(args->euid);
392 error = sys_setreuid(td, &bsd);
394 LIN_SDT_PROBE1(uid16, linux_setreuid16, return, error);
399 linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args)
401 struct setresgid_args bsd;
404 LIN_SDT_PROBE3(uid16, linux_setresgid16, entry, args->rgid, args->egid,
407 bsd.rgid = CAST_NOCHG(args->rgid);
408 bsd.egid = CAST_NOCHG(args->egid);
409 bsd.sgid = CAST_NOCHG(args->sgid);
410 error = sys_setresgid(td, &bsd);
412 LIN_SDT_PROBE1(uid16, linux_setresgid16, return, error);
417 linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args)
419 struct setresuid_args bsd;
422 LIN_SDT_PROBE3(uid16, linux_setresuid16, entry, args->ruid, args->euid,
425 bsd.ruid = CAST_NOCHG(args->ruid);
426 bsd.euid = CAST_NOCHG(args->euid);
427 bsd.suid = CAST_NOCHG(args->suid);
428 error = sys_setresuid(td, &bsd);
430 LIN_SDT_PROBE1(uid16, linux_setresuid16, return, error);