xref: /freebsd/sys/kern/kern_loginclass.c (revision a7963b975847c6ad1c59cd7761d2f6b2a4bb9eb6)
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>
60b720dc97SMateusz 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  */
71b720dc97SMateusz Guzik static struct rwlock loginclasses_lock;
72b720dc97SMateusz 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 
90b720dc97SMateusz Guzik 	rw_wlock(&loginclasses_lock);
91b720dc97SMateusz Guzik 	if (!refcount_release(&lc->lc_refcount)) {
92b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
932bfc50bcSEdward Tomasz Napierala 		return;
942bfc50bcSEdward Tomasz Napierala 	}
95b720dc97SMateusz Guzik 
96b720dc97SMateusz Guzik 	racct_destroy(&lc->lc_racct);
97b720dc97SMateusz Guzik 	LIST_REMOVE(lc, lc_next);
98b720dc97SMateusz Guzik 	rw_wunlock(&loginclasses_lock);
99b720dc97SMateusz Guzik 
100b720dc97SMateusz Guzik 	free(lc, M_LOGINCLASS);
101b720dc97SMateusz Guzik }
102b720dc97SMateusz Guzik 
103b720dc97SMateusz Guzik /*
104b720dc97SMateusz Guzik  * Look up a loginclass struct for the parameter name.
105b720dc97SMateusz Guzik  * loginclasses_lock must be locked.
106b720dc97SMateusz Guzik  * Increase refcount on loginclass struct returned.
107b720dc97SMateusz Guzik  */
108b720dc97SMateusz Guzik static struct loginclass *
109b720dc97SMateusz Guzik loginclass_lookup(const char *name)
110b720dc97SMateusz Guzik {
111b720dc97SMateusz Guzik 	struct loginclass *lc;
112b720dc97SMateusz Guzik 
113b720dc97SMateusz Guzik 	rw_assert(&loginclasses_lock, RA_LOCKED);
114b720dc97SMateusz Guzik 	LIST_FOREACH(lc, &loginclasses, lc_next)
115b720dc97SMateusz Guzik 		if (strcmp(name, lc->lc_name) == 0) {
116b720dc97SMateusz Guzik 			loginclass_hold(lc);
117b720dc97SMateusz Guzik 			break;
118b720dc97SMateusz Guzik 		}
119b720dc97SMateusz Guzik 
120b720dc97SMateusz 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 {
133b720dc97SMateusz 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 
138b720dc97SMateusz Guzik 	rw_rlock(&loginclasses_lock);
139b720dc97SMateusz Guzik 	lc = loginclass_lookup(name);
140b720dc97SMateusz Guzik 	rw_runlock(&loginclasses_lock);
141b720dc97SMateusz Guzik 	if (lc != NULL)
1422bfc50bcSEdward Tomasz Napierala 		return (lc);
143b720dc97SMateusz Guzik 
144b720dc97SMateusz Guzik 	new_lc = malloc(sizeof(*new_lc), M_LOGINCLASS, M_ZERO | M_WAITOK);
145b720dc97SMateusz Guzik 	racct_create(&new_lc->lc_racct);
146b720dc97SMateusz Guzik 	refcount_init(&new_lc->lc_refcount, 1);
147b720dc97SMateusz Guzik 	strcpy(new_lc->lc_name, name);
148b720dc97SMateusz Guzik 
149b720dc97SMateusz Guzik 	rw_wlock(&loginclasses_lock);
150b720dc97SMateusz Guzik 	/*
151b720dc97SMateusz Guzik 	 * There's a chance someone created our loginclass while we
152b720dc97SMateusz Guzik 	 * were in malloc and not holding the lock, so we have to
153b720dc97SMateusz Guzik 	 * make sure we don't insert a duplicate loginclass.
154b720dc97SMateusz Guzik 	 */
155b720dc97SMateusz Guzik 	if ((lc = loginclass_lookup(name)) == NULL) {
156b720dc97SMateusz Guzik 		LIST_INSERT_HEAD(&loginclasses, new_lc, lc_next);
157b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
158b720dc97SMateusz Guzik 		lc = new_lc;
159b720dc97SMateusz Guzik 	} else {
160b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
161b720dc97SMateusz Guzik 		racct_destroy(&new_lc->lc_racct);
162b720dc97SMateusz Guzik 		free(new_lc, M_LOGINCLASS);
1632bfc50bcSEdward Tomasz Napierala 	}
1642bfc50bcSEdward Tomasz Napierala 
165b720dc97SMateusz 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 	struct loginclass *lc;
182*a7963b97SMateusz Guzik 	size_t lcnamelen;
1832bfc50bcSEdward Tomasz Napierala 
184*a7963b97SMateusz Guzik 	lc = td->td_ucred->cr_loginclass;
1852bfc50bcSEdward Tomasz Napierala 	lcnamelen = strlen(lc->lc_name) + 1;
1862bfc50bcSEdward Tomasz Napierala 	if (lcnamelen > uap->namelen)
187*a7963b97SMateusz Guzik 		return (ERANGE);
188*a7963b97SMateusz Guzik 	return (copyout(lc->lc_name, uap->namebuf, lcnamelen));
1892bfc50bcSEdward Tomasz Napierala }
1902bfc50bcSEdward Tomasz Napierala 
1912bfc50bcSEdward Tomasz Napierala /*
1922bfc50bcSEdward Tomasz Napierala  * Set login class name.
1932bfc50bcSEdward Tomasz Napierala  */
1942bfc50bcSEdward Tomasz Napierala #ifndef _SYS_SYSPROTO_H_
1952bfc50bcSEdward Tomasz Napierala struct setloginclass_args {
1962bfc50bcSEdward Tomasz Napierala 	const char	*namebuf;
1972bfc50bcSEdward Tomasz Napierala };
1982bfc50bcSEdward Tomasz Napierala #endif
1992bfc50bcSEdward Tomasz Napierala /* ARGSUSED */
2002bfc50bcSEdward Tomasz Napierala int
2018451d0ddSKip Macy sys_setloginclass(struct thread *td, struct setloginclass_args *uap)
2022bfc50bcSEdward Tomasz Napierala {
2032bfc50bcSEdward Tomasz Napierala 	struct proc *p = td->td_proc;
2042bfc50bcSEdward Tomasz Napierala 	int error;
2052bfc50bcSEdward Tomasz Napierala 	char lcname[MAXLOGNAME];
2062bfc50bcSEdward Tomasz Napierala 	struct loginclass *newlc;
2072bfc50bcSEdward Tomasz Napierala 	struct ucred *newcred, *oldcred;
2082bfc50bcSEdward Tomasz Napierala 
2092bfc50bcSEdward Tomasz Napierala 	error = priv_check(td, PRIV_PROC_SETLOGINCLASS);
2102bfc50bcSEdward Tomasz Napierala 	if (error != 0)
2112bfc50bcSEdward Tomasz Napierala 		return (error);
2122bfc50bcSEdward Tomasz Napierala 	error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL);
2132bfc50bcSEdward Tomasz Napierala 	if (error != 0)
2142bfc50bcSEdward Tomasz Napierala 		return (error);
2152bfc50bcSEdward Tomasz Napierala 
2162bfc50bcSEdward Tomasz Napierala 	newlc = loginclass_find(lcname);
2172bfc50bcSEdward Tomasz Napierala 	if (newlc == NULL)
2182bfc50bcSEdward Tomasz Napierala 		return (EINVAL);
2192bfc50bcSEdward Tomasz Napierala 	newcred = crget();
2202bfc50bcSEdward Tomasz Napierala 
2212bfc50bcSEdward Tomasz Napierala 	PROC_LOCK(p);
2222bfc50bcSEdward Tomasz Napierala 	oldcred = crcopysafe(p, newcred);
2232bfc50bcSEdward Tomasz Napierala 	newcred->cr_loginclass = newlc;
2242bfc50bcSEdward Tomasz Napierala 	p->p_ucred = newcred;
2252bfc50bcSEdward Tomasz Napierala 	PROC_UNLOCK(p);
226097055e2SEdward Tomasz Napierala #ifdef RACCT
227097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
228097055e2SEdward Tomasz Napierala #endif
2292bfc50bcSEdward Tomasz Napierala 	loginclass_free(oldcred->cr_loginclass);
2302bfc50bcSEdward Tomasz Napierala 	crfree(oldcred);
2312bfc50bcSEdward Tomasz Napierala 
2322bfc50bcSEdward Tomasz Napierala 	return (0);
2332bfc50bcSEdward Tomasz Napierala }
2342bfc50bcSEdward Tomasz Napierala 
235097055e2SEdward Tomasz Napierala void
236097055e2SEdward Tomasz Napierala loginclass_racct_foreach(void (*callback)(struct racct *racct,
237097055e2SEdward Tomasz Napierala     void *arg2, void *arg3), void *arg2, void *arg3)
238097055e2SEdward Tomasz Napierala {
239097055e2SEdward Tomasz Napierala 	struct loginclass *lc;
240097055e2SEdward Tomasz Napierala 
241b720dc97SMateusz Guzik 	rw_rlock(&loginclasses_lock);
242097055e2SEdward Tomasz Napierala 	LIST_FOREACH(lc, &loginclasses, lc_next)
243097055e2SEdward Tomasz Napierala 		(callback)(lc->lc_racct, arg2, arg3);
244b720dc97SMateusz Guzik 	rw_runlock(&loginclasses_lock);
245097055e2SEdward Tomasz Napierala }
246