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