Modify kern/makesyscall.sh to prefix all kernel system call procedures
[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 $
753fd850 27 * $DragonFly: src/sys/emulation/linux/linux_uid16.c,v 1.11 2006/06/05 07:26:09 dillon Exp $
984263bc
MD
28 */
29
30#include "opt_compat.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
136178b3 34#include <sys/kern_syscall.h>
fad57d0e 35#include <sys/nlookup.h>
984263bc
MD
36#include <sys/proc.h>
37#include <sys/sysproto.h>
136178b3 38#include <sys/thread.h>
984263bc 39
932f49b9
MD
40#include <arch_linux/linux.h>
41#include <arch_linux/linux_proto.h>
1f2de5d4 42#include "linux_util.h"
984263bc
MD
43
44DUMMY(setfsuid16);
45DUMMY(setfsgid16);
46DUMMY(getresuid16);
47DUMMY(getresgid16);
48
136178b3 49#define CAST_NOCHG(x) ((x == 0xFFFF) ? -1 : x)
984263bc
MD
50
51int
753fd850 52sys_linux_chown16(struct linux_chown16_args *args)
984263bc 53{
fad57d0e 54 struct nlookupdata nd;
136178b3 55 char *path;
90b9818c 56 int error;
984263bc 57
136178b3
DRJ
58 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
59 if (error)
60 return (error);
984263bc
MD
61#ifdef DEBUG
62 if (ldebug(chown16))
136178b3 63 printf(ARGS(chown16, "%s, %d, %d"), path, args->uid,
984263bc
MD
64 args->gid);
65#endif
fad57d0e
MD
66 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
67 if (error == 0) {
68 error = kern_chown(&nd, CAST_NOCHG(args->uid),
69 CAST_NOCHG(args->gid));
70 }
71 nlookup_done(&nd);
136178b3 72 linux_free_path(&path);
90b9818c 73 return(error);
984263bc
MD
74}
75
76int
753fd850 77sys_linux_lchown16(struct linux_lchown16_args *args)
984263bc 78{
fad57d0e 79 struct nlookupdata nd;
136178b3 80 char *path;
90b9818c 81 int error;
984263bc 82
136178b3
DRJ
83 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
84 if (error)
85 return (error);
984263bc
MD
86#ifdef DEBUG
87 if (ldebug(lchown16))
136178b3 88 printf(ARGS(lchown16, "%s, %d, %d"), path, args->uid,
984263bc
MD
89 args->gid);
90#endif
fad57d0e
MD
91 error = nlookup_init(&nd, path, UIO_SYSSPACE, 0);
92 if (error == 0) {
93 error = kern_chown(&nd, CAST_NOCHG(args->uid),
94 CAST_NOCHG(args->gid));
95 }
96 nlookup_done(&nd);
136178b3 97 linux_free_path(&path);
90b9818c 98 return(error);
984263bc
MD
99}
100
101int
753fd850 102sys_linux_setgroups16(struct linux_setgroups16_args *args)
984263bc 103{
41c20dac 104 struct proc *p = curproc;
984263bc
MD
105 struct ucred *newcred, *oldcred;
106 l_gid16_t linux_gidset[NGROUPS];
107 gid_t *bsd_gidset;
108 int ngrp, error;
109
110#ifdef DEBUG
111 if (ldebug(setgroups16))
112 printf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
113#endif
114
115 ngrp = args->gidsetsize;
116 oldcred = p->p_ucred;
117
118 /*
119 * cr_groups[0] holds egid. Setting the whole set from
120 * the supplied set will cause egid to be changed too.
121 * Keep cr_groups[0] unchanged to prevent that.
122 */
123
dadab5e9 124 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
984263bc
MD
125 return (error);
126
127 if (ngrp >= NGROUPS)
128 return (EINVAL);
129
130 newcred = crdup(oldcred);
131 if (ngrp > 0) {
132 error = copyin((caddr_t)args->gidset, linux_gidset,
133 ngrp * sizeof(l_gid16_t));
134 if (error)
135 return (error);
136
137 newcred->cr_ngroups = ngrp + 1;
138
139 bsd_gidset = newcred->cr_groups;
140 ngrp--;
141 while (ngrp >= 0) {
142 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
143 ngrp--;
144 }
145 }
146 else
147 newcred->cr_ngroups = 1;
148
41c20dac 149 setsugid();
984263bc
MD
150 p->p_ucred = newcred;
151 crfree(oldcred);
152 return (0);
153}
154
155int
753fd850 156sys_linux_getgroups16(struct linux_getgroups16_args *args)
984263bc 157{
41c20dac 158 struct proc *p = curproc;
984263bc
MD
159 struct ucred *cred;
160 l_gid16_t linux_gidset[NGROUPS];
161 gid_t *bsd_gidset;
162 int bsd_gidsetsz, ngrp, error;
163
164#ifdef DEBUG
165 if (ldebug(getgroups16))
166 printf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
167#endif
168
169 cred = p->p_ucred;
170 bsd_gidset = cred->cr_groups;
171 bsd_gidsetsz = cred->cr_ngroups - 1;
172
173 /*
174 * cr_groups[0] holds egid. Returning the whole set
175 * here will cause a duplicate. Exclude cr_groups[0]
176 * to prevent that.
177 */
178
179 if ((ngrp = args->gidsetsize) == 0) {
c7114eea 180 args->sysmsg_result = bsd_gidsetsz;
984263bc
MD
181 return (0);
182 }
183
184 if (ngrp < bsd_gidsetsz)
185 return (EINVAL);
186
187 ngrp = 0;
188 while (ngrp < bsd_gidsetsz) {
189 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
190 ngrp++;
191 }
192
193 error = copyout(linux_gidset, (caddr_t)args->gidset,
194 ngrp * sizeof(l_gid16_t));
195 if (error)
196 return (error);
197
c7114eea 198 args->sysmsg_result = ngrp;
984263bc
MD
199 return (0);
200}
201
202/*
203 * The FreeBSD native getgid(2) and getuid(2) also modify p->p_retval[1]
204 * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that
205 * are assumed to be preserved. The following lightweight syscalls fixes
206 * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in
207 * linux_misc.c
208 *
209 * linux_getgid16() - MP SAFE
210 * linux_getuid16() - MP SAFE
211 */
212
213int
753fd850 214sys_linux_getgid16(struct linux_getgid16_args *args)
984263bc 215{
41c20dac 216 struct proc *p = curproc;
984263bc 217
c7114eea 218 args->sysmsg_result = p->p_ucred->cr_rgid;
984263bc
MD
219 return (0);
220}
221
222int
753fd850 223sys_linux_getuid16(struct linux_getuid16_args *args)
984263bc 224{
41c20dac 225 struct proc *p = curproc;
984263bc 226
c7114eea 227 args->sysmsg_result = p->p_ucred->cr_ruid;
984263bc
MD
228 return (0);
229}
230
231int
753fd850 232sys_linux_getegid16(struct linux_getegid16_args *args)
984263bc
MD
233{
234 struct getegid_args bsd;
90b9818c
MD
235 int error;
236
c7114eea 237 bsd.sysmsg_result = 0;
984263bc 238
753fd850 239 error = sys_getegid(&bsd);
c7114eea 240 args->sysmsg_result = bsd.sysmsg_result;
90b9818c 241 return(error);
984263bc
MD
242}
243
244int
753fd850 245sys_linux_geteuid16(struct linux_geteuid16_args *args)
984263bc
MD
246{
247 struct geteuid_args bsd;
90b9818c
MD
248 int error;
249
c7114eea 250 bsd.sysmsg_result = 0;
984263bc 251
753fd850 252 error = sys_geteuid(&bsd);
c7114eea 253 args->sysmsg_result = bsd.sysmsg_result;
90b9818c 254 return(error);
984263bc
MD
255}
256
257int
753fd850 258sys_linux_setgid16(struct linux_setgid16_args *args)
984263bc
MD
259{
260 struct setgid_args bsd;
90b9818c 261 int error;
984263bc
MD
262
263 bsd.gid = args->gid;
c7114eea 264 bsd.sysmsg_result = 0;
90b9818c 265
753fd850 266 error = sys_setgid(&bsd);
c7114eea 267 args->sysmsg_result = bsd.sysmsg_result;
90b9818c 268 return(error);
984263bc
MD
269}
270
271int
753fd850 272sys_linux_setuid16(struct linux_setuid16_args *args)
984263bc
MD
273{
274 struct setuid_args bsd;
90b9818c 275 int error;
984263bc
MD
276
277 bsd.uid = args->uid;
c7114eea 278 bsd.sysmsg_result = 0;
90b9818c 279
753fd850 280 error = sys_setuid(&bsd);
c7114eea 281 args->sysmsg_result = bsd.sysmsg_result;
90b9818c 282 return(error);
984263bc
MD
283}
284
285int
753fd850 286sys_linux_setregid16(struct linux_setregid16_args *args)
984263bc
MD
287{
288 struct setregid_args bsd;
90b9818c 289 int error;
984263bc
MD
290
291 bsd.rgid = CAST_NOCHG(args->rgid);
292 bsd.egid = CAST_NOCHG(args->egid);
c7114eea 293 bsd.sysmsg_result = 0;
90b9818c 294
753fd850 295 error = sys_setregid(&bsd);
c7114eea 296 args->sysmsg_result = bsd.sysmsg_result;
90b9818c 297 return(error);
984263bc
MD
298}
299
300int
753fd850 301sys_linux_setreuid16(struct linux_setreuid16_args *args)
984263bc
MD
302{
303 struct setreuid_args bsd;
90b9818c 304 int error;
984263bc
MD
305
306 bsd.ruid = CAST_NOCHG(args->ruid);
307 bsd.euid = CAST_NOCHG(args->euid);
c7114eea 308 bsd.sysmsg_result = 0;
90b9818c 309
753fd850 310 error = sys_setreuid(&bsd);
c7114eea 311 args->sysmsg_result = bsd.sysmsg_result;
90b9818c 312 return(error);
984263bc
MD
313}
314
315int
753fd850 316sys_linux_setresgid16(struct linux_setresgid16_args *args)
984263bc
MD
317{
318 struct setresgid_args bsd;
90b9818c 319 int error;
984263bc
MD
320
321 bsd.rgid = CAST_NOCHG(args->rgid);
322 bsd.egid = CAST_NOCHG(args->egid);
323 bsd.sgid = CAST_NOCHG(args->sgid);
c7114eea 324 bsd.sysmsg_result = 0;
90b9818c 325
753fd850 326 error = sys_setresgid(&bsd);
c7114eea 327 args->sysmsg_result = bsd.sysmsg_result;
90b9818c 328 return(error);
984263bc
MD
329}
330
331int
753fd850 332sys_linux_setresuid16(struct linux_setresuid16_args *args)
984263bc
MD
333{
334 struct setresuid_args bsd;
90b9818c 335 int error;
984263bc
MD
336
337 bsd.ruid = CAST_NOCHG(args->ruid);
338 bsd.euid = CAST_NOCHG(args->euid);
339 bsd.suid = CAST_NOCHG(args->suid);
c7114eea 340 bsd.sysmsg_result = 0;
90b9818c 341
753fd850 342 error = sys_setresuid(&bsd);
c7114eea 343 args->sysmsg_result = bsd.sysmsg_result;
90b9818c 344 return(error);
984263bc 345}
90b9818c 346