Fix minor bug in last commit, add the required register keyword back in
[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 $
dadab5e9 27 * $DragonFly: src/sys/emulation/linux/linux_uid16.c,v 1.4 2003/06/25 03:55:44 dillon Exp $
984263bc
MD
28 */
29
30#include "opt_compat.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/proc.h>
35#include <sys/sysproto.h>
36
37#include <machine/../linux/linux.h>
38#include <machine/../linux/linux_proto.h>
39#include <compat/linux/linux_util.h>
40
41DUMMY(setfsuid16);
42DUMMY(setfsgid16);
43DUMMY(getresuid16);
44DUMMY(getresgid16);
45
46#define CAST_NOCHG(x) (x == 0xFFFF) ? -1 : x;
47
48int
41c20dac 49linux_chown16(struct linux_chown16_args *args)
984263bc
MD
50{
51 struct chown_args bsd;
52 caddr_t sg;
53
54 sg = stackgap_init();
41c20dac 55 CHECKALTEXIST(&sg, args->path);
984263bc
MD
56
57#ifdef DEBUG
58 if (ldebug(chown16))
59 printf(ARGS(chown16, "%s, %d, %d"), args->path, args->uid,
60 args->gid);
61#endif
62
63 bsd.path = args->path;
64 bsd.uid = CAST_NOCHG(args->uid);
65 bsd.gid = CAST_NOCHG(args->gid);
41c20dac 66 return (chown(&bsd));
984263bc
MD
67}
68
69int
41c20dac 70linux_lchown16(struct linux_lchown16_args *args)
984263bc
MD
71{
72 struct lchown_args bsd;
73 caddr_t sg;
74
75 sg = stackgap_init();
41c20dac 76 CHECKALTEXIST(&sg, args->path);
984263bc
MD
77
78#ifdef DEBUG
79 if (ldebug(lchown16))
80 printf(ARGS(lchown16, "%s, %d, %d"), args->path, args->uid,
81 args->gid);
82#endif
83
84 bsd.path = args->path;
85 bsd.uid = CAST_NOCHG(args->uid);
86 bsd.gid = CAST_NOCHG(args->gid);
41c20dac 87 return (lchown(&bsd));
984263bc
MD
88}
89
90int
41c20dac 91linux_setgroups16(struct linux_setgroups16_args *args)
984263bc 92{
41c20dac 93 struct proc *p = curproc;
984263bc
MD
94 struct ucred *newcred, *oldcred;
95 l_gid16_t linux_gidset[NGROUPS];
96 gid_t *bsd_gidset;
97 int ngrp, error;
98
99#ifdef DEBUG
100 if (ldebug(setgroups16))
101 printf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
102#endif
103
104 ngrp = args->gidsetsize;
105 oldcred = p->p_ucred;
106
107 /*
108 * cr_groups[0] holds egid. Setting the whole set from
109 * the supplied set will cause egid to be changed too.
110 * Keep cr_groups[0] unchanged to prevent that.
111 */
112
dadab5e9 113 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
984263bc
MD
114 return (error);
115
116 if (ngrp >= NGROUPS)
117 return (EINVAL);
118
119 newcred = crdup(oldcred);
120 if (ngrp > 0) {
121 error = copyin((caddr_t)args->gidset, linux_gidset,
122 ngrp * sizeof(l_gid16_t));
123 if (error)
124 return (error);
125
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
41c20dac 138 setsugid();
984263bc
MD
139 p->p_ucred = newcred;
140 crfree(oldcred);
141 return (0);
142}
143
144int
41c20dac 145linux_getgroups16(struct linux_getgroups16_args *args)
984263bc 146{
41c20dac 147 struct proc *p = curproc;
984263bc
MD
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 = p->p_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 p->p_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, (caddr_t)args->gidset,
183 ngrp * sizeof(l_gid16_t));
184 if (error)
185 return (error);
186
187 p->p_retval[0] = ngrp;
188 return (0);
189}
190
191/*
192 * The FreeBSD native getgid(2) and getuid(2) also modify p->p_retval[1]
193 * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that
194 * are assumed to be preserved. The following lightweight syscalls fixes
195 * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in
196 * linux_misc.c
197 *
198 * linux_getgid16() - MP SAFE
199 * linux_getuid16() - MP SAFE
200 */
201
202int
41c20dac 203linux_getgid16(struct linux_getgid16_args *args)
984263bc 204{
41c20dac 205 struct proc *p = curproc;
984263bc 206
41c20dac 207 p->p_retval[0] = p->p_ucred->cr_rgid;
984263bc
MD
208 return (0);
209}
210
211int
41c20dac 212linux_getuid16(struct linux_getuid16_args *args)
984263bc 213{
41c20dac 214 struct proc *p = curproc;
984263bc 215
41c20dac 216 p->p_retval[0] = p->p_ucred->cr_ruid;
984263bc
MD
217 return (0);
218}
219
220int
41c20dac 221linux_getegid16(struct linux_getegid16_args *args)
984263bc
MD
222{
223 struct getegid_args bsd;
224
41c20dac 225 return (getegid(&bsd));
984263bc
MD
226}
227
228int
41c20dac 229linux_geteuid16(struct linux_geteuid16_args *args)
984263bc
MD
230{
231 struct geteuid_args bsd;
232
41c20dac 233 return (geteuid(&bsd));
984263bc
MD
234}
235
236int
41c20dac 237linux_setgid16(struct linux_setgid16_args *args)
984263bc
MD
238{
239 struct setgid_args bsd;
240
241 bsd.gid = args->gid;
41c20dac 242 return (setgid(&bsd));
984263bc
MD
243}
244
245int
41c20dac 246linux_setuid16(struct linux_setuid16_args *args)
984263bc
MD
247{
248 struct setuid_args bsd;
249
250 bsd.uid = args->uid;
41c20dac 251 return (setuid(&bsd));
984263bc
MD
252}
253
254int
41c20dac 255linux_setregid16(struct linux_setregid16_args *args)
984263bc
MD
256{
257 struct setregid_args bsd;
258
259 bsd.rgid = CAST_NOCHG(args->rgid);
260 bsd.egid = CAST_NOCHG(args->egid);
41c20dac 261 return (setregid(&bsd));
984263bc
MD
262}
263
264int
41c20dac 265linux_setreuid16(struct linux_setreuid16_args *args)
984263bc
MD
266{
267 struct setreuid_args bsd;
268
269 bsd.ruid = CAST_NOCHG(args->ruid);
270 bsd.euid = CAST_NOCHG(args->euid);
41c20dac 271 return (setreuid(&bsd));
984263bc
MD
272}
273
274int
41c20dac 275linux_setresgid16(struct linux_setresgid16_args *args)
984263bc
MD
276{
277 struct setresgid_args bsd;
278
279 bsd.rgid = CAST_NOCHG(args->rgid);
280 bsd.egid = CAST_NOCHG(args->egid);
281 bsd.sgid = CAST_NOCHG(args->sgid);
41c20dac 282 return (setresgid(&bsd));
984263bc
MD
283}
284
285int
41c20dac 286linux_setresuid16(struct linux_setresuid16_args *args)
984263bc
MD
287{
288 struct setresuid_args bsd;
289
290 bsd.ruid = CAST_NOCHG(args->ruid);
291 bsd.euid = CAST_NOCHG(args->euid);
292 bsd.suid = CAST_NOCHG(args->suid);
41c20dac 293 return (setresuid(&bsd));
984263bc 294}