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