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>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/sysproto.h>
43 #include <sys/systm.h>
46 #include <machine/../linux32/linux.h>
47 #include <machine/../linux32/linux32_proto.h>
49 #include <machine/../linux/linux.h>
50 #include <machine/../linux/linux_proto.h>
53 #include <compat/linux/linux_dtrace.h>
54 #include <compat/linux/linux_util.h>
57 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
60 * DTrace probes in this module.
62 LIN_SDT_PROBE_DEFINE3(uid16, linux_chown16, entry, "char *", "l_uid16_t",
64 LIN_SDT_PROBE_DEFINE1(uid16, linux_chown16, conv_path, "char *");
65 LIN_SDT_PROBE_DEFINE1(uid16, linux_chown16, return, "int");
66 LIN_SDT_PROBE_DEFINE3(uid16, linux_lchown16, entry, "char *", "l_uid16_t",
68 LIN_SDT_PROBE_DEFINE1(uid16, linux_lchown16, conv_path, "char *");
69 LIN_SDT_PROBE_DEFINE1(uid16, linux_lchown16, return, "int");
70 LIN_SDT_PROBE_DEFINE2(uid16, linux_setgroups16, entry, "l_uint", "l_gid16_t *");
71 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, copyin_error, "int");
72 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, priv_check_cred_error, "int");
73 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, return, "int");
74 LIN_SDT_PROBE_DEFINE2(uid16, linux_getgroups16, entry, "l_uint", "l_gid16_t *");
75 LIN_SDT_PROBE_DEFINE1(uid16, linux_getgroups16, copyout_error, "int");
76 LIN_SDT_PROBE_DEFINE1(uid16, linux_getgroups16, return, "int");
77 LIN_SDT_PROBE_DEFINE0(uid16, linux_getgid16, entry);
78 LIN_SDT_PROBE_DEFINE1(uid16, linux_getgid16, return, "int");
79 LIN_SDT_PROBE_DEFINE0(uid16, linux_getuid16, entry);
80 LIN_SDT_PROBE_DEFINE1(uid16, linux_getuid16, return, "int");
81 LIN_SDT_PROBE_DEFINE0(uid16, linux_getegid16, entry);
82 LIN_SDT_PROBE_DEFINE1(uid16, linux_getegid16, return, "int");
83 LIN_SDT_PROBE_DEFINE0(uid16, linux_geteuid16, entry);
84 LIN_SDT_PROBE_DEFINE1(uid16, linux_geteuid16, return, "int");
85 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgid16, entry, "l_gid16_t");
86 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgid16, return, "int");
87 LIN_SDT_PROBE_DEFINE1(uid16, linux_setuid16, entry, "l_uid16_t");
88 LIN_SDT_PROBE_DEFINE1(uid16, linux_setuid16, return, "int");
89 LIN_SDT_PROBE_DEFINE2(uid16, linux_setregid16, entry, "l_gid16_t", "l_gid16_t");
90 LIN_SDT_PROBE_DEFINE1(uid16, linux_setregid16, return, "int");
91 LIN_SDT_PROBE_DEFINE2(uid16, linux_setreuid16, entry, "l_uid16_t", "l_uid16_t");
92 LIN_SDT_PROBE_DEFINE1(uid16, linux_setreuid16, return, "int");
93 LIN_SDT_PROBE_DEFINE3(uid16, linux_setresgid16, entry, "l_gid16_t", "l_gid16_t",
95 LIN_SDT_PROBE_DEFINE1(uid16, linux_setresgid16, return, "int");
96 LIN_SDT_PROBE_DEFINE3(uid16, linux_setresuid16, entry, "l_uid16_t", "l_uid16_t",
98 LIN_SDT_PROBE_DEFINE1(uid16, linux_setresuid16, return, "int");
105 #define CAST_NOCHG(x) ((x == 0xFFFF) ? -1 : x)
108 linux_chown16(struct thread *td, struct linux_chown16_args *args)
113 LCONVPATHEXIST(td, args->path, &path);
116 * The DTrace probes have to be after the LCONVPATHEXIST, as
117 * LCONVPATHEXIST may return on its own and we do not want to
118 * have a stray entry without the corresponding return.
120 LIN_SDT_PROBE3(uid16, linux_chown16, entry, args->path, args->uid,
122 LIN_SDT_PROBE1(uid16, linux_chown16, conv_path, path);
124 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE,
125 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), 0);
128 LIN_SDT_PROBE1(uid16, linux_chown16, return, error);
133 linux_lchown16(struct thread *td, struct linux_lchown16_args *args)
138 LCONVPATHEXIST(td, args->path, &path);
141 * The DTrace probes have to be after the LCONVPATHEXIST, as
142 * LCONVPATHEXIST may return on its own and we do not want to
143 * have a stray entry without the corresponding return.
145 LIN_SDT_PROBE3(uid16, linux_lchown16, entry, args->path, args->uid,
147 LIN_SDT_PROBE1(uid16, linux_lchown16, conv_path, path);
149 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE,
150 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), AT_SYMLINK_NOFOLLOW);
153 LIN_SDT_PROBE1(uid16, linux_lchown16, return, error);
158 linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args)
160 struct ucred *newcred, *oldcred;
161 l_gid16_t *linux_gidset;
166 LIN_SDT_PROBE2(uid16, linux_setgroups16, entry, args->gidsetsize,
169 ngrp = args->gidsetsize;
170 if (ngrp < 0 || ngrp >= ngroups_max + 1) {
171 LIN_SDT_PROBE1(uid16, linux_setgroups16, return, EINVAL);
174 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
175 error = copyin(args->gidset, linux_gidset, ngrp * sizeof(l_gid16_t));
177 LIN_SDT_PROBE1(uid16, linux_setgroups16, copyin_error, error);
178 LIN_SDT_PROBE1(uid16, linux_setgroups16, return, error);
179 free(linux_gidset, M_LINUX);
185 oldcred = crcopysafe(p, newcred);
188 * cr_groups[0] holds egid. Setting the whole set from
189 * the supplied set will cause egid to be changed too.
190 * Keep cr_groups[0] unchanged to prevent that.
193 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
197 LIN_SDT_PROBE1(uid16, linux_setgroups16, priv_check_cred_error,
203 newcred->cr_ngroups = ngrp + 1;
205 bsd_gidset = newcred->cr_groups;
208 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
213 newcred->cr_ngroups = 1;
215 setsugid(td->td_proc);
216 proc_set_cred(p, newcred);
221 free(linux_gidset, M_LINUX);
223 LIN_SDT_PROBE1(uid16, linux_setgroups16, return, error);
228 linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args)
231 l_gid16_t *linux_gidset;
233 int bsd_gidsetsz, ngrp, error;
235 LIN_SDT_PROBE2(uid16, linux_getgroups16, entry, args->gidsetsize,
239 bsd_gidset = cred->cr_groups;
240 bsd_gidsetsz = cred->cr_ngroups - 1;
243 * cr_groups[0] holds egid. Returning the whole set
244 * here will cause a duplicate. Exclude cr_groups[0]
248 if ((ngrp = args->gidsetsize) == 0) {
249 td->td_retval[0] = bsd_gidsetsz;
251 LIN_SDT_PROBE1(uid16, linux_getgroups16, return, 0);
255 if (ngrp < bsd_gidsetsz) {
256 LIN_SDT_PROBE1(uid16, linux_getgroups16, return, EINVAL);
261 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
263 while (ngrp < bsd_gidsetsz) {
264 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
268 error = copyout(linux_gidset, args->gidset, ngrp * sizeof(l_gid16_t));
269 free(linux_gidset, M_LINUX);
271 LIN_SDT_PROBE1(uid16, linux_getgroups16, copyout_error, error);
272 LIN_SDT_PROBE1(uid16, linux_getgroups16, return, error);
276 td->td_retval[0] = ngrp;
278 LIN_SDT_PROBE1(uid16, linux_getgroups16, return, 0);
283 * The FreeBSD native getgid(2) and getuid(2) also modify td->td_retval[1]
284 * when COMPAT_43 is defined. This clobbers registers that are assumed to
285 * be preserved. The following lightweight syscalls fixes this. See also
286 * linux_getpid(2), linux_getgid(2) and linux_getuid(2) in linux_misc.c
288 * linux_getgid16() - MP SAFE
289 * linux_getuid16() - MP SAFE
293 linux_getgid16(struct thread *td, struct linux_getgid16_args *args)
296 LIN_SDT_PROBE0(uid16, linux_getgid16, entry);
298 td->td_retval[0] = td->td_ucred->cr_rgid;
300 LIN_SDT_PROBE1(uid16, linux_getgid16, return, 0);
305 linux_getuid16(struct thread *td, struct linux_getuid16_args *args)
308 LIN_SDT_PROBE0(uid16, linux_getuid16, entry);
310 td->td_retval[0] = td->td_ucred->cr_ruid;
312 LIN_SDT_PROBE1(uid16, linux_getuid16, return, 0);
317 linux_getegid16(struct thread *td, struct linux_getegid16_args *args)
319 struct getegid_args bsd;
322 LIN_SDT_PROBE0(uid16, linux_getegid16, entry);
324 error = sys_getegid(td, &bsd);
326 LIN_SDT_PROBE1(uid16, linux_getegid16, return, error);
331 linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args)
333 struct geteuid_args bsd;
336 LIN_SDT_PROBE0(uid16, linux_geteuid16, entry);
338 error = sys_geteuid(td, &bsd);
340 LIN_SDT_PROBE1(uid16, linux_geteuid16, return, error);
345 linux_setgid16(struct thread *td, struct linux_setgid16_args *args)
347 struct setgid_args bsd;
350 LIN_SDT_PROBE1(uid16, linux_setgid16, entry, args->gid);
353 error = sys_setgid(td, &bsd);
355 LIN_SDT_PROBE1(uid16, linux_setgid16, return, error);
360 linux_setuid16(struct thread *td, struct linux_setuid16_args *args)
362 struct setuid_args bsd;
365 LIN_SDT_PROBE1(uid16, linux_setuid16, entry, args->uid);
368 error = sys_setuid(td, &bsd);
370 LIN_SDT_PROBE1(uid16, linux_setuid16, return, error);
375 linux_setregid16(struct thread *td, struct linux_setregid16_args *args)
377 struct setregid_args bsd;
380 LIN_SDT_PROBE2(uid16, linux_setregid16, entry, args->rgid, args->egid);
382 bsd.rgid = CAST_NOCHG(args->rgid);
383 bsd.egid = CAST_NOCHG(args->egid);
384 error = sys_setregid(td, &bsd);
386 LIN_SDT_PROBE1(uid16, linux_setregid16, return, error);
391 linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args)
393 struct setreuid_args bsd;
396 LIN_SDT_PROBE2(uid16, linux_setreuid16, entry, args->ruid, args->euid);
398 bsd.ruid = CAST_NOCHG(args->ruid);
399 bsd.euid = CAST_NOCHG(args->euid);
400 error = sys_setreuid(td, &bsd);
402 LIN_SDT_PROBE1(uid16, linux_setreuid16, return, error);
407 linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args)
409 struct setresgid_args bsd;
412 LIN_SDT_PROBE3(uid16, linux_setresgid16, entry, args->rgid, args->egid,
415 bsd.rgid = CAST_NOCHG(args->rgid);
416 bsd.egid = CAST_NOCHG(args->egid);
417 bsd.sgid = CAST_NOCHG(args->sgid);
418 error = sys_setresgid(td, &bsd);
420 LIN_SDT_PROBE1(uid16, linux_setresgid16, return, error);
425 linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args)
427 struct setresuid_args bsd;
430 LIN_SDT_PROBE3(uid16, linux_setresuid16, entry, args->ruid, args->euid,
433 bsd.ruid = CAST_NOCHG(args->ruid);
434 bsd.euid = CAST_NOCHG(args->euid);
435 bsd.suid = CAST_NOCHG(args->suid);
436 error = sys_setresuid(td, &bsd);
438 LIN_SDT_PROBE1(uid16, linux_setresuid16, return, error);