xref: /freebsd/sys/compat/linux/linux_uid16.c (revision 10b59a9b4add0320d52c15ce057dd697261e7dfc)
1 /*-
2  * Copyright (c) 2001  The FreeBSD Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_compat.h"
31 
32 #include <sys/fcntl.h>
33 #include <sys/param.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/priv.h>
38 #include <sys/proc.h>
39 #include <sys/syscallsubr.h>
40 #include <sys/sysproto.h>
41 #include <sys/systm.h>
42 
43 #ifdef COMPAT_LINUX32
44 #include <machine/../linux32/linux.h>
45 #include <machine/../linux32/linux32_proto.h>
46 #else
47 #include <machine/../linux/linux.h>
48 #include <machine/../linux/linux_proto.h>
49 #endif
50 
51 #include <compat/linux/linux_util.h>
52 
53 DUMMY(setfsuid16);
54 DUMMY(setfsgid16);
55 DUMMY(getresuid16);
56 DUMMY(getresgid16);
57 
58 #define	CAST_NOCHG(x)	((x == 0xFFFF) ? -1 : x)
59 
60 int
61 linux_chown16(struct thread *td, struct linux_chown16_args *args)
62 {
63 	char *path;
64 	int error;
65 
66 	LCONVPATHEXIST(td, args->path, &path);
67 
68 #ifdef DEBUG
69 	if (ldebug(chown16))
70 		printf(ARGS(chown16, "%s, %d, %d"), path, args->uid, args->gid);
71 #endif
72 	error = kern_chown(td, path, UIO_SYSSPACE, CAST_NOCHG(args->uid),
73 	    CAST_NOCHG(args->gid));
74 	LFREEPATH(path);
75 	return (error);
76 }
77 
78 int
79 linux_lchown16(struct thread *td, struct linux_lchown16_args *args)
80 {
81 	char *path;
82 	int error;
83 
84 	LCONVPATHEXIST(td, args->path, &path);
85 
86 #ifdef DEBUG
87 	if (ldebug(lchown16))
88 		printf(ARGS(lchown16, "%s, %d, %d"), path, args->uid,
89 		    args->gid);
90 #endif
91 	error = kern_lchown(td, path, UIO_SYSSPACE, CAST_NOCHG(args->uid),
92 	    CAST_NOCHG(args->gid));
93 	LFREEPATH(path);
94 	return (error);
95 }
96 
97 int
98 linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args)
99 {
100 	struct ucred *newcred, *oldcred;
101 	l_gid16_t *linux_gidset;
102 	gid_t *bsd_gidset;
103 	int ngrp, error;
104 	struct proc *p;
105 
106 #ifdef DEBUG
107 	if (ldebug(setgroups16))
108 		printf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
109 #endif
110 
111 	ngrp = args->gidsetsize;
112 	if (ngrp < 0 || ngrp >= ngroups_max + 1)
113 		return (EINVAL);
114 	linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
115 	error = copyin(args->gidset, linux_gidset, ngrp * sizeof(l_gid16_t));
116 	if (error) {
117 		free(linux_gidset, M_TEMP);
118 		return (error);
119 	}
120 	newcred = crget();
121 	p = td->td_proc;
122 	PROC_LOCK(p);
123 	oldcred = crcopysafe(p, newcred);
124 
125 	/*
126 	 * cr_groups[0] holds egid. Setting the whole set from
127 	 * the supplied set will cause egid to be changed too.
128 	 * Keep cr_groups[0] unchanged to prevent that.
129 	 */
130 
131 	if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
132 		PROC_UNLOCK(p);
133 		crfree(newcred);
134 		goto out;
135 	}
136 
137 	if (ngrp > 0) {
138 		newcred->cr_ngroups = ngrp + 1;
139 
140 		bsd_gidset = newcred->cr_groups;
141 		ngrp--;
142 		while (ngrp >= 0) {
143 			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
144 			ngrp--;
145 		}
146 	}
147 	else
148 		newcred->cr_ngroups = 1;
149 
150 	setsugid(td->td_proc);
151 	p->p_ucred = newcred;
152 	PROC_UNLOCK(p);
153 	crfree(oldcred);
154 	error = 0;
155 out:
156 	free(linux_gidset, M_TEMP);
157 	return (error);
158 }
159 
160 int
161 linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args)
162 {
163 	struct ucred *cred;
164 	l_gid16_t *linux_gidset;
165 	gid_t *bsd_gidset;
166 	int bsd_gidsetsz, ngrp, error;
167 
168 #ifdef DEBUG
169 	if (ldebug(getgroups16))
170 		printf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
171 #endif
172 
173 	cred = td->td_ucred;
174 	bsd_gidset = cred->cr_groups;
175 	bsd_gidsetsz = cred->cr_ngroups - 1;
176 
177 	/*
178 	 * cr_groups[0] holds egid. Returning the whole set
179 	 * here will cause a duplicate. Exclude cr_groups[0]
180 	 * to prevent that.
181 	 */
182 
183 	if ((ngrp = args->gidsetsize) == 0) {
184 		td->td_retval[0] = bsd_gidsetsz;
185 		return (0);
186 	}
187 
188 	if (ngrp < bsd_gidsetsz)
189 		return (EINVAL);
190 
191 	ngrp = 0;
192 	linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
193 	    M_TEMP, M_WAITOK);
194 	while (ngrp < bsd_gidsetsz) {
195 		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
196 		ngrp++;
197 	}
198 
199 	error = copyout(linux_gidset, args->gidset, ngrp * sizeof(l_gid16_t));
200 	free(linux_gidset, M_TEMP);
201 	if (error)
202 		return (error);
203 
204 	td->td_retval[0] = ngrp;
205 	return (0);
206 }
207 
208 /*
209  * The FreeBSD native getgid(2) and getuid(2) also modify td->td_retval[1]
210  * when COMPAT_43 is defined. This clobbers registers that are assumed to
211  * be preserved. The following lightweight syscalls fixes this. See also
212  * linux_getpid(2), linux_getgid(2) and linux_getuid(2) in linux_misc.c
213  *
214  * linux_getgid16() - MP SAFE
215  * linux_getuid16() - MP SAFE
216  */
217 
218 int
219 linux_getgid16(struct thread *td, struct linux_getgid16_args *args)
220 {
221 
222 	td->td_retval[0] = td->td_ucred->cr_rgid;
223 	return (0);
224 }
225 
226 int
227 linux_getuid16(struct thread *td, struct linux_getuid16_args *args)
228 {
229 
230 	td->td_retval[0] = td->td_ucred->cr_ruid;
231 	return (0);
232 }
233 
234 int
235 linux_getegid16(struct thread *td, struct linux_getegid16_args *args)
236 {
237 	struct getegid_args bsd;
238 
239 	return (sys_getegid(td, &bsd));
240 }
241 
242 int
243 linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args)
244 {
245 	struct geteuid_args bsd;
246 
247 	return (sys_geteuid(td, &bsd));
248 }
249 
250 int
251 linux_setgid16(struct thread *td, struct linux_setgid16_args *args)
252 {
253 	struct setgid_args bsd;
254 
255 	bsd.gid = args->gid;
256 	return (sys_setgid(td, &bsd));
257 }
258 
259 int
260 linux_setuid16(struct thread *td, struct linux_setuid16_args *args)
261 {
262 	struct setuid_args bsd;
263 
264 	bsd.uid = args->uid;
265 	return (sys_setuid(td, &bsd));
266 }
267 
268 int
269 linux_setregid16(struct thread *td, struct linux_setregid16_args *args)
270 {
271 	struct setregid_args bsd;
272 
273 	bsd.rgid = CAST_NOCHG(args->rgid);
274 	bsd.egid = CAST_NOCHG(args->egid);
275 	return (sys_setregid(td, &bsd));
276 }
277 
278 int
279 linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args)
280 {
281 	struct setreuid_args bsd;
282 
283 	bsd.ruid = CAST_NOCHG(args->ruid);
284 	bsd.euid = CAST_NOCHG(args->euid);
285 	return (sys_setreuid(td, &bsd));
286 }
287 
288 int
289 linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args)
290 {
291 	struct setresgid_args bsd;
292 
293 	bsd.rgid = CAST_NOCHG(args->rgid);
294 	bsd.egid = CAST_NOCHG(args->egid);
295 	bsd.sgid = CAST_NOCHG(args->sgid);
296 	return (sys_setresgid(td, &bsd));
297 }
298 
299 int
300 linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args)
301 {
302 	struct setresuid_args bsd;
303 
304 	bsd.ruid = CAST_NOCHG(args->ruid);
305 	bsd.euid = CAST_NOCHG(args->euid);
306 	bsd.suid = CAST_NOCHG(args->suid);
307 	return (sys_setresuid(td, &bsd));
308 }
309