1 /* $NetBSD: linux_uid16.c,v 1.2 2008/04/28 20:23:44 martin Exp $ */
4 * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Eric Haszlakiewicz.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_uid16.c,v 1.2 2008/04/28 20:23:44 martin Exp $");
35 #include <sys/param.h>
37 #include <sys/kauth.h>
38 #include <sys/syscallargs.h>
40 #include <compat/linux/common/linux_types.h>
41 #include <compat/linux/common/linux_signal.h>
42 #include <compat/linux/linux_syscallargs.h>
44 #define LINUXTOBSD_UID(u) \
45 (((u) == (linux_uid16_t)-1) ? -1 : (u))
46 #define LINUXTOBSD_GID(g) \
47 (((g) == (linux_gid16_t)-1) ? -1 : (g))
49 #define BSDTOLINUX_UID(u) \
50 (((u) & ~0xffff) ? (linux_uid16_t)65534 : (linux_uid16_t)(u))
51 #define BSDTOLINUX_GID(g) \
52 (((g) & ~0xffff) ? (linux_gid16_t)65534 : (linux_gid16_t)(g))
54 #ifndef COMPAT_LINUX32
56 linux_sys_chown16(struct lwp
*l
, const struct linux_sys_chown16_args
*uap
, register_t
*retval
)
59 syscallarg(const char *) path;
60 syscallarg(linux_uid16_t) uid;
61 syscallarg(linux_gid16_t) gid;
63 struct sys___posix_chown_args bca
;
65 SCARG(&bca
, path
) = SCARG(uap
, path
);
66 SCARG(&bca
, uid
) = LINUXTOBSD_UID(SCARG(uap
, uid
));
67 SCARG(&bca
, gid
) = LINUXTOBSD_GID(SCARG(uap
, gid
));
69 return sys___posix_chown(l
, &bca
, retval
);
73 linux_sys_fchown16(struct lwp
*l
, const struct linux_sys_fchown16_args
*uap
, register_t
*retval
)
77 syscallarg(linux_uid16_t) uid;
78 syscallarg(linux_gid16_t) gid;
80 struct sys___posix_fchown_args bfa
;
82 SCARG(&bfa
, fd
) = SCARG(uap
, fd
);
83 SCARG(&bfa
, uid
) = LINUXTOBSD_UID(SCARG(uap
, uid
));
84 SCARG(&bfa
, gid
) = LINUXTOBSD_GID(SCARG(uap
, gid
));
86 return sys___posix_fchown(l
, &bfa
, retval
);
90 linux_sys_lchown16(struct lwp
*l
, const struct linux_sys_lchown16_args
*uap
, register_t
*retval
)
93 syscallarg(char *) path;
94 syscallarg(linux_uid16_t) uid;
95 syscallarg(linux_gid16_t) gid;
97 struct sys___posix_lchown_args bla
;
99 SCARG(&bla
, path
) = SCARG(uap
, path
);
100 SCARG(&bla
, uid
) = LINUXTOBSD_UID(SCARG(uap
, uid
));
101 SCARG(&bla
, gid
) = LINUXTOBSD_GID(SCARG(uap
, gid
));
103 return sys___posix_lchown(l
, &bla
, retval
);
107 linux_sys_setreuid16(struct lwp
*l
, const struct linux_sys_setreuid16_args
*uap
, register_t
*retval
)
110 syscallarg(linux_uid16_t) ruid;
111 syscallarg(linux_uid16_t) euid;
113 struct sys_setreuid_args bsa
;
115 SCARG(&bsa
, ruid
) = LINUXTOBSD_UID(SCARG(uap
, ruid
));
116 SCARG(&bsa
, euid
) = LINUXTOBSD_UID(SCARG(uap
, euid
));
118 return sys_setreuid(l
, &bsa
, retval
);
122 linux_sys_setregid16(struct lwp
*l
, const struct linux_sys_setregid16_args
*uap
, register_t
*retval
)
125 syscallarg(linux_gid16_t) rgid;
126 syscallarg(linux_gid16_t) egid;
128 struct sys_setregid_args bsa
;
130 SCARG(&bsa
, rgid
) = LINUXTOBSD_GID(SCARG(uap
, rgid
));
131 SCARG(&bsa
, egid
) = LINUXTOBSD_GID(SCARG(uap
, egid
));
133 return sys_setregid(l
, &bsa
, retval
);
137 linux_sys_setresuid16(struct lwp
*l
, const struct linux_sys_setresuid16_args
*uap
, register_t
*retval
)
140 syscallarg(linux_uid16_t) ruid;
141 syscallarg(linux_uid16_t) euid;
142 syscallarg(linux_uid16_t) suid;
144 struct linux_sys_setresuid_args lsa
;
146 SCARG(&lsa
, ruid
) = LINUXTOBSD_UID(SCARG(uap
, ruid
));
147 SCARG(&lsa
, euid
) = LINUXTOBSD_UID(SCARG(uap
, euid
));
148 SCARG(&lsa
, suid
) = LINUXTOBSD_UID(SCARG(uap
, suid
));
150 return linux_sys_setresuid(l
, &lsa
, retval
);
154 linux_sys_setresgid16(struct lwp
*l
, const struct linux_sys_setresgid16_args
*uap
, register_t
*retval
)
157 syscallarg(linux_gid16_t) rgid;
158 syscallarg(linux_gid16_t) egid;
159 syscallarg(linux_gid16_t) sgid;
161 struct linux_sys_setresgid_args lsa
;
163 SCARG(&lsa
, rgid
) = LINUXTOBSD_GID(SCARG(uap
, rgid
));
164 SCARG(&lsa
, egid
) = LINUXTOBSD_GID(SCARG(uap
, egid
));
165 SCARG(&lsa
, sgid
) = LINUXTOBSD_GID(SCARG(uap
, sgid
));
167 return linux_sys_setresgid(l
, &lsa
, retval
);
171 linux_sys_getresuid16(struct lwp
*l
, const struct linux_sys_getresuid16_args
*uap
, register_t
*retval
)
174 syscallarg(linux_uid16_t *) ruid;
175 syscallarg(linux_uid16_t *) euid;
176 syscallarg(linux_uid16_t *) suid;
178 kauth_cred_t pc
= l
->l_cred
;
183 buid
= kauth_cred_getuid(pc
);
184 luid
= BSDTOLINUX_UID(buid
);
185 if ((error
= copyout(&luid
, SCARG(uap
, ruid
), sizeof(luid
))) != 0)
188 buid
= kauth_cred_geteuid(pc
);
189 luid
= BSDTOLINUX_UID(buid
);
190 if ((error
= copyout(&luid
, SCARG(uap
, euid
), sizeof(luid
))) != 0)
193 buid
= kauth_cred_getsvuid(pc
);
194 luid
= BSDTOLINUX_UID(buid
);
195 return (copyout(&luid
, SCARG(uap
, suid
), sizeof(luid
)));
199 linux_sys_getresgid16(struct lwp
*l
, const struct linux_sys_getresgid16_args
*uap
, register_t
*retval
)
202 syscallarg(linux_gid16_t *) rgid;
203 syscallarg(linux_gid16_t *) egid;
204 syscallarg(linux_gid16_t *) sgid;
206 kauth_cred_t pc
= l
->l_cred
;
211 bgid
= kauth_cred_getgid(pc
);
212 lgid
= BSDTOLINUX_GID(bgid
);
213 if ((error
= copyout(&lgid
, SCARG(uap
, rgid
), sizeof(lgid
))) != 0)
216 bgid
= kauth_cred_getegid(pc
);
217 lgid
= BSDTOLINUX_GID(bgid
);
218 if ((error
= copyout(&lgid
, SCARG(uap
, egid
), sizeof(lgid
))) != 0)
221 bgid
= kauth_cred_getsvgid(pc
);
222 lgid
= BSDTOLINUX_GID(bgid
);
223 return (copyout(&lgid
, SCARG(uap
, sgid
), sizeof(lgid
)));
225 #endif /* !COMPAT_LINUX32 */
228 linux_sys_getgroups16(struct lwp
*l
, const struct linux_sys_getgroups16_args
*uap
, register_t
*retval
)
231 syscallarg(int) gidsetsize;
232 syscallarg(linux_gid16_t *) gidset;
234 linux_gid16_t lset
[16];
235 linux_gid16_t
*gidset
;
240 ngrps
= kauth_cred_ngroups(l
->l_cred
);
242 if (SCARG(uap
, gidsetsize
) == 0)
244 if (SCARG(uap
, gidsetsize
) < ngrps
)
247 gidset
= SCARG(uap
, gidset
);
248 for (i
= 0; i
< (n
= ngrps
); i
+= n
, gidset
+= n
) {
250 if (n
> __arraycount(lset
))
251 n
= __arraycount(lset
);
252 for (j
= 0; j
< n
; j
++)
253 lset
[j
] = kauth_cred_group(l
->l_cred
, i
+ j
);
254 error
= copyout(lset
, gidset
, n
* sizeof(lset
[0]));
263 * It is very unlikly that any problem using 16bit groups is written
264 * to allow for more than 16 of them, so don't bother trying to
267 #define COMPAT_NGROUPS16 16
270 linux_sys_setgroups16(struct lwp
*l
, const struct linux_sys_setgroups16_args
*uap
, register_t
*retval
)
273 syscallarg(int) gidsetsize;
274 syscallarg(linux_gid16_t *) gidset;
276 linux_gid16_t lset
[COMPAT_NGROUPS16
];
279 gid_t grbuf
[COMPAT_NGROUPS16
];
280 unsigned int i
, ngroups
= SCARG(uap
, gidsetsize
);
282 if (ngroups
> COMPAT_NGROUPS16
)
284 error
= copyin(SCARG(uap
, gidset
), lset
, ngroups
);
288 for (i
= 0; i
< ngroups
; i
++)
291 ncred
= kauth_cred_alloc();
292 error
= kauth_cred_setgroups(ncred
, grbuf
, SCARG(uap
, gidsetsize
),
295 kauth_cred_free(ncred
);
299 return kauth_proc_setgroups(l
, ncred
);