2 * Copyright (c) 2001 The FreeBSD Project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/compat/linux/linux_uid16.c,v 1.4.2.1 2001/10/21 03:57:35 marcel Exp $
27 * $DragonFly: src/sys/emulation/linux/linux_uid16.c,v 1.12 2006/12/23 00:27:02 swildner Exp $
30 #include "opt_compat.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kern_syscall.h>
35 #include <sys/nlookup.h>
37 #include <sys/sysproto.h>
38 #include <sys/thread.h>
40 #include <arch_linux/linux.h>
41 #include <arch_linux/linux_proto.h>
42 #include "linux_util.h"
49 #define CAST_NOCHG(x) ((x == 0xFFFF) ? -1 : x)
52 sys_linux_chown16(struct linux_chown16_args
*args
)
54 struct nlookupdata nd
;
58 error
= linux_copyin_path(args
->path
, &path
, LINUX_PATH_EXISTS
);
63 kprintf(ARGS(chown16
, "%s, %d, %d"), path
, args
->uid
,
66 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
68 error
= kern_chown(&nd
, CAST_NOCHG(args
->uid
),
69 CAST_NOCHG(args
->gid
));
72 linux_free_path(&path
);
77 sys_linux_lchown16(struct linux_lchown16_args
*args
)
79 struct nlookupdata nd
;
83 error
= linux_copyin_path(args
->path
, &path
, LINUX_PATH_EXISTS
);
88 kprintf(ARGS(lchown16
, "%s, %d, %d"), path
, args
->uid
,
91 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, 0);
93 error
= kern_chown(&nd
, CAST_NOCHG(args
->uid
),
94 CAST_NOCHG(args
->gid
));
97 linux_free_path(&path
);
102 sys_linux_setgroups16(struct linux_setgroups16_args
*args
)
104 struct proc
*p
= curproc
;
105 struct ucred
*newcred
, *oldcred
;
106 l_gid16_t linux_gidset
[NGROUPS
];
111 if (ldebug(setgroups16
))
112 kprintf(ARGS(setgroups16
, "%d, *"), args
->gidsetsize
);
115 ngrp
= args
->gidsetsize
;
116 oldcred
= p
->p_ucred
;
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.
124 if ((error
= suser_cred(oldcred
, PRISON_ROOT
)) != 0)
130 newcred
= crdup(oldcred
);
132 error
= copyin((caddr_t
)args
->gidset
, linux_gidset
,
133 ngrp
* sizeof(l_gid16_t
));
137 newcred
->cr_ngroups
= ngrp
+ 1;
139 bsd_gidset
= newcred
->cr_groups
;
142 bsd_gidset
[ngrp
+ 1] = linux_gidset
[ngrp
];
147 newcred
->cr_ngroups
= 1;
150 p
->p_ucred
= newcred
;
156 sys_linux_getgroups16(struct linux_getgroups16_args
*args
)
158 struct proc
*p
= curproc
;
160 l_gid16_t linux_gidset
[NGROUPS
];
162 int bsd_gidsetsz
, ngrp
, error
;
165 if (ldebug(getgroups16
))
166 kprintf(ARGS(getgroups16
, "%d, *"), args
->gidsetsize
);
170 bsd_gidset
= cred
->cr_groups
;
171 bsd_gidsetsz
= cred
->cr_ngroups
- 1;
174 * cr_groups[0] holds egid. Returning the whole set
175 * here will cause a duplicate. Exclude cr_groups[0]
179 if ((ngrp
= args
->gidsetsize
) == 0) {
180 args
->sysmsg_result
= bsd_gidsetsz
;
184 if (ngrp
< bsd_gidsetsz
)
188 while (ngrp
< bsd_gidsetsz
) {
189 linux_gidset
[ngrp
] = bsd_gidset
[ngrp
+ 1];
193 error
= copyout(linux_gidset
, (caddr_t
)args
->gidset
,
194 ngrp
* sizeof(l_gid16_t
));
198 args
->sysmsg_result
= ngrp
;
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
209 * linux_getgid16() - MP SAFE
210 * linux_getuid16() - MP SAFE
214 sys_linux_getgid16(struct linux_getgid16_args
*args
)
216 struct proc
*p
= curproc
;
218 args
->sysmsg_result
= p
->p_ucred
->cr_rgid
;
223 sys_linux_getuid16(struct linux_getuid16_args
*args
)
225 struct proc
*p
= curproc
;
227 args
->sysmsg_result
= p
->p_ucred
->cr_ruid
;
232 sys_linux_getegid16(struct linux_getegid16_args
*args
)
234 struct getegid_args bsd
;
237 bsd
.sysmsg_result
= 0;
239 error
= sys_getegid(&bsd
);
240 args
->sysmsg_result
= bsd
.sysmsg_result
;
245 sys_linux_geteuid16(struct linux_geteuid16_args
*args
)
247 struct geteuid_args bsd
;
250 bsd
.sysmsg_result
= 0;
252 error
= sys_geteuid(&bsd
);
253 args
->sysmsg_result
= bsd
.sysmsg_result
;
258 sys_linux_setgid16(struct linux_setgid16_args
*args
)
260 struct setgid_args bsd
;
264 bsd
.sysmsg_result
= 0;
266 error
= sys_setgid(&bsd
);
267 args
->sysmsg_result
= bsd
.sysmsg_result
;
272 sys_linux_setuid16(struct linux_setuid16_args
*args
)
274 struct setuid_args bsd
;
278 bsd
.sysmsg_result
= 0;
280 error
= sys_setuid(&bsd
);
281 args
->sysmsg_result
= bsd
.sysmsg_result
;
286 sys_linux_setregid16(struct linux_setregid16_args
*args
)
288 struct setregid_args bsd
;
291 bsd
.rgid
= CAST_NOCHG(args
->rgid
);
292 bsd
.egid
= CAST_NOCHG(args
->egid
);
293 bsd
.sysmsg_result
= 0;
295 error
= sys_setregid(&bsd
);
296 args
->sysmsg_result
= bsd
.sysmsg_result
;
301 sys_linux_setreuid16(struct linux_setreuid16_args
*args
)
303 struct setreuid_args bsd
;
306 bsd
.ruid
= CAST_NOCHG(args
->ruid
);
307 bsd
.euid
= CAST_NOCHG(args
->euid
);
308 bsd
.sysmsg_result
= 0;
310 error
= sys_setreuid(&bsd
);
311 args
->sysmsg_result
= bsd
.sysmsg_result
;
316 sys_linux_setresgid16(struct linux_setresgid16_args
*args
)
318 struct setresgid_args bsd
;
321 bsd
.rgid
= CAST_NOCHG(args
->rgid
);
322 bsd
.egid
= CAST_NOCHG(args
->egid
);
323 bsd
.sgid
= CAST_NOCHG(args
->sgid
);
324 bsd
.sysmsg_result
= 0;
326 error
= sys_setresgid(&bsd
);
327 args
->sysmsg_result
= bsd
.sysmsg_result
;
332 sys_linux_setresuid16(struct linux_setresuid16_args
*args
)
334 struct setresuid_args bsd
;
337 bsd
.ruid
= CAST_NOCHG(args
->ruid
);
338 bsd
.euid
= CAST_NOCHG(args
->euid
);
339 bsd
.suid
= CAST_NOCHG(args
->suid
);
340 bsd
.sysmsg_result
= 0;
342 error
= sys_setresuid(&bsd
);
343 args
->sysmsg_result
= bsd
.sysmsg_result
;