xref: /freebsd/sys/compat/linux/linux_uid16.c (revision 0c54932d50a0cbffdd083bf6b2e8d587902f90c9)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001  The FreeBSD Project
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_compat.h"
33 
34 #include <sys/fcntl.h>
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/priv.h>
41 #include <sys/proc.h>
42 #include <sys/sdt.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysproto.h>
45 #include <sys/systm.h>
46 
47 #ifdef COMPAT_LINUX32
48 #include <machine/../linux32/linux.h>
49 #include <machine/../linux32/linux32_proto.h>
50 #else
51 #include <machine/../linux/linux.h>
52 #include <machine/../linux/linux_proto.h>
53 #endif
54 
55 #include <compat/linux/linux_dtrace.h>
56 #include <compat/linux/linux_util.h>
57 
58 /* DTrace init */
59 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
60 
61 /**
62  * DTrace probes in this module.
63  */
64 LIN_SDT_PROBE_DEFINE3(uid16, linux_chown16, entry, "char *", "l_uid16_t",
65     "l_gid16_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",
69     "l_gid16_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",
96     "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",
99     "l_uid16_t");
100 LIN_SDT_PROBE_DEFINE1(uid16, linux_setresuid16, return, "int");
101 
102 DUMMY(setfsuid16);
103 DUMMY(setfsgid16);
104 DUMMY(getresuid16);
105 DUMMY(getresgid16);
106 
107 #define	CAST_NOCHG(x)	((x == 0xFFFF) ? -1 : x)
108 
109 int
110 linux_chown16(struct thread *td, struct linux_chown16_args *args)
111 {
112 	char *path;
113 	int error;
114 
115 	if (!LUSECONVPATH(td) && !SDT_PROBES_ENABLED()) {
116 		error = kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
117 		    CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), 0);
118 	} else {
119 		LCONVPATHEXIST(td, args->path, &path);
120 		/*
121 		 * The DTrace probes have to be after the LCONVPATHEXIST, as
122 		 * LCONVPATHEXIST may return on its own and we do not want to
123 		 * have a stray entry without the corresponding return.
124 		 */
125 		LIN_SDT_PROBE3(uid16, linux_chown16, entry, args->path, args->uid,
126 		    args->gid);
127 		LIN_SDT_PROBE1(uid16, linux_chown16, conv_path, path);
128 
129 		error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE,
130 		    CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), 0);
131 		LFREEPATH(path);
132 		LIN_SDT_PROBE1(uid16, linux_chown16, return, error);
133 	}
134 	return (error);
135 }
136 
137 int
138 linux_lchown16(struct thread *td, struct linux_lchown16_args *args)
139 {
140 	char *path;
141 	int error;
142 
143 	if (!LUSECONVPATH(td) && !SDT_PROBES_ENABLED()) {
144 		error = kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
145 		    CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), AT_SYMLINK_NOFOLLOW);
146 	} else {
147 		LCONVPATHEXIST(td, args->path, &path);
148 
149 		/*
150 		 * The DTrace probes have to be after the LCONVPATHEXIST, as
151 		 * LCONVPATHEXIST may return on its own and we do not want to
152 		 * have a stray entry without the corresponding return.
153 		 */
154 		LIN_SDT_PROBE3(uid16, linux_lchown16, entry, args->path, args->uid,
155 		    args->gid);
156 		LIN_SDT_PROBE1(uid16, linux_lchown16, conv_path, path);
157 
158 		error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE,
159 		    CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), AT_SYMLINK_NOFOLLOW);
160 		LFREEPATH(path);
161 		LIN_SDT_PROBE1(uid16, linux_lchown16, return, error);
162 	}
163 	return (error);
164 }
165 
166 int
167 linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args)
168 {
169 	struct ucred *newcred, *oldcred;
170 	l_gid16_t *linux_gidset;
171 	gid_t *bsd_gidset;
172 	int ngrp, error;
173 	struct proc *p;
174 
175 	LIN_SDT_PROBE2(uid16, linux_setgroups16, entry, args->gidsetsize,
176 	    args->gidset);
177 
178 	ngrp = args->gidsetsize;
179 	if (ngrp < 0 || ngrp >= ngroups_max + 1) {
180 		LIN_SDT_PROBE1(uid16, linux_setgroups16, return, EINVAL);
181 		return (EINVAL);
182 	}
183 	linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
184 	error = copyin(args->gidset, linux_gidset, ngrp * sizeof(l_gid16_t));
185 	if (error) {
186 		LIN_SDT_PROBE1(uid16, linux_setgroups16, copyin_error, error);
187 		LIN_SDT_PROBE1(uid16, linux_setgroups16, return, error);
188 		free(linux_gidset, M_LINUX);
189 		return (error);
190 	}
191 	newcred = crget();
192 	p = td->td_proc;
193 	PROC_LOCK(p);
194 	oldcred = crcopysafe(p, newcred);
195 
196 	/*
197 	 * cr_groups[0] holds egid. Setting the whole set from
198 	 * the supplied set will cause egid to be changed too.
199 	 * Keep cr_groups[0] unchanged to prevent that.
200 	 */
201 
202 	if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
203 		PROC_UNLOCK(p);
204 		crfree(newcred);
205 
206 		LIN_SDT_PROBE1(uid16, linux_setgroups16, priv_check_cred_error,
207 		    error);
208 		goto out;
209 	}
210 
211 	if (ngrp > 0) {
212 		newcred->cr_ngroups = ngrp + 1;
213 
214 		bsd_gidset = newcred->cr_groups;
215 		ngrp--;
216 		while (ngrp >= 0) {
217 			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
218 			ngrp--;
219 		}
220 	}
221 	else
222 		newcred->cr_ngroups = 1;
223 
224 	setsugid(td->td_proc);
225 	proc_set_cred(p, newcred);
226 	PROC_UNLOCK(p);
227 	crfree(oldcred);
228 	error = 0;
229 out:
230 	free(linux_gidset, M_LINUX);
231 
232 	LIN_SDT_PROBE1(uid16, linux_setgroups16, return, error);
233 	return (error);
234 }
235 
236 int
237 linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args)
238 {
239 	struct ucred *cred;
240 	l_gid16_t *linux_gidset;
241 	gid_t *bsd_gidset;
242 	int bsd_gidsetsz, ngrp, error;
243 
244 	LIN_SDT_PROBE2(uid16, linux_getgroups16, entry, args->gidsetsize,
245 	    args->gidset);
246 
247 	cred = td->td_ucred;
248 	bsd_gidset = cred->cr_groups;
249 	bsd_gidsetsz = cred->cr_ngroups - 1;
250 
251 	/*
252 	 * cr_groups[0] holds egid. Returning the whole set
253 	 * here will cause a duplicate. Exclude cr_groups[0]
254 	 * to prevent that.
255 	 */
256 
257 	if ((ngrp = args->gidsetsize) == 0) {
258 		td->td_retval[0] = bsd_gidsetsz;
259 
260 		LIN_SDT_PROBE1(uid16, linux_getgroups16, return, 0);
261 		return (0);
262 	}
263 
264 	if (ngrp < bsd_gidsetsz) {
265 		LIN_SDT_PROBE1(uid16, linux_getgroups16, return, EINVAL);
266 		return (EINVAL);
267 	}
268 
269 	ngrp = 0;
270 	linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
271 	    M_LINUX, M_WAITOK);
272 	while (ngrp < bsd_gidsetsz) {
273 		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
274 		ngrp++;
275 	}
276 
277 	error = copyout(linux_gidset, args->gidset, ngrp * sizeof(l_gid16_t));
278 	free(linux_gidset, M_LINUX);
279 	if (error) {
280 		LIN_SDT_PROBE1(uid16, linux_getgroups16, copyout_error, error);
281 		LIN_SDT_PROBE1(uid16, linux_getgroups16, return, error);
282 		return (error);
283 	}
284 
285 	td->td_retval[0] = ngrp;
286 
287 	LIN_SDT_PROBE1(uid16, linux_getgroups16, return, 0);
288 	return (0);
289 }
290 
291 int
292 linux_getgid16(struct thread *td, struct linux_getgid16_args *args)
293 {
294 
295 	LIN_SDT_PROBE0(uid16, linux_getgid16, entry);
296 
297 	td->td_retval[0] = td->td_ucred->cr_rgid;
298 
299 	LIN_SDT_PROBE1(uid16, linux_getgid16, return, 0);
300 	return (0);
301 }
302 
303 int
304 linux_getuid16(struct thread *td, struct linux_getuid16_args *args)
305 {
306 
307 	LIN_SDT_PROBE0(uid16, linux_getuid16, entry);
308 
309 	td->td_retval[0] = td->td_ucred->cr_ruid;
310 
311 	LIN_SDT_PROBE1(uid16, linux_getuid16, return, 0);
312 	return (0);
313 }
314 
315 int
316 linux_getegid16(struct thread *td, struct linux_getegid16_args *args)
317 {
318 	struct getegid_args bsd;
319 	int error;
320 
321 	LIN_SDT_PROBE0(uid16, linux_getegid16, entry);
322 
323 	error = sys_getegid(td, &bsd);
324 
325 	LIN_SDT_PROBE1(uid16, linux_getegid16, return, error);
326 	return (error);
327 }
328 
329 int
330 linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args)
331 {
332 	struct geteuid_args bsd;
333 	int error;
334 
335 	LIN_SDT_PROBE0(uid16, linux_geteuid16, entry);
336 
337 	error = sys_geteuid(td, &bsd);
338 
339 	LIN_SDT_PROBE1(uid16, linux_geteuid16, return, error);
340 	return (error);
341 }
342 
343 int
344 linux_setgid16(struct thread *td, struct linux_setgid16_args *args)
345 {
346 	struct setgid_args bsd;
347 	int error;
348 
349 	LIN_SDT_PROBE1(uid16, linux_setgid16, entry, args->gid);
350 
351 	bsd.gid = args->gid;
352 	error = sys_setgid(td, &bsd);
353 
354 	LIN_SDT_PROBE1(uid16, linux_setgid16, return, error);
355 	return (error);
356 }
357 
358 int
359 linux_setuid16(struct thread *td, struct linux_setuid16_args *args)
360 {
361 	struct setuid_args bsd;
362 	int error;
363 
364 	LIN_SDT_PROBE1(uid16, linux_setuid16, entry, args->uid);
365 
366 	bsd.uid = args->uid;
367 	error = sys_setuid(td, &bsd);
368 
369 	LIN_SDT_PROBE1(uid16, linux_setuid16, return, error);
370 	return (error);
371 }
372 
373 int
374 linux_setregid16(struct thread *td, struct linux_setregid16_args *args)
375 {
376 	struct setregid_args bsd;
377 	int error;
378 
379 	LIN_SDT_PROBE2(uid16, linux_setregid16, entry, args->rgid, args->egid);
380 
381 	bsd.rgid = CAST_NOCHG(args->rgid);
382 	bsd.egid = CAST_NOCHG(args->egid);
383 	error = sys_setregid(td, &bsd);
384 
385 	LIN_SDT_PROBE1(uid16, linux_setregid16, return, error);
386 	return (error);
387 }
388 
389 int
390 linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args)
391 {
392 	struct setreuid_args bsd;
393 	int error;
394 
395 	LIN_SDT_PROBE2(uid16, linux_setreuid16, entry, args->ruid, args->euid);
396 
397 	bsd.ruid = CAST_NOCHG(args->ruid);
398 	bsd.euid = CAST_NOCHG(args->euid);
399 	error = sys_setreuid(td, &bsd);
400 
401 	LIN_SDT_PROBE1(uid16, linux_setreuid16, return, error);
402 	return (error);
403 }
404 
405 int
406 linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args)
407 {
408 	struct setresgid_args bsd;
409 	int error;
410 
411 	LIN_SDT_PROBE3(uid16, linux_setresgid16, entry, args->rgid, args->egid,
412 	    args->sgid);
413 
414 	bsd.rgid = CAST_NOCHG(args->rgid);
415 	bsd.egid = CAST_NOCHG(args->egid);
416 	bsd.sgid = CAST_NOCHG(args->sgid);
417 	error = sys_setresgid(td, &bsd);
418 
419 	LIN_SDT_PROBE1(uid16, linux_setresgid16, return, error);
420 	return (error);
421 }
422 
423 int
424 linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args)
425 {
426 	struct setresuid_args bsd;
427 	int error;
428 
429 	LIN_SDT_PROBE3(uid16, linux_setresuid16, entry, args->ruid, args->euid,
430 	    args->suid);
431 
432 	bsd.ruid = CAST_NOCHG(args->ruid);
433 	bsd.euid = CAST_NOCHG(args->euid);
434 	bsd.suid = CAST_NOCHG(args->suid);
435 	error = sys_setresuid(td, &bsd);
436 
437 	LIN_SDT_PROBE1(uid16, linux_setresuid16, return, error);
438 	return (error);
439 }
440