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