xref: /freebsd/sys/compat/linux/linux_uid16.c (revision d5728351a58b87764f3302eb8c4ca9f35e99e8b6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/fcntl.h>
31 #include <sys/lock.h>
32 #include <sys/malloc.h>
33 #include <sys/mutex.h>
34 #include <sys/priv.h>
35 #include <sys/proc.h>
36 #include <sys/syscallsubr.h>
37 #include <sys/sysproto.h>
38 
39 #ifdef COMPAT_LINUX32
40 #include <machine/../linux32/linux.h>
41 #include <machine/../linux32/linux32_proto.h>
42 #else
43 #include <machine/../linux/linux.h>
44 #include <machine/../linux/linux_proto.h>
45 #endif
46 
47 #include <compat/linux/linux_dtrace.h>
48 #include <compat/linux/linux_util.h>
49 
50 /* DTrace init */
51 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
52 
53 /**
54  * DTrace probes in this module.
55  */
56 LIN_SDT_PROBE_DEFINE1(uid16, linux_chown16, conv_path, "char *");
57 LIN_SDT_PROBE_DEFINE1(uid16, linux_lchown16, conv_path, "char *");
58 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, copyin_error, "int");
59 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, priv_check_cred_error, "int");
60 LIN_SDT_PROBE_DEFINE1(uid16, linux_getgroups16, copyout_error, "int");
61 
62 DUMMY(getresuid16);
63 DUMMY(getresgid16);
64 
65 #define	CAST_NOCHG(x)	((x == 0xFFFF) ? -1 : x)
66 
67 int
68 linux_chown16(struct thread *td, struct linux_chown16_args *args)
69 {
70 
71 	return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
72 	    CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), 0));
73 }
74 
75 int
76 linux_lchown16(struct thread *td, struct linux_lchown16_args *args)
77 {
78 
79 	return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
80 	    CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), AT_SYMLINK_NOFOLLOW));
81 }
82 
83 int
84 linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args)
85 {
86 	const int ngrp = args->gidsetsize;
87 	struct ucred *newcred, *oldcred;
88 	l_gid16_t *linux_gidset;
89 	int error;
90 	struct proc *p;
91 
92 	if (ngrp < 0 || ngrp > ngroups_max)
93 		return (EINVAL);
94 	linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
95 	error = copyin(args->gidset, linux_gidset, ngrp * sizeof(l_gid16_t));
96 	if (error) {
97 		LIN_SDT_PROBE1(uid16, linux_setgroups16, copyin_error, error);
98 		free(linux_gidset, M_LINUX);
99 		return (error);
100 	}
101 
102 	newcred = crget();
103 	crextend(newcred, ngrp);
104 	p = td->td_proc;
105 	PROC_LOCK(p);
106 	oldcred = crcopysafe(p, newcred);
107 
108 	if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
109 		PROC_UNLOCK(p);
110 		crfree(newcred);
111 
112 		LIN_SDT_PROBE1(uid16, linux_setgroups16, priv_check_cred_error,
113 		    error);
114 		goto out;
115 	}
116 
117 	newcred->cr_ngroups = ngrp;
118 	for (int i = 0; i < ngrp; i++)
119 		newcred->cr_groups[i] = linux_gidset[i];
120 	newcred->cr_flags |= CRED_FLAG_GROUPSET;
121 
122 	setsugid(td->td_proc);
123 	proc_set_cred(p, newcred);
124 	PROC_UNLOCK(p);
125 	crfree(oldcred);
126 	error = 0;
127 out:
128 	free(linux_gidset, M_LINUX);
129 
130 	return (error);
131 }
132 
133 int
134 linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args)
135 {
136 	const struct ucred *const cred = td->td_ucred;
137 	l_gid16_t *linux_gidset;
138 	int ngrp, error;
139 
140 	ngrp = args->gidsetsize;
141 
142 	if (ngrp == 0) {
143 		td->td_retval[0] = cred->cr_ngroups;
144 		return (0);
145 	}
146 	if (ngrp < cred->cr_ngroups)
147 		return (EINVAL);
148 
149 	ngrp = cred->cr_ngroups;
150 
151 	linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
152 	for (int i = 0; i < ngrp; ++i)
153 		linux_gidset[i] = cred->cr_groups[i];
154 
155 	error = copyout(linux_gidset, args->gidset, ngrp * sizeof(l_gid16_t));
156 	free(linux_gidset, M_LINUX);
157 
158 	if (error != 0) {
159 		LIN_SDT_PROBE1(uid16, linux_getgroups16, copyout_error, error);
160 		return (error);
161 	}
162 
163 	td->td_retval[0] = ngrp;
164 
165 	return (0);
166 }
167 
168 int
169 linux_getgid16(struct thread *td, struct linux_getgid16_args *args)
170 {
171 
172 	td->td_retval[0] = td->td_ucred->cr_rgid;
173 
174 	return (0);
175 }
176 
177 int
178 linux_getuid16(struct thread *td, struct linux_getuid16_args *args)
179 {
180 
181 	td->td_retval[0] = td->td_ucred->cr_ruid;
182 
183 	return (0);
184 }
185 
186 int
187 linux_getegid16(struct thread *td, struct linux_getegid16_args *args)
188 {
189 	struct getegid_args bsd;
190 	int error;
191 
192 	error = sys_getegid(td, &bsd);
193 
194 	return (error);
195 }
196 
197 int
198 linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args)
199 {
200 	struct geteuid_args bsd;
201 	int error;
202 
203 	error = sys_geteuid(td, &bsd);
204 
205 	return (error);
206 }
207 
208 int
209 linux_setgid16(struct thread *td, struct linux_setgid16_args *args)
210 {
211 	struct setgid_args bsd;
212 	int error;
213 
214 	bsd.gid = args->gid;
215 	error = sys_setgid(td, &bsd);
216 
217 	return (error);
218 }
219 
220 int
221 linux_setuid16(struct thread *td, struct linux_setuid16_args *args)
222 {
223 	struct setuid_args bsd;
224 	int error;
225 
226 	bsd.uid = args->uid;
227 	error = sys_setuid(td, &bsd);
228 
229 	return (error);
230 }
231 
232 int
233 linux_setregid16(struct thread *td, struct linux_setregid16_args *args)
234 {
235 	struct setregid_args bsd;
236 	int error;
237 
238 	bsd.rgid = CAST_NOCHG(args->rgid);
239 	bsd.egid = CAST_NOCHG(args->egid);
240 	error = sys_setregid(td, &bsd);
241 
242 	return (error);
243 }
244 
245 int
246 linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args)
247 {
248 	struct setreuid_args bsd;
249 	int error;
250 
251 	bsd.ruid = CAST_NOCHG(args->ruid);
252 	bsd.euid = CAST_NOCHG(args->euid);
253 	error = sys_setreuid(td, &bsd);
254 
255 	return (error);
256 }
257 
258 int
259 linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args)
260 {
261 	struct setresgid_args bsd;
262 	int error;
263 
264 	bsd.rgid = CAST_NOCHG(args->rgid);
265 	bsd.egid = CAST_NOCHG(args->egid);
266 	bsd.sgid = CAST_NOCHG(args->sgid);
267 	error = sys_setresgid(td, &bsd);
268 
269 	return (error);
270 }
271 
272 int
273 linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args)
274 {
275 	struct setresuid_args bsd;
276 	int error;
277 
278 	bsd.ruid = CAST_NOCHG(args->ruid);
279 	bsd.euid = CAST_NOCHG(args->euid);
280 	bsd.suid = CAST_NOCHG(args->suid);
281 	error = sys_setresuid(td, &bsd);
282 
283 	return (error);
284 }
285 
286 int
287 linux_setfsuid16(struct thread *td, struct linux_setfsuid16_args *args)
288 {
289 	td->td_retval[0] = td->td_ucred->cr_uid;
290 	return (0);
291 }
292 
293 int
294 linux_setfsgid16(struct thread *td, struct linux_setfsgid16_args *args)
295 {
296 	td->td_retval[0] = td->td_ucred->cr_gid;
297 	return (0);
298 }
299