xref: /illumos-gate/usr/src/uts/common/syscall/gid.c (revision 2d6eb4a5e0a47d30189497241345dc5466bb68ab)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f48205beScasper  * Common Development and Distribution License (the "License").
6f48205beScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22bda89588Sjp151216  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  *	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
357c478bd9Sstevel@tonic-gate #include <sys/errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/proc.h>
377c478bd9Sstevel@tonic-gate #include <sys/debug.h>
387c478bd9Sstevel@tonic-gate #include <sys/policy.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate int
setgid(gid_t gid)427c478bd9Sstevel@tonic-gate setgid(gid_t gid)
437c478bd9Sstevel@tonic-gate {
44f48205beScasper 	proc_t *p;
457c478bd9Sstevel@tonic-gate 	int error;
467c478bd9Sstevel@tonic-gate 	int do_nocd = 0;
477c478bd9Sstevel@tonic-gate 	cred_t	*cr, *newcr;
48f48205beScasper 	ksid_t ksid, *ksp;
49bda89588Sjp151216 	zone_t	*zone = crgetzone(CRED());
507c478bd9Sstevel@tonic-gate 
51bda89588Sjp151216 
52bda89588Sjp151216 	if (!VALID_GID(gid, zone))
537c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
547c478bd9Sstevel@tonic-gate 
55f48205beScasper 	if (gid > MAXUID) {
56bda89588Sjp151216 		if (ksid_lookupbygid(zone, gid, &ksid) != 0)
57f48205beScasper 			return (set_errno(EINVAL));
58f48205beScasper 		ksp = &ksid;
59f48205beScasper 	} else {
60f48205beScasper 		ksp = NULL;
61f48205beScasper 	}
62f48205beScasper 
637c478bd9Sstevel@tonic-gate 	/*
647c478bd9Sstevel@tonic-gate 	 * Need to pre-allocate the new cred structure before grabbing
65*ddf7fe95Scasper 	 * the p_crlock mutex.  We cannot hold the mutex across the
66*ddf7fe95Scasper 	 * secpolicy functions.
677c478bd9Sstevel@tonic-gate 	 */
68f48205beScasper 	newcr = cralloc_ksid();
697c478bd9Sstevel@tonic-gate 	p = ttoproc(curthread);
707c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
71*ddf7fe95Scasper retry:
727c478bd9Sstevel@tonic-gate 	cr = p->p_cred;
73*ddf7fe95Scasper 	crhold(cr);
74*ddf7fe95Scasper 	mutex_exit(&p->p_crlock);
75*ddf7fe95Scasper 
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if ((gid == cr->cr_rgid || gid == cr->cr_sgid) &&
787c478bd9Sstevel@tonic-gate 	    secpolicy_allow_setid(cr, -1, B_TRUE) != 0) {
79*ddf7fe95Scasper 		mutex_enter(&p->p_crlock);
80*ddf7fe95Scasper 		crfree(cr);
81*ddf7fe95Scasper 		if (cr != p->p_cred)
82*ddf7fe95Scasper 			goto retry;
837c478bd9Sstevel@tonic-gate 		error = 0;
847c478bd9Sstevel@tonic-gate 		crcopy_to(cr, newcr);
857c478bd9Sstevel@tonic-gate 		p->p_cred = newcr;
867c478bd9Sstevel@tonic-gate 		newcr->cr_gid = gid;
87f48205beScasper 		crsetsid(newcr, ksp, KSID_GROUP);
88*ddf7fe95Scasper 		mutex_exit(&p->p_crlock);
897c478bd9Sstevel@tonic-gate 	} else if ((error = secpolicy_allow_setid(cr, -1, B_FALSE)) == 0) {
90*ddf7fe95Scasper 		mutex_enter(&p->p_crlock);
91*ddf7fe95Scasper 		crfree(cr);
92*ddf7fe95Scasper 		if (cr != p->p_cred)
93*ddf7fe95Scasper 			goto retry;
947c478bd9Sstevel@tonic-gate 		/*
957c478bd9Sstevel@tonic-gate 		 * A privileged process that makes itself look like a
967c478bd9Sstevel@tonic-gate 		 * set-gid process must be marked to produce no core dump.
977c478bd9Sstevel@tonic-gate 		 */
987c478bd9Sstevel@tonic-gate 		if (cr->cr_gid != gid ||
997c478bd9Sstevel@tonic-gate 		    cr->cr_rgid != gid ||
1007c478bd9Sstevel@tonic-gate 		    cr->cr_sgid != gid)
1017c478bd9Sstevel@tonic-gate 			do_nocd = 1;
1027c478bd9Sstevel@tonic-gate 		crcopy_to(cr, newcr);
1037c478bd9Sstevel@tonic-gate 		p->p_cred = newcr;
1047c478bd9Sstevel@tonic-gate 		newcr->cr_gid = gid;
1057c478bd9Sstevel@tonic-gate 		newcr->cr_rgid = gid;
1067c478bd9Sstevel@tonic-gate 		newcr->cr_sgid = gid;
107f48205beScasper 		crsetsid(newcr, ksp, KSID_GROUP);
108*ddf7fe95Scasper 		mutex_exit(&p->p_crlock);
109f48205beScasper 	} else {
1107c478bd9Sstevel@tonic-gate 		crfree(newcr);
111*ddf7fe95Scasper 		crfree(cr);
112f48205beScasper 		if (ksp != NULL)
113f48205beScasper 			ksid_rele(ksp);
114f48205beScasper 
115f48205beScasper 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (error == 0) {
1187c478bd9Sstevel@tonic-gate 		if (do_nocd) {
1197c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
1207c478bd9Sstevel@tonic-gate 			p->p_flag |= SNOCD;
1217c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 		crset(p, newcr);	/* broadcast to process threads */
1247c478bd9Sstevel@tonic-gate 		return (0);
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 	return (set_errno(error));
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate int64_t
getgid(void)1307c478bd9Sstevel@tonic-gate getgid(void)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	rval_t	r;
1337c478bd9Sstevel@tonic-gate 	cred_t	*cr;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	cr = curthread->t_cred;
1367c478bd9Sstevel@tonic-gate 	r.r_val1 = cr->cr_rgid;
1377c478bd9Sstevel@tonic-gate 	r.r_val2 = cr->cr_gid;
1387c478bd9Sstevel@tonic-gate 	return (r.r_vals);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate int
setegid(gid_t gid)1427c478bd9Sstevel@tonic-gate setegid(gid_t gid)
1437c478bd9Sstevel@tonic-gate {
144f48205beScasper 	proc_t *p;
145f48205beScasper 	cred_t	*cr, *newcr;
1467c478bd9Sstevel@tonic-gate 	int error = EPERM;
1477c478bd9Sstevel@tonic-gate 	int do_nocd = 0;
148f48205beScasper 	ksid_t ksid, *ksp;
149bda89588Sjp151216 	zone_t	*zone = crgetzone(CRED());
1507c478bd9Sstevel@tonic-gate 
151bda89588Sjp151216 	if (!VALID_GID(gid, zone))
1527c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1537c478bd9Sstevel@tonic-gate 
154f48205beScasper 	if (gid > MAXUID) {
155bda89588Sjp151216 		if (ksid_lookupbygid(zone, gid, &ksid) != 0)
156f48205beScasper 			return (set_errno(EINVAL));
157f48205beScasper 		ksp = &ksid;
158f48205beScasper 	} else {
159f48205beScasper 		ksp = NULL;
160f48205beScasper 	}
1617c478bd9Sstevel@tonic-gate 	/*
1627c478bd9Sstevel@tonic-gate 	 * Need to pre-allocate the new cred structure before grabbing
1637c478bd9Sstevel@tonic-gate 	 * the p_crlock mutex.
1647c478bd9Sstevel@tonic-gate 	 */
165f48205beScasper 	newcr = cralloc_ksid();
1667c478bd9Sstevel@tonic-gate 	p = ttoproc(curthread);
1677c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
168*ddf7fe95Scasper retry:
169*ddf7fe95Scasper 	crhold(cr = p->p_cred);
170*ddf7fe95Scasper 	mutex_exit(&p->p_crlock);
171*ddf7fe95Scasper 
1727c478bd9Sstevel@tonic-gate 	if (gid == cr->cr_rgid || gid == cr->cr_gid || gid == cr->cr_sgid ||
1737c478bd9Sstevel@tonic-gate 	    (error = secpolicy_allow_setid(cr, -1, B_FALSE)) == 0) {
174*ddf7fe95Scasper 		mutex_enter(&p->p_crlock);
175*ddf7fe95Scasper 		crfree(cr);
176*ddf7fe95Scasper 		if (cr != p->p_cred)
177*ddf7fe95Scasper 			goto retry;
1787c478bd9Sstevel@tonic-gate 		/*
1797c478bd9Sstevel@tonic-gate 		 * A privileged process that makes itself look like a
1807c478bd9Sstevel@tonic-gate 		 * set-gid process must be marked to produce no core dump.
1817c478bd9Sstevel@tonic-gate 		 */
1827c478bd9Sstevel@tonic-gate 		if (cr->cr_gid != gid && error == 0)
1837c478bd9Sstevel@tonic-gate 			do_nocd = 1;
1847c478bd9Sstevel@tonic-gate 		error = 0;
1857c478bd9Sstevel@tonic-gate 		crcopy_to(cr, newcr);
1867c478bd9Sstevel@tonic-gate 		p->p_cred = newcr;
1877c478bd9Sstevel@tonic-gate 		newcr->cr_gid = gid;
188f48205beScasper 		crsetsid(newcr, ksp, KSID_GROUP);
189*ddf7fe95Scasper 		mutex_exit(&p->p_crlock);
190f48205beScasper 	} else {
1917c478bd9Sstevel@tonic-gate 		crfree(newcr);
192*ddf7fe95Scasper 		crfree(cr);
193f48205beScasper 		if (ksp != NULL)
194f48205beScasper 			ksid_rele(ksp);
195f48205beScasper 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (error == 0) {
1987c478bd9Sstevel@tonic-gate 		if (do_nocd) {
1997c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
2007c478bd9Sstevel@tonic-gate 			p->p_flag |= SNOCD;
2017c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 		crset(p, newcr);	/* broadcast to process threads */
2047c478bd9Sstevel@tonic-gate 		return (0);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 	return (set_errno(error));
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * Buy-back from SunOS 4.x
2117c478bd9Sstevel@tonic-gate  *
2127c478bd9Sstevel@tonic-gate  * Like setgid() and setegid() combined -except- that non-root users
2137c478bd9Sstevel@tonic-gate  * can change cr_rgid to cr_gid, and the semantics of cr_sgid are
2147c478bd9Sstevel@tonic-gate  * subtly different.
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate int
setregid(gid_t rgid,gid_t egid)2177c478bd9Sstevel@tonic-gate setregid(gid_t rgid, gid_t egid)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	proc_t *p;
2207c478bd9Sstevel@tonic-gate 	int error = EPERM;
2217c478bd9Sstevel@tonic-gate 	int do_nocd = 0;
2227c478bd9Sstevel@tonic-gate 	cred_t *cr, *newcr;
223f48205beScasper 	ksid_t ksid, *ksp;
224bda89588Sjp151216 	zone_t	*zone = crgetzone(CRED());
2257c478bd9Sstevel@tonic-gate 
226bda89588Sjp151216 	if ((rgid != -1 && !VALID_GID(rgid, zone)) ||
227bda89588Sjp151216 	    (egid != -1 && !VALID_GID(egid, zone)))
2287c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2297c478bd9Sstevel@tonic-gate 
230f48205beScasper 	if (egid != -1 && egid > MAXUID) {
231bda89588Sjp151216 		if (ksid_lookupbygid(zone, egid, &ksid) != 0)
232f48205beScasper 			return (set_errno(EINVAL));
233f48205beScasper 		ksp = &ksid;
234f48205beScasper 	} else {
235f48205beScasper 		ksp = NULL;
236f48205beScasper 	}
2377c478bd9Sstevel@tonic-gate 	/*
2387c478bd9Sstevel@tonic-gate 	 * Need to pre-allocate the new cred structure before grabbing
2397c478bd9Sstevel@tonic-gate 	 * the p_crlock mutex.
2407c478bd9Sstevel@tonic-gate 	 */
241f48205beScasper 	newcr = cralloc_ksid();
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	p = ttoproc(curthread);
2447c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
2457c478bd9Sstevel@tonic-gate 	cr = p->p_cred;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if ((rgid == -1 ||
2487c478bd9Sstevel@tonic-gate 	    rgid == cr->cr_rgid || rgid == cr->cr_gid || rgid == cr->cr_sgid) &&
2497c478bd9Sstevel@tonic-gate 	    (egid == -1 || egid == cr->cr_rgid || egid == cr->cr_gid ||
2507c478bd9Sstevel@tonic-gate 	    egid == cr->cr_sgid) ||
2517c478bd9Sstevel@tonic-gate 	    (error = secpolicy_allow_setid(cr, -1, B_FALSE)) == 0) {
2527c478bd9Sstevel@tonic-gate 		crhold(cr);
2537c478bd9Sstevel@tonic-gate 		crcopy_to(cr, newcr);
2547c478bd9Sstevel@tonic-gate 		p->p_cred = newcr;
2557c478bd9Sstevel@tonic-gate 
256f48205beScasper 		if (egid != -1) {
2577c478bd9Sstevel@tonic-gate 			newcr->cr_gid = egid;
258f48205beScasper 			crsetsid(newcr, ksp, KSID_GROUP);
259f48205beScasper 		}
2607c478bd9Sstevel@tonic-gate 		if (rgid != -1)
2617c478bd9Sstevel@tonic-gate 			newcr->cr_rgid = rgid;
2627c478bd9Sstevel@tonic-gate 		/*
2637c478bd9Sstevel@tonic-gate 		 * "If the real gid is being changed, or the effective gid is
2647c478bd9Sstevel@tonic-gate 		 * being changed to a value not equal to the real gid, the
2657c478bd9Sstevel@tonic-gate 		 * saved gid is set to the new effective gid."
2667c478bd9Sstevel@tonic-gate 		 */
2677c478bd9Sstevel@tonic-gate 		if (rgid != -1 ||
2687c478bd9Sstevel@tonic-gate 		    (egid != -1 && newcr->cr_gid != newcr->cr_rgid))
2697c478bd9Sstevel@tonic-gate 			newcr->cr_sgid = newcr->cr_gid;
2707c478bd9Sstevel@tonic-gate 		/*
2717c478bd9Sstevel@tonic-gate 		 * A privileged process that makes itself look like a
2727c478bd9Sstevel@tonic-gate 		 * set-gid process must be marked to produce no core dump.
2737c478bd9Sstevel@tonic-gate 		 */
2747c478bd9Sstevel@tonic-gate 		if ((cr->cr_gid != newcr->cr_gid ||
2757c478bd9Sstevel@tonic-gate 		    cr->cr_rgid != newcr->cr_rgid ||
2767c478bd9Sstevel@tonic-gate 		    cr->cr_sgid != newcr->cr_sgid) && error == 0)
2777c478bd9Sstevel@tonic-gate 			do_nocd = 1;
2787c478bd9Sstevel@tonic-gate 		error = 0;
2797c478bd9Sstevel@tonic-gate 		crfree(cr);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (error == 0) {
2847c478bd9Sstevel@tonic-gate 		if (do_nocd) {
2857c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
2867c478bd9Sstevel@tonic-gate 			p->p_flag |= SNOCD;
2877c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 		crset(p, newcr);	/* broadcast to process threads */
2907c478bd9Sstevel@tonic-gate 		return (0);
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 	crfree(newcr);
293f48205beScasper 	if (ksp != NULL)
294f48205beScasper 		ksid_rele(ksp);
2957c478bd9Sstevel@tonic-gate 	return (set_errno(error));
2967c478bd9Sstevel@tonic-gate }
297