linux_uid16.c revision 1.1.14.2 1 1.1.14.2 matt /* $NetBSD: linux_uid16.c,v 1.1.14.2 2008/03/23 02:04:33 matt Exp $ */
2 1.1.14.2 matt
3 1.1.14.2 matt /*-
4 1.1.14.2 matt * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5 1.1.14.2 matt * All rights reserved.
6 1.1.14.2 matt *
7 1.1.14.2 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1.14.2 matt * by Frank van der Linden and Eric Haszlakiewicz.
9 1.1.14.2 matt *
10 1.1.14.2 matt * Redistribution and use in source and binary forms, with or without
11 1.1.14.2 matt * modification, are permitted provided that the following conditions
12 1.1.14.2 matt * are met:
13 1.1.14.2 matt * 1. Redistributions of source code must retain the above copyright
14 1.1.14.2 matt * notice, this list of conditions and the following disclaimer.
15 1.1.14.2 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.14.2 matt * notice, this list of conditions and the following disclaimer in the
17 1.1.14.2 matt * documentation and/or other materials provided with the distribution.
18 1.1.14.2 matt * 3. All advertising materials mentioning features or use of this software
19 1.1.14.2 matt * must display the following acknowledgement:
20 1.1.14.2 matt * This product includes software developed by the NetBSD
21 1.1.14.2 matt * Foundation, Inc. and its contributors.
22 1.1.14.2 matt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.14.2 matt * contributors may be used to endorse or promote products derived
24 1.1.14.2 matt * from this software without specific prior written permission.
25 1.1.14.2 matt *
26 1.1.14.2 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.14.2 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.14.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.14.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.14.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.14.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.14.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.14.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.14.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.14.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.14.2 matt * POSSIBILITY OF SUCH DAMAGE.
37 1.1.14.2 matt */
38 1.1.14.2 matt
39 1.1.14.2 matt #include <sys/cdefs.h>
40 1.1.14.2 matt __KERNEL_RCSID(0, "$NetBSD: linux_uid16.c,v 1.1.14.2 2008/03/23 02:04:33 matt Exp $");
41 1.1.14.2 matt
42 1.1.14.2 matt #include <sys/param.h>
43 1.1.14.2 matt #include <sys/proc.h>
44 1.1.14.2 matt #include <sys/kauth.h>
45 1.1.14.2 matt #include <sys/syscallargs.h>
46 1.1.14.2 matt
47 1.1.14.2 matt #include <compat/linux/common/linux_types.h>
48 1.1.14.2 matt #include <compat/linux/common/linux_signal.h>
49 1.1.14.2 matt #include <compat/linux/linux_syscallargs.h>
50 1.1.14.2 matt
51 1.1.14.2 matt #define LINUXTOBSD_UID(u) \
52 1.1.14.2 matt (((u) == (linux_uid16_t)-1) ? -1 : (u))
53 1.1.14.2 matt #define LINUXTOBSD_GID(g) \
54 1.1.14.2 matt (((g) == (linux_gid16_t)-1) ? -1 : (g))
55 1.1.14.2 matt
56 1.1.14.2 matt #define BSDTOLINUX_UID(u) \
57 1.1.14.2 matt (((u) & ~0xffff) ? (linux_uid16_t)65534 : (linux_uid16_t)(u))
58 1.1.14.2 matt #define BSDTOLINUX_GID(g) \
59 1.1.14.2 matt (((g) & ~0xffff) ? (linux_gid16_t)65534 : (linux_gid16_t)(g))
60 1.1.14.2 matt
61 1.1.14.2 matt #ifndef COMPAT_LINUX32
62 1.1.14.2 matt int
63 1.1.14.2 matt linux_sys_chown16(struct lwp *l, const struct linux_sys_chown16_args *uap, register_t *retval)
64 1.1.14.2 matt {
65 1.1.14.2 matt /* {
66 1.1.14.2 matt syscallarg(const char *) path;
67 1.1.14.2 matt syscallarg(linux_uid16_t) uid;
68 1.1.14.2 matt syscallarg(linux_gid16_t) gid;
69 1.1.14.2 matt } */
70 1.1.14.2 matt struct sys___posix_chown_args bca;
71 1.1.14.2 matt
72 1.1.14.2 matt SCARG(&bca, path) = SCARG(uap, path);
73 1.1.14.2 matt SCARG(&bca, uid) = LINUXTOBSD_UID(SCARG(uap, uid));
74 1.1.14.2 matt SCARG(&bca, gid) = LINUXTOBSD_GID(SCARG(uap, gid));
75 1.1.14.2 matt
76 1.1.14.2 matt return sys___posix_chown(l, &bca, retval);
77 1.1.14.2 matt }
78 1.1.14.2 matt
79 1.1.14.2 matt int
80 1.1.14.2 matt linux_sys_fchown16(struct lwp *l, const struct linux_sys_fchown16_args *uap, register_t *retval)
81 1.1.14.2 matt {
82 1.1.14.2 matt /* {
83 1.1.14.2 matt syscallarg(int) fd;
84 1.1.14.2 matt syscallarg(linux_uid16_t) uid;
85 1.1.14.2 matt syscallarg(linux_gid16_t) gid;
86 1.1.14.2 matt } */
87 1.1.14.2 matt struct sys___posix_fchown_args bfa;
88 1.1.14.2 matt
89 1.1.14.2 matt SCARG(&bfa, fd) = SCARG(uap, fd);
90 1.1.14.2 matt SCARG(&bfa, uid) = LINUXTOBSD_UID(SCARG(uap, uid));
91 1.1.14.2 matt SCARG(&bfa, gid) = LINUXTOBSD_GID(SCARG(uap, gid));
92 1.1.14.2 matt
93 1.1.14.2 matt return sys___posix_fchown(l, &bfa, retval);
94 1.1.14.2 matt }
95 1.1.14.2 matt
96 1.1.14.2 matt int
97 1.1.14.2 matt linux_sys_lchown16(struct lwp *l, const struct linux_sys_lchown16_args *uap, register_t *retval)
98 1.1.14.2 matt {
99 1.1.14.2 matt /* {
100 1.1.14.2 matt syscallarg(char *) path;
101 1.1.14.2 matt syscallarg(linux_uid16_t) uid;
102 1.1.14.2 matt syscallarg(linux_gid16_t) gid;
103 1.1.14.2 matt } */
104 1.1.14.2 matt struct sys___posix_lchown_args bla;
105 1.1.14.2 matt
106 1.1.14.2 matt SCARG(&bla, path) = SCARG(uap, path);
107 1.1.14.2 matt SCARG(&bla, uid) = LINUXTOBSD_UID(SCARG(uap, uid));
108 1.1.14.2 matt SCARG(&bla, gid) = LINUXTOBSD_GID(SCARG(uap, gid));
109 1.1.14.2 matt
110 1.1.14.2 matt return sys___posix_lchown(l, &bla, retval);
111 1.1.14.2 matt }
112 1.1.14.2 matt
113 1.1.14.2 matt int
114 1.1.14.2 matt linux_sys_setreuid16(struct lwp *l, const struct linux_sys_setreuid16_args *uap, register_t *retval)
115 1.1.14.2 matt {
116 1.1.14.2 matt /* {
117 1.1.14.2 matt syscallarg(linux_uid16_t) ruid;
118 1.1.14.2 matt syscallarg(linux_uid16_t) euid;
119 1.1.14.2 matt } */
120 1.1.14.2 matt struct sys_setreuid_args bsa;
121 1.1.14.2 matt
122 1.1.14.2 matt SCARG(&bsa, ruid) = LINUXTOBSD_UID(SCARG(uap, ruid));
123 1.1.14.2 matt SCARG(&bsa, euid) = LINUXTOBSD_UID(SCARG(uap, euid));
124 1.1.14.2 matt
125 1.1.14.2 matt return sys_setreuid(l, &bsa, retval);
126 1.1.14.2 matt }
127 1.1.14.2 matt
128 1.1.14.2 matt int
129 1.1.14.2 matt linux_sys_setregid16(struct lwp *l, const struct linux_sys_setregid16_args *uap, register_t *retval)
130 1.1.14.2 matt {
131 1.1.14.2 matt /* {
132 1.1.14.2 matt syscallarg(linux_gid16_t) rgid;
133 1.1.14.2 matt syscallarg(linux_gid16_t) egid;
134 1.1.14.2 matt } */
135 1.1.14.2 matt struct sys_setregid_args bsa;
136 1.1.14.2 matt
137 1.1.14.2 matt SCARG(&bsa, rgid) = LINUXTOBSD_GID(SCARG(uap, rgid));
138 1.1.14.2 matt SCARG(&bsa, egid) = LINUXTOBSD_GID(SCARG(uap, egid));
139 1.1.14.2 matt
140 1.1.14.2 matt return sys_setregid(l, &bsa, retval);
141 1.1.14.2 matt }
142 1.1.14.2 matt
143 1.1.14.2 matt int
144 1.1.14.2 matt linux_sys_setresuid16(struct lwp *l, const struct linux_sys_setresuid16_args *uap, register_t *retval)
145 1.1.14.2 matt {
146 1.1.14.2 matt /* {
147 1.1.14.2 matt syscallarg(linux_uid16_t) ruid;
148 1.1.14.2 matt syscallarg(linux_uid16_t) euid;
149 1.1.14.2 matt syscallarg(linux_uid16_t) suid;
150 1.1.14.2 matt } */
151 1.1.14.2 matt struct linux_sys_setresuid_args lsa;
152 1.1.14.2 matt
153 1.1.14.2 matt SCARG(&lsa, ruid) = LINUXTOBSD_UID(SCARG(uap, ruid));
154 1.1.14.2 matt SCARG(&lsa, euid) = LINUXTOBSD_UID(SCARG(uap, euid));
155 1.1.14.2 matt SCARG(&lsa, suid) = LINUXTOBSD_UID(SCARG(uap, suid));
156 1.1.14.2 matt
157 1.1.14.2 matt return linux_sys_setresuid(l, &lsa, retval);
158 1.1.14.2 matt }
159 1.1.14.2 matt
160 1.1.14.2 matt int
161 1.1.14.2 matt linux_sys_setresgid16(struct lwp *l, const struct linux_sys_setresgid16_args *uap, register_t *retval)
162 1.1.14.2 matt {
163 1.1.14.2 matt /* {
164 1.1.14.2 matt syscallarg(linux_gid16_t) rgid;
165 1.1.14.2 matt syscallarg(linux_gid16_t) egid;
166 1.1.14.2 matt syscallarg(linux_gid16_t) sgid;
167 1.1.14.2 matt } */
168 1.1.14.2 matt struct linux_sys_setresgid_args lsa;
169 1.1.14.2 matt
170 1.1.14.2 matt SCARG(&lsa, rgid) = LINUXTOBSD_GID(SCARG(uap, rgid));
171 1.1.14.2 matt SCARG(&lsa, egid) = LINUXTOBSD_GID(SCARG(uap, egid));
172 1.1.14.2 matt SCARG(&lsa, sgid) = LINUXTOBSD_GID(SCARG(uap, sgid));
173 1.1.14.2 matt
174 1.1.14.2 matt return linux_sys_setresgid(l, &lsa, retval);
175 1.1.14.2 matt }
176 1.1.14.2 matt
177 1.1.14.2 matt int
178 1.1.14.2 matt linux_sys_getresuid16(struct lwp *l, const struct linux_sys_getresuid16_args *uap, register_t *retval)
179 1.1.14.2 matt {
180 1.1.14.2 matt /* {
181 1.1.14.2 matt syscallarg(linux_uid16_t) ruid;
182 1.1.14.2 matt syscallarg(linux_uid16_t) euid;
183 1.1.14.2 matt syscallarg(linux_uid16_t) suid;
184 1.1.14.2 matt } */
185 1.1.14.2 matt kauth_cred_t pc = l->l_cred;
186 1.1.14.2 matt int error;
187 1.1.14.2 matt uid_t buid;
188 1.1.14.2 matt linux_uid16_t luid;
189 1.1.14.2 matt
190 1.1.14.2 matt buid = kauth_cred_getuid(pc);
191 1.1.14.2 matt luid = BSDTOLINUX_UID(buid);
192 1.1.14.2 matt if ((error = copyout(&luid, SCARG(uap, ruid), sizeof(luid))) != 0)
193 1.1.14.2 matt return error;
194 1.1.14.2 matt
195 1.1.14.2 matt buid = kauth_cred_geteuid(pc);
196 1.1.14.2 matt luid = BSDTOLINUX_UID(buid);
197 1.1.14.2 matt if ((error = copyout(&luid, SCARG(uap, euid), sizeof(luid))) != 0)
198 1.1.14.2 matt return error;
199 1.1.14.2 matt
200 1.1.14.2 matt buid = kauth_cred_getsvuid(pc);
201 1.1.14.2 matt luid = BSDTOLINUX_UID(buid);
202 1.1.14.2 matt return (copyout(&luid, SCARG(uap, suid), sizeof(luid)));
203 1.1.14.2 matt }
204 1.1.14.2 matt
205 1.1.14.2 matt int
206 1.1.14.2 matt linux_sys_getresgid16(struct lwp *l, const struct linux_sys_getresgid16_args *uap, register_t *retval)
207 1.1.14.2 matt {
208 1.1.14.2 matt /* {
209 1.1.14.2 matt syscallarg(linux_gid16_t) rgid;
210 1.1.14.2 matt syscallarg(linux_gid16_t) egid;
211 1.1.14.2 matt syscallarg(linux_gid16_t) sgid;
212 1.1.14.2 matt } */
213 1.1.14.2 matt kauth_cred_t pc = l->l_cred;
214 1.1.14.2 matt int error;
215 1.1.14.2 matt gid_t bgid;
216 1.1.14.2 matt linux_gid16_t lgid;
217 1.1.14.2 matt
218 1.1.14.2 matt bgid = kauth_cred_getgid(pc);
219 1.1.14.2 matt lgid = BSDTOLINUX_GID(bgid);
220 1.1.14.2 matt if ((error = copyout(&lgid, SCARG(uap, rgid), sizeof(lgid))) != 0)
221 1.1.14.2 matt return error;
222 1.1.14.2 matt
223 1.1.14.2 matt bgid = kauth_cred_getegid(pc);
224 1.1.14.2 matt lgid = BSDTOLINUX_GID(bgid);
225 1.1.14.2 matt if ((error = copyout(&lgid, SCARG(uap, egid), sizeof(lgid))) != 0)
226 1.1.14.2 matt return error;
227 1.1.14.2 matt
228 1.1.14.2 matt bgid = kauth_cred_getsvgid(pc);
229 1.1.14.2 matt lgid = BSDTOLINUX_GID(bgid);
230 1.1.14.2 matt return (copyout(&lgid, SCARG(uap, sgid), sizeof(lgid)));
231 1.1.14.2 matt }
232 1.1.14.2 matt #endif /* !COMPAT_LINUX32 */
233 1.1.14.2 matt
234 1.1.14.2 matt int
235 1.1.14.2 matt linux_sys_getgroups16(struct lwp *l, const struct linux_sys_getgroups16_args *uap, register_t *retval)
236 1.1.14.2 matt {
237 1.1.14.2 matt /* {
238 1.1.14.2 matt syscallarg(int) gidsetsize;
239 1.1.14.2 matt syscallarg(linux_gid16_t *) gidset;
240 1.1.14.2 matt } */
241 1.1.14.2 matt linux_gid16_t lset[16];
242 1.1.14.2 matt linux_gid16_t *gidset;
243 1.1.14.2 matt unsigned int ngrps;
244 1.1.14.2 matt int i, n, j;
245 1.1.14.2 matt int error;
246 1.1.14.2 matt
247 1.1.14.2 matt ngrps = kauth_cred_ngroups(l->l_cred);
248 1.1.14.2 matt *retval = ngrps;
249 1.1.14.2 matt if (SCARG(uap, gidsetsize) == 0)
250 1.1.14.2 matt return 0;
251 1.1.14.2 matt if (SCARG(uap, gidsetsize) < ngrps)
252 1.1.14.2 matt return EINVAL;
253 1.1.14.2 matt
254 1.1.14.2 matt gidset = SCARG(uap, gidset);
255 1.1.14.2 matt for (i = 0; i < (n = ngrps); i += n, gidset += n) {
256 1.1.14.2 matt n -= i;
257 1.1.14.2 matt if (n > __arraycount(lset))
258 1.1.14.2 matt n = __arraycount(lset);
259 1.1.14.2 matt for (j = 0; j < n; j++)
260 1.1.14.2 matt lset[j] = kauth_cred_group(l->l_cred, i + j);
261 1.1.14.2 matt error = copyout(lset, gidset, n * sizeof(lset[0]));
262 1.1.14.2 matt if (error != 0)
263 1.1.14.2 matt return error;
264 1.1.14.2 matt }
265 1.1.14.2 matt
266 1.1.14.2 matt return 0;
267 1.1.14.2 matt }
268 1.1.14.2 matt
269 1.1.14.2 matt /*
270 1.1.14.2 matt * It is very unlikly that any problem using 16bit groups is written
271 1.1.14.2 matt * to allow for more than 16 of them, so don't bother trying to
272 1.1.14.2 matt * support that.
273 1.1.14.2 matt */
274 1.1.14.2 matt #define COMPAT_NGROUPS16 16
275 1.1.14.2 matt
276 1.1.14.2 matt int
277 1.1.14.2 matt linux_sys_setgroups16(struct lwp *l, const struct linux_sys_setgroups16_args *uap, register_t *retval)
278 1.1.14.2 matt {
279 1.1.14.2 matt /* {
280 1.1.14.2 matt syscallarg(int) gidsetsize;
281 1.1.14.2 matt syscallarg(linux_gid16_t *) gidset;
282 1.1.14.2 matt } */
283 1.1.14.2 matt linux_gid16_t lset[COMPAT_NGROUPS16];
284 1.1.14.2 matt kauth_cred_t ncred;
285 1.1.14.2 matt int error;
286 1.1.14.2 matt gid_t grbuf[COMPAT_NGROUPS16];
287 1.1.14.2 matt unsigned int i, ngroups = SCARG(uap, gidsetsize);
288 1.1.14.2 matt
289 1.1.14.2 matt if (ngroups > COMPAT_NGROUPS16)
290 1.1.14.2 matt return EINVAL;
291 1.1.14.2 matt error = copyin(SCARG(uap, gidset), lset, ngroups);
292 1.1.14.2 matt if (error != 0)
293 1.1.14.2 matt return error;
294 1.1.14.2 matt
295 1.1.14.2 matt for (i = 0; i < ngroups; i++)
296 1.1.14.2 matt grbuf[i] = lset[i];
297 1.1.14.2 matt
298 1.1.14.2 matt ncred = kauth_cred_alloc();
299 1.1.14.2 matt error = kauth_cred_setgroups(ncred, grbuf, SCARG(uap, gidsetsize),
300 1.1.14.2 matt -1, UIO_SYSSPACE);
301 1.1.14.2 matt if (error != 0) {
302 1.1.14.2 matt kauth_cred_free(ncred);
303 1.1.14.2 matt return error;
304 1.1.14.2 matt }
305 1.1.14.2 matt
306 1.1.14.2 matt return kauth_proc_setgroups(l, ncred);
307 1.1.14.2 matt }
308