1 /*- 2 * Copyright (c) 2011 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 /* 33 * Processes may set login class name using setloginclass(2). This 34 * is usually done through call to setusercontext(3), by programs 35 * such as login(1), based on information from master.passwd(5). Kernel 36 * uses this information to enforce per-class resource limits. Current 37 * login class can be determined using id(1). Login class is inherited 38 * from the parent process during fork(2). If not set, it defaults 39 * to "default". 40 * 41 * Code in this file implements setloginclass(2) and getloginclass(2) 42 * system calls, and maintains class name storage and retrieval. 43 */ 44 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 #include <sys/param.h> 49 #include <sys/eventhandler.h> 50 #include <sys/kernel.h> 51 #include <sys/lock.h> 52 #include <sys/loginclass.h> 53 #include <sys/malloc.h> 54 #include <sys/types.h> 55 #include <sys/priv.h> 56 #include <sys/proc.h> 57 #include <sys/queue.h> 58 #include <sys/racct.h> 59 #include <sys/refcount.h> 60 #include <sys/rwlock.h> 61 #include <sys/sysproto.h> 62 #include <sys/systm.h> 63 64 static MALLOC_DEFINE(M_LOGINCLASS, "loginclass", "loginclass structures"); 65 66 LIST_HEAD(, loginclass) loginclasses; 67 68 /* 69 * Lock protecting loginclasses list. 70 */ 71 static struct rwlock loginclasses_lock; 72 RW_SYSINIT(loginclasses_init, &loginclasses_lock, "loginclasses lock"); 73 74 void 75 loginclass_hold(struct loginclass *lc) 76 { 77 78 refcount_acquire(&lc->lc_refcount); 79 } 80 81 void 82 loginclass_free(struct loginclass *lc) 83 { 84 int old; 85 86 old = lc->lc_refcount; 87 if (old > 1 && atomic_cmpset_int(&lc->lc_refcount, old, old - 1)) 88 return; 89 90 rw_wlock(&loginclasses_lock); 91 if (!refcount_release(&lc->lc_refcount)) { 92 rw_wunlock(&loginclasses_lock); 93 return; 94 } 95 96 racct_destroy(&lc->lc_racct); 97 LIST_REMOVE(lc, lc_next); 98 rw_wunlock(&loginclasses_lock); 99 100 free(lc, M_LOGINCLASS); 101 } 102 103 /* 104 * Look up a loginclass struct for the parameter name. 105 * loginclasses_lock must be locked. 106 * Increase refcount on loginclass struct returned. 107 */ 108 static struct loginclass * 109 loginclass_lookup(const char *name) 110 { 111 struct loginclass *lc; 112 113 rw_assert(&loginclasses_lock, RA_LOCKED); 114 LIST_FOREACH(lc, &loginclasses, lc_next) 115 if (strcmp(name, lc->lc_name) == 0) { 116 loginclass_hold(lc); 117 break; 118 } 119 120 return (lc); 121 } 122 123 /* 124 * Return loginclass structure with a corresponding name. Not 125 * performance critical, as it's used mainly by setloginclass(2), 126 * which happens once per login session. Caller has to use 127 * loginclass_free() on the returned value when it's no longer 128 * needed. 129 */ 130 struct loginclass * 131 loginclass_find(const char *name) 132 { 133 struct loginclass *lc, *new_lc; 134 135 if (name[0] == '\0' || strlen(name) >= MAXLOGNAME) 136 return (NULL); 137 138 rw_rlock(&loginclasses_lock); 139 lc = loginclass_lookup(name); 140 rw_runlock(&loginclasses_lock); 141 if (lc != NULL) 142 return (lc); 143 144 new_lc = malloc(sizeof(*new_lc), M_LOGINCLASS, M_ZERO | M_WAITOK); 145 racct_create(&new_lc->lc_racct); 146 refcount_init(&new_lc->lc_refcount, 1); 147 strcpy(new_lc->lc_name, name); 148 149 rw_wlock(&loginclasses_lock); 150 /* 151 * There's a chance someone created our loginclass while we 152 * were in malloc and not holding the lock, so we have to 153 * make sure we don't insert a duplicate loginclass. 154 */ 155 if ((lc = loginclass_lookup(name)) == NULL) { 156 LIST_INSERT_HEAD(&loginclasses, new_lc, lc_next); 157 rw_wunlock(&loginclasses_lock); 158 lc = new_lc; 159 } else { 160 rw_wunlock(&loginclasses_lock); 161 racct_destroy(&new_lc->lc_racct); 162 free(new_lc, M_LOGINCLASS); 163 } 164 165 return (lc); 166 } 167 168 /* 169 * Get login class name. 170 */ 171 #ifndef _SYS_SYSPROTO_H_ 172 struct getloginclass_args { 173 char *namebuf; 174 size_t namelen; 175 }; 176 #endif 177 /* ARGSUSED */ 178 int 179 sys_getloginclass(struct thread *td, struct getloginclass_args *uap) 180 { 181 struct loginclass *lc; 182 size_t lcnamelen; 183 184 lc = td->td_ucred->cr_loginclass; 185 lcnamelen = strlen(lc->lc_name) + 1; 186 if (lcnamelen > uap->namelen) 187 return (ERANGE); 188 return (copyout(lc->lc_name, uap->namebuf, lcnamelen)); 189 } 190 191 /* 192 * Set login class name. 193 */ 194 #ifndef _SYS_SYSPROTO_H_ 195 struct setloginclass_args { 196 const char *namebuf; 197 }; 198 #endif 199 /* ARGSUSED */ 200 int 201 sys_setloginclass(struct thread *td, struct setloginclass_args *uap) 202 { 203 struct proc *p = td->td_proc; 204 int error; 205 char lcname[MAXLOGNAME]; 206 struct loginclass *newlc; 207 struct ucred *newcred, *oldcred; 208 209 error = priv_check(td, PRIV_PROC_SETLOGINCLASS); 210 if (error != 0) 211 return (error); 212 error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL); 213 if (error != 0) 214 return (error); 215 216 newlc = loginclass_find(lcname); 217 if (newlc == NULL) 218 return (EINVAL); 219 newcred = crget(); 220 221 PROC_LOCK(p); 222 oldcred = crcopysafe(p, newcred); 223 newcred->cr_loginclass = newlc; 224 proc_set_cred(p, newcred); 225 PROC_UNLOCK(p); 226 #ifdef RACCT 227 racct_proc_ucred_changed(p, oldcred, newcred); 228 #endif 229 loginclass_free(oldcred->cr_loginclass); 230 crfree(oldcred); 231 232 return (0); 233 } 234 235 void 236 loginclass_racct_foreach(void (*callback)(struct racct *racct, 237 void *arg2, void *arg3), void *arg2, void *arg3) 238 { 239 struct loginclass *lc; 240 241 rw_rlock(&loginclasses_lock); 242 LIST_FOREACH(lc, &loginclasses, lc_next) 243 (callback)(lc->lc_racct, arg2, arg3); 244 rw_runlock(&loginclasses_lock); 245 } 246