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