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