xref: /freebsd/sys/kern/kern_loginclass.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
12bfc50bcSEdward Tomasz Napierala /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
42bfc50bcSEdward Tomasz Napierala  * Copyright (c) 2011 The FreeBSD Foundation
52bfc50bcSEdward Tomasz Napierala  *
62bfc50bcSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
72bfc50bcSEdward Tomasz Napierala  * from the FreeBSD Foundation.
82bfc50bcSEdward Tomasz Napierala  *
92bfc50bcSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
102bfc50bcSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
112bfc50bcSEdward Tomasz Napierala  * are met:
122bfc50bcSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
132bfc50bcSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
142bfc50bcSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
152bfc50bcSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
162bfc50bcSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
172bfc50bcSEdward Tomasz Napierala  *
182bfc50bcSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
192bfc50bcSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202bfc50bcSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212bfc50bcSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
222bfc50bcSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232bfc50bcSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242bfc50bcSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252bfc50bcSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262bfc50bcSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272bfc50bcSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282bfc50bcSEdward Tomasz Napierala  * SUCH DAMAGE.
292bfc50bcSEdward Tomasz Napierala  */
302bfc50bcSEdward Tomasz Napierala 
312bfc50bcSEdward Tomasz Napierala /*
322bfc50bcSEdward Tomasz Napierala  * Processes may set login class name using setloginclass(2).  This
332bfc50bcSEdward Tomasz Napierala  * is usually done through call to setusercontext(3), by programs
342bfc50bcSEdward Tomasz Napierala  * such as login(1), based on information from master.passwd(5).  Kernel
352bfc50bcSEdward Tomasz Napierala  * uses this information to enforce per-class resource limits.  Current
362bfc50bcSEdward Tomasz Napierala  * login class can be determined using id(1).  Login class is inherited
372bfc50bcSEdward Tomasz Napierala  * from the parent process during fork(2).  If not set, it defaults
382bfc50bcSEdward Tomasz Napierala  * to "default".
392bfc50bcSEdward Tomasz Napierala  *
402bfc50bcSEdward Tomasz Napierala  * Code in this file implements setloginclass(2) and getloginclass(2)
412bfc50bcSEdward Tomasz Napierala  * system calls, and maintains class name storage and retrieval.
422bfc50bcSEdward Tomasz Napierala  */
432bfc50bcSEdward Tomasz Napierala 
442bfc50bcSEdward Tomasz Napierala #include <sys/param.h>
452bfc50bcSEdward Tomasz Napierala #include <sys/eventhandler.h>
462bfc50bcSEdward Tomasz Napierala #include <sys/kernel.h>
472bfc50bcSEdward Tomasz Napierala #include <sys/lock.h>
482bfc50bcSEdward Tomasz Napierala #include <sys/loginclass.h>
492bfc50bcSEdward Tomasz Napierala #include <sys/malloc.h>
502bfc50bcSEdward Tomasz Napierala #include <sys/types.h>
512bfc50bcSEdward Tomasz Napierala #include <sys/priv.h>
522bfc50bcSEdward Tomasz Napierala #include <sys/proc.h>
532bfc50bcSEdward Tomasz Napierala #include <sys/queue.h>
54097055e2SEdward Tomasz Napierala #include <sys/racct.h>
55f87beb93SAndriy Gapon #include <sys/rctl.h>
562bfc50bcSEdward Tomasz Napierala #include <sys/refcount.h>
57b720dc97SMateusz Guzik #include <sys/rwlock.h>
582bfc50bcSEdward Tomasz Napierala #include <sys/sysproto.h>
592bfc50bcSEdward Tomasz Napierala #include <sys/systm.h>
602bfc50bcSEdward Tomasz Napierala 
612bfc50bcSEdward Tomasz Napierala static MALLOC_DEFINE(M_LOGINCLASS, "loginclass", "loginclass structures");
622bfc50bcSEdward Tomasz Napierala 
632bfc50bcSEdward Tomasz Napierala LIST_HEAD(, loginclass)	loginclasses;
642bfc50bcSEdward Tomasz Napierala 
652bfc50bcSEdward Tomasz Napierala /*
662bfc50bcSEdward Tomasz Napierala  * Lock protecting loginclasses list.
672bfc50bcSEdward Tomasz Napierala  */
68b720dc97SMateusz Guzik static struct rwlock loginclasses_lock;
69b720dc97SMateusz Guzik RW_SYSINIT(loginclasses_init, &loginclasses_lock, "loginclasses lock");
702bfc50bcSEdward Tomasz Napierala 
712bfc50bcSEdward Tomasz Napierala void
loginclass_hold(struct loginclass * lc)722bfc50bcSEdward Tomasz Napierala loginclass_hold(struct loginclass *lc)
732bfc50bcSEdward Tomasz Napierala {
742bfc50bcSEdward Tomasz Napierala 
752bfc50bcSEdward Tomasz Napierala 	refcount_acquire(&lc->lc_refcount);
762bfc50bcSEdward Tomasz Napierala }
772bfc50bcSEdward Tomasz Napierala 
782bfc50bcSEdward Tomasz Napierala void
loginclass_free(struct loginclass * lc)792bfc50bcSEdward Tomasz Napierala loginclass_free(struct loginclass *lc)
802bfc50bcSEdward Tomasz Napierala {
812bfc50bcSEdward Tomasz Napierala 
826ff4688bSMateusz Guzik 	if (refcount_release_if_not_last(&lc->lc_refcount))
832bfc50bcSEdward Tomasz Napierala 		return;
842bfc50bcSEdward Tomasz Napierala 
85b720dc97SMateusz Guzik 	rw_wlock(&loginclasses_lock);
86b720dc97SMateusz Guzik 	if (!refcount_release(&lc->lc_refcount)) {
87b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
882bfc50bcSEdward Tomasz Napierala 		return;
892bfc50bcSEdward Tomasz Napierala 	}
90b720dc97SMateusz Guzik 
91b720dc97SMateusz Guzik 	racct_destroy(&lc->lc_racct);
92b720dc97SMateusz Guzik 	LIST_REMOVE(lc, lc_next);
93b720dc97SMateusz Guzik 	rw_wunlock(&loginclasses_lock);
94b720dc97SMateusz Guzik 
95b720dc97SMateusz Guzik 	free(lc, M_LOGINCLASS);
96b720dc97SMateusz Guzik }
97b720dc97SMateusz Guzik 
98b720dc97SMateusz Guzik /*
99b720dc97SMateusz Guzik  * Look up a loginclass struct for the parameter name.
100b720dc97SMateusz Guzik  * loginclasses_lock must be locked.
101b720dc97SMateusz Guzik  * Increase refcount on loginclass struct returned.
102b720dc97SMateusz Guzik  */
103b720dc97SMateusz Guzik static struct loginclass *
loginclass_lookup(const char * name)104b720dc97SMateusz Guzik loginclass_lookup(const char *name)
105b720dc97SMateusz Guzik {
106b720dc97SMateusz Guzik 	struct loginclass *lc;
107b720dc97SMateusz Guzik 
108b720dc97SMateusz Guzik 	rw_assert(&loginclasses_lock, RA_LOCKED);
109b720dc97SMateusz Guzik 	LIST_FOREACH(lc, &loginclasses, lc_next)
110b720dc97SMateusz Guzik 		if (strcmp(name, lc->lc_name) == 0) {
111b720dc97SMateusz Guzik 			loginclass_hold(lc);
112b720dc97SMateusz Guzik 			break;
113b720dc97SMateusz Guzik 		}
114b720dc97SMateusz Guzik 
115b720dc97SMateusz Guzik 	return (lc);
1162bfc50bcSEdward Tomasz Napierala }
1172bfc50bcSEdward Tomasz Napierala 
1182bfc50bcSEdward Tomasz Napierala /*
1192bfc50bcSEdward Tomasz Napierala  * Return loginclass structure with a corresponding name.  Not
1202bfc50bcSEdward Tomasz Napierala  * performance critical, as it's used mainly by setloginclass(2),
1212bfc50bcSEdward Tomasz Napierala  * which happens once per login session.  Caller has to use
1222bfc50bcSEdward Tomasz Napierala  * loginclass_free() on the returned value when it's no longer
1232bfc50bcSEdward Tomasz Napierala  * needed.
1242bfc50bcSEdward Tomasz Napierala  */
1252bfc50bcSEdward Tomasz Napierala struct loginclass *
loginclass_find(const char * name)1262bfc50bcSEdward Tomasz Napierala loginclass_find(const char *name)
1272bfc50bcSEdward Tomasz Napierala {
128b720dc97SMateusz Guzik 	struct loginclass *lc, *new_lc;
1292bfc50bcSEdward Tomasz Napierala 
1302bfc50bcSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXLOGNAME)
1312bfc50bcSEdward Tomasz Napierala 		return (NULL);
1322bfc50bcSEdward Tomasz Napierala 
1335d03f1e1SMateusz Guzik 	lc = curthread->td_ucred->cr_loginclass;
134c0b5261bSMateusz Guzik 	if (strcmp(name, lc->lc_name) == 0) {
135c0b5261bSMateusz Guzik 		loginclass_hold(lc);
136c0b5261bSMateusz Guzik 		return (lc);
137c0b5261bSMateusz Guzik 	}
138c0b5261bSMateusz Guzik 
139b720dc97SMateusz Guzik 	rw_rlock(&loginclasses_lock);
140b720dc97SMateusz Guzik 	lc = loginclass_lookup(name);
141b720dc97SMateusz Guzik 	rw_runlock(&loginclasses_lock);
142b720dc97SMateusz Guzik 	if (lc != NULL)
1432bfc50bcSEdward Tomasz Napierala 		return (lc);
144b720dc97SMateusz Guzik 
145b720dc97SMateusz Guzik 	new_lc = malloc(sizeof(*new_lc), M_LOGINCLASS, M_ZERO | M_WAITOK);
146b720dc97SMateusz Guzik 	racct_create(&new_lc->lc_racct);
147b720dc97SMateusz Guzik 	refcount_init(&new_lc->lc_refcount, 1);
148b720dc97SMateusz Guzik 	strcpy(new_lc->lc_name, name);
149b720dc97SMateusz Guzik 
150b720dc97SMateusz Guzik 	rw_wlock(&loginclasses_lock);
151b720dc97SMateusz Guzik 	/*
152b720dc97SMateusz Guzik 	 * There's a chance someone created our loginclass while we
153b720dc97SMateusz Guzik 	 * were in malloc and not holding the lock, so we have to
154b720dc97SMateusz Guzik 	 * make sure we don't insert a duplicate loginclass.
155b720dc97SMateusz Guzik 	 */
156b720dc97SMateusz Guzik 	if ((lc = loginclass_lookup(name)) == NULL) {
157b720dc97SMateusz Guzik 		LIST_INSERT_HEAD(&loginclasses, new_lc, lc_next);
158b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
159b720dc97SMateusz Guzik 		lc = new_lc;
160b720dc97SMateusz Guzik 	} else {
161b720dc97SMateusz Guzik 		rw_wunlock(&loginclasses_lock);
162b720dc97SMateusz Guzik 		racct_destroy(&new_lc->lc_racct);
163b720dc97SMateusz Guzik 		free(new_lc, M_LOGINCLASS);
1642bfc50bcSEdward Tomasz Napierala 	}
1652bfc50bcSEdward Tomasz Napierala 
166b720dc97SMateusz Guzik 	return (lc);
1672bfc50bcSEdward Tomasz Napierala }
1682bfc50bcSEdward Tomasz Napierala 
1692bfc50bcSEdward Tomasz Napierala /*
1702bfc50bcSEdward Tomasz Napierala  * Get login class name.
1712bfc50bcSEdward Tomasz Napierala  */
1722bfc50bcSEdward Tomasz Napierala #ifndef _SYS_SYSPROTO_H_
1732bfc50bcSEdward Tomasz Napierala struct getloginclass_args {
1742bfc50bcSEdward Tomasz Napierala 	char	*namebuf;
1752bfc50bcSEdward Tomasz Napierala 	size_t	namelen;
1762bfc50bcSEdward Tomasz Napierala };
1772bfc50bcSEdward Tomasz Napierala #endif
1782bfc50bcSEdward Tomasz Napierala /* ARGSUSED */
1792bfc50bcSEdward Tomasz Napierala int
sys_getloginclass(struct thread * td,struct getloginclass_args * uap)1808451d0ddSKip Macy sys_getloginclass(struct thread *td, struct getloginclass_args *uap)
1812bfc50bcSEdward Tomasz Napierala {
1822bfc50bcSEdward Tomasz Napierala 	struct loginclass *lc;
183a7963b97SMateusz Guzik 	size_t lcnamelen;
1842bfc50bcSEdward Tomasz Napierala 
185a7963b97SMateusz Guzik 	lc = td->td_ucred->cr_loginclass;
1862bfc50bcSEdward Tomasz Napierala 	lcnamelen = strlen(lc->lc_name) + 1;
1872bfc50bcSEdward Tomasz Napierala 	if (lcnamelen > uap->namelen)
188a7963b97SMateusz Guzik 		return (ERANGE);
189a7963b97SMateusz Guzik 	return (copyout(lc->lc_name, uap->namebuf, lcnamelen));
1902bfc50bcSEdward Tomasz Napierala }
1912bfc50bcSEdward Tomasz Napierala 
1922bfc50bcSEdward Tomasz Napierala /*
1932bfc50bcSEdward Tomasz Napierala  * Set login class name.
1942bfc50bcSEdward Tomasz Napierala  */
1952bfc50bcSEdward Tomasz Napierala #ifndef _SYS_SYSPROTO_H_
1962bfc50bcSEdward Tomasz Napierala struct setloginclass_args {
1972bfc50bcSEdward Tomasz Napierala 	const char	*namebuf;
1982bfc50bcSEdward Tomasz Napierala };
1992bfc50bcSEdward Tomasz Napierala #endif
2002bfc50bcSEdward Tomasz Napierala /* ARGSUSED */
2012bfc50bcSEdward Tomasz Napierala int
sys_setloginclass(struct thread * td,struct setloginclass_args * uap)2028451d0ddSKip Macy sys_setloginclass(struct thread *td, struct setloginclass_args *uap)
2032bfc50bcSEdward Tomasz Napierala {
2042bfc50bcSEdward Tomasz Napierala 	struct proc *p = td->td_proc;
2052bfc50bcSEdward Tomasz Napierala 	int error;
2062bfc50bcSEdward Tomasz Napierala 	char lcname[MAXLOGNAME];
2072bfc50bcSEdward Tomasz Napierala 	struct loginclass *newlc;
2082bfc50bcSEdward Tomasz Napierala 	struct ucred *newcred, *oldcred;
2092bfc50bcSEdward Tomasz Napierala 
2102bfc50bcSEdward Tomasz Napierala 	error = priv_check(td, PRIV_PROC_SETLOGINCLASS);
2112bfc50bcSEdward Tomasz Napierala 	if (error != 0)
2122bfc50bcSEdward Tomasz Napierala 		return (error);
2132bfc50bcSEdward Tomasz Napierala 	error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL);
2142bfc50bcSEdward Tomasz Napierala 	if (error != 0)
2152bfc50bcSEdward Tomasz Napierala 		return (error);
2162bfc50bcSEdward Tomasz Napierala 
2172bfc50bcSEdward Tomasz Napierala 	newlc = loginclass_find(lcname);
2182bfc50bcSEdward Tomasz Napierala 	if (newlc == NULL)
2192bfc50bcSEdward Tomasz Napierala 		return (EINVAL);
2202bfc50bcSEdward Tomasz Napierala 	newcred = crget();
2212bfc50bcSEdward Tomasz Napierala 
2222bfc50bcSEdward Tomasz Napierala 	PROC_LOCK(p);
2232bfc50bcSEdward Tomasz Napierala 	oldcred = crcopysafe(p, newcred);
2242bfc50bcSEdward Tomasz Napierala 	newcred->cr_loginclass = newlc;
225daf63fd2SMateusz Guzik 	proc_set_cred(p, newcred);
226097055e2SEdward Tomasz Napierala #ifdef RACCT
227097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
228f87beb93SAndriy Gapon 	crhold(newcred);
229f87beb93SAndriy Gapon #endif
230f87beb93SAndriy Gapon 	PROC_UNLOCK(p);
231f87beb93SAndriy Gapon #ifdef RCTL
232f87beb93SAndriy Gapon 	rctl_proc_ucred_changed(p, newcred);
233f87beb93SAndriy Gapon 	crfree(newcred);
234097055e2SEdward Tomasz Napierala #endif
2352bfc50bcSEdward Tomasz Napierala 	loginclass_free(oldcred->cr_loginclass);
2362bfc50bcSEdward Tomasz Napierala 	crfree(oldcred);
2372bfc50bcSEdward Tomasz Napierala 
2382bfc50bcSEdward Tomasz Napierala 	return (0);
2392bfc50bcSEdward Tomasz Napierala }
2402bfc50bcSEdward Tomasz Napierala 
241097055e2SEdward Tomasz Napierala void
loginclass_racct_foreach(void (* callback)(struct racct * racct,void * arg2,void * arg3),void (* pre)(void),void (* post)(void),void * arg2,void * arg3)242097055e2SEdward Tomasz Napierala loginclass_racct_foreach(void (*callback)(struct racct *racct,
24315db3c07SEdward Tomasz Napierala     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
24415db3c07SEdward Tomasz Napierala     void *arg2, void *arg3)
245097055e2SEdward Tomasz Napierala {
246097055e2SEdward Tomasz Napierala 	struct loginclass *lc;
247097055e2SEdward Tomasz Napierala 
248b720dc97SMateusz Guzik 	rw_rlock(&loginclasses_lock);
24915db3c07SEdward Tomasz Napierala 	if (pre != NULL)
25015db3c07SEdward Tomasz Napierala 		(pre)();
251097055e2SEdward Tomasz Napierala 	LIST_FOREACH(lc, &loginclasses, lc_next)
252097055e2SEdward Tomasz Napierala 		(callback)(lc->lc_racct, arg2, arg3);
25315db3c07SEdward Tomasz Napierala 	if (post != NULL)
25415db3c07SEdward Tomasz Napierala 		(post)();
255b720dc97SMateusz Guzik 	rw_runlock(&loginclasses_lock);
256097055e2SEdward Tomasz Napierala }
257