1 /* $NetBSD: kern_uidinfo.c,v 1.7 2012/06/09 02:55:32 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1982, 1986, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: kern_uidinfo.c,v 1.7 2012/06/09 02:55:32 christos Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kmem.h> 43 #include <sys/proc.h> 44 #include <sys/atomic.h> 45 #include <sys/uidinfo.h> 46 #include <sys/sysctl.h> 47 #include <sys/kauth.h> 48 #include <sys/cpu.h> 49 50 static SLIST_HEAD(uihashhead, uidinfo) *uihashtbl; 51 static u_long uihash; 52 53 #define UIHASH(uid) (&uihashtbl[(uid) & uihash]) 54 55 static int 56 sysctl_kern_uidinfo_cnt(SYSCTLFN_ARGS) 57 { 58 static const struct { 59 const char *name; 60 u_int value; 61 } nv[] = { 62 #define _MEM(n) { # n, offsetof(struct uidinfo, ui_ ## n) } 63 _MEM(proccnt), 64 _MEM(lwpcnt), 65 _MEM(lockcnt), 66 _MEM(sbsize), 67 #undef _MEM 68 }; 69 70 for (size_t i = 0; i < __arraycount(nv); i++) 71 if (strcmp(nv[i].name, rnode->sysctl_name) == 0) { 72 uint64_t cnt; 73 struct sysctlnode node = *rnode; 74 struct uidinfo *uip; 75 76 node.sysctl_data = &cnt; 77 uip = uid_find(kauth_cred_geteuid(l->l_cred)); 78 79 *(uint64_t *)node.sysctl_data = 80 *(u_long *)((char *)uip + nv[i].value); 81 82 return sysctl_lookup(SYSCTLFN_CALL(&node)); 83 } 84 85 return EINVAL; 86 } 87 88 static void 89 sysctl_kern_uidinfo_setup(void) 90 { 91 const struct sysctlnode *rnode, *cnode; 92 struct sysctllog *kern_uidinfo_sysctllog; 93 94 kern_uidinfo_sysctllog = NULL; 95 sysctl_createv(&kern_uidinfo_sysctllog, 0, NULL, &rnode, 96 CTLFLAG_PERMANENT, 97 CTLTYPE_NODE, "uidinfo", 98 SYSCTL_DESCR("Resource usage per uid"), 99 NULL, 0, NULL, 0, 100 CTL_KERN, CTL_CREATE, CTL_EOL); 101 102 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode, 103 CTLFLAG_PERMANENT, 104 CTLTYPE_QUAD, "proccnt", 105 SYSCTL_DESCR("Number of processes for the current user"), 106 sysctl_kern_uidinfo_cnt, 0, NULL, 0, 107 CTL_CREATE, CTL_EOL); 108 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode, 109 CTLFLAG_PERMANENT, 110 CTLTYPE_QUAD, "lwpcnt", 111 SYSCTL_DESCR("Number of lwps for the current user"), 112 sysctl_kern_uidinfo_cnt, 0, NULL, 0, 113 CTL_CREATE, CTL_EOL); 114 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode, 115 CTLFLAG_PERMANENT, 116 CTLTYPE_QUAD, "lockcnt", 117 SYSCTL_DESCR("Number of locks for the current user"), 118 sysctl_kern_uidinfo_cnt, 0, NULL, 0, 119 CTL_CREATE, CTL_EOL); 120 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode, 121 CTLFLAG_PERMANENT, 122 CTLTYPE_QUAD, "sbsize", 123 SYSCTL_DESCR("Socket buffers used for the current user"), 124 sysctl_kern_uidinfo_cnt, 0, NULL, 0, 125 CTL_CREATE, CTL_EOL); 126 } 127 128 void 129 uid_init(void) 130 { 131 132 /* 133 * In case of MP system, SLIST_FOREACH would force a cache line 134 * write-back for every modified 'uidinfo', thus we try to keep the 135 * lists short. 136 */ 137 const u_int uihash_sz = (maxcpus > 1 ? 1024 : 64); 138 139 uihashtbl = hashinit(uihash_sz, HASH_SLIST, true, &uihash); 140 141 /* 142 * Ensure that uid 0 is always in the user hash table, as 143 * sbreserve() expects it available from interrupt context. 144 */ 145 (void)uid_find(0); 146 sysctl_kern_uidinfo_setup(); 147 } 148 149 struct uidinfo * 150 uid_find(uid_t uid) 151 { 152 struct uidinfo *uip, *uip_first, *newuip; 153 struct uihashhead *uipp; 154 155 uipp = UIHASH(uid); 156 newuip = NULL; 157 158 /* 159 * To make insertion atomic, abstraction of SLIST will be violated. 160 */ 161 uip_first = uipp->slh_first; 162 again: 163 SLIST_FOREACH(uip, uipp, ui_hash) { 164 if (uip->ui_uid != uid) 165 continue; 166 if (newuip != NULL) 167 kmem_free(newuip, sizeof(*newuip)); 168 return uip; 169 } 170 if (newuip == NULL) 171 newuip = kmem_zalloc(sizeof(*newuip), KM_SLEEP); 172 newuip->ui_uid = uid; 173 174 /* 175 * If atomic insert is unsuccessful, another thread might be 176 * allocated this 'uid', thus full re-check is needed. 177 */ 178 newuip->ui_hash.sle_next = uip_first; 179 membar_producer(); 180 uip = atomic_cas_ptr(&uipp->slh_first, uip_first, newuip); 181 if (uip != uip_first) { 182 uip_first = uip; 183 goto again; 184 } 185 186 return newuip; 187 } 188 189 /* 190 * Change the count associated with number of processes 191 * a given user is using. 192 */ 193 int 194 chgproccnt(uid_t uid, int diff) 195 { 196 struct uidinfo *uip; 197 long proccnt; 198 199 uip = uid_find(uid); 200 proccnt = atomic_add_long_nv(&uip->ui_proccnt, diff); 201 KASSERT(proccnt >= 0); 202 return proccnt; 203 } 204 205 /* 206 * Change the count associated with number of lwps 207 * a given user is using. 208 */ 209 int 210 chglwpcnt(uid_t uid, int diff) 211 { 212 struct uidinfo *uip; 213 long lwpcnt; 214 215 uip = uid_find(uid); 216 lwpcnt = atomic_add_long_nv(&uip->ui_lwpcnt, diff); 217 KASSERT(lwpcnt >= 0); 218 return lwpcnt; 219 } 220 221 int 222 chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t xmax) 223 { 224 rlim_t nsb; 225 const long diff = to - *hiwat; 226 227 nsb = (rlim_t)atomic_add_long_nv((long *)&uip->ui_sbsize, diff); 228 if (diff > 0 && nsb > xmax) { 229 atomic_add_long((long *)&uip->ui_sbsize, -diff); 230 return 0; 231 } 232 *hiwat = to; 233 return 1; 234 } 235