1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T 24*7c478bd9Sstevel@tonic-gate * Copyright 2001-2003 Sun Microsystems, Inc. All rights reserved. 25*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 26*7c478bd9Sstevel@tonic-gate */ 27*7c478bd9Sstevel@tonic-gate 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* from SVr4.0 1.78 */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 33*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/systm.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/errno.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/proc.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/debug.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/policy.h> 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate int 43*7c478bd9Sstevel@tonic-gate setgroups(int gidsetsize, gid_t *gidset) 44*7c478bd9Sstevel@tonic-gate { 45*7c478bd9Sstevel@tonic-gate proc_t *p; 46*7c478bd9Sstevel@tonic-gate cred_t *cr, *newcr; 47*7c478bd9Sstevel@tonic-gate int i; 48*7c478bd9Sstevel@tonic-gate int n = gidsetsize; 49*7c478bd9Sstevel@tonic-gate gid_t *groups = NULL; 50*7c478bd9Sstevel@tonic-gate int error; 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate /* Perform the cheapest tests before grabbing p_crlock */ 53*7c478bd9Sstevel@tonic-gate if (n > ngroups_max || n < 0) 54*7c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate if (n != 0) { 57*7c478bd9Sstevel@tonic-gate groups = kmem_alloc(n * sizeof (gid_t), KM_SLEEP); 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate if (copyin(gidset, groups, n * sizeof (gid_t)) != 0) { 60*7c478bd9Sstevel@tonic-gate kmem_free(groups, n * sizeof (gid_t)); 61*7c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 62*7c478bd9Sstevel@tonic-gate } 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) { 65*7c478bd9Sstevel@tonic-gate if (groups[i] < 0 || groups[i] > MAXUID) { 66*7c478bd9Sstevel@tonic-gate kmem_free(groups, n * sizeof (gid_t)); 67*7c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 68*7c478bd9Sstevel@tonic-gate } 69*7c478bd9Sstevel@tonic-gate } 70*7c478bd9Sstevel@tonic-gate } 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate /* 73*7c478bd9Sstevel@tonic-gate * Need to pre-allocate the new cred structure before acquiring 74*7c478bd9Sstevel@tonic-gate * the p_crlock mutex. 75*7c478bd9Sstevel@tonic-gate */ 76*7c478bd9Sstevel@tonic-gate newcr = cralloc(); 77*7c478bd9Sstevel@tonic-gate p = ttoproc(curthread); 78*7c478bd9Sstevel@tonic-gate mutex_enter(&p->p_crlock); 79*7c478bd9Sstevel@tonic-gate cr = p->p_cred; 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate if ((error = secpolicy_allow_setid(cr, -1, B_FALSE)) != 0) { 82*7c478bd9Sstevel@tonic-gate mutex_exit(&p->p_crlock); 83*7c478bd9Sstevel@tonic-gate if (groups != NULL) 84*7c478bd9Sstevel@tonic-gate kmem_free(groups, n * sizeof (gid_t)); 85*7c478bd9Sstevel@tonic-gate crfree(newcr); 86*7c478bd9Sstevel@tonic-gate return (set_errno(error)); 87*7c478bd9Sstevel@tonic-gate } 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate crdup_to(cr, newcr); 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate if (n != 0) { 92*7c478bd9Sstevel@tonic-gate bcopy(groups, newcr->cr_groups, n * sizeof (gid_t)); 93*7c478bd9Sstevel@tonic-gate kmem_free(groups, n * sizeof (gid_t)); 94*7c478bd9Sstevel@tonic-gate } 95*7c478bd9Sstevel@tonic-gate 96*7c478bd9Sstevel@tonic-gate newcr->cr_ngroups = n; 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate p->p_cred = newcr; 99*7c478bd9Sstevel@tonic-gate crhold(newcr); /* hold for the current thread */ 100*7c478bd9Sstevel@tonic-gate crfree(cr); /* free the old one */ 101*7c478bd9Sstevel@tonic-gate mutex_exit(&p->p_crlock); 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate /* 104*7c478bd9Sstevel@tonic-gate * Broadcast new cred to process threads (including the current one). 105*7c478bd9Sstevel@tonic-gate */ 106*7c478bd9Sstevel@tonic-gate crset(p, newcr); 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate return (0); 109*7c478bd9Sstevel@tonic-gate } 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate int 112*7c478bd9Sstevel@tonic-gate getgroups(int gidsetsize, gid_t *gidset) 113*7c478bd9Sstevel@tonic-gate { 114*7c478bd9Sstevel@tonic-gate struct cred *cr; 115*7c478bd9Sstevel@tonic-gate int n; 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate cr = curthread->t_cred; 118*7c478bd9Sstevel@tonic-gate n = (int)cr->cr_ngroups; 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate if (gidsetsize != 0) { 121*7c478bd9Sstevel@tonic-gate if (gidsetsize < n) 122*7c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 123*7c478bd9Sstevel@tonic-gate if (copyout(cr->cr_groups, gidset, n * sizeof (gid_t))) 124*7c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate return (n); 128*7c478bd9Sstevel@tonic-gate } 129