xref: /freebsd/sys/kern/kern_loginclass.c (revision b720dc97495e6b0ac04eb818f40cb93a2c98ed71)
12bfc50bcSEdward Tomasz Napierala /*-
22bfc50bcSEdward Tomasz Napierala  * Copyright (c) 2011 The FreeBSD Foundation
32bfc50bcSEdward Tomasz Napierala  * All rights reserved.
42bfc50bcSEdward Tomasz Napierala  *
52bfc50bcSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
62bfc50bcSEdward Tomasz Napierala  * from the FreeBSD Foundation.
72bfc50bcSEdward Tomasz Napierala  *
82bfc50bcSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
92bfc50bcSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
102bfc50bcSEdward Tomasz Napierala  * are met:
112bfc50bcSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
122bfc50bcSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
132bfc50bcSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
142bfc50bcSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
152bfc50bcSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
162bfc50bcSEdward Tomasz Napierala  *
172bfc50bcSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
182bfc50bcSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192bfc50bcSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
202bfc50bcSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
212bfc50bcSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
222bfc50bcSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
232bfc50bcSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
242bfc50bcSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
252bfc50bcSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
262bfc50bcSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272bfc50bcSEdward Tomasz Napierala  * SUCH DAMAGE.
282bfc50bcSEdward Tomasz Napierala  *
292bfc50bcSEdward Tomasz Napierala  * $FreeBSD$
302bfc50bcSEdward Tomasz Napierala  */
312bfc50bcSEdward Tomasz Napierala 
322bfc50bcSEdward Tomasz Napierala /*
332bfc50bcSEdward Tomasz Napierala  * Processes may set login class name using setloginclass(2).  This
342bfc50bcSEdward Tomasz Napierala  * is usually done through call to setusercontext(3), by programs
352bfc50bcSEdward Tomasz Napierala  * such as login(1), based on information from master.passwd(5).  Kernel
362bfc50bcSEdward Tomasz Napierala  * uses this information to enforce per-class resource limits.  Current
372bfc50bcSEdward Tomasz Napierala  * login class can be determined using id(1).  Login class is inherited
382bfc50bcSEdward Tomasz Napierala  * from the parent process during fork(2).  If not set, it defaults
392bfc50bcSEdward Tomasz Napierala  * to "default".
402bfc50bcSEdward Tomasz Napierala  *
412bfc50bcSEdward Tomasz Napierala  * Code in this file implements setloginclass(2) and getloginclass(2)
422bfc50bcSEdward Tomasz Napierala  * system calls, and maintains class name storage and retrieval.
432bfc50bcSEdward Tomasz Napierala  */
442bfc50bcSEdward Tomasz Napierala 
452bfc50bcSEdward Tomasz Napierala #include <sys/cdefs.h>
462bfc50bcSEdward Tomasz Napierala __FBSDID("$FreeBSD$");
472bfc50bcSEdward Tomasz Napierala 
482bfc50bcSEdward Tomasz Napierala #include <sys/param.h>
492bfc50bcSEdward Tomasz Napierala #include <sys/eventhandler.h>
502bfc50bcSEdward Tomasz Napierala #include <sys/kernel.h>
512bfc50bcSEdward Tomasz Napierala #include <sys/lock.h>
522bfc50bcSEdward Tomasz Napierala #include <sys/loginclass.h>
532bfc50bcSEdward Tomasz Napierala #include <sys/malloc.h>
542bfc50bcSEdward Tomasz Napierala #include <sys/types.h>
552bfc50bcSEdward Tomasz Napierala #include <sys/priv.h>
562bfc50bcSEdward Tomasz Napierala #include <sys/proc.h>
572bfc50bcSEdward Tomasz Napierala #include <sys/queue.h>
58097055e2SEdward Tomasz Napierala #include <sys/racct.h>
592bfc50bcSEdward Tomasz Napierala #include <sys/refcount.h>
60*b720dc97SMateusz Guzik #include <sys/rwlock.h>
612bfc50bcSEdward Tomasz Napierala #include <sys/sysproto.h>
622bfc50bcSEdward Tomasz Napierala #include <sys/systm.h>
632bfc50bcSEdward Tomasz Napierala 
642bfc50bcSEdward Tomasz Napierala static MALLOC_DEFINE(M_LOGINCLASS, "loginclass", "loginclass structures");
652bfc50bcSEdward Tomasz Napierala 
662bfc50bcSEdward Tomasz Napierala LIST_HEAD(, loginclass)	loginclasses;
672bfc50bcSEdward Tomasz Napierala 
682bfc50bcSEdward Tomasz Napierala /*
692bfc50bcSEdward Tomasz Napierala  * Lock protecting loginclasses list.
702bfc50bcSEdward Tomasz Napierala  */
71*b720dc97SMateusz Guzik static struct rwlock loginclasses_lock;
72*b720dc97SMateusz Guzik RW_SYSINIT(loginclasses_init, &loginclasses_lock, "loginclasses lock");
732bfc50bcSEdward Tomasz Napierala 
742bfc50bcSEdward Tomasz Napierala void
752bfc50bcSEdward Tomasz Napierala loginclass_hold(struct loginclass *lc)
762bfc50bcSEdward Tomasz Napierala {
772bfc50bcSEdward Tomasz Napierala 
782bfc50bcSEdward Tomasz Napierala 	refcount_acquire(&lc->lc_refcount);
792bfc50bcSEdward Tomasz Napierala }
802bfc50bcSEdward Tomasz Napierala 
812bfc50bcSEdward Tomasz Napierala void
822bfc50bcSEdward Tomasz Napierala loginclass_free(struct loginclass *lc)
832bfc50bcSEdward Tomasz Napierala {
842bfc50bcSEdward Tomasz Napierala 	int old;
852bfc50bcSEdward Tomasz Napierala 
862bfc50bcSEdward Tomasz Napierala 	old = lc->lc_refcount;
872bfc50bcSEdward Tomasz Napierala 	if (old > 1 && atomic_cmpset_int(&lc->lc_refcount, old, old - 1))
882bfc50bcSEdward Tomasz Napierala 		return;
892bfc50bcSEdward Tomasz Napierala 
90*b720dc97SMateusz Guzik 	rw_wlock(&loginclasses_lock);
91*b720dc97SMateusz Guzik 	if (!refcount_release(&lc->lc_refcount)) {
92*b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
932bfc50bcSEdward Tomasz Napierala 		return;
942bfc50bcSEdward Tomasz Napierala 	}
95*b720dc97SMateusz Guzik 
96*b720dc97SMateusz Guzik 	racct_destroy(&lc->lc_racct);
97*b720dc97SMateusz Guzik 	LIST_REMOVE(lc, lc_next);
98*b720dc97SMateusz Guzik 	rw_wunlock(&loginclasses_lock);
99*b720dc97SMateusz Guzik 
100*b720dc97SMateusz Guzik 	free(lc, M_LOGINCLASS);
101*b720dc97SMateusz Guzik }
102*b720dc97SMateusz Guzik 
103*b720dc97SMateusz Guzik /*
104*b720dc97SMateusz Guzik  * Look up a loginclass struct for the parameter name.
105*b720dc97SMateusz Guzik  * loginclasses_lock must be locked.
106*b720dc97SMateusz Guzik  * Increase refcount on loginclass struct returned.
107*b720dc97SMateusz Guzik  */
108*b720dc97SMateusz Guzik static struct loginclass *
109*b720dc97SMateusz Guzik loginclass_lookup(const char *name)
110*b720dc97SMateusz Guzik {
111*b720dc97SMateusz Guzik 	struct loginclass *lc;
112*b720dc97SMateusz Guzik 
113*b720dc97SMateusz Guzik 	rw_assert(&loginclasses_lock, RA_LOCKED);
114*b720dc97SMateusz Guzik 	LIST_FOREACH(lc, &loginclasses, lc_next)
115*b720dc97SMateusz Guzik 		if (strcmp(name, lc->lc_name) == 0) {
116*b720dc97SMateusz Guzik 			loginclass_hold(lc);
117*b720dc97SMateusz Guzik 			break;
118*b720dc97SMateusz Guzik 		}
119*b720dc97SMateusz Guzik 
120*b720dc97SMateusz Guzik 	return (lc);
1212bfc50bcSEdward Tomasz Napierala }
1222bfc50bcSEdward Tomasz Napierala 
1232bfc50bcSEdward Tomasz Napierala /*
1242bfc50bcSEdward Tomasz Napierala  * Return loginclass structure with a corresponding name.  Not
1252bfc50bcSEdward Tomasz Napierala  * performance critical, as it's used mainly by setloginclass(2),
1262bfc50bcSEdward Tomasz Napierala  * which happens once per login session.  Caller has to use
1272bfc50bcSEdward Tomasz Napierala  * loginclass_free() on the returned value when it's no longer
1282bfc50bcSEdward Tomasz Napierala  * needed.
1292bfc50bcSEdward Tomasz Napierala  */
1302bfc50bcSEdward Tomasz Napierala struct loginclass *
1312bfc50bcSEdward Tomasz Napierala loginclass_find(const char *name)
1322bfc50bcSEdward Tomasz Napierala {
133*b720dc97SMateusz Guzik 	struct loginclass *lc, *new_lc;
1342bfc50bcSEdward Tomasz Napierala 
1352bfc50bcSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXLOGNAME)
1362bfc50bcSEdward Tomasz Napierala 		return (NULL);
1372bfc50bcSEdward Tomasz Napierala 
138*b720dc97SMateusz Guzik 	rw_rlock(&loginclasses_lock);
139*b720dc97SMateusz Guzik 	lc = loginclass_lookup(name);
140*b720dc97SMateusz Guzik 	rw_runlock(&loginclasses_lock);
141*b720dc97SMateusz Guzik 	if (lc != NULL)
1422bfc50bcSEdward Tomasz Napierala 		return (lc);
143*b720dc97SMateusz Guzik 
144*b720dc97SMateusz Guzik 	new_lc = malloc(sizeof(*new_lc), M_LOGINCLASS, M_ZERO | M_WAITOK);
145*b720dc97SMateusz Guzik 	racct_create(&new_lc->lc_racct);
146*b720dc97SMateusz Guzik 	refcount_init(&new_lc->lc_refcount, 1);
147*b720dc97SMateusz Guzik 	strcpy(new_lc->lc_name, name);
148*b720dc97SMateusz Guzik 
149*b720dc97SMateusz Guzik 	rw_wlock(&loginclasses_lock);
150*b720dc97SMateusz Guzik 	/*
151*b720dc97SMateusz Guzik 	 * There's a chance someone created our loginclass while we
152*b720dc97SMateusz Guzik 	 * were in malloc and not holding the lock, so we have to
153*b720dc97SMateusz Guzik 	 * make sure we don't insert a duplicate loginclass.
154*b720dc97SMateusz Guzik 	 */
155*b720dc97SMateusz Guzik 	if ((lc = loginclass_lookup(name)) == NULL) {
156*b720dc97SMateusz Guzik 		LIST_INSERT_HEAD(&loginclasses, new_lc, lc_next);
157*b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
158*b720dc97SMateusz Guzik 		lc = new_lc;
159*b720dc97SMateusz Guzik 	} else {
160*b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
161*b720dc97SMateusz Guzik 		racct_destroy(&new_lc->lc_racct);
162*b720dc97SMateusz Guzik 		free(new_lc, M_LOGINCLASS);
1632bfc50bcSEdward Tomasz Napierala 	}
1642bfc50bcSEdward Tomasz Napierala 
165*b720dc97SMateusz Guzik 	return (lc);
1662bfc50bcSEdward Tomasz Napierala }
1672bfc50bcSEdward Tomasz Napierala 
1682bfc50bcSEdward Tomasz Napierala /*
1692bfc50bcSEdward Tomasz Napierala  * Get login class name.
1702bfc50bcSEdward Tomasz Napierala  */
1712bfc50bcSEdward Tomasz Napierala #ifndef _SYS_SYSPROTO_H_
1722bfc50bcSEdward Tomasz Napierala struct getloginclass_args {
1732bfc50bcSEdward Tomasz Napierala 	char	*namebuf;
1742bfc50bcSEdward Tomasz Napierala 	size_t	namelen;
1752bfc50bcSEdward Tomasz Napierala };
1762bfc50bcSEdward Tomasz Napierala #endif
1772bfc50bcSEdward Tomasz Napierala /* ARGSUSED */
1782bfc50bcSEdward Tomasz Napierala int
1798451d0ddSKip Macy sys_getloginclass(struct thread *td, struct getloginclass_args *uap)
1802bfc50bcSEdward Tomasz Napierala {
1812bfc50bcSEdward Tomasz Napierala 	int error = 0;
1822bfc50bcSEdward Tomasz Napierala 	size_t lcnamelen;
1832bfc50bcSEdward Tomasz Napierala 	struct proc *p;
1842bfc50bcSEdward Tomasz Napierala 	struct loginclass *lc;
1852bfc50bcSEdward Tomasz Napierala 
1862bfc50bcSEdward Tomasz Napierala 	p = td->td_proc;
1872bfc50bcSEdward Tomasz Napierala 	PROC_LOCK(p);
1882bfc50bcSEdward Tomasz Napierala 	lc = p->p_ucred->cr_loginclass;
1892bfc50bcSEdward Tomasz Napierala 	loginclass_hold(lc);
1902bfc50bcSEdward Tomasz Napierala 	PROC_UNLOCK(p);
1912bfc50bcSEdward Tomasz Napierala 
1922bfc50bcSEdward Tomasz Napierala 	lcnamelen = strlen(lc->lc_name) + 1;
1932bfc50bcSEdward Tomasz Napierala 	if (lcnamelen > uap->namelen)
1942bfc50bcSEdward Tomasz Napierala 		error = ERANGE;
1952bfc50bcSEdward Tomasz Napierala 	if (error == 0)
1962bfc50bcSEdward Tomasz Napierala 		error = copyout(lc->lc_name, uap->namebuf, lcnamelen);
1972bfc50bcSEdward Tomasz Napierala 	loginclass_free(lc);
1982bfc50bcSEdward Tomasz Napierala 	return (error);
1992bfc50bcSEdward Tomasz Napierala }
2002bfc50bcSEdward Tomasz Napierala 
2012bfc50bcSEdward Tomasz Napierala /*
2022bfc50bcSEdward Tomasz Napierala  * Set login class name.
2032bfc50bcSEdward Tomasz Napierala  */
2042bfc50bcSEdward Tomasz Napierala #ifndef _SYS_SYSPROTO_H_
2052bfc50bcSEdward Tomasz Napierala struct setloginclass_args {
2062bfc50bcSEdward Tomasz Napierala 	const char	*namebuf;
2072bfc50bcSEdward Tomasz Napierala };
2082bfc50bcSEdward Tomasz Napierala #endif
2092bfc50bcSEdward Tomasz Napierala /* ARGSUSED */
2102bfc50bcSEdward Tomasz Napierala int
2118451d0ddSKip Macy sys_setloginclass(struct thread *td, struct setloginclass_args *uap)
2122bfc50bcSEdward Tomasz Napierala {
2132bfc50bcSEdward Tomasz Napierala 	struct proc *p = td->td_proc;
2142bfc50bcSEdward Tomasz Napierala 	int error;
2152bfc50bcSEdward Tomasz Napierala 	char lcname[MAXLOGNAME];
2162bfc50bcSEdward Tomasz Napierala 	struct loginclass *newlc;
2172bfc50bcSEdward Tomasz Napierala 	struct ucred *newcred, *oldcred;
2182bfc50bcSEdward Tomasz Napierala 
2192bfc50bcSEdward Tomasz Napierala 	error = priv_check(td, PRIV_PROC_SETLOGINCLASS);
2202bfc50bcSEdward Tomasz Napierala 	if (error != 0)
2212bfc50bcSEdward Tomasz Napierala 		return (error);
2222bfc50bcSEdward Tomasz Napierala 	error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL);
2232bfc50bcSEdward Tomasz Napierala 	if (error != 0)
2242bfc50bcSEdward Tomasz Napierala 		return (error);
2252bfc50bcSEdward Tomasz Napierala 
2262bfc50bcSEdward Tomasz Napierala 	newlc = loginclass_find(lcname);
2272bfc50bcSEdward Tomasz Napierala 	if (newlc == NULL)
2282bfc50bcSEdward Tomasz Napierala 		return (EINVAL);
2292bfc50bcSEdward Tomasz Napierala 	newcred = crget();
2302bfc50bcSEdward Tomasz Napierala 
2312bfc50bcSEdward Tomasz Napierala 	PROC_LOCK(p);
2322bfc50bcSEdward Tomasz Napierala 	oldcred = crcopysafe(p, newcred);
2332bfc50bcSEdward Tomasz Napierala 	newcred->cr_loginclass = newlc;
2342bfc50bcSEdward Tomasz Napierala 	p->p_ucred = newcred;
2352bfc50bcSEdward Tomasz Napierala 	PROC_UNLOCK(p);
236097055e2SEdward Tomasz Napierala #ifdef RACCT
237097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
238097055e2SEdward Tomasz Napierala #endif
2392bfc50bcSEdward Tomasz Napierala 	loginclass_free(oldcred->cr_loginclass);
2402bfc50bcSEdward Tomasz Napierala 	crfree(oldcred);
2412bfc50bcSEdward Tomasz Napierala 
2422bfc50bcSEdward Tomasz Napierala 	return (0);
2432bfc50bcSEdward Tomasz Napierala }
2442bfc50bcSEdward Tomasz Napierala 
245097055e2SEdward Tomasz Napierala void
246097055e2SEdward Tomasz Napierala loginclass_racct_foreach(void (*callback)(struct racct *racct,
247097055e2SEdward Tomasz Napierala     void *arg2, void *arg3), void *arg2, void *arg3)
248097055e2SEdward Tomasz Napierala {
249097055e2SEdward Tomasz Napierala 	struct loginclass *lc;
250097055e2SEdward Tomasz Napierala 
251*b720dc97SMateusz Guzik 	rw_rlock(&loginclasses_lock);
252097055e2SEdward Tomasz Napierala 	LIST_FOREACH(lc, &loginclasses, lc_next)
253097055e2SEdward Tomasz Napierala 		(callback)(lc->lc_racct, arg2, arg3);
254*b720dc97SMateusz Guzik 	rw_runlock(&loginclasses_lock);
255097055e2SEdward Tomasz Napierala }
256