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