Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / emulation / linux / linux_uid16.c
CommitLineData
984263bc
MD
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: src/sys/compat/linux/linux_uid16.c,v 1.4.2.1 2001/10/21 03:57:35 marcel Exp $
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
40DUMMY(setfsuid16);
41DUMMY(setfsgid16);
42DUMMY(getresuid16);
43DUMMY(getresgid16);
44
45#define CAST_NOCHG(x) (x == 0xFFFF) ? -1 : x;
46
47int
48linux_chown16(struct proc *p, struct linux_chown16_args *args)
49{
50 struct chown_args bsd;
51 caddr_t sg;
52
53 sg = stackgap_init();
54 CHECKALTEXIST(p, &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(p, &bsd));
66}
67
68int
69linux_lchown16(struct proc *p, struct linux_lchown16_args *args)
70{
71 struct lchown_args bsd;
72 caddr_t sg;
73
74 sg = stackgap_init();
75 CHECKALTEXIST(p, &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(p, &bsd));
87}
88
89int
90linux_setgroups16(struct proc *p, 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 = p->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(p);
137 p->p_ucred = newcred;
138 crfree(oldcred);
139 return (0);
140}
141
142int
143linux_getgroups16(struct proc *p, 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 = p->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 p->p_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 p->p_retval[0] = ngrp;
185 return (0);
186}
187
188/*
189 * The FreeBSD native getgid(2) and getuid(2) also modify p->p_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
199int
200linux_getgid16(struct proc *p, struct linux_getgid16_args *args)
201{
202
203 p->p_retval[0] = p->p_cred->p_rgid;
204 return (0);
205}
206
207int
208linux_getuid16(struct proc *p, struct linux_getuid16_args *args)
209{
210
211 p->p_retval[0] = p->p_cred->p_ruid;
212 return (0);
213}
214
215int
216linux_getegid16(struct proc *p, struct linux_getegid16_args *args)
217{
218 struct getegid_args bsd;
219
220 return (getegid(p, &bsd));
221}
222
223int
224linux_geteuid16(struct proc *p, struct linux_geteuid16_args *args)
225{
226 struct geteuid_args bsd;
227
228 return (geteuid(p, &bsd));
229}
230
231int
232linux_setgid16(struct proc *p, struct linux_setgid16_args *args)
233{
234 struct setgid_args bsd;
235
236 bsd.gid = args->gid;
237 return (setgid(p, &bsd));
238}
239
240int
241linux_setuid16(struct proc *p, struct linux_setuid16_args *args)
242{
243 struct setuid_args bsd;
244
245 bsd.uid = args->uid;
246 return (setuid(p, &bsd));
247}
248
249int
250linux_setregid16(struct proc *p, 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(p, &bsd));
257}
258
259int
260linux_setreuid16(struct proc *p, 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(p, &bsd));
267}
268
269int
270linux_setresgid16(struct proc *p, 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(p, &bsd));
278}
279
280int
281linux_setresuid16(struct proc *p, 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(p, &bsd));
289}