xref: /freebsd/sys/compat/linux/linux_uid16.c (revision 0f8a8416a4e07ddeaecc4eafd6f418da0f21efc7)
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(setfsuid16);
63 DUMMY(setfsgid16);
64 DUMMY(getresuid16);
65 DUMMY(getresgid16);
66 
67 #define	CAST_NOCHG(x)	((x == 0xFFFF) ? -1 : x)
68 
69 int
70 linux_chown16(struct thread *td, struct linux_chown16_args *args)
71 {
72 
73 	return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
74 	    CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), 0));
75 }
76 
77 int
78 linux_lchown16(struct thread *td, struct linux_lchown16_args *args)
79 {
80 
81 	return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
82 	    CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), AT_SYMLINK_NOFOLLOW));
83 }
84 
85 int
86 linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args)
87 {
88 	struct ucred *newcred, *oldcred;
89 	l_gid16_t *linux_gidset;
90 	int ngrp, error;
91 	struct proc *p;
92 
93 	ngrp = args->gidsetsize;
94 	if (ngrp < 0 || ngrp >= ngroups_max)
95 		return (EINVAL);
96 	linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
97 	error = copyin(args->gidset, linux_gidset, ngrp * sizeof(l_gid16_t));
98 	if (error) {
99 		LIN_SDT_PROBE1(uid16, linux_setgroups16, copyin_error, error);
100 		free(linux_gidset, M_LINUX);
101 		return (error);
102 	}
103 	newcred = crget();
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 
121 	setsugid(td->td_proc);
122 	proc_set_cred(p, newcred);
123 	PROC_UNLOCK(p);
124 	crfree(oldcred);
125 	error = 0;
126 out:
127 	free(linux_gidset, M_LINUX);
128 
129 	return (error);
130 }
131 
132 int
133 linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args)
134 {
135 	struct ucred *cred;
136 	l_gid16_t *linux_gidset;
137 	gid_t *bsd_gidset;
138 	int bsd_gidsetsz, ngrp, error;
139 
140 	cred = td->td_ucred;
141 	bsd_gidset = cred->cr_groups;
142 	bsd_gidsetsz = cred->cr_ngroups;
143 
144 	if ((ngrp = args->gidsetsize) == 0) {
145 		td->td_retval[0] = bsd_gidsetsz;
146 		return (0);
147 	}
148 
149 	if (ngrp < bsd_gidsetsz)
150 		return (EINVAL);
151 
152 	ngrp = 0;
153 	linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
154 	    M_LINUX, M_WAITOK);
155 	while (ngrp < bsd_gidsetsz) {
156 		linux_gidset[ngrp] = bsd_gidset[ngrp];
157 		ngrp++;
158 	}
159 
160 	error = copyout(linux_gidset, args->gidset, ngrp * sizeof(l_gid16_t));
161 	free(linux_gidset, M_LINUX);
162 	if (error) {
163 		LIN_SDT_PROBE1(uid16, linux_getgroups16, copyout_error, error);
164 		return (error);
165 	}
166 
167 	td->td_retval[0] = ngrp;
168 
169 	return (0);
170 }
171 
172 int
173 linux_getgid16(struct thread *td, struct linux_getgid16_args *args)
174 {
175 
176 	td->td_retval[0] = td->td_ucred->cr_rgid;
177 
178 	return (0);
179 }
180 
181 int
182 linux_getuid16(struct thread *td, struct linux_getuid16_args *args)
183 {
184 
185 	td->td_retval[0] = td->td_ucred->cr_ruid;
186 
187 	return (0);
188 }
189 
190 int
191 linux_getegid16(struct thread *td, struct linux_getegid16_args *args)
192 {
193 	struct getegid_args bsd;
194 	int error;
195 
196 	error = sys_getegid(td, &bsd);
197 
198 	return (error);
199 }
200 
201 int
202 linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args)
203 {
204 	struct geteuid_args bsd;
205 	int error;
206 
207 	error = sys_geteuid(td, &bsd);
208 
209 	return (error);
210 }
211 
212 int
213 linux_setgid16(struct thread *td, struct linux_setgid16_args *args)
214 {
215 	struct setgid_args bsd;
216 	int error;
217 
218 	bsd.gid = args->gid;
219 	error = sys_setgid(td, &bsd);
220 
221 	return (error);
222 }
223 
224 int
225 linux_setuid16(struct thread *td, struct linux_setuid16_args *args)
226 {
227 	struct setuid_args bsd;
228 	int error;
229 
230 	bsd.uid = args->uid;
231 	error = sys_setuid(td, &bsd);
232 
233 	return (error);
234 }
235 
236 int
237 linux_setregid16(struct thread *td, struct linux_setregid16_args *args)
238 {
239 	struct setregid_args bsd;
240 	int error;
241 
242 	bsd.rgid = CAST_NOCHG(args->rgid);
243 	bsd.egid = CAST_NOCHG(args->egid);
244 	error = sys_setregid(td, &bsd);
245 
246 	return (error);
247 }
248 
249 int
250 linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args)
251 {
252 	struct setreuid_args bsd;
253 	int error;
254 
255 	bsd.ruid = CAST_NOCHG(args->ruid);
256 	bsd.euid = CAST_NOCHG(args->euid);
257 	error = sys_setreuid(td, &bsd);
258 
259 	return (error);
260 }
261 
262 int
263 linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args)
264 {
265 	struct setresgid_args bsd;
266 	int error;
267 
268 	bsd.rgid = CAST_NOCHG(args->rgid);
269 	bsd.egid = CAST_NOCHG(args->egid);
270 	bsd.sgid = CAST_NOCHG(args->sgid);
271 	error = sys_setresgid(td, &bsd);
272 
273 	return (error);
274 }
275 
276 int
277 linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args)
278 {
279 	struct setresuid_args bsd;
280 	int error;
281 
282 	bsd.ruid = CAST_NOCHG(args->ruid);
283 	bsd.euid = CAST_NOCHG(args->euid);
284 	bsd.suid = CAST_NOCHG(args->suid);
285 	error = sys_setresuid(td, &bsd);
286 
287 	return (error);
288 }
289