2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Processes may set login class name using setloginclass(2). This
36 * is usually done through call to setusercontext(3), by programs
37 * such as login(1), based on information from master.passwd(5). Kernel
38 * uses this information to enforce per-class resource limits. Current
39 * login class can be determined using id(1). Login class is inherited
40 * from the parent process during fork(2). If not set, it defaults
43 * Code in this file implements setloginclass(2) and getloginclass(2)
44 * system calls, and maintains class name storage and retrieval.
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
50 #include <sys/param.h>
51 #include <sys/eventhandler.h>
52 #include <sys/kernel.h>
54 #include <sys/loginclass.h>
55 #include <sys/malloc.h>
56 #include <sys/types.h>
59 #include <sys/queue.h>
60 #include <sys/racct.h>
62 #include <sys/refcount.h>
63 #include <sys/rwlock.h>
64 #include <sys/sysproto.h>
65 #include <sys/systm.h>
67 static MALLOC_DEFINE(M_LOGINCLASS, "loginclass", "loginclass structures");
69 LIST_HEAD(, loginclass) loginclasses;
72 * Lock protecting loginclasses list.
74 static struct rwlock loginclasses_lock;
75 RW_SYSINIT(loginclasses_init, &loginclasses_lock, "loginclasses lock");
78 loginclass_hold(struct loginclass *lc)
81 refcount_acquire(&lc->lc_refcount);
85 loginclass_free(struct loginclass *lc)
88 if (refcount_release_if_not_last(&lc->lc_refcount))
91 rw_wlock(&loginclasses_lock);
92 if (!refcount_release(&lc->lc_refcount)) {
93 rw_wunlock(&loginclasses_lock);
97 racct_destroy(&lc->lc_racct);
98 LIST_REMOVE(lc, lc_next);
99 rw_wunlock(&loginclasses_lock);
101 free(lc, M_LOGINCLASS);
105 * Look up a loginclass struct for the parameter name.
106 * loginclasses_lock must be locked.
107 * Increase refcount on loginclass struct returned.
109 static struct loginclass *
110 loginclass_lookup(const char *name)
112 struct loginclass *lc;
114 rw_assert(&loginclasses_lock, RA_LOCKED);
115 LIST_FOREACH(lc, &loginclasses, lc_next)
116 if (strcmp(name, lc->lc_name) == 0) {
125 * Return loginclass structure with a corresponding name. Not
126 * performance critical, as it's used mainly by setloginclass(2),
127 * which happens once per login session. Caller has to use
128 * loginclass_free() on the returned value when it's no longer
132 loginclass_find(const char *name)
134 struct loginclass *lc, *new_lc;
136 if (name[0] == '\0' || strlen(name) >= MAXLOGNAME)
139 lc = curthread->td_ucred->cr_loginclass;
140 if (strcmp(name, lc->lc_name) == 0) {
145 rw_rlock(&loginclasses_lock);
146 lc = loginclass_lookup(name);
147 rw_runlock(&loginclasses_lock);
151 new_lc = malloc(sizeof(*new_lc), M_LOGINCLASS, M_ZERO | M_WAITOK);
152 racct_create(&new_lc->lc_racct);
153 refcount_init(&new_lc->lc_refcount, 1);
154 strcpy(new_lc->lc_name, name);
156 rw_wlock(&loginclasses_lock);
158 * There's a chance someone created our loginclass while we
159 * were in malloc and not holding the lock, so we have to
160 * make sure we don't insert a duplicate loginclass.
162 if ((lc = loginclass_lookup(name)) == NULL) {
163 LIST_INSERT_HEAD(&loginclasses, new_lc, lc_next);
164 rw_wunlock(&loginclasses_lock);
167 rw_wunlock(&loginclasses_lock);
168 racct_destroy(&new_lc->lc_racct);
169 free(new_lc, M_LOGINCLASS);
176 * Get login class name.
178 #ifndef _SYS_SYSPROTO_H_
179 struct getloginclass_args {
186 sys_getloginclass(struct thread *td, struct getloginclass_args *uap)
188 struct loginclass *lc;
191 lc = td->td_ucred->cr_loginclass;
192 lcnamelen = strlen(lc->lc_name) + 1;
193 if (lcnamelen > uap->namelen)
195 return (copyout(lc->lc_name, uap->namebuf, lcnamelen));
199 * Set login class name.
201 #ifndef _SYS_SYSPROTO_H_
202 struct setloginclass_args {
208 sys_setloginclass(struct thread *td, struct setloginclass_args *uap)
210 struct proc *p = td->td_proc;
212 char lcname[MAXLOGNAME];
213 struct loginclass *newlc;
214 struct ucred *newcred, *oldcred;
216 error = priv_check(td, PRIV_PROC_SETLOGINCLASS);
219 error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL);
223 newlc = loginclass_find(lcname);
229 oldcred = crcopysafe(p, newcred);
230 newcred->cr_loginclass = newlc;
231 proc_set_cred(p, newcred);
233 racct_proc_ucred_changed(p, oldcred, newcred);
238 rctl_proc_ucred_changed(p, newcred);
241 loginclass_free(oldcred->cr_loginclass);
248 loginclass_racct_foreach(void (*callback)(struct racct *racct,
249 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
250 void *arg2, void *arg3)
252 struct loginclass *lc;
254 rw_rlock(&loginclasses_lock);
257 LIST_FOREACH(lc, &loginclasses, lc_next)
258 (callback)(lc->lc_racct, arg2, arg3);
261 rw_runlock(&loginclasses_lock);