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