xref: /freebsd/sys/kern/kern_loginclass.c (revision 8a36da99deb0e19363ec04e4d3facd869c1028f5)
12bfc50bcSEdward Tomasz Napierala /*-
2*8a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*8a36da99SPedro F. Giffuni  *
42bfc50bcSEdward Tomasz Napierala  * Copyright (c) 2011 The FreeBSD Foundation
52bfc50bcSEdward Tomasz Napierala  * All rights reserved.
62bfc50bcSEdward Tomasz Napierala  *
72bfc50bcSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
82bfc50bcSEdward Tomasz Napierala  * from the FreeBSD Foundation.
92bfc50bcSEdward Tomasz Napierala  *
102bfc50bcSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
112bfc50bcSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
122bfc50bcSEdward Tomasz Napierala  * are met:
132bfc50bcSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
142bfc50bcSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
152bfc50bcSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
162bfc50bcSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
172bfc50bcSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
182bfc50bcSEdward Tomasz Napierala  *
192bfc50bcSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
202bfc50bcSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212bfc50bcSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222bfc50bcSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
232bfc50bcSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242bfc50bcSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252bfc50bcSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262bfc50bcSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272bfc50bcSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282bfc50bcSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292bfc50bcSEdward Tomasz Napierala  * SUCH DAMAGE.
302bfc50bcSEdward Tomasz Napierala  *
312bfc50bcSEdward Tomasz Napierala  * $FreeBSD$
322bfc50bcSEdward Tomasz Napierala  */
332bfc50bcSEdward Tomasz Napierala 
342bfc50bcSEdward Tomasz Napierala /*
352bfc50bcSEdward Tomasz Napierala  * Processes may set login class name using setloginclass(2).  This
362bfc50bcSEdward Tomasz Napierala  * is usually done through call to setusercontext(3), by programs
372bfc50bcSEdward Tomasz Napierala  * such as login(1), based on information from master.passwd(5).  Kernel
382bfc50bcSEdward Tomasz Napierala  * uses this information to enforce per-class resource limits.  Current
392bfc50bcSEdward Tomasz Napierala  * login class can be determined using id(1).  Login class is inherited
402bfc50bcSEdward Tomasz Napierala  * from the parent process during fork(2).  If not set, it defaults
412bfc50bcSEdward Tomasz Napierala  * to "default".
422bfc50bcSEdward Tomasz Napierala  *
432bfc50bcSEdward Tomasz Napierala  * Code in this file implements setloginclass(2) and getloginclass(2)
442bfc50bcSEdward Tomasz Napierala  * system calls, and maintains class name storage and retrieval.
452bfc50bcSEdward Tomasz Napierala  */
462bfc50bcSEdward Tomasz Napierala 
472bfc50bcSEdward Tomasz Napierala #include <sys/cdefs.h>
482bfc50bcSEdward Tomasz Napierala __FBSDID("$FreeBSD$");
492bfc50bcSEdward Tomasz Napierala 
502bfc50bcSEdward Tomasz Napierala #include <sys/param.h>
512bfc50bcSEdward Tomasz Napierala #include <sys/eventhandler.h>
522bfc50bcSEdward Tomasz Napierala #include <sys/kernel.h>
532bfc50bcSEdward Tomasz Napierala #include <sys/lock.h>
542bfc50bcSEdward Tomasz Napierala #include <sys/loginclass.h>
552bfc50bcSEdward Tomasz Napierala #include <sys/malloc.h>
562bfc50bcSEdward Tomasz Napierala #include <sys/types.h>
572bfc50bcSEdward Tomasz Napierala #include <sys/priv.h>
582bfc50bcSEdward Tomasz Napierala #include <sys/proc.h>
592bfc50bcSEdward Tomasz Napierala #include <sys/queue.h>
60097055e2SEdward Tomasz Napierala #include <sys/racct.h>
612bfc50bcSEdward Tomasz Napierala #include <sys/refcount.h>
62b720dc97SMateusz Guzik #include <sys/rwlock.h>
632bfc50bcSEdward Tomasz Napierala #include <sys/sysproto.h>
642bfc50bcSEdward Tomasz Napierala #include <sys/systm.h>
652bfc50bcSEdward Tomasz Napierala 
662bfc50bcSEdward Tomasz Napierala static MALLOC_DEFINE(M_LOGINCLASS, "loginclass", "loginclass structures");
672bfc50bcSEdward Tomasz Napierala 
682bfc50bcSEdward Tomasz Napierala LIST_HEAD(, loginclass)	loginclasses;
692bfc50bcSEdward Tomasz Napierala 
702bfc50bcSEdward Tomasz Napierala /*
712bfc50bcSEdward Tomasz Napierala  * Lock protecting loginclasses list.
722bfc50bcSEdward Tomasz Napierala  */
73b720dc97SMateusz Guzik static struct rwlock loginclasses_lock;
74b720dc97SMateusz Guzik RW_SYSINIT(loginclasses_init, &loginclasses_lock, "loginclasses lock");
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 
92b720dc97SMateusz Guzik 	rw_wlock(&loginclasses_lock);
93b720dc97SMateusz Guzik 	if (!refcount_release(&lc->lc_refcount)) {
94b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
952bfc50bcSEdward Tomasz Napierala 		return;
962bfc50bcSEdward Tomasz Napierala 	}
97b720dc97SMateusz Guzik 
98b720dc97SMateusz Guzik 	racct_destroy(&lc->lc_racct);
99b720dc97SMateusz Guzik 	LIST_REMOVE(lc, lc_next);
100b720dc97SMateusz Guzik 	rw_wunlock(&loginclasses_lock);
101b720dc97SMateusz Guzik 
102b720dc97SMateusz Guzik 	free(lc, M_LOGINCLASS);
103b720dc97SMateusz Guzik }
104b720dc97SMateusz Guzik 
105b720dc97SMateusz Guzik /*
106b720dc97SMateusz Guzik  * Look up a loginclass struct for the parameter name.
107b720dc97SMateusz Guzik  * loginclasses_lock must be locked.
108b720dc97SMateusz Guzik  * Increase refcount on loginclass struct returned.
109b720dc97SMateusz Guzik  */
110b720dc97SMateusz Guzik static struct loginclass *
111b720dc97SMateusz Guzik loginclass_lookup(const char *name)
112b720dc97SMateusz Guzik {
113b720dc97SMateusz Guzik 	struct loginclass *lc;
114b720dc97SMateusz Guzik 
115b720dc97SMateusz Guzik 	rw_assert(&loginclasses_lock, RA_LOCKED);
116b720dc97SMateusz Guzik 	LIST_FOREACH(lc, &loginclasses, lc_next)
117b720dc97SMateusz Guzik 		if (strcmp(name, lc->lc_name) == 0) {
118b720dc97SMateusz Guzik 			loginclass_hold(lc);
119b720dc97SMateusz Guzik 			break;
120b720dc97SMateusz Guzik 		}
121b720dc97SMateusz Guzik 
122b720dc97SMateusz Guzik 	return (lc);
1232bfc50bcSEdward Tomasz Napierala }
1242bfc50bcSEdward Tomasz Napierala 
1252bfc50bcSEdward Tomasz Napierala /*
1262bfc50bcSEdward Tomasz Napierala  * Return loginclass structure with a corresponding name.  Not
1272bfc50bcSEdward Tomasz Napierala  * performance critical, as it's used mainly by setloginclass(2),
1282bfc50bcSEdward Tomasz Napierala  * which happens once per login session.  Caller has to use
1292bfc50bcSEdward Tomasz Napierala  * loginclass_free() on the returned value when it's no longer
1302bfc50bcSEdward Tomasz Napierala  * needed.
1312bfc50bcSEdward Tomasz Napierala  */
1322bfc50bcSEdward Tomasz Napierala struct loginclass *
1332bfc50bcSEdward Tomasz Napierala loginclass_find(const char *name)
1342bfc50bcSEdward Tomasz Napierala {
135b720dc97SMateusz Guzik 	struct loginclass *lc, *new_lc;
1362bfc50bcSEdward Tomasz Napierala 
1372bfc50bcSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXLOGNAME)
1382bfc50bcSEdward Tomasz Napierala 		return (NULL);
1392bfc50bcSEdward Tomasz Napierala 
1405d03f1e1SMateusz Guzik 	lc = curthread->td_ucred->cr_loginclass;
141c0b5261bSMateusz Guzik 	if (strcmp(name, lc->lc_name) == 0) {
142c0b5261bSMateusz Guzik 		loginclass_hold(lc);
143c0b5261bSMateusz Guzik 		return (lc);
144c0b5261bSMateusz Guzik 	}
145c0b5261bSMateusz Guzik 
146b720dc97SMateusz Guzik 	rw_rlock(&loginclasses_lock);
147b720dc97SMateusz Guzik 	lc = loginclass_lookup(name);
148b720dc97SMateusz Guzik 	rw_runlock(&loginclasses_lock);
149b720dc97SMateusz Guzik 	if (lc != NULL)
1502bfc50bcSEdward Tomasz Napierala 		return (lc);
151b720dc97SMateusz Guzik 
152b720dc97SMateusz Guzik 	new_lc = malloc(sizeof(*new_lc), M_LOGINCLASS, M_ZERO | M_WAITOK);
153b720dc97SMateusz Guzik 	racct_create(&new_lc->lc_racct);
154b720dc97SMateusz Guzik 	refcount_init(&new_lc->lc_refcount, 1);
155b720dc97SMateusz Guzik 	strcpy(new_lc->lc_name, name);
156b720dc97SMateusz Guzik 
157b720dc97SMateusz Guzik 	rw_wlock(&loginclasses_lock);
158b720dc97SMateusz Guzik 	/*
159b720dc97SMateusz Guzik 	 * There's a chance someone created our loginclass while we
160b720dc97SMateusz Guzik 	 * were in malloc and not holding the lock, so we have to
161b720dc97SMateusz Guzik 	 * make sure we don't insert a duplicate loginclass.
162b720dc97SMateusz Guzik 	 */
163b720dc97SMateusz Guzik 	if ((lc = loginclass_lookup(name)) == NULL) {
164b720dc97SMateusz Guzik 		LIST_INSERT_HEAD(&loginclasses, new_lc, lc_next);
165b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
166b720dc97SMateusz Guzik 		lc = new_lc;
167b720dc97SMateusz Guzik 	} else {
168b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
169b720dc97SMateusz Guzik 		racct_destroy(&new_lc->lc_racct);
170b720dc97SMateusz Guzik 		free(new_lc, M_LOGINCLASS);
1712bfc50bcSEdward Tomasz Napierala 	}
1722bfc50bcSEdward Tomasz Napierala 
173b720dc97SMateusz Guzik 	return (lc);
1742bfc50bcSEdward Tomasz Napierala }
1752bfc50bcSEdward Tomasz Napierala 
1762bfc50bcSEdward Tomasz Napierala /*
1772bfc50bcSEdward Tomasz Napierala  * Get login class name.
1782bfc50bcSEdward Tomasz Napierala  */
1792bfc50bcSEdward Tomasz Napierala #ifndef _SYS_SYSPROTO_H_
1802bfc50bcSEdward Tomasz Napierala struct getloginclass_args {
1812bfc50bcSEdward Tomasz Napierala 	char	*namebuf;
1822bfc50bcSEdward Tomasz Napierala 	size_t	namelen;
1832bfc50bcSEdward Tomasz Napierala };
1842bfc50bcSEdward Tomasz Napierala #endif
1852bfc50bcSEdward Tomasz Napierala /* ARGSUSED */
1862bfc50bcSEdward Tomasz Napierala int
1878451d0ddSKip Macy sys_getloginclass(struct thread *td, struct getloginclass_args *uap)
1882bfc50bcSEdward Tomasz Napierala {
1892bfc50bcSEdward Tomasz Napierala 	struct loginclass *lc;
190a7963b97SMateusz Guzik 	size_t lcnamelen;
1912bfc50bcSEdward Tomasz Napierala 
192a7963b97SMateusz Guzik 	lc = td->td_ucred->cr_loginclass;
1932bfc50bcSEdward Tomasz Napierala 	lcnamelen = strlen(lc->lc_name) + 1;
1942bfc50bcSEdward Tomasz Napierala 	if (lcnamelen > uap->namelen)
195a7963b97SMateusz Guzik 		return (ERANGE);
196a7963b97SMateusz Guzik 	return (copyout(lc->lc_name, uap->namebuf, lcnamelen));
1972bfc50bcSEdward Tomasz Napierala }
1982bfc50bcSEdward Tomasz Napierala 
1992bfc50bcSEdward Tomasz Napierala /*
2002bfc50bcSEdward Tomasz Napierala  * Set login class name.
2012bfc50bcSEdward Tomasz Napierala  */
2022bfc50bcSEdward Tomasz Napierala #ifndef _SYS_SYSPROTO_H_
2032bfc50bcSEdward Tomasz Napierala struct setloginclass_args {
2042bfc50bcSEdward Tomasz Napierala 	const char	*namebuf;
2052bfc50bcSEdward Tomasz Napierala };
2062bfc50bcSEdward Tomasz Napierala #endif
2072bfc50bcSEdward Tomasz Napierala /* ARGSUSED */
2082bfc50bcSEdward Tomasz Napierala int
2098451d0ddSKip Macy sys_setloginclass(struct thread *td, struct setloginclass_args *uap)
2102bfc50bcSEdward Tomasz Napierala {
2112bfc50bcSEdward Tomasz Napierala 	struct proc *p = td->td_proc;
2122bfc50bcSEdward Tomasz Napierala 	int error;
2132bfc50bcSEdward Tomasz Napierala 	char lcname[MAXLOGNAME];
2142bfc50bcSEdward Tomasz Napierala 	struct loginclass *newlc;
2152bfc50bcSEdward Tomasz Napierala 	struct ucred *newcred, *oldcred;
2162bfc50bcSEdward Tomasz Napierala 
2172bfc50bcSEdward Tomasz Napierala 	error = priv_check(td, PRIV_PROC_SETLOGINCLASS);
2182bfc50bcSEdward Tomasz Napierala 	if (error != 0)
2192bfc50bcSEdward Tomasz Napierala 		return (error);
2202bfc50bcSEdward Tomasz Napierala 	error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL);
2212bfc50bcSEdward Tomasz Napierala 	if (error != 0)
2222bfc50bcSEdward Tomasz Napierala 		return (error);
2232bfc50bcSEdward Tomasz Napierala 
2242bfc50bcSEdward Tomasz Napierala 	newlc = loginclass_find(lcname);
2252bfc50bcSEdward Tomasz Napierala 	if (newlc == NULL)
2262bfc50bcSEdward Tomasz Napierala 		return (EINVAL);
2272bfc50bcSEdward Tomasz Napierala 	newcred = crget();
2282bfc50bcSEdward Tomasz Napierala 
2292bfc50bcSEdward Tomasz Napierala 	PROC_LOCK(p);
2302bfc50bcSEdward Tomasz Napierala 	oldcred = crcopysafe(p, newcred);
2312bfc50bcSEdward Tomasz Napierala 	newcred->cr_loginclass = newlc;
232daf63fd2SMateusz Guzik 	proc_set_cred(p, newcred);
2332bfc50bcSEdward Tomasz Napierala 	PROC_UNLOCK(p);
234097055e2SEdward Tomasz Napierala #ifdef RACCT
235097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
236097055e2SEdward Tomasz Napierala #endif
2372bfc50bcSEdward Tomasz Napierala 	loginclass_free(oldcred->cr_loginclass);
2382bfc50bcSEdward Tomasz Napierala 	crfree(oldcred);
2392bfc50bcSEdward Tomasz Napierala 
2402bfc50bcSEdward Tomasz Napierala 	return (0);
2412bfc50bcSEdward Tomasz Napierala }
2422bfc50bcSEdward Tomasz Napierala 
243097055e2SEdward Tomasz Napierala void
244097055e2SEdward Tomasz Napierala loginclass_racct_foreach(void (*callback)(struct racct *racct,
24515db3c07SEdward Tomasz Napierala     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
24615db3c07SEdward Tomasz Napierala     void *arg2, void *arg3)
247097055e2SEdward Tomasz Napierala {
248097055e2SEdward Tomasz Napierala 	struct loginclass *lc;
249097055e2SEdward Tomasz Napierala 
250b720dc97SMateusz Guzik 	rw_rlock(&loginclasses_lock);
25115db3c07SEdward Tomasz Napierala 	if (pre != NULL)
25215db3c07SEdward Tomasz Napierala 		(pre)();
253097055e2SEdward Tomasz Napierala 	LIST_FOREACH(lc, &loginclasses, lc_next)
254097055e2SEdward Tomasz Napierala 		(callback)(lc->lc_racct, arg2, arg3);
25515db3c07SEdward Tomasz Napierala 	if (post != NULL)
25615db3c07SEdward Tomasz Napierala 		(post)();
257b720dc97SMateusz Guzik 	rw_runlock(&loginclasses_lock);
258097055e2SEdward Tomasz Napierala }
259