xref: /freebsd/sys/kern/kern_loginclass.c (revision 5d03f1e11f0bee00e5a155e6ccb69f59f70ba3c8)
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;
134c0b5261bSMateusz Guzik 	struct ucred *cred;
1352bfc50bcSEdward Tomasz Napierala 
1362bfc50bcSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXLOGNAME)
1372bfc50bcSEdward Tomasz Napierala 		return (NULL);
1382bfc50bcSEdward Tomasz Napierala 
139*5d03f1e1SMateusz Guzik 	lc = curthread->td_ucred->cr_loginclass;
140c0b5261bSMateusz Guzik 	if (strcmp(name, lc->lc_name) == 0) {
141c0b5261bSMateusz Guzik 		loginclass_hold(lc);
142c0b5261bSMateusz Guzik 		return (lc);
143c0b5261bSMateusz Guzik 	}
144c0b5261bSMateusz Guzik 
145b720dc97SMateusz Guzik 	rw_rlock(&loginclasses_lock);
146b720dc97SMateusz Guzik 	lc = loginclass_lookup(name);
147b720dc97SMateusz Guzik 	rw_runlock(&loginclasses_lock);
148b720dc97SMateusz Guzik 	if (lc != NULL)
1492bfc50bcSEdward Tomasz Napierala 		return (lc);
150b720dc97SMateusz Guzik 
151b720dc97SMateusz Guzik 	new_lc = malloc(sizeof(*new_lc), M_LOGINCLASS, M_ZERO | M_WAITOK);
152b720dc97SMateusz Guzik 	racct_create(&new_lc->lc_racct);
153b720dc97SMateusz Guzik 	refcount_init(&new_lc->lc_refcount, 1);
154b720dc97SMateusz Guzik 	strcpy(new_lc->lc_name, name);
155b720dc97SMateusz Guzik 
156b720dc97SMateusz Guzik 	rw_wlock(&loginclasses_lock);
157b720dc97SMateusz Guzik 	/*
158b720dc97SMateusz Guzik 	 * There's a chance someone created our loginclass while we
159b720dc97SMateusz Guzik 	 * were in malloc and not holding the lock, so we have to
160b720dc97SMateusz Guzik 	 * make sure we don't insert a duplicate loginclass.
161b720dc97SMateusz Guzik 	 */
162b720dc97SMateusz Guzik 	if ((lc = loginclass_lookup(name)) == NULL) {
163b720dc97SMateusz Guzik 		LIST_INSERT_HEAD(&loginclasses, new_lc, lc_next);
164b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
165b720dc97SMateusz Guzik 		lc = new_lc;
166b720dc97SMateusz Guzik 	} else {
167b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
168b720dc97SMateusz Guzik 		racct_destroy(&new_lc->lc_racct);
169b720dc97SMateusz Guzik 		free(new_lc, M_LOGINCLASS);
1702bfc50bcSEdward Tomasz Napierala 	}
1712bfc50bcSEdward Tomasz Napierala 
172b720dc97SMateusz Guzik 	return (lc);
1732bfc50bcSEdward Tomasz Napierala }
1742bfc50bcSEdward Tomasz Napierala 
1752bfc50bcSEdward Tomasz Napierala /*
1762bfc50bcSEdward Tomasz Napierala  * Get login class name.
1772bfc50bcSEdward Tomasz Napierala  */
1782bfc50bcSEdward Tomasz Napierala #ifndef _SYS_SYSPROTO_H_
1792bfc50bcSEdward Tomasz Napierala struct getloginclass_args {
1802bfc50bcSEdward Tomasz Napierala 	char	*namebuf;
1812bfc50bcSEdward Tomasz Napierala 	size_t	namelen;
1822bfc50bcSEdward Tomasz Napierala };
1832bfc50bcSEdward Tomasz Napierala #endif
1842bfc50bcSEdward Tomasz Napierala /* ARGSUSED */
1852bfc50bcSEdward Tomasz Napierala int
1868451d0ddSKip Macy sys_getloginclass(struct thread *td, struct getloginclass_args *uap)
1872bfc50bcSEdward Tomasz Napierala {
1882bfc50bcSEdward Tomasz Napierala 	struct loginclass *lc;
189a7963b97SMateusz Guzik 	size_t lcnamelen;
1902bfc50bcSEdward Tomasz Napierala 
191a7963b97SMateusz Guzik 	lc = td->td_ucred->cr_loginclass;
1922bfc50bcSEdward Tomasz Napierala 	lcnamelen = strlen(lc->lc_name) + 1;
1932bfc50bcSEdward Tomasz Napierala 	if (lcnamelen > uap->namelen)
194a7963b97SMateusz Guzik 		return (ERANGE);
195a7963b97SMateusz Guzik 	return (copyout(lc->lc_name, uap->namebuf, lcnamelen));
1962bfc50bcSEdward Tomasz Napierala }
1972bfc50bcSEdward Tomasz Napierala 
1982bfc50bcSEdward Tomasz Napierala /*
1992bfc50bcSEdward Tomasz Napierala  * Set login class name.
2002bfc50bcSEdward Tomasz Napierala  */
2012bfc50bcSEdward Tomasz Napierala #ifndef _SYS_SYSPROTO_H_
2022bfc50bcSEdward Tomasz Napierala struct setloginclass_args {
2032bfc50bcSEdward Tomasz Napierala 	const char	*namebuf;
2042bfc50bcSEdward Tomasz Napierala };
2052bfc50bcSEdward Tomasz Napierala #endif
2062bfc50bcSEdward Tomasz Napierala /* ARGSUSED */
2072bfc50bcSEdward Tomasz Napierala int
2088451d0ddSKip Macy sys_setloginclass(struct thread *td, struct setloginclass_args *uap)
2092bfc50bcSEdward Tomasz Napierala {
2102bfc50bcSEdward Tomasz Napierala 	struct proc *p = td->td_proc;
2112bfc50bcSEdward Tomasz Napierala 	int error;
2122bfc50bcSEdward Tomasz Napierala 	char lcname[MAXLOGNAME];
2132bfc50bcSEdward Tomasz Napierala 	struct loginclass *newlc;
2142bfc50bcSEdward Tomasz Napierala 	struct ucred *newcred, *oldcred;
2152bfc50bcSEdward Tomasz Napierala 
2162bfc50bcSEdward Tomasz Napierala 	error = priv_check(td, PRIV_PROC_SETLOGINCLASS);
2172bfc50bcSEdward Tomasz Napierala 	if (error != 0)
2182bfc50bcSEdward Tomasz Napierala 		return (error);
2192bfc50bcSEdward Tomasz Napierala 	error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL);
2202bfc50bcSEdward Tomasz Napierala 	if (error != 0)
2212bfc50bcSEdward Tomasz Napierala 		return (error);
2222bfc50bcSEdward Tomasz Napierala 
2232bfc50bcSEdward Tomasz Napierala 	newlc = loginclass_find(lcname);
2242bfc50bcSEdward Tomasz Napierala 	if (newlc == NULL)
2252bfc50bcSEdward Tomasz Napierala 		return (EINVAL);
2262bfc50bcSEdward Tomasz Napierala 	newcred = crget();
2272bfc50bcSEdward Tomasz Napierala 
2282bfc50bcSEdward Tomasz Napierala 	PROC_LOCK(p);
2292bfc50bcSEdward Tomasz Napierala 	oldcred = crcopysafe(p, newcred);
2302bfc50bcSEdward Tomasz Napierala 	newcred->cr_loginclass = newlc;
231daf63fd2SMateusz Guzik 	proc_set_cred(p, newcred);
2322bfc50bcSEdward Tomasz Napierala 	PROC_UNLOCK(p);
233097055e2SEdward Tomasz Napierala #ifdef RACCT
234097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
235097055e2SEdward Tomasz Napierala #endif
2362bfc50bcSEdward Tomasz Napierala 	loginclass_free(oldcred->cr_loginclass);
2372bfc50bcSEdward Tomasz Napierala 	crfree(oldcred);
2382bfc50bcSEdward Tomasz Napierala 
2392bfc50bcSEdward Tomasz Napierala 	return (0);
2402bfc50bcSEdward Tomasz Napierala }
2412bfc50bcSEdward Tomasz Napierala 
242097055e2SEdward Tomasz Napierala void
243097055e2SEdward Tomasz Napierala loginclass_racct_foreach(void (*callback)(struct racct *racct,
24415db3c07SEdward Tomasz Napierala     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
24515db3c07SEdward Tomasz Napierala     void *arg2, void *arg3)
246097055e2SEdward Tomasz Napierala {
247097055e2SEdward Tomasz Napierala 	struct loginclass *lc;
248097055e2SEdward Tomasz Napierala 
249b720dc97SMateusz Guzik 	rw_rlock(&loginclasses_lock);
25015db3c07SEdward Tomasz Napierala 	if (pre != NULL)
25115db3c07SEdward Tomasz Napierala 		(pre)();
252097055e2SEdward Tomasz Napierala 	LIST_FOREACH(lc, &loginclasses, lc_next)
253097055e2SEdward Tomasz Napierala 		(callback)(lc->lc_racct, arg2, arg3);
25415db3c07SEdward Tomasz Napierala 	if (post != NULL)
25515db3c07SEdward Tomasz Napierala 		(post)();
256b720dc97SMateusz Guzik 	rw_runlock(&loginclasses_lock);
257097055e2SEdward Tomasz Napierala }
258