]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_loginclass.c
Import device-tree files from Linux 6.6
[FreeBSD/FreeBSD.git] / sys / kern / kern_loginclass.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 The FreeBSD Foundation
5  *
6  * This software was developed by Edward Tomasz Napierala under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30
31 /*
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
38  * to "default".
39  *
40  * Code in this file implements setloginclass(2) and getloginclass(2)
41  * system calls, and maintains class name storage and retrieval.
42  */
43
44 #include <sys/param.h>
45 #include <sys/eventhandler.h>
46 #include <sys/kernel.h>
47 #include <sys/lock.h>
48 #include <sys/loginclass.h>
49 #include <sys/malloc.h>
50 #include <sys/types.h>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/queue.h>
54 #include <sys/racct.h>
55 #include <sys/rctl.h>
56 #include <sys/refcount.h>
57 #include <sys/rwlock.h>
58 #include <sys/sysproto.h>
59 #include <sys/systm.h>
60
61 static MALLOC_DEFINE(M_LOGINCLASS, "loginclass", "loginclass structures");
62
63 LIST_HEAD(, loginclass) loginclasses;
64
65 /*
66  * Lock protecting loginclasses list.
67  */
68 static struct rwlock loginclasses_lock;
69 RW_SYSINIT(loginclasses_init, &loginclasses_lock, "loginclasses lock");
70
71 void
72 loginclass_hold(struct loginclass *lc)
73 {
74
75         refcount_acquire(&lc->lc_refcount);
76 }
77
78 void
79 loginclass_free(struct loginclass *lc)
80 {
81
82         if (refcount_release_if_not_last(&lc->lc_refcount))
83                 return;
84
85         rw_wlock(&loginclasses_lock);
86         if (!refcount_release(&lc->lc_refcount)) {
87                 rw_wunlock(&loginclasses_lock);
88                 return;
89         }
90
91         racct_destroy(&lc->lc_racct);
92         LIST_REMOVE(lc, lc_next);
93         rw_wunlock(&loginclasses_lock);
94
95         free(lc, M_LOGINCLASS);
96 }
97
98 /*
99  * Look up a loginclass struct for the parameter name.
100  * loginclasses_lock must be locked.
101  * Increase refcount on loginclass struct returned.
102  */
103 static struct loginclass *
104 loginclass_lookup(const char *name)
105 {
106         struct loginclass *lc;
107
108         rw_assert(&loginclasses_lock, RA_LOCKED);
109         LIST_FOREACH(lc, &loginclasses, lc_next)
110                 if (strcmp(name, lc->lc_name) == 0) {
111                         loginclass_hold(lc);
112                         break;
113                 }
114
115         return (lc);
116 }
117
118 /*
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
123  * needed.
124  */
125 struct loginclass *
126 loginclass_find(const char *name)
127 {
128         struct loginclass *lc, *new_lc;
129
130         if (name[0] == '\0' || strlen(name) >= MAXLOGNAME)
131                 return (NULL);
132
133         lc = curthread->td_ucred->cr_loginclass;
134         if (strcmp(name, lc->lc_name) == 0) {
135                 loginclass_hold(lc);
136                 return (lc);
137         }
138
139         rw_rlock(&loginclasses_lock);
140         lc = loginclass_lookup(name);
141         rw_runlock(&loginclasses_lock);
142         if (lc != NULL)
143                 return (lc);
144
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);
149
150         rw_wlock(&loginclasses_lock);
151         /*
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.
155          */
156         if ((lc = loginclass_lookup(name)) == NULL) {
157                 LIST_INSERT_HEAD(&loginclasses, new_lc, lc_next);
158                 rw_wunlock(&loginclasses_lock);
159                 lc = new_lc;
160         } else {
161                 rw_wunlock(&loginclasses_lock);
162                 racct_destroy(&new_lc->lc_racct);
163                 free(new_lc, M_LOGINCLASS);
164         }
165
166         return (lc);
167 }
168
169 /*
170  * Get login class name.
171  */
172 #ifndef _SYS_SYSPROTO_H_
173 struct getloginclass_args {
174         char    *namebuf;
175         size_t  namelen;
176 };
177 #endif
178 /* ARGSUSED */
179 int
180 sys_getloginclass(struct thread *td, struct getloginclass_args *uap)
181 {
182         struct loginclass *lc;
183         size_t lcnamelen;
184
185         lc = td->td_ucred->cr_loginclass;
186         lcnamelen = strlen(lc->lc_name) + 1;
187         if (lcnamelen > uap->namelen)
188                 return (ERANGE);
189         return (copyout(lc->lc_name, uap->namebuf, lcnamelen));
190 }
191
192 /*
193  * Set login class name.
194  */
195 #ifndef _SYS_SYSPROTO_H_
196 struct setloginclass_args {
197         const char      *namebuf;
198 };
199 #endif
200 /* ARGSUSED */
201 int
202 sys_setloginclass(struct thread *td, struct setloginclass_args *uap)
203 {
204         struct proc *p = td->td_proc;
205         int error;
206         char lcname[MAXLOGNAME];
207         struct loginclass *newlc;
208         struct ucred *newcred, *oldcred;
209
210         error = priv_check(td, PRIV_PROC_SETLOGINCLASS);
211         if (error != 0)
212                 return (error);
213         error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL);
214         if (error != 0)
215                 return (error);
216
217         newlc = loginclass_find(lcname);
218         if (newlc == NULL)
219                 return (EINVAL);
220         newcred = crget();
221
222         PROC_LOCK(p);
223         oldcred = crcopysafe(p, newcred);
224         newcred->cr_loginclass = newlc;
225         proc_set_cred(p, newcred);
226 #ifdef RACCT
227         racct_proc_ucred_changed(p, oldcred, newcred);
228         crhold(newcred);
229 #endif
230         PROC_UNLOCK(p);
231 #ifdef RCTL
232         rctl_proc_ucred_changed(p, newcred);
233         crfree(newcred);
234 #endif
235         loginclass_free(oldcred->cr_loginclass);
236         crfree(oldcred);
237
238         return (0);
239 }
240
241 void
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)
245 {
246         struct loginclass *lc;
247
248         rw_rlock(&loginclasses_lock);
249         if (pre != NULL)
250                 (pre)();
251         LIST_FOREACH(lc, &loginclasses, lc_next)
252                 (callback)(lc->lc_racct, arg2, arg3);
253         if (post != NULL)
254                 (post)();
255         rw_runlock(&loginclasses_lock);
256 }