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