2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2011 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Processes may set login class name using setloginclass(2). This
33 * is usually done through call to setusercontext(3), by programs
34 * such as login(1), based on information from master.passwd(5). Kernel
35 * uses this information to enforce per-class resource limits. Current
36 * login class can be determined using id(1). Login class is inherited
37 * from the parent process during fork(2). If not set, it defaults
40 * Code in this file implements setloginclass(2) and getloginclass(2)
41 * system calls, and maintains class name storage and retrieval.
44 #include <sys/param.h>
45 #include <sys/eventhandler.h>
46 #include <sys/kernel.h>
48 #include <sys/loginclass.h>
49 #include <sys/malloc.h>
50 #include <sys/types.h>
53 #include <sys/queue.h>
54 #include <sys/racct.h>
56 #include <sys/refcount.h>
57 #include <sys/rwlock.h>
58 #include <sys/sysproto.h>
59 #include <sys/systm.h>
61 static MALLOC_DEFINE(M_LOGINCLASS, "loginclass", "loginclass structures");
63 LIST_HEAD(, loginclass) loginclasses;
66 * Lock protecting loginclasses list.
68 static struct rwlock loginclasses_lock;
69 RW_SYSINIT(loginclasses_init, &loginclasses_lock, "loginclasses lock");
72 loginclass_hold(struct loginclass *lc)
75 refcount_acquire(&lc->lc_refcount);
79 loginclass_free(struct loginclass *lc)
82 if (refcount_release_if_not_last(&lc->lc_refcount))
85 rw_wlock(&loginclasses_lock);
86 if (!refcount_release(&lc->lc_refcount)) {
87 rw_wunlock(&loginclasses_lock);
91 racct_destroy(&lc->lc_racct);
92 LIST_REMOVE(lc, lc_next);
93 rw_wunlock(&loginclasses_lock);
95 free(lc, M_LOGINCLASS);
99 * Look up a loginclass struct for the parameter name.
100 * loginclasses_lock must be locked.
101 * Increase refcount on loginclass struct returned.
103 static struct loginclass *
104 loginclass_lookup(const char *name)
106 struct loginclass *lc;
108 rw_assert(&loginclasses_lock, RA_LOCKED);
109 LIST_FOREACH(lc, &loginclasses, lc_next)
110 if (strcmp(name, lc->lc_name) == 0) {
119 * Return loginclass structure with a corresponding name. Not
120 * performance critical, as it's used mainly by setloginclass(2),
121 * which happens once per login session. Caller has to use
122 * loginclass_free() on the returned value when it's no longer
126 loginclass_find(const char *name)
128 struct loginclass *lc, *new_lc;
130 if (name[0] == '\0' || strlen(name) >= MAXLOGNAME)
133 lc = curthread->td_ucred->cr_loginclass;
134 if (strcmp(name, lc->lc_name) == 0) {
139 rw_rlock(&loginclasses_lock);
140 lc = loginclass_lookup(name);
141 rw_runlock(&loginclasses_lock);
145 new_lc = malloc(sizeof(*new_lc), M_LOGINCLASS, M_ZERO | M_WAITOK);
146 racct_create(&new_lc->lc_racct);
147 refcount_init(&new_lc->lc_refcount, 1);
148 strcpy(new_lc->lc_name, name);
150 rw_wlock(&loginclasses_lock);
152 * There's a chance someone created our loginclass while we
153 * were in malloc and not holding the lock, so we have to
154 * make sure we don't insert a duplicate loginclass.
156 if ((lc = loginclass_lookup(name)) == NULL) {
157 LIST_INSERT_HEAD(&loginclasses, new_lc, lc_next);
158 rw_wunlock(&loginclasses_lock);
161 rw_wunlock(&loginclasses_lock);
162 racct_destroy(&new_lc->lc_racct);
163 free(new_lc, M_LOGINCLASS);
170 * Get login class name.
172 #ifndef _SYS_SYSPROTO_H_
173 struct getloginclass_args {
180 sys_getloginclass(struct thread *td, struct getloginclass_args *uap)
182 struct loginclass *lc;
185 lc = td->td_ucred->cr_loginclass;
186 lcnamelen = strlen(lc->lc_name) + 1;
187 if (lcnamelen > uap->namelen)
189 return (copyout(lc->lc_name, uap->namebuf, lcnamelen));
193 * Set login class name.
195 #ifndef _SYS_SYSPROTO_H_
196 struct setloginclass_args {
202 sys_setloginclass(struct thread *td, struct setloginclass_args *uap)
204 struct proc *p = td->td_proc;
206 char lcname[MAXLOGNAME];
207 struct loginclass *newlc;
208 struct ucred *newcred, *oldcred;
210 error = priv_check(td, PRIV_PROC_SETLOGINCLASS);
213 error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL);
217 newlc = loginclass_find(lcname);
223 oldcred = crcopysafe(p, newcred);
224 newcred->cr_loginclass = newlc;
225 proc_set_cred(p, newcred);
227 racct_proc_ucred_changed(p, oldcred, newcred);
232 rctl_proc_ucred_changed(p, newcred);
235 loginclass_free(oldcred->cr_loginclass);
242 loginclass_racct_foreach(void (*callback)(struct racct *racct,
243 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
244 void *arg2, void *arg3)
246 struct loginclass *lc;
248 rw_rlock(&loginclasses_lock);
251 LIST_FOREACH(lc, &loginclasses, lc_next)
252 (callback)(lc->lc_racct, arg2, arg3);
255 rw_runlock(&loginclasses_lock);