xref: /freebsd/sys/kern/kern_loginclass.c (revision 8451d0dd78c0a9c0383485b18aba9f081a9db8bb)
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/mutex.h>
552bfc50bcSEdward Tomasz Napierala #include <sys/types.h>
562bfc50bcSEdward Tomasz Napierala #include <sys/priv.h>
572bfc50bcSEdward Tomasz Napierala #include <sys/proc.h>
582bfc50bcSEdward Tomasz Napierala #include <sys/queue.h>
59097055e2SEdward Tomasz Napierala #include <sys/racct.h>
602bfc50bcSEdward Tomasz Napierala #include <sys/refcount.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  */
712bfc50bcSEdward Tomasz Napierala static struct mtx loginclasses_lock;
722bfc50bcSEdward Tomasz Napierala 
732bfc50bcSEdward Tomasz Napierala static void lc_init(void);
742bfc50bcSEdward Tomasz Napierala SYSINIT(loginclass, SI_SUB_CPU, SI_ORDER_FIRST, lc_init, NULL);
752bfc50bcSEdward Tomasz Napierala 
762bfc50bcSEdward Tomasz Napierala void
772bfc50bcSEdward Tomasz Napierala loginclass_hold(struct loginclass *lc)
782bfc50bcSEdward Tomasz Napierala {
792bfc50bcSEdward Tomasz Napierala 
802bfc50bcSEdward Tomasz Napierala 	refcount_acquire(&lc->lc_refcount);
812bfc50bcSEdward Tomasz Napierala }
822bfc50bcSEdward Tomasz Napierala 
832bfc50bcSEdward Tomasz Napierala void
842bfc50bcSEdward Tomasz Napierala loginclass_free(struct loginclass *lc)
852bfc50bcSEdward Tomasz Napierala {
862bfc50bcSEdward Tomasz Napierala 	int old;
872bfc50bcSEdward Tomasz Napierala 
882bfc50bcSEdward Tomasz Napierala 	old = lc->lc_refcount;
892bfc50bcSEdward Tomasz Napierala 	if (old > 1 && atomic_cmpset_int(&lc->lc_refcount, old, old - 1))
902bfc50bcSEdward Tomasz Napierala 		return;
912bfc50bcSEdward Tomasz Napierala 
922bfc50bcSEdward Tomasz Napierala 	mtx_lock(&loginclasses_lock);
932bfc50bcSEdward Tomasz Napierala 	if (refcount_release(&lc->lc_refcount)) {
94097055e2SEdward Tomasz Napierala 		racct_destroy(&lc->lc_racct);
952bfc50bcSEdward Tomasz Napierala 		LIST_REMOVE(lc, lc_next);
962bfc50bcSEdward Tomasz Napierala 		mtx_unlock(&loginclasses_lock);
972bfc50bcSEdward Tomasz Napierala 		free(lc, M_LOGINCLASS);
982bfc50bcSEdward Tomasz Napierala 
992bfc50bcSEdward Tomasz Napierala 		return;
1002bfc50bcSEdward Tomasz Napierala 	}
1012bfc50bcSEdward Tomasz Napierala 	mtx_unlock(&loginclasses_lock);
1022bfc50bcSEdward Tomasz Napierala }
1032bfc50bcSEdward Tomasz Napierala 
1042bfc50bcSEdward Tomasz Napierala /*
1052bfc50bcSEdward Tomasz Napierala  * Return loginclass structure with a corresponding name.  Not
1062bfc50bcSEdward Tomasz Napierala  * performance critical, as it's used mainly by setloginclass(2),
1072bfc50bcSEdward Tomasz Napierala  * which happens once per login session.  Caller has to use
1082bfc50bcSEdward Tomasz Napierala  * loginclass_free() on the returned value when it's no longer
1092bfc50bcSEdward Tomasz Napierala  * needed.
1102bfc50bcSEdward Tomasz Napierala  */
1112bfc50bcSEdward Tomasz Napierala struct loginclass *
1122bfc50bcSEdward Tomasz Napierala loginclass_find(const char *name)
1132bfc50bcSEdward Tomasz Napierala {
1142bfc50bcSEdward Tomasz Napierala 	struct loginclass *lc, *newlc;
1152bfc50bcSEdward Tomasz Napierala 
1162bfc50bcSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXLOGNAME)
1172bfc50bcSEdward Tomasz Napierala 		return (NULL);
1182bfc50bcSEdward Tomasz Napierala 
1192bfc50bcSEdward Tomasz Napierala 	newlc = malloc(sizeof(*newlc), M_LOGINCLASS, M_ZERO | M_WAITOK);
120097055e2SEdward Tomasz Napierala 	racct_create(&newlc->lc_racct);
1212bfc50bcSEdward Tomasz Napierala 
1222bfc50bcSEdward Tomasz Napierala 	mtx_lock(&loginclasses_lock);
1232bfc50bcSEdward Tomasz Napierala 	LIST_FOREACH(lc, &loginclasses, lc_next) {
1242bfc50bcSEdward Tomasz Napierala 		if (strcmp(name, lc->lc_name) != 0)
1252bfc50bcSEdward Tomasz Napierala 			continue;
1262bfc50bcSEdward Tomasz Napierala 
1272bfc50bcSEdward Tomasz Napierala 		/* Found loginclass with a matching name? */
1282bfc50bcSEdward Tomasz Napierala 		loginclass_hold(lc);
1292bfc50bcSEdward Tomasz Napierala 		mtx_unlock(&loginclasses_lock);
130097055e2SEdward Tomasz Napierala 		racct_destroy(&newlc->lc_racct);
1312bfc50bcSEdward Tomasz Napierala 		free(newlc, M_LOGINCLASS);
1322bfc50bcSEdward Tomasz Napierala 		return (lc);
1332bfc50bcSEdward Tomasz Napierala 	}
1342bfc50bcSEdward Tomasz Napierala 
1352bfc50bcSEdward Tomasz Napierala 	/* Add new loginclass. */
1362bfc50bcSEdward Tomasz Napierala 	strcpy(newlc->lc_name, name);
1372bfc50bcSEdward Tomasz Napierala 	refcount_init(&newlc->lc_refcount, 1);
1382bfc50bcSEdward Tomasz Napierala 	LIST_INSERT_HEAD(&loginclasses, newlc, lc_next);
1392bfc50bcSEdward Tomasz Napierala 	mtx_unlock(&loginclasses_lock);
1402bfc50bcSEdward Tomasz Napierala 
1412bfc50bcSEdward Tomasz Napierala 	return (newlc);
1422bfc50bcSEdward Tomasz Napierala }
1432bfc50bcSEdward Tomasz Napierala 
1442bfc50bcSEdward Tomasz Napierala /*
1452bfc50bcSEdward Tomasz Napierala  * Get login class name.
1462bfc50bcSEdward Tomasz Napierala  */
1472bfc50bcSEdward Tomasz Napierala #ifndef _SYS_SYSPROTO_H_
1482bfc50bcSEdward Tomasz Napierala struct getloginclass_args {
1492bfc50bcSEdward Tomasz Napierala 	char	*namebuf;
1502bfc50bcSEdward Tomasz Napierala 	size_t	namelen;
1512bfc50bcSEdward Tomasz Napierala };
1522bfc50bcSEdward Tomasz Napierala #endif
1532bfc50bcSEdward Tomasz Napierala /* ARGSUSED */
1542bfc50bcSEdward Tomasz Napierala int
155*8451d0ddSKip Macy sys_getloginclass(struct thread *td, struct getloginclass_args *uap)
1562bfc50bcSEdward Tomasz Napierala {
1572bfc50bcSEdward Tomasz Napierala 	int error = 0;
1582bfc50bcSEdward Tomasz Napierala 	size_t lcnamelen;
1592bfc50bcSEdward Tomasz Napierala 	struct proc *p;
1602bfc50bcSEdward Tomasz Napierala 	struct loginclass *lc;
1612bfc50bcSEdward Tomasz Napierala 
1622bfc50bcSEdward Tomasz Napierala 	p = td->td_proc;
1632bfc50bcSEdward Tomasz Napierala 	PROC_LOCK(p);
1642bfc50bcSEdward Tomasz Napierala 	lc = p->p_ucred->cr_loginclass;
1652bfc50bcSEdward Tomasz Napierala 	loginclass_hold(lc);
1662bfc50bcSEdward Tomasz Napierala 	PROC_UNLOCK(p);
1672bfc50bcSEdward Tomasz Napierala 
1682bfc50bcSEdward Tomasz Napierala 	lcnamelen = strlen(lc->lc_name) + 1;
1692bfc50bcSEdward Tomasz Napierala 	if (lcnamelen > uap->namelen)
1702bfc50bcSEdward Tomasz Napierala 		error = ERANGE;
1712bfc50bcSEdward Tomasz Napierala 	if (error == 0)
1722bfc50bcSEdward Tomasz Napierala 		error = copyout(lc->lc_name, uap->namebuf, lcnamelen);
1732bfc50bcSEdward Tomasz Napierala 	loginclass_free(lc);
1742bfc50bcSEdward Tomasz Napierala 	return (error);
1752bfc50bcSEdward Tomasz Napierala }
1762bfc50bcSEdward Tomasz Napierala 
1772bfc50bcSEdward Tomasz Napierala /*
1782bfc50bcSEdward Tomasz Napierala  * Set login class name.
1792bfc50bcSEdward Tomasz Napierala  */
1802bfc50bcSEdward Tomasz Napierala #ifndef _SYS_SYSPROTO_H_
1812bfc50bcSEdward Tomasz Napierala struct setloginclass_args {
1822bfc50bcSEdward Tomasz Napierala 	const char	*namebuf;
1832bfc50bcSEdward Tomasz Napierala };
1842bfc50bcSEdward Tomasz Napierala #endif
1852bfc50bcSEdward Tomasz Napierala /* ARGSUSED */
1862bfc50bcSEdward Tomasz Napierala int
187*8451d0ddSKip Macy sys_setloginclass(struct thread *td, struct setloginclass_args *uap)
1882bfc50bcSEdward Tomasz Napierala {
1892bfc50bcSEdward Tomasz Napierala 	struct proc *p = td->td_proc;
1902bfc50bcSEdward Tomasz Napierala 	int error;
1912bfc50bcSEdward Tomasz Napierala 	char lcname[MAXLOGNAME];
1922bfc50bcSEdward Tomasz Napierala 	struct loginclass *newlc;
1932bfc50bcSEdward Tomasz Napierala 	struct ucred *newcred, *oldcred;
1942bfc50bcSEdward Tomasz Napierala 
1952bfc50bcSEdward Tomasz Napierala 	error = priv_check(td, PRIV_PROC_SETLOGINCLASS);
1962bfc50bcSEdward Tomasz Napierala 	if (error != 0)
1972bfc50bcSEdward Tomasz Napierala 		return (error);
1982bfc50bcSEdward Tomasz Napierala 	error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL);
1992bfc50bcSEdward Tomasz Napierala 	if (error != 0)
2002bfc50bcSEdward Tomasz Napierala 		return (error);
2012bfc50bcSEdward Tomasz Napierala 
2022bfc50bcSEdward Tomasz Napierala 	newlc = loginclass_find(lcname);
2032bfc50bcSEdward Tomasz Napierala 	if (newlc == NULL)
2042bfc50bcSEdward Tomasz Napierala 		return (EINVAL);
2052bfc50bcSEdward Tomasz Napierala 	newcred = crget();
2062bfc50bcSEdward Tomasz Napierala 
2072bfc50bcSEdward Tomasz Napierala 	PROC_LOCK(p);
2082bfc50bcSEdward Tomasz Napierala 	oldcred = crcopysafe(p, newcred);
2092bfc50bcSEdward Tomasz Napierala 	newcred->cr_loginclass = newlc;
2102bfc50bcSEdward Tomasz Napierala 	p->p_ucred = newcred;
2112bfc50bcSEdward Tomasz Napierala 	PROC_UNLOCK(p);
212097055e2SEdward Tomasz Napierala #ifdef RACCT
213097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
214097055e2SEdward Tomasz Napierala #endif
2152bfc50bcSEdward Tomasz Napierala 	loginclass_free(oldcred->cr_loginclass);
2162bfc50bcSEdward Tomasz Napierala 	crfree(oldcred);
2172bfc50bcSEdward Tomasz Napierala 
2182bfc50bcSEdward Tomasz Napierala 	return (0);
2192bfc50bcSEdward Tomasz Napierala }
2202bfc50bcSEdward Tomasz Napierala 
221097055e2SEdward Tomasz Napierala void
222097055e2SEdward Tomasz Napierala loginclass_racct_foreach(void (*callback)(struct racct *racct,
223097055e2SEdward Tomasz Napierala     void *arg2, void *arg3), void *arg2, void *arg3)
224097055e2SEdward Tomasz Napierala {
225097055e2SEdward Tomasz Napierala 	struct loginclass *lc;
226097055e2SEdward Tomasz Napierala 
227097055e2SEdward Tomasz Napierala 	mtx_lock(&loginclasses_lock);
228097055e2SEdward Tomasz Napierala 	LIST_FOREACH(lc, &loginclasses, lc_next)
229097055e2SEdward Tomasz Napierala 		(callback)(lc->lc_racct, arg2, arg3);
230097055e2SEdward Tomasz Napierala 	mtx_unlock(&loginclasses_lock);
231097055e2SEdward Tomasz Napierala }
232097055e2SEdward Tomasz Napierala 
2332bfc50bcSEdward Tomasz Napierala static void
2342bfc50bcSEdward Tomasz Napierala lc_init(void)
2352bfc50bcSEdward Tomasz Napierala {
2362bfc50bcSEdward Tomasz Napierala 
2372bfc50bcSEdward Tomasz Napierala 	mtx_init(&loginclasses_lock, "loginclasses lock", NULL, MTX_DEF);
2382bfc50bcSEdward Tomasz Napierala }
239