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