1219304Strasz/*- 2219304Strasz * Copyright (c) 2011 The FreeBSD Foundation 3219304Strasz * All rights reserved. 4219304Strasz * 5219304Strasz * This software was developed by Edward Tomasz Napierala under sponsorship 6219304Strasz * from the FreeBSD Foundation. 7219304Strasz * 8219304Strasz * Redistribution and use in source and binary forms, with or without 9219304Strasz * modification, are permitted provided that the following conditions 10219304Strasz * are met: 11219304Strasz * 1. Redistributions of source code must retain the above copyright 12219304Strasz * notice, this list of conditions and the following disclaimer. 13219304Strasz * 2. Redistributions in binary form must reproduce the above copyright 14219304Strasz * notice, this list of conditions and the following disclaimer in the 15219304Strasz * documentation and/or other materials provided with the distribution. 16219304Strasz * 17219304Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18219304Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19219304Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20219304Strasz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21219304Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22219304Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23219304Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24219304Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25219304Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26219304Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27219304Strasz * SUCH DAMAGE. 28219304Strasz * 29219304Strasz * $FreeBSD: releng/11.0/sys/kern/kern_loginclass.c 290857 2015-11-15 12:10:51Z trasz $ 30219304Strasz */ 31219304Strasz 32219304Strasz/* 33219304Strasz * Processes may set login class name using setloginclass(2). This 34219304Strasz * is usually done through call to setusercontext(3), by programs 35219304Strasz * such as login(1), based on information from master.passwd(5). Kernel 36219304Strasz * uses this information to enforce per-class resource limits. Current 37219304Strasz * login class can be determined using id(1). Login class is inherited 38219304Strasz * from the parent process during fork(2). If not set, it defaults 39219304Strasz * to "default". 40219304Strasz * 41219304Strasz * Code in this file implements setloginclass(2) and getloginclass(2) 42219304Strasz * system calls, and maintains class name storage and retrieval. 43219304Strasz */ 44219304Strasz 45219304Strasz#include <sys/cdefs.h> 46219304Strasz__FBSDID("$FreeBSD: releng/11.0/sys/kern/kern_loginclass.c 290857 2015-11-15 12:10:51Z trasz $"); 47219304Strasz 48219304Strasz#include <sys/param.h> 49219304Strasz#include <sys/eventhandler.h> 50219304Strasz#include <sys/kernel.h> 51219304Strasz#include <sys/lock.h> 52219304Strasz#include <sys/loginclass.h> 53219304Strasz#include <sys/malloc.h> 54219304Strasz#include <sys/types.h> 55219304Strasz#include <sys/priv.h> 56219304Strasz#include <sys/proc.h> 57219304Strasz#include <sys/queue.h> 58220137Strasz#include <sys/racct.h> 59219304Strasz#include <sys/refcount.h> 60273763Smjg#include <sys/rwlock.h> 61219304Strasz#include <sys/sysproto.h> 62219304Strasz#include <sys/systm.h> 63219304Strasz 64219304Straszstatic MALLOC_DEFINE(M_LOGINCLASS, "loginclass", "loginclass structures"); 65219304Strasz 66219304StraszLIST_HEAD(, loginclass) loginclasses; 67219304Strasz 68219304Strasz/* 69219304Strasz * Lock protecting loginclasses list. 70219304Strasz */ 71273763Smjgstatic struct rwlock loginclasses_lock; 72273763SmjgRW_SYSINIT(loginclasses_init, &loginclasses_lock, "loginclasses lock"); 73219304Strasz 74219304Straszvoid 75219304Straszloginclass_hold(struct loginclass *lc) 76219304Strasz{ 77219304Strasz 78219304Strasz refcount_acquire(&lc->lc_refcount); 79219304Strasz} 80219304Strasz 81219304Straszvoid 82219304Straszloginclass_free(struct loginclass *lc) 83219304Strasz{ 84219304Strasz int old; 85219304Strasz 86219304Strasz old = lc->lc_refcount; 87219304Strasz if (old > 1 && atomic_cmpset_int(&lc->lc_refcount, old, old - 1)) 88219304Strasz return; 89219304Strasz 90273763Smjg rw_wlock(&loginclasses_lock); 91273763Smjg if (!refcount_release(&lc->lc_refcount)) { 92273763Smjg rw_wunlock(&loginclasses_lock); 93219304Strasz return; 94219304Strasz } 95273763Smjg 96273763Smjg racct_destroy(&lc->lc_racct); 97273763Smjg LIST_REMOVE(lc, lc_next); 98273763Smjg rw_wunlock(&loginclasses_lock); 99273763Smjg 100273763Smjg free(lc, M_LOGINCLASS); 101219304Strasz} 102219304Strasz 103219304Strasz/* 104273763Smjg * Look up a loginclass struct for the parameter name. 105273763Smjg * loginclasses_lock must be locked. 106273763Smjg * Increase refcount on loginclass struct returned. 107273763Smjg */ 108273763Smjgstatic struct loginclass * 109273763Smjgloginclass_lookup(const char *name) 110273763Smjg{ 111273763Smjg struct loginclass *lc; 112273763Smjg 113273763Smjg rw_assert(&loginclasses_lock, RA_LOCKED); 114273763Smjg LIST_FOREACH(lc, &loginclasses, lc_next) 115273763Smjg if (strcmp(name, lc->lc_name) == 0) { 116273763Smjg loginclass_hold(lc); 117273763Smjg break; 118273763Smjg } 119273763Smjg 120273763Smjg return (lc); 121273763Smjg} 122273763Smjg 123273763Smjg/* 124219304Strasz * Return loginclass structure with a corresponding name. Not 125219304Strasz * performance critical, as it's used mainly by setloginclass(2), 126219304Strasz * which happens once per login session. Caller has to use 127219304Strasz * loginclass_free() on the returned value when it's no longer 128219304Strasz * needed. 129219304Strasz */ 130219304Straszstruct loginclass * 131219304Straszloginclass_find(const char *name) 132219304Strasz{ 133273763Smjg struct loginclass *lc, *new_lc; 134219304Strasz 135219304Strasz if (name[0] == '\0' || strlen(name) >= MAXLOGNAME) 136219304Strasz return (NULL); 137219304Strasz 138273763Smjg rw_rlock(&loginclasses_lock); 139273763Smjg lc = loginclass_lookup(name); 140273763Smjg rw_runlock(&loginclasses_lock); 141273763Smjg if (lc != NULL) 142273763Smjg return (lc); 143219304Strasz 144273763Smjg new_lc = malloc(sizeof(*new_lc), M_LOGINCLASS, M_ZERO | M_WAITOK); 145273763Smjg racct_create(&new_lc->lc_racct); 146273763Smjg refcount_init(&new_lc->lc_refcount, 1); 147273763Smjg strcpy(new_lc->lc_name, name); 148219304Strasz 149273763Smjg rw_wlock(&loginclasses_lock); 150273763Smjg /* 151273763Smjg * There's a chance someone created our loginclass while we 152273763Smjg * were in malloc and not holding the lock, so we have to 153273763Smjg * make sure we don't insert a duplicate loginclass. 154273763Smjg */ 155273763Smjg if ((lc = loginclass_lookup(name)) == NULL) { 156273763Smjg LIST_INSERT_HEAD(&loginclasses, new_lc, lc_next); 157273763Smjg rw_wunlock(&loginclasses_lock); 158273763Smjg lc = new_lc; 159273763Smjg } else { 160273763Smjg rw_wunlock(&loginclasses_lock); 161273763Smjg racct_destroy(&new_lc->lc_racct); 162273763Smjg free(new_lc, M_LOGINCLASS); 163219304Strasz } 164219304Strasz 165273763Smjg return (lc); 166219304Strasz} 167219304Strasz 168219304Strasz/* 169219304Strasz * Get login class name. 170219304Strasz */ 171219304Strasz#ifndef _SYS_SYSPROTO_H_ 172219304Straszstruct getloginclass_args { 173219304Strasz char *namebuf; 174219304Strasz size_t namelen; 175219304Strasz}; 176219304Strasz#endif 177219304Strasz/* ARGSUSED */ 178219304Straszint 179225617Skmacysys_getloginclass(struct thread *td, struct getloginclass_args *uap) 180219304Strasz{ 181273764Smjg struct loginclass *lc; 182219304Strasz size_t lcnamelen; 183219304Strasz 184273764Smjg lc = td->td_ucred->cr_loginclass; 185219304Strasz lcnamelen = strlen(lc->lc_name) + 1; 186219304Strasz if (lcnamelen > uap->namelen) 187273764Smjg return (ERANGE); 188273764Smjg return (copyout(lc->lc_name, uap->namebuf, lcnamelen)); 189219304Strasz} 190219304Strasz 191219304Strasz/* 192219304Strasz * Set login class name. 193219304Strasz */ 194219304Strasz#ifndef _SYS_SYSPROTO_H_ 195219304Straszstruct setloginclass_args { 196219304Strasz const char *namebuf; 197219304Strasz}; 198219304Strasz#endif 199219304Strasz/* ARGSUSED */ 200219304Straszint 201225617Skmacysys_setloginclass(struct thread *td, struct setloginclass_args *uap) 202219304Strasz{ 203219304Strasz struct proc *p = td->td_proc; 204219304Strasz int error; 205219304Strasz char lcname[MAXLOGNAME]; 206219304Strasz struct loginclass *newlc; 207219304Strasz struct ucred *newcred, *oldcred; 208219304Strasz 209219304Strasz error = priv_check(td, PRIV_PROC_SETLOGINCLASS); 210219304Strasz if (error != 0) 211219304Strasz return (error); 212219304Strasz error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL); 213219304Strasz if (error != 0) 214219304Strasz return (error); 215219304Strasz 216219304Strasz newlc = loginclass_find(lcname); 217219304Strasz if (newlc == NULL) 218219304Strasz return (EINVAL); 219219304Strasz newcred = crget(); 220219304Strasz 221219304Strasz PROC_LOCK(p); 222219304Strasz oldcred = crcopysafe(p, newcred); 223219304Strasz newcred->cr_loginclass = newlc; 224280130Smjg proc_set_cred(p, newcred); 225219304Strasz PROC_UNLOCK(p); 226220137Strasz#ifdef RACCT 227220137Strasz racct_proc_ucred_changed(p, oldcred, newcred); 228220137Strasz#endif 229219304Strasz loginclass_free(oldcred->cr_loginclass); 230219304Strasz crfree(oldcred); 231219304Strasz 232219304Strasz return (0); 233219304Strasz} 234219304Strasz 235220137Straszvoid 236220137Straszloginclass_racct_foreach(void (*callback)(struct racct *racct, 237290857Strasz void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 238290857Strasz void *arg2, void *arg3) 239220137Strasz{ 240220137Strasz struct loginclass *lc; 241220137Strasz 242273763Smjg rw_rlock(&loginclasses_lock); 243290857Strasz if (pre != NULL) 244290857Strasz (pre)(); 245220137Strasz LIST_FOREACH(lc, &loginclasses, lc_next) 246220137Strasz (callback)(lc->lc_racct, arg2, arg3); 247290857Strasz if (post != NULL) 248290857Strasz (post)(); 249273763Smjg rw_runlock(&loginclasses_lock); 250220137Strasz} 251