]>
Commit | Line | Data |
---|---|---|
b0d623f7 A |
1 | /*- |
2 | * Copyright (c) 2008-2009 Apple Inc. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of | |
14 | * its contributors may be used to endorse or promote products derived | |
15 | * from this software without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS 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 APPLE OR ITS CONTRIBUTORS BE LIABLE FOR | |
21 | * 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, | |
25 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
27 | * POSSIBILITY OF SUCH DAMAGE. | |
28 | */ | |
29 | ||
6d2010ae A |
30 | #include <stdarg.h> |
31 | ||
b0d623f7 | 32 | #include <sys/kernel.h> |
6d2010ae | 33 | #include <sys/fcntl.h> |
b0d623f7 | 34 | #include <sys/kauth.h> |
6d2010ae A |
35 | #include <sys/conf.h> |
36 | #include <sys/poll.h> | |
cb323159 | 37 | #include <sys/priv.h> |
b0d623f7 | 38 | #include <sys/queue.h> |
6d2010ae | 39 | #include <sys/signalvar.h> |
b0d623f7 A |
40 | #include <sys/syscall.h> |
41 | #include <sys/sysent.h> | |
42 | #include <sys/sysproto.h> | |
43 | #include <sys/systm.h> | |
44 | #include <sys/ucred.h> | |
6d2010ae A |
45 | #include <sys/user.h> |
46 | ||
47 | #include <miscfs/devfs/devfs.h> | |
b0d623f7 A |
48 | |
49 | #include <libkern/OSAtomic.h> | |
50 | ||
51 | #include <bsm/audit.h> | |
6d2010ae A |
52 | #include <bsm/audit_internal.h> |
53 | #include <bsm/audit_kevents.h> | |
54 | ||
b0d623f7 A |
55 | #include <security/audit/audit.h> |
56 | #include <security/audit/audit_bsd.h> | |
6d2010ae | 57 | #include <security/audit/audit_ioctl.h> |
b0d623f7 A |
58 | #include <security/audit/audit_private.h> |
59 | ||
60 | #include <vm/vm_protos.h> | |
6d2010ae | 61 | #include <mach/mach_port.h> |
b0d623f7 A |
62 | #include <kern/audit_sessionport.h> |
63 | ||
6d2010ae | 64 | #include <libkern/OSDebug.h> |
b0d623f7 A |
65 | |
66 | /* | |
67 | * Audit Session Entry. This is treated as an object with public and private | |
68 | * data. The se_auinfo field is the only information that is public and | |
69 | * needs to be the first entry. | |
70 | */ | |
71 | struct au_sentry { | |
0a7de745 A |
72 | auditinfo_addr_t se_auinfo; /* Public audit session data. */ |
73 | #define se_asid se_auinfo.ai_asid | |
74 | #define se_auid se_auinfo.ai_auid | |
75 | #define se_mask se_auinfo.ai_mask | |
76 | #define se_termid se_auinfo.ai_termid | |
77 | #define se_flags se_auinfo.ai_flags | |
78 | ||
79 | long se_refcnt; /* Reference count. */ | |
80 | long se_procnt; /* Processes in session. */ | |
81 | ipc_port_t se_port; /* Session port. */ | |
82 | LIST_ENTRY(au_sentry) se_link; /* Hash bucket link list (1) */ | |
b0d623f7 A |
83 | }; |
84 | typedef struct au_sentry au_sentry_t; | |
85 | ||
0a7de745 | 86 | #define AU_SENTRY_PTR(aia_p) ((au_sentry_t *)(aia_p)) |
b0d623f7 | 87 | |
6d2010ae | 88 | /* |
0a7de745 | 89 | * The default au_sentry/auditinfo_addr entry for ucred. |
6d2010ae A |
90 | */ |
91 | ||
92 | static au_sentry_t audit_default_se = { | |
93 | .se_auinfo = { | |
0a7de745 A |
94 | .ai_auid = AU_DEFAUDITID, |
95 | .ai_asid = AU_DEFAUDITSID, | |
96 | .ai_termid = { .at_type = AU_IPv4, }, | |
6d2010ae | 97 | }, |
0a7de745 | 98 | .se_refcnt = 1, |
6d2010ae A |
99 | .se_procnt = 1, |
100 | }; | |
101 | ||
f427ee49 | 102 | struct auditinfo_addr * const audit_default_aia_p = &audit_default_se.se_auinfo; |
6d2010ae | 103 | |
cb323159 | 104 | /* Copied from <ipc/ipc_object.h> */ |
c3c9b80d | 105 | #define IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND 0x1 |
6d2010ae | 106 | kern_return_t ipc_object_copyin(ipc_space_t, mach_port_name_t, |
cb323159 | 107 | mach_msg_type_name_t, ipc_port_t *, mach_port_context_t, mach_msg_guard_flags_t *, uint32_t); |
6d2010ae A |
108 | void ipc_port_release_send(ipc_port_t); |
109 | ||
110 | #if CONFIG_AUDIT | |
111 | ||
112 | ||
113 | /* | |
114 | * Currently the hash table is a fixed size. | |
115 | */ | |
0a7de745 A |
116 | #define HASH_TABLE_SIZE 97 |
117 | #define HASH_ASID(asid) (audit_session_hash(asid) % HASH_TABLE_SIZE) | |
6d2010ae | 118 | |
0a7de745 | 119 | static struct rwlock se_entry_lck; /* (1) lock for se_link above */ |
b0d623f7 A |
120 | |
121 | LIST_HEAD(au_sentry_head, au_sentry); | |
122 | static struct au_sentry_head *au_sentry_bucket = NULL; | |
123 | ||
6d2010ae A |
124 | #define AU_HISTORY_LOGGING 0 |
125 | #if AU_HISTORY_LOGGING | |
126 | typedef enum au_history_event { | |
127 | AU_HISTORY_EVENT_UNKNOWN = 0, | |
128 | AU_HISTORY_EVENT_REF = 1, | |
129 | AU_HISTORY_EVENT_UNREF = 2, | |
130 | AU_HISTORY_EVENT_BIRTH = 3, | |
131 | AU_HISTORY_EVENT_DEATH = 4, | |
132 | AU_HISTORY_EVENT_FIND = 5 | |
133 | } au_history_event_t; | |
134 | ||
135 | #define AU_HISTORY_MAX_STACK_DEPTH 8 | |
136 | ||
137 | struct au_history { | |
0a7de745 A |
138 | struct au_sentry *ptr; |
139 | struct au_sentry se; | |
140 | void *stack[AU_HISTORY_MAX_STACK_DEPTH]; | |
141 | unsigned int stack_depth; | |
142 | au_history_event_t event; | |
6d2010ae A |
143 | }; |
144 | ||
145 | static struct au_history *au_history; | |
0a7de745 A |
146 | static size_t au_history_size = 65536; |
147 | static unsigned int au_history_index; | |
6d2010ae A |
148 | |
149 | static inline unsigned int | |
150 | au_history_entries(void) | |
151 | { | |
0a7de745 | 152 | if (au_history_index >= au_history_size) { |
6d2010ae | 153 | return au_history_size; |
0a7de745 | 154 | } else { |
6d2010ae | 155 | return au_history_index; |
0a7de745 | 156 | } |
6d2010ae A |
157 | } |
158 | ||
159 | static inline void | |
160 | au_history_record(au_sentry_t *se, au_history_event_t event) | |
161 | { | |
162 | struct au_history *p; | |
163 | unsigned int i; | |
164 | ||
165 | i = OSAddAtomic(1, &au_history_index); | |
166 | p = &au_history[i % au_history_size]; | |
167 | ||
168 | bzero(p, sizeof(*p)); | |
169 | p->event = event; | |
170 | bcopy(se, &p->se, sizeof(p->se)); | |
171 | p->stack_depth = OSBacktrace(&p->stack[0], AU_HISTORY_MAX_STACK_DEPTH); | |
172 | p->ptr = se; | |
173 | } | |
174 | #else | |
175 | #define au_history_record(se, event) do {} while (0) | |
176 | #endif | |
177 | ||
178 | MALLOC_DEFINE(M_AU_SESSION, "audit_session", "Audit session data"); | |
179 | ||
0a7de745 A |
180 | static void audit_ref_session(au_sentry_t *se); |
181 | static void audit_unref_session(au_sentry_t *se); | |
6d2010ae | 182 | |
0a7de745 | 183 | static void audit_session_event(int event, auditinfo_addr_t *aia_p); |
6d2010ae A |
184 | |
185 | /* | |
186 | * Audit session device. | |
187 | */ | |
188 | ||
189 | static MALLOC_DEFINE(M_AUDIT_SDEV, "audit_sdev", "Audit sdevs"); | |
190 | static MALLOC_DEFINE(M_AUDIT_SDEV_ENTRY, "audit_sdevent", | |
191 | "Audit sdev entries and buffers"); | |
192 | ||
193 | /* | |
194 | * Default audit sdev buffer parameters. | |
195 | */ | |
0a7de745 A |
196 | #define AUDIT_SDEV_QLIMIT_DEFAULT 128 |
197 | #define AUDIT_SDEV_QLIMIT_MIN 1 | |
198 | #define AUDIT_SDEV_QLIMIT_MAX 1024 | |
6d2010ae | 199 | |
b0d623f7 | 200 | /* |
6d2010ae | 201 | * Entry structure. |
b0d623f7 | 202 | */ |
0a7de745 A |
203 | struct audit_sdev_entry { |
204 | void *ase_record; | |
205 | u_int ase_record_len; | |
206 | TAILQ_ENTRY(audit_sdev_entry) ase_queue; | |
b0d623f7 | 207 | }; |
b0d623f7 | 208 | |
6d2010ae | 209 | /* |
0a7de745 | 210 | * Per audit sdev structure. |
6d2010ae A |
211 | */ |
212 | ||
213 | struct audit_sdev { | |
0a7de745 | 214 | int asdev_open; |
6d2010ae | 215 | |
0a7de745 A |
216 | #define AUDIT_SDEV_ASYNC 0x00000001 |
217 | #define AUDIT_SDEV_NBIO 0x00000002 | |
6d2010ae | 218 | |
0a7de745 A |
219 | #define AUDIT_SDEV_ALLSESSIONS 0x00010000 |
220 | u_int asdev_flags; | |
6d2010ae | 221 | |
0a7de745 A |
222 | struct selinfo asdev_selinfo; |
223 | pid_t asdev_sigio; | |
6d2010ae | 224 | |
0a7de745 A |
225 | au_id_t asdev_auid; |
226 | au_asid_t asdev_asid; | |
6d2010ae A |
227 | |
228 | /* Per-sdev mutex for most fields in this struct. */ | |
0a7de745 | 229 | struct mtx asdev_mtx; |
6d2010ae A |
230 | |
231 | /* | |
232 | * Per-sdev sleep lock serializing user-generated reads and | |
233 | * flushes. uiomove() is called to copy out the current head | |
234 | * record's data whie the record remains in the queue, so we | |
235 | * prevent other threads from removing it using this lock. | |
236 | */ | |
0a7de745 | 237 | struct slck asdev_sx; |
6d2010ae A |
238 | |
239 | /* | |
0a7de745 | 240 | * Condition variable to signal when data has been delivered to |
6d2010ae A |
241 | * a sdev. |
242 | */ | |
0a7de745 | 243 | struct cv asdev_cv; |
6d2010ae A |
244 | |
245 | /* Count and bound of records in the queue. */ | |
0a7de745 A |
246 | u_int asdev_qlen; |
247 | u_int asdev_qlimit; | |
6d2010ae A |
248 | |
249 | /* The number of bytes of data across all records. */ | |
0a7de745 A |
250 | u_int asdev_qbyteslen; |
251 | ||
252 | /* | |
6d2010ae A |
253 | * The amount read so far of the first record in the queue. |
254 | * (The number of bytes available for reading in the queue is | |
255 | * qbyteslen - qoffset.) | |
256 | */ | |
0a7de745 | 257 | u_int asdev_qoffset; |
6d2010ae A |
258 | |
259 | /* | |
260 | * Per-sdev operation statistics. | |
261 | */ | |
0a7de745 A |
262 | u_int64_t asdev_inserts; /* Records added. */ |
263 | u_int64_t asdev_reads; /* Records read. */ | |
264 | u_int64_t asdev_drops; /* Records dropped. */ | |
6d2010ae A |
265 | |
266 | /* | |
267 | * Current pending record list. This is protected by a | |
268 | * combination of asdev_mtx and asdev_sx. Note that both | |
269 | * locks are required to remove a record from the head of the | |
270 | * queue, as an in-progress read may sleep while copying and, | |
271 | * therefore, cannot hold asdev_mtx. | |
272 | */ | |
0a7de745 | 273 | TAILQ_HEAD(, audit_sdev_entry) asdev_queue; |
6d2010ae A |
274 | |
275 | /* Global sdev list. */ | |
0a7de745 | 276 | TAILQ_ENTRY(audit_sdev) asdev_list; |
b0d623f7 | 277 | }; |
b0d623f7 | 278 | |
0a7de745 A |
279 | #define AUDIT_SDEV_LOCK(asdev) mtx_lock(&(asdev)->asdev_mtx) |
280 | #define AUDIT_SDEV_LOCK_ASSERT(asdev) mtx_assert(&(asdev)->asdev_mtx, \ | |
281 | MA_OWNED) | |
282 | #define AUDIT_SDEV_LOCK_DESTROY(asdev) mtx_destroy(&(asdev)->asdev_mtx) | |
283 | #define AUDIT_SDEV_LOCK_INIT(asdev) mtx_init(&(asdev)->asdev_mtx, \ | |
284 | "audit_sdev_mtx", NULL, MTX_DEF) | |
285 | #define AUDIT_SDEV_UNLOCK(asdev) mtx_unlock(&(asdev)->asdev_mtx) | |
286 | #define AUDIT_SDEV_MTX(asdev) (&(asdev)->asdev_mtx) | |
287 | ||
288 | #define AUDIT_SDEV_SX_LOCK_DESTROY(asd) slck_destroy(&(asd)->asdev_sx) | |
289 | #define AUDIT_SDEV_SX_LOCK_INIT(asd) slck_init(&(asd)->asdev_sx, \ | |
290 | "audit_sdev_sx") | |
291 | #define AUDIT_SDEV_SX_XLOCK_ASSERT(asd) slck_assert(&(asd)->asdev_sx, \ | |
292 | SA_XLOCKED) | |
293 | #define AUDIT_SDEV_SX_XLOCK_SIG(asd) slck_lock_sig(&(asd)->asdev_sx) | |
294 | #define AUDIT_SDEV_SX_XUNLOCK(asd) slck_unlock(&(asd)->asdev_sx) | |
b0d623f7 | 295 | |
6d2010ae A |
296 | /* |
297 | * Cloning variables and constants. | |
298 | */ | |
0a7de745 A |
299 | #define AUDIT_SDEV_NAME "auditsessions" |
300 | #define MAX_AUDIT_SDEVS 32 | |
6d2010ae A |
301 | |
302 | static int audit_sdev_major; | |
303 | static void *devnode; | |
304 | ||
305 | /* | |
306 | * Global list of audit sdevs. The list is protected by a rw lock. | |
307 | * Individaul record queues are protected by per-sdev locks. These | |
0a7de745 | 308 | * locks synchronize between threads walking the list to deliver to |
6d2010ae A |
309 | * individual sdevs and adds/removes of sdevs. |
310 | */ | |
311 | static TAILQ_HEAD(, audit_sdev) audit_sdev_list; | |
0a7de745 | 312 | static struct rwlock audit_sdev_lock; |
6d2010ae | 313 | |
0a7de745 A |
314 | #define AUDIT_SDEV_LIST_LOCK_INIT() rw_init(&audit_sdev_lock, \ |
315 | "audit_sdev_list_lock") | |
316 | #define AUDIT_SDEV_LIST_RLOCK() rw_rlock(&audit_sdev_lock) | |
317 | #define AUDIT_SDEV_LIST_RUNLOCK() rw_runlock(&audit_sdev_lock) | |
318 | #define AUDIT_SDEV_LIST_WLOCK() rw_wlock(&audit_sdev_lock) | |
319 | #define AUDIT_SDEV_LIST_WLOCK_ASSERT() rw_assert(&audit_sdev_lock, \ | |
320 | RA_WLOCKED) | |
321 | #define AUDIT_SDEV_LIST_WUNLOCK() rw_wunlock(&audit_sdev_lock) | |
6d2010ae A |
322 | |
323 | /* | |
324 | * dev_t doesn't have a pointer for "softc" data so we have to keep track of | |
325 | * it with the following global array (indexed by the minor number). | |
326 | * | |
327 | * XXX We may want to dynamically grow this as need. | |
328 | */ | |
0a7de745 | 329 | static struct audit_sdev *audit_sdev_dtab[MAX_AUDIT_SDEVS]; |
b0d623f7 A |
330 | |
331 | /* | |
6d2010ae | 332 | * Special device methods and definition. |
b0d623f7 | 333 | */ |
0a7de745 A |
334 | static open_close_fcn_t audit_sdev_open; |
335 | static open_close_fcn_t audit_sdev_close; | |
336 | static read_write_fcn_t audit_sdev_read; | |
337 | static ioctl_fcn_t audit_sdev_ioctl; | |
338 | static select_fcn_t audit_sdev_poll; | |
6d2010ae | 339 | |
f427ee49 | 340 | static const struct cdevsw audit_sdev_cdevsw = { |
6d2010ae A |
341 | .d_open = audit_sdev_open, |
342 | .d_close = audit_sdev_close, | |
343 | .d_read = audit_sdev_read, | |
344 | .d_write = eno_rdwrt, | |
345 | .d_ioctl = audit_sdev_ioctl, | |
346 | .d_stop = eno_stop, | |
347 | .d_reset = eno_reset, | |
348 | .d_ttys = NULL, | |
349 | .d_select = audit_sdev_poll, | |
350 | .d_mmap = eno_mmap, | |
351 | .d_strategy = eno_strat, | |
352 | .d_type = 0 | |
b0d623f7 A |
353 | }; |
354 | ||
355 | /* | |
6d2010ae A |
356 | * Global statistics on audit sdevs. |
357 | */ | |
0a7de745 A |
358 | static int audit_sdev_count; /* Current number of sdevs. */ |
359 | static u_int64_t audit_sdev_ever; /* Sdevs ever allocated. */ | |
360 | static u_int64_t audit_sdev_records; /* Total records seen. */ | |
361 | static u_int64_t audit_sdev_drops; /* Global record drop count. */ | |
6d2010ae A |
362 | |
363 | static int audit_sdev_init(void); | |
b0d623f7 | 364 | |
0a7de745 A |
365 | #define AUDIT_SENTRY_RWLOCK_INIT() rw_init(&se_entry_lck, \ |
366 | "se_entry_lck") | |
367 | #define AUDIT_SENTRY_RLOCK() rw_rlock(&se_entry_lck) | |
368 | #define AUDIT_SENTRY_WLOCK() rw_wlock(&se_entry_lck) | |
369 | #define AUDIT_SENTRY_RWLOCK_ASSERT() rw_assert(&se_entry_lck, RA_LOCKED) | |
370 | #define AUDIT_SENTRY_RUNLOCK() rw_runlock(&se_entry_lck) | |
371 | #define AUDIT_SENTRY_WUNLOCK() rw_wunlock(&se_entry_lck) | |
b0d623f7 | 372 | |
6d2010ae A |
373 | /* Access control on the auditinfo_addr.ai_flags member. */ |
374 | static uint64_t audit_session_superuser_set_sflags_mask; | |
375 | static uint64_t audit_session_superuser_clear_sflags_mask; | |
376 | static uint64_t audit_session_member_set_sflags_mask; | |
377 | static uint64_t audit_session_member_clear_sflags_mask; | |
0a7de745 A |
378 | SYSCTL_NODE(, OID_AUTO, audit, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Audit controls"); |
379 | SYSCTL_NODE(_audit, OID_AUTO, session, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Audit sessions"); | |
6d2010ae A |
380 | SYSCTL_QUAD(_audit_session, OID_AUTO, superuser_set_sflags_mask, CTLFLAG_RW | CTLFLAG_LOCKED, |
381 | &audit_session_superuser_set_sflags_mask, | |
382 | "Audit session flags settable by superuser"); | |
383 | SYSCTL_QUAD(_audit_session, OID_AUTO, superuser_clear_sflags_mask, CTLFLAG_RW | CTLFLAG_LOCKED, | |
384 | &audit_session_superuser_clear_sflags_mask, | |
385 | "Audit session flags clearable by superuser"); | |
386 | SYSCTL_QUAD(_audit_session, OID_AUTO, member_set_sflags_mask, CTLFLAG_RW | CTLFLAG_LOCKED, | |
387 | &audit_session_member_set_sflags_mask, | |
388 | "Audit session flags settable by a session member"); | |
389 | SYSCTL_QUAD(_audit_session, OID_AUTO, member_clear_sflags_mask, CTLFLAG_RW | CTLFLAG_LOCKED, | |
390 | &audit_session_member_clear_sflags_mask, | |
391 | "Audit session flags clearable by a session member"); | |
392 | ||
743345f9 A |
393 | extern int set_security_token_task_internal(proc_t p, void *task); |
394 | ||
0a7de745 A |
395 | #define AUDIT_SESSION_DEBUG 0 |
396 | #if AUDIT_SESSION_DEBUG | |
6d2010ae | 397 | /* |
0a7de745 | 398 | * The following is debugging code that can be used to get a snapshot of the |
6d2010ae A |
399 | * session state. The audit session information is read out using sysctl: |
400 | * | |
401 | * error = sysctlbyname("kern.audit_session_debug", buffer_ptr, &buffer_len, | |
0a7de745 | 402 | * NULL, 0); |
6d2010ae | 403 | */ |
b0d623f7 A |
404 | #include <kern/kalloc.h> |
405 | ||
6d2010ae A |
406 | /* |
407 | * The per session record structure for the snapshot data. | |
408 | */ | |
b0d623f7 | 409 | struct au_sentry_debug { |
0a7de745 A |
410 | auditinfo_addr_t se_auinfo; |
411 | int64_t se_refcnt; /* refereence count */ | |
412 | int64_t se_procnt; /* process count */ | |
413 | int64_t se_ptcnt; /* process count from | |
414 | * proc table */ | |
b0d623f7 A |
415 | }; |
416 | typedef struct au_sentry_debug au_sentry_debug_t; | |
417 | ||
418 | static int audit_sysctl_session_debug(struct sysctl_oid *oidp, void *arg1, | |
419 | int arg2, struct sysctl_req *req); | |
420 | ||
6d2010ae A |
421 | SYSCTL_PROC(_kern, OID_AUTO, audit_session_debug, CTLFLAG_RD | CTLFLAG_LOCKED, |
422 | NULL, 0, audit_sysctl_session_debug, "S,audit_session_debug", | |
b0d623f7 A |
423 | "Current session debug info for auditing."); |
424 | ||
425 | /* | |
6d2010ae A |
426 | * Callouts for proc_interate() which is used to reconcile the audit session |
427 | * proc state information with the proc table. We get everything we need | |
428 | * in the filterfn while the proc_lock() is held so we really don't need the | |
429 | * callout() function. | |
430 | */ | |
0a7de745 | 431 | static int |
6d2010ae A |
432 | audit_session_debug_callout(__unused proc_t p, __unused void *arg) |
433 | { | |
0a7de745 | 434 | return PROC_RETURNED_DONE; |
6d2010ae A |
435 | } |
436 | ||
437 | static int | |
438 | audit_session_debug_filterfn(proc_t p, void *st) | |
439 | { | |
0a7de745 | 440 | kauth_cred_t cred = p->p_ucred; |
6d2010ae A |
441 | auditinfo_addr_t *aia_p = cred->cr_audit.as_aia_p; |
442 | au_sentry_debug_t *sed_tab = (au_sentry_debug_t *) st; | |
443 | au_sentry_debug_t *sdtp; | |
444 | au_sentry_t *se; | |
445 | ||
446 | if (IS_VALID_SESSION(aia_p)) { | |
447 | sdtp = &sed_tab[0]; | |
448 | do { | |
449 | if (aia_p->ai_asid == sdtp->se_asid) { | |
450 | sdtp->se_ptcnt++; | |
451 | ||
452 | /* Do some santy checks. */ | |
453 | se = AU_SENTRY_PTR(aia_p); | |
454 | if (se->se_refcnt != sdtp->se_refcnt) { | |
455 | sdtp->se_refcnt = | |
456 | (int64_t)se->se_refcnt; | |
457 | } | |
458 | if (se->se_procnt != sdtp->se_procnt) { | |
459 | sdtp->se_procnt = | |
460 | (int64_t)se->se_procnt; | |
461 | } | |
462 | break; | |
463 | } | |
464 | sdtp++; | |
465 | } while (sdtp->se_asid != 0 && sdtp->se_auid != 0); | |
466 | } else { | |
467 | /* add it to the default sesison */ | |
468 | sed_tab->se_ptcnt++; | |
469 | } | |
470 | ||
0a7de745 | 471 | return 0; |
6d2010ae A |
472 | } |
473 | ||
474 | /* | |
475 | * Copy out the session debug info via the sysctl interface. | |
b0d623f7 | 476 | * |
b0d623f7 A |
477 | */ |
478 | static int | |
479 | audit_sysctl_session_debug(__unused struct sysctl_oid *oidp, | |
480 | __unused void *arg1, __unused int arg2, struct sysctl_req *req) | |
481 | { | |
482 | au_sentry_t *se; | |
483 | au_sentry_debug_t *sed_tab, *next_sed; | |
484 | int i, entry_cnt = 0; | |
485 | size_t sz; | |
486 | int err = 0; | |
487 | ||
488 | /* | |
489 | * This provides a read-only node. | |
490 | */ | |
0a7de745 A |
491 | if (req->newptr != USER_ADDR_NULL) { |
492 | return EPERM; | |
493 | } | |
b0d623f7 A |
494 | |
495 | /* | |
496 | * Walk the audit session hash table to determine the size. | |
497 | */ | |
498 | AUDIT_SENTRY_RLOCK(); | |
0a7de745 | 499 | for (i = 0; i < HASH_TABLE_SIZE; i++) { |
b0d623f7 | 500 | LIST_FOREACH(se, &au_sentry_bucket[i], se_link) |
0a7de745 A |
501 | if (se != NULL) { |
502 | entry_cnt++; | |
503 | } | |
504 | } | |
b0d623f7 | 505 | |
6d2010ae | 506 | entry_cnt++; /* add one for the default entry */ |
b0d623f7 | 507 | /* |
0a7de745 | 508 | * If just querying then return the space required. There is an |
b0d623f7 A |
509 | * obvious race condition here so we just fudge this by 3 in case |
510 | * the audit session table grows. | |
511 | */ | |
512 | if (req->oldptr == USER_ADDR_NULL) { | |
513 | req->oldidx = (entry_cnt + 3) * sizeof(au_sentry_debug_t); | |
514 | AUDIT_SENTRY_RUNLOCK(); | |
0a7de745 | 515 | return 0; |
b0d623f7 A |
516 | } |
517 | ||
518 | /* | |
519 | * Alloc a temporary buffer. | |
520 | */ | |
521 | if (req->oldlen < (entry_cnt * sizeof(au_sentry_debug_t))) { | |
522 | AUDIT_SENTRY_RUNLOCK(); | |
0a7de745 | 523 | return ENOMEM; |
b0d623f7 A |
524 | } |
525 | /* | |
526 | * We hold the lock over the alloc since we don't want the table to | |
527 | * grow on us. Therefore, use the non-blocking version of kalloc(). | |
528 | */ | |
f427ee49 A |
529 | sed_tab = (au_sentry_debug_t *)kheap_alloc(KHEAP_TEMP, |
530 | entry_cnt * sizeof(au_sentry_debug_t), Z_NOWAIT | Z_ZERO); | |
b0d623f7 A |
531 | if (sed_tab == NULL) { |
532 | AUDIT_SENTRY_RUNLOCK(); | |
0a7de745 | 533 | return ENOMEM; |
b0d623f7 | 534 | } |
b0d623f7 A |
535 | |
536 | /* | |
537 | * Walk the audit session hash table and build the record array. | |
538 | */ | |
539 | sz = 0; | |
540 | next_sed = sed_tab; | |
6d2010ae | 541 | /* add the first entry for processes not tracked in sessions. */ |
0a7de745 | 542 | bcopy(audit_default_aia_p, &next_sed->se_auinfo, sizeof(au_sentry_t)); |
6d2010ae A |
543 | next_sed->se_refcnt = (int64_t)audit_default_se.se_refcnt; |
544 | next_sed->se_procnt = (int64_t)audit_default_se.se_procnt; | |
545 | next_sed++; | |
546 | sz += sizeof(au_sentry_debug_t); | |
0a7de745 | 547 | for (i = 0; i < HASH_TABLE_SIZE; i++) { |
b0d623f7 A |
548 | LIST_FOREACH(se, &au_sentry_bucket[i], se_link) { |
549 | if (se != NULL) { | |
6d2010ae A |
550 | next_sed->se_auinfo = se->se_auinfo; |
551 | next_sed->se_refcnt = (int64_t)se->se_refcnt; | |
552 | next_sed->se_procnt = (int64_t)se->se_procnt; | |
b0d623f7 A |
553 | next_sed++; |
554 | sz += sizeof(au_sentry_debug_t); | |
555 | } | |
556 | } | |
557 | } | |
558 | AUDIT_SENTRY_RUNLOCK(); | |
559 | ||
6d2010ae | 560 | /* Reconcile with the process table. */ |
cb323159 | 561 | proc_iterate(PROC_ALLPROCLIST | PROC_ZOMBPROCLIST, |
6d2010ae A |
562 | audit_session_debug_callout, NULL, |
563 | audit_session_debug_filterfn, (void *)&sed_tab[0]); | |
564 | ||
565 | ||
b0d623f7 A |
566 | req->oldlen = sz; |
567 | err = SYSCTL_OUT(req, sed_tab, sz); | |
f427ee49 | 568 | kheap_free(KHEAP_TEMP, sed_tab, entry_cnt * sizeof(au_sentry_debug_t)); |
b0d623f7 | 569 | |
0a7de745 | 570 | return err; |
b0d623f7 A |
571 | } |
572 | ||
573 | #endif /* AUDIT_SESSION_DEBUG */ | |
574 | ||
6d2010ae A |
575 | /* |
576 | * Create and commit a session audit event. The proc and se arguments needs to | |
577 | * be that of the subject and not necessarily the current process. | |
578 | */ | |
579 | static void | |
580 | audit_session_event(int event, auditinfo_addr_t *aia_p) | |
581 | { | |
582 | struct kaudit_record *ar; | |
583 | ||
584 | KASSERT(AUE_SESSION_START == event || AUE_SESSION_UPDATE == event || | |
585 | AUE_SESSION_END == event || AUE_SESSION_CLOSE == event, | |
586 | ("audit_session_event: invalid event: %d", event)); | |
587 | ||
0a7de745 | 588 | if (NULL == aia_p) { |
6d2010ae | 589 | return; |
0a7de745 | 590 | } |
6d2010ae | 591 | |
0a7de745 | 592 | /* |
6d2010ae | 593 | * Create a new audit record. The record will contain the subject |
0a7de745 | 594 | * ruid, rgid, egid, pid, auid, asid, amask, and term_addr |
6d2010ae A |
595 | * (implicitly added by audit_new). |
596 | */ | |
597 | ar = audit_new(event, PROC_NULL, /* Not used */ NULL); | |
0a7de745 | 598 | if (NULL == ar) { |
6d2010ae | 599 | return; |
0a7de745 | 600 | } |
6d2010ae A |
601 | |
602 | /* | |
603 | * Audit session events are always generated because they are used | |
604 | * by some userland consumers so just set the preselect flag. | |
605 | */ | |
606 | ar->k_ar_commit |= AR_PRESELECT_FILTER; | |
607 | ||
0a7de745 | 608 | /* |
6d2010ae A |
609 | * Populate the subject information. Note that the ruid, rgid, |
610 | * egid, and pid values are incorrect. We only need the auditinfo_addr | |
611 | * information. | |
612 | */ | |
613 | ar->k_ar.ar_subj_ruid = 0; | |
614 | ar->k_ar.ar_subj_rgid = 0; | |
615 | ar->k_ar.ar_subj_egid = 0; | |
616 | ar->k_ar.ar_subj_pid = 0; | |
617 | ar->k_ar.ar_subj_auid = aia_p->ai_auid; | |
618 | ar->k_ar.ar_subj_asid = aia_p->ai_asid; | |
619 | bcopy(&aia_p->ai_termid, &ar->k_ar.ar_subj_term_addr, | |
620 | sizeof(struct au_tid_addr)); | |
621 | ||
622 | /* Add the audit masks to the record. */ | |
623 | ar->k_ar.ar_arg_amask.am_success = aia_p->ai_mask.am_success; | |
624 | ar->k_ar.ar_arg_amask.am_failure = aia_p->ai_mask.am_failure; | |
625 | ARG_SET_VALID(ar, ARG_AMASK); | |
626 | ||
627 | /* Add the audit session flags to the record. */ | |
0a7de745 | 628 | ar->k_ar.ar_arg_value64 = aia_p->ai_flags; |
6d2010ae A |
629 | ARG_SET_VALID(ar, ARG_VALUE64); |
630 | ||
631 | ||
632 | /* Commit the record to the queue. */ | |
633 | audit_commit(ar, 0, 0); | |
634 | } | |
635 | ||
b0d623f7 A |
636 | /* |
637 | * Hash the audit session ID using a simple 32-bit mix. | |
638 | */ | |
0a7de745 | 639 | static inline uint32_t |
b0d623f7 A |
640 | audit_session_hash(au_asid_t asid) |
641 | { | |
642 | uint32_t a = (uint32_t) asid; | |
643 | ||
0a7de745 | 644 | a = (a - (a << 6)) ^ (a >> 17); |
b0d623f7 A |
645 | a = (a - (a << 9)) ^ (a << 4); |
646 | a = (a - (a << 3)) ^ (a << 10); | |
647 | a = a ^ (a >> 15); | |
0a7de745 A |
648 | |
649 | return a; | |
b0d623f7 A |
650 | } |
651 | ||
652 | /* | |
653 | * Do an hash lookup and find the session entry for a given ASID. Return NULL | |
0a7de745 A |
654 | * if not found. If the session is found then audit_session_find takes a |
655 | * reference. | |
b0d623f7 A |
656 | */ |
657 | static au_sentry_t * | |
658 | audit_session_find(au_asid_t asid) | |
659 | { | |
0a7de745 A |
660 | uint32_t hkey; |
661 | au_sentry_t *found_se; | |
b0d623f7 A |
662 | |
663 | AUDIT_SENTRY_RWLOCK_ASSERT(); | |
664 | ||
665 | hkey = HASH_ASID(asid); | |
666 | ||
667 | LIST_FOREACH(found_se, &au_sentry_bucket[hkey], se_link) | |
0a7de745 A |
668 | if (found_se->se_asid == asid) { |
669 | au_history_record(found_se, AU_HISTORY_EVENT_FIND); | |
670 | audit_ref_session(found_se); | |
671 | return found_se; | |
672 | } | |
673 | return NULL; | |
b0d623f7 A |
674 | } |
675 | ||
b0d623f7 A |
676 | /* |
677 | * Remove the given audit_session entry from the hash table. | |
678 | */ | |
679 | static void | |
680 | audit_session_remove(au_sentry_t *se) | |
681 | { | |
0a7de745 A |
682 | uint32_t hkey; |
683 | au_sentry_t *found_se, *tmp_se; | |
b0d623f7 | 684 | |
6d2010ae | 685 | au_history_record(se, AU_HISTORY_EVENT_DEATH); |
0a7de745 | 686 | KASSERT(se->se_refcnt == 0, ("audit_session_remove: ref count != 0")); |
6d2010ae | 687 | KASSERT(se != &audit_default_se, |
0a7de745 | 688 | ("audit_session_remove: removing default session")); |
b0d623f7 A |
689 | |
690 | hkey = HASH_ASID(se->se_asid); | |
691 | ||
692 | AUDIT_SENTRY_WLOCK(); | |
6d2010ae A |
693 | /* |
694 | * Check and see if someone got a reference before we got the lock. | |
695 | */ | |
696 | if (se->se_refcnt != 0) { | |
697 | AUDIT_SENTRY_WUNLOCK(); | |
698 | return; | |
699 | } | |
700 | ||
701 | audit_session_portdestroy(&se->se_port); | |
b0d623f7 A |
702 | LIST_FOREACH_SAFE(found_se, &au_sentry_bucket[hkey], se_link, tmp_se) { |
703 | if (found_se == se) { | |
6d2010ae A |
704 | /* |
705 | * Generate an audit event to notify userland of the | |
706 | * session close. | |
707 | */ | |
708 | audit_session_event(AUE_SESSION_CLOSE, | |
709 | &found_se->se_auinfo); | |
b0d623f7 A |
710 | |
711 | LIST_REMOVE(found_se, se_link); | |
712 | AUDIT_SENTRY_WUNLOCK(); | |
b0d623f7 A |
713 | free(found_se, M_AU_SESSION); |
714 | ||
715 | return; | |
716 | } | |
717 | } | |
718 | AUDIT_SENTRY_WUNLOCK(); | |
719 | } | |
720 | ||
721 | /* | |
722 | * Reference the session by incrementing the sentry ref count. | |
723 | */ | |
724 | static void | |
725 | audit_ref_session(au_sentry_t *se) | |
726 | { | |
727 | long old_val; | |
728 | ||
0a7de745 | 729 | if (se == NULL || se == &audit_default_se) { |
6d2010ae | 730 | return; |
0a7de745 | 731 | } |
6d2010ae A |
732 | |
733 | au_history_record(se, AU_HISTORY_EVENT_REF); | |
734 | ||
b0d623f7 A |
735 | old_val = OSAddAtomicLong(1, &se->se_refcnt); |
736 | KASSERT(old_val < 100000, | |
737 | ("audit_ref_session: Too many references on session.")); | |
738 | } | |
739 | ||
740 | /* | |
741 | * Decrement the sentry ref count and remove the session entry if last one. | |
742 | */ | |
743 | static void | |
744 | audit_unref_session(au_sentry_t *se) | |
745 | { | |
746 | long old_val; | |
747 | ||
0a7de745 | 748 | if (se == NULL || se == &audit_default_se) { |
6d2010ae | 749 | return; |
0a7de745 | 750 | } |
6d2010ae A |
751 | |
752 | au_history_record(se, AU_HISTORY_EVENT_UNREF); | |
753 | ||
b0d623f7 | 754 | old_val = OSAddAtomicLong(-1, &se->se_refcnt); |
0a7de745 | 755 | if (old_val == 1) { |
b0d623f7 | 756 | audit_session_remove(se); |
0a7de745 | 757 | } |
b0d623f7 A |
758 | KASSERT(old_val > 0, |
759 | ("audit_unref_session: Too few references on session.")); | |
760 | } | |
761 | ||
762 | /* | |
763 | * Increment the process count in the session. | |
764 | */ | |
765 | static void | |
766 | audit_inc_procount(au_sentry_t *se) | |
767 | { | |
768 | long old_val; | |
769 | ||
0a7de745 | 770 | if (se == NULL || se == &audit_default_se) { |
6d2010ae | 771 | return; |
0a7de745 A |
772 | } |
773 | ||
b0d623f7 A |
774 | old_val = OSAddAtomicLong(1, &se->se_procnt); |
775 | KASSERT(old_val <= PID_MAX, | |
776 | ("audit_inc_procount: proc count > PID_MAX")); | |
777 | } | |
778 | ||
779 | /* | |
780 | * Decrement the process count and add a knote if it is the last process | |
781 | * to exit the session. | |
782 | */ | |
783 | static void | |
784 | audit_dec_procount(au_sentry_t *se) | |
785 | { | |
786 | long old_val; | |
787 | ||
0a7de745 | 788 | if (se == NULL || se == &audit_default_se) { |
6d2010ae | 789 | return; |
0a7de745 | 790 | } |
6d2010ae | 791 | |
b0d623f7 | 792 | old_val = OSAddAtomicLong(-1, &se->se_procnt); |
6d2010ae A |
793 | /* |
794 | * If this was the last process generate an audit event to notify | |
795 | * userland of the session ending. | |
796 | */ | |
0a7de745 | 797 | if (old_val == 1) { |
6d2010ae | 798 | audit_session_event(AUE_SESSION_END, &se->se_auinfo); |
0a7de745 | 799 | } |
b0d623f7 A |
800 | KASSERT(old_val >= 1, |
801 | ("audit_dec_procount: proc count < 0")); | |
0a7de745 | 802 | } |
b0d623f7 A |
803 | |
804 | /* | |
805 | * Update the session entry and check to see if anything was updated. | |
806 | * Returns: | |
0a7de745 | 807 | * 0 Nothing was updated (We don't care about process preselection masks) |
b0d623f7 A |
808 | * 1 Something was updated. |
809 | */ | |
810 | static int | |
811 | audit_update_sentry(au_sentry_t *se, auditinfo_addr_t *new_aia) | |
812 | { | |
813 | auditinfo_addr_t *aia = &se->se_auinfo; | |
814 | int update; | |
815 | ||
0a7de745 A |
816 | KASSERT(new_aia != audit_default_aia_p, |
817 | ("audit_update_sentry: Trying to update the default aia.")); | |
b0d623f7 A |
818 | |
819 | update = (aia->ai_auid != new_aia->ai_auid || | |
820 | bcmp(&aia->ai_termid, &new_aia->ai_termid, | |
0a7de745 | 821 | sizeof(new_aia->ai_termid)) || |
b0d623f7 A |
822 | aia->ai_flags != new_aia->ai_flags); |
823 | ||
0a7de745 | 824 | if (update) { |
b0d623f7 | 825 | bcopy(new_aia, aia, sizeof(*aia)); |
0a7de745 | 826 | } |
b0d623f7 | 827 | |
0a7de745 | 828 | return update; |
b0d623f7 A |
829 | } |
830 | ||
831 | /* | |
832 | * Return the next session ID. The range of kernel generated audit session IDs | |
833 | * is ASSIGNED_ASID_MIN to ASSIGNED_ASID_MAX. | |
834 | */ | |
0a7de745 | 835 | static uint32_t |
b0d623f7 A |
836 | audit_session_nextid(void) |
837 | { | |
0a7de745 | 838 | static uint32_t next_asid = ASSIGNED_ASID_MIN; |
b0d623f7 A |
839 | |
840 | AUDIT_SENTRY_RWLOCK_ASSERT(); | |
841 | ||
0a7de745 | 842 | if (next_asid > ASSIGNED_ASID_MAX) { |
b0d623f7 | 843 | next_asid = ASSIGNED_ASID_MIN; |
0a7de745 | 844 | } |
b0d623f7 | 845 | |
0a7de745 | 846 | return next_asid++; |
b0d623f7 A |
847 | } |
848 | ||
849 | /* | |
850 | * Allocated a new audit_session entry and add it to the hash table. If the | |
851 | * given ASID is set to AU_ASSIGN_ASID then audit_session_new() will pick an | |
852 | * audit session ID. Otherwise, it attempts use the one given. It creates a | |
853 | * reference to the entry that must be unref'ed. | |
854 | */ | |
855 | static auditinfo_addr_t * | |
6d2010ae | 856 | audit_session_new(auditinfo_addr_t *new_aia_p, auditinfo_addr_t *old_aia_p) |
b0d623f7 | 857 | { |
6d2010ae | 858 | au_asid_t new_asid; |
b0d623f7 | 859 | au_sentry_t *se = NULL; |
6d2010ae | 860 | au_sentry_t *found_se = NULL; |
b0d623f7 | 861 | auditinfo_addr_t *aia = NULL; |
0a7de745 | 862 | |
6d2010ae | 863 | KASSERT(new_aia_p != NULL, ("audit_session_new: new_aia_p == NULL")); |
b0d623f7 | 864 | |
0a7de745 | 865 | new_asid = new_aia_p->ai_asid; |
b0d623f7 | 866 | |
b0d623f7 A |
867 | /* |
868 | * Alloc a new session entry now so we don't wait holding the lock. | |
869 | */ | |
870 | se = malloc(sizeof(au_sentry_t), M_AU_SESSION, M_WAITOK | M_ZERO); | |
871 | ||
b0d623f7 A |
872 | /* |
873 | * Find an unique session ID, if desired. | |
874 | */ | |
875 | AUDIT_SENTRY_WLOCK(); | |
6d2010ae | 876 | if (new_asid == AU_ASSIGN_ASID) { |
b0d623f7 | 877 | do { |
6d2010ae A |
878 | new_asid = (au_asid_t)audit_session_nextid(); |
879 | found_se = audit_session_find(new_asid); | |
0a7de745 A |
880 | |
881 | /* | |
6d2010ae A |
882 | * If the session ID is currently active then drop the |
883 | * reference and try again. | |
884 | */ | |
0a7de745 | 885 | if (found_se != NULL) { |
6d2010ae | 886 | audit_unref_session(found_se); |
0a7de745 | 887 | } else { |
6d2010ae | 888 | break; |
0a7de745 A |
889 | } |
890 | } while (1); | |
b0d623f7 | 891 | } else { |
b0d623f7 A |
892 | /* |
893 | * Check to see if the requested ASID is already in the | |
894 | * hash table. If so, update it with the new auditinfo. | |
0a7de745 | 895 | */ |
6d2010ae | 896 | if ((found_se = audit_session_find(new_asid)) != NULL) { |
b0d623f7 A |
897 | int updated; |
898 | ||
6d2010ae | 899 | updated = audit_update_sentry(found_se, new_aia_p); |
b0d623f7 A |
900 | |
901 | AUDIT_SENTRY_WUNLOCK(); | |
b0d623f7 A |
902 | free(se, M_AU_SESSION); |
903 | ||
6d2010ae | 904 | /* If a different session then add this process in. */ |
0a7de745 | 905 | if (new_aia_p != old_aia_p) { |
6d2010ae | 906 | audit_inc_procount(found_se); |
0a7de745 | 907 | } |
b0d623f7 A |
908 | |
909 | /* | |
6d2010ae A |
910 | * If the session information was updated then |
911 | * generate an audit event to notify userland. | |
b0d623f7 | 912 | */ |
0a7de745 | 913 | if (updated) { |
6d2010ae A |
914 | audit_session_event(AUE_SESSION_UPDATE, |
915 | &found_se->se_auinfo); | |
0a7de745 | 916 | } |
b0d623f7 | 917 | |
0a7de745 | 918 | return &found_se->se_auinfo; |
b0d623f7 A |
919 | } |
920 | } | |
921 | ||
922 | /* | |
923 | * Start the reference and proc count at 1 to account for the process | |
924 | * that invoked this via setaudit_addr() (or friends). | |
925 | */ | |
926 | se->se_refcnt = se->se_procnt = 1; | |
927 | ||
928 | /* | |
929 | * Populate the new session entry. Note that process masks are stored | |
930 | * in kauth ucred so just zero them here. | |
931 | */ | |
932 | se->se_port = IPC_PORT_NULL; | |
933 | aia = &se->se_auinfo; | |
6d2010ae A |
934 | aia->ai_asid = new_asid; |
935 | aia->ai_auid = new_aia_p->ai_auid; | |
936 | bzero(&new_aia_p->ai_mask, sizeof(new_aia_p->ai_mask)); | |
937 | bcopy(&new_aia_p->ai_termid, &aia->ai_termid, sizeof(aia->ai_termid)); | |
938 | aia->ai_flags = new_aia_p->ai_flags; | |
b0d623f7 A |
939 | |
940 | /* | |
941 | * Add it to the hash table. | |
942 | */ | |
6d2010ae | 943 | LIST_INSERT_HEAD(&au_sentry_bucket[HASH_ASID(new_asid)], se, se_link); |
b0d623f7 A |
944 | AUDIT_SENTRY_WUNLOCK(); |
945 | ||
946 | /* | |
6d2010ae | 947 | * Generate an audit event to notify userland of the new session. |
b0d623f7 | 948 | */ |
6d2010ae A |
949 | audit_session_event(AUE_SESSION_START, aia); |
950 | au_history_record(se, AU_HISTORY_EVENT_BIRTH); | |
0a7de745 | 951 | return aia; |
b0d623f7 A |
952 | } |
953 | ||
954 | /* | |
955 | * Lookup an existing session. A copy of the audit session info for a given | |
956 | * ASID is returned in ret_aia. Returns 0 on success. | |
957 | */ | |
958 | int | |
959 | audit_session_lookup(au_asid_t asid, auditinfo_addr_t *ret_aia) | |
960 | { | |
961 | au_sentry_t *se = NULL; | |
962 | ||
0a7de745 A |
963 | if ((uint32_t)asid > ASSIGNED_ASID_MAX) { |
964 | return -1; | |
965 | } | |
b0d623f7 A |
966 | AUDIT_SENTRY_RLOCK(); |
967 | if ((se = audit_session_find(asid)) == NULL) { | |
968 | AUDIT_SENTRY_RUNLOCK(); | |
0a7de745 | 969 | return 1; |
b0d623f7 | 970 | } |
6d2010ae A |
971 | /* We have a reference on the session so it is safe to drop the lock. */ |
972 | AUDIT_SENTRY_RUNLOCK(); | |
0a7de745 | 973 | if (ret_aia != NULL) { |
b0d623f7 | 974 | bcopy(&se->se_auinfo, ret_aia, sizeof(*ret_aia)); |
0a7de745 | 975 | } |
6d2010ae | 976 | audit_unref_session(se); |
b0d623f7 | 977 | |
0a7de745 | 978 | return 0; |
b0d623f7 A |
979 | } |
980 | ||
6d2010ae A |
981 | void |
982 | audit_session_aiaref(auditinfo_addr_t *aia_p) | |
983 | { | |
6d2010ae A |
984 | audit_ref_session(AU_SENTRY_PTR(aia_p)); |
985 | } | |
0a7de745 | 986 | |
b0d623f7 A |
987 | /* |
988 | * Add a reference to the session entry. | |
989 | */ | |
990 | void | |
991 | audit_session_ref(kauth_cred_t cred) | |
992 | { | |
993 | auditinfo_addr_t *aia_p; | |
994 | ||
995 | KASSERT(IS_VALID_CRED(cred), | |
996 | ("audit_session_ref: Invalid kauth_cred.")); | |
997 | ||
0a7de745 | 998 | aia_p = cred->cr_audit.as_aia_p; |
6d2010ae A |
999 | audit_session_aiaref(aia_p); |
1000 | } | |
1001 | ||
0a7de745 A |
1002 | void |
1003 | audit_session_aiaunref(auditinfo_addr_t *aia_p) | |
6d2010ae | 1004 | { |
6d2010ae | 1005 | audit_unref_session(AU_SENTRY_PTR(aia_p)); |
b0d623f7 A |
1006 | } |
1007 | ||
0a7de745 | 1008 | /* |
b0d623f7 A |
1009 | * Remove a reference to the session entry. |
1010 | */ | |
1011 | void | |
1012 | audit_session_unref(kauth_cred_t cred) | |
1013 | { | |
1014 | auditinfo_addr_t *aia_p; | |
1015 | ||
1016 | KASSERT(IS_VALID_CRED(cred), | |
1017 | ("audit_session_unref: Invalid kauth_cred.")); | |
1018 | ||
0a7de745 | 1019 | aia_p = cred->cr_audit.as_aia_p; |
6d2010ae | 1020 | audit_session_aiaunref(aia_p); |
b0d623f7 A |
1021 | } |
1022 | ||
6d2010ae A |
1023 | /* |
1024 | * Increment the per audit session process count. Assumes that the caller has | |
1025 | * a reference on the process' cred. | |
1026 | */ | |
b0d623f7 | 1027 | void |
6d2010ae | 1028 | audit_session_procnew(proc_t p) |
b0d623f7 | 1029 | { |
6d2010ae | 1030 | kauth_cred_t cred = p->p_ucred; |
b0d623f7 | 1031 | auditinfo_addr_t *aia_p; |
0a7de745 A |
1032 | |
1033 | KASSERT(IS_VALID_CRED(cred), | |
b0d623f7 A |
1034 | ("audit_session_procnew: Invalid kauth_cred.")); |
1035 | ||
0a7de745 | 1036 | aia_p = cred->cr_audit.as_aia_p; |
b0d623f7 | 1037 | |
6d2010ae | 1038 | audit_inc_procount(AU_SENTRY_PTR(aia_p)); |
b0d623f7 A |
1039 | } |
1040 | ||
6d2010ae A |
1041 | /* |
1042 | * Decrement the per audit session process count. Assumes that the caller has | |
1043 | * a reference on the cred. | |
1044 | */ | |
b0d623f7 | 1045 | void |
6d2010ae | 1046 | audit_session_procexit(proc_t p) |
b0d623f7 | 1047 | { |
6d2010ae | 1048 | kauth_cred_t cred = p->p_ucred; |
b0d623f7 A |
1049 | auditinfo_addr_t *aia_p; |
1050 | ||
0a7de745 | 1051 | KASSERT(IS_VALID_CRED(cred), |
b0d623f7 A |
1052 | ("audit_session_procexit: Invalid kauth_cred.")); |
1053 | ||
0a7de745 | 1054 | aia_p = cred->cr_audit.as_aia_p; |
b0d623f7 | 1055 | |
6d2010ae | 1056 | audit_dec_procount(AU_SENTRY_PTR(aia_p)); |
b0d623f7 A |
1057 | } |
1058 | ||
1059 | /* | |
0a7de745 | 1060 | * Init the audit session code. |
b0d623f7 A |
1061 | */ |
1062 | void | |
1063 | audit_session_init(void) | |
1064 | { | |
1065 | int i; | |
1066 | ||
1067 | KASSERT((ASSIGNED_ASID_MAX - ASSIGNED_ASID_MIN) > PID_MAX, | |
1068 | ("audit_session_init: ASSIGNED_ASID_MAX is not large enough.")); | |
0a7de745 | 1069 | |
b0d623f7 | 1070 | AUDIT_SENTRY_RWLOCK_INIT(); |
b0d623f7 A |
1071 | |
1072 | au_sentry_bucket = malloc( sizeof(struct au_sentry) * | |
1073 | HASH_TABLE_SIZE, M_AU_SESSION, M_WAITOK | M_ZERO); | |
1074 | ||
0a7de745 | 1075 | for (i = 0; i < HASH_TABLE_SIZE; i++) { |
b0d623f7 | 1076 | LIST_INIT(&au_sentry_bucket[i]); |
0a7de745 | 1077 | } |
b0d623f7 | 1078 | |
6d2010ae A |
1079 | (void)audit_sdev_init(); |
1080 | #if AU_HISTORY_LOGGING | |
1081 | au_history = malloc(sizeof(struct au_history) * au_history_size, | |
0a7de745 | 1082 | M_AU_SESSION, M_WAITOK | M_ZERO); |
6d2010ae | 1083 | #endif |
b0d623f7 A |
1084 | } |
1085 | ||
6d2010ae A |
1086 | static int |
1087 | audit_session_update_check(kauth_cred_t cred, auditinfo_addr_t *old, | |
1088 | auditinfo_addr_t *new) | |
b0d623f7 | 1089 | { |
6d2010ae A |
1090 | uint64_t n; |
1091 | ||
1092 | /* If the current audit ID is not the default then it is immutable. */ | |
0a7de745 A |
1093 | if (old->ai_auid != AU_DEFAUDITID && old->ai_auid != new->ai_auid) { |
1094 | return EINVAL; | |
1095 | } | |
6d2010ae A |
1096 | |
1097 | /* If the current termid is not the default then it is immutable. */ | |
1098 | if ((old->ai_termid.at_type != AU_IPv4 || | |
0a7de745 A |
1099 | old->ai_termid.at_port != 0 || |
1100 | old->ai_termid.at_addr[0] != 0) && | |
6d2010ae | 1101 | (old->ai_termid.at_port != new->ai_termid.at_port || |
0a7de745 A |
1102 | old->ai_termid.at_type != new->ai_termid.at_type || |
1103 | 0 != bcmp(&old->ai_termid.at_addr, &new->ai_termid.at_addr, | |
1104 | sizeof(old->ai_termid.at_addr)))) { | |
1105 | return EINVAL; | |
1106 | } | |
6d2010ae A |
1107 | |
1108 | /* The flags may be set only according to the | |
1109 | * audit_session_*_set_sflags_masks. | |
b0d623f7 | 1110 | */ |
6d2010ae A |
1111 | n = ~old->ai_flags & new->ai_flags; |
1112 | if (0 != n && | |
1113 | !((n == (audit_session_superuser_set_sflags_mask & n) && | |
0a7de745 A |
1114 | kauth_cred_issuser(cred)) || |
1115 | (n == (audit_session_member_set_sflags_mask & n) && | |
1116 | old->ai_asid == new->ai_asid))) { | |
1117 | return EINVAL; | |
1118 | } | |
6d2010ae A |
1119 | |
1120 | /* The flags may be cleared only according to the | |
1121 | * audit_session_*_clear_sflags_masks. | |
b0d623f7 | 1122 | */ |
6d2010ae A |
1123 | n = ~new->ai_flags & old->ai_flags; |
1124 | if (0 != n && | |
1125 | !((n == (audit_session_superuser_clear_sflags_mask & n) && | |
0a7de745 A |
1126 | kauth_cred_issuser(cred)) || |
1127 | (n == (audit_session_member_clear_sflags_mask & n) && | |
1128 | old->ai_asid == new->ai_asid))) { | |
1129 | return EINVAL; | |
1130 | } | |
6d2010ae A |
1131 | |
1132 | /* The audit masks are mutable. */ | |
0a7de745 | 1133 | return 0; |
b0d623f7 A |
1134 | } |
1135 | ||
1136 | /* | |
0a7de745 | 1137 | * Safely update kauth cred of the given process with new the given audit info. |
b0d623f7 | 1138 | */ |
6d2010ae A |
1139 | int |
1140 | audit_session_setaia(proc_t p, auditinfo_addr_t *new_aia_p) | |
b0d623f7 | 1141 | { |
6d2010ae A |
1142 | kauth_cred_t my_cred, my_new_cred; |
1143 | struct au_session as; | |
1144 | struct au_session tmp_as; | |
1145 | auditinfo_addr_t caia, *old_aia_p; | |
1146 | int ret; | |
b0d623f7 | 1147 | |
6d2010ae A |
1148 | /* |
1149 | * If this is going to modify an existing session then do some | |
1150 | * immutable checks. | |
1151 | */ | |
1152 | if (audit_session_lookup(new_aia_p->ai_asid, &caia) == 0) { | |
1153 | my_cred = kauth_cred_proc_ref(p); | |
1154 | ret = audit_session_update_check(my_cred, &caia, new_aia_p); | |
1155 | kauth_cred_unref(&my_cred); | |
0a7de745 A |
1156 | if (ret) { |
1157 | return ret; | |
1158 | } | |
6d2010ae | 1159 | } |
b0d623f7 | 1160 | |
6d2010ae A |
1161 | my_cred = kauth_cred_proc_ref(p); |
1162 | bcopy(&new_aia_p->ai_mask, &as.as_mask, sizeof(as.as_mask)); | |
1163 | old_aia_p = my_cred->cr_audit.as_aia_p; | |
1164 | /* audit_session_new() adds a reference on the session */ | |
1165 | as.as_aia_p = audit_session_new(new_aia_p, old_aia_p); | |
b0d623f7 | 1166 | |
6d2010ae | 1167 | /* If the process left a session then update the process count. */ |
0a7de745 | 1168 | if (old_aia_p != new_aia_p) { |
6d2010ae | 1169 | audit_dec_procount(AU_SENTRY_PTR(old_aia_p)); |
0a7de745 | 1170 | } |
b0d623f7 | 1171 | |
b0d623f7 A |
1172 | |
1173 | /* | |
6d2010ae A |
1174 | * We are modifying the audit info in a credential so we need a new |
1175 | * credential (or take another reference on an existing credential that | |
1176 | * matches our new one). We must do this because the audit info in the | |
1177 | * credential is used as part of our hash key. Get current credential | |
1178 | * in the target process and take a reference while we muck with it. | |
b0d623f7 | 1179 | */ |
6d2010ae | 1180 | for (;;) { |
b0d623f7 A |
1181 | /* |
1182 | * Set the credential with new info. If there is no change, | |
1183 | * we get back the same credential we passed in; if there is | |
1184 | * a change, we drop the reference on the credential we | |
1185 | * passed in. The subsequent compare is safe, because it is | |
1186 | * a pointer compare rather than a contents compare. | |
1187 | */ | |
1188 | bcopy(&as, &tmp_as, sizeof(tmp_as)); | |
1189 | my_new_cred = kauth_cred_setauditinfo(my_cred, &tmp_as); | |
1190 | ||
1191 | if (my_cred != my_new_cred) { | |
4bd07ac2 | 1192 | proc_ucred_lock(p); |
b0d623f7 A |
1193 | /* Need to protect for a race where another thread also |
1194 | * changed the credential after we took our reference. | |
1195 | * If p_ucred has changed then we should restart this | |
1196 | * again with the new cred. | |
1197 | */ | |
1198 | if (p->p_ucred != my_cred) { | |
4bd07ac2 | 1199 | proc_ucred_unlock(p); |
b0d623f7 A |
1200 | audit_session_unref(my_new_cred); |
1201 | kauth_cred_unref(&my_new_cred); | |
1202 | /* try again */ | |
1203 | my_cred = kauth_cred_proc_ref(p); | |
1204 | continue; | |
1205 | } | |
1206 | p->p_ucred = my_new_cred; | |
6d2010ae A |
1207 | /* update cred on proc */ |
1208 | PROC_UPDATE_CREDS_ONPROC(p); | |
4bd07ac2 | 1209 | proc_ucred_unlock(p); |
b0d623f7 A |
1210 | } |
1211 | /* | |
1212 | * Drop old proc reference or our extra reference. | |
1213 | */ | |
1214 | kauth_cred_unref(&my_cred); | |
1215 | break; | |
1216 | } | |
b0d623f7 | 1217 | |
6d2010ae A |
1218 | /* Drop the reference taken by audit_session_new() above. */ |
1219 | audit_unref_session(AU_SENTRY_PTR(as.as_aia_p)); | |
1220 | ||
1221 | /* Propagate the change from the process to the Mach task. */ | |
b0d623f7 A |
1222 | set_security_token(p); |
1223 | ||
0a7de745 | 1224 | return 0; |
b0d623f7 A |
1225 | } |
1226 | ||
1227 | /* | |
1228 | * audit_session_self (system call) | |
1229 | * | |
0a7de745 | 1230 | * Description: Obtain a Mach send right for the current session. |
b0d623f7 A |
1231 | * |
1232 | * Parameters: p Process calling audit_session_self(). | |
0a7de745 | 1233 | * |
b0d623f7 | 1234 | * Returns: *ret_port Named Mach send right, which may be |
0a7de745 | 1235 | * MACH_PORT_NULL in the failure case. |
b0d623f7 A |
1236 | * |
1237 | * Errno: 0 Success | |
0a7de745 A |
1238 | * EINVAL The calling process' session has not be set. |
1239 | * ESRCH Bad process, can't get valid cred for process. | |
1240 | * ENOMEM Port allocation failed due to no free memory. | |
b0d623f7 A |
1241 | */ |
1242 | int | |
1243 | audit_session_self(proc_t p, __unused struct audit_session_self_args *uap, | |
1244 | mach_port_name_t *ret_port) | |
1245 | { | |
1246 | ipc_port_t sendport = IPC_PORT_NULL; | |
1247 | kauth_cred_t cred = NULL; | |
1248 | auditinfo_addr_t *aia_p; | |
1249 | au_sentry_t *se; | |
1250 | int err = 0; | |
1251 | ||
1252 | cred = kauth_cred_proc_ref(p); | |
1253 | if (!IS_VALID_CRED(cred)) { | |
1254 | err = ESRCH; | |
1255 | goto done; | |
1256 | } | |
1257 | ||
1258 | aia_p = cred->cr_audit.as_aia_p; | |
1259 | if (!IS_VALID_SESSION(aia_p)) { | |
6d2010ae | 1260 | /* Can't join the default session. */ |
b0d623f7 A |
1261 | err = EINVAL; |
1262 | goto done; | |
1263 | } | |
1264 | ||
0a7de745 | 1265 | se = AU_SENTRY_PTR(aia_p); |
b0d623f7 | 1266 | |
0a7de745 | 1267 | /* |
b0d623f7 A |
1268 | * Processes that join using this mach port will inherit this process' |
1269 | * pre-selection masks. | |
1270 | */ | |
0a7de745 | 1271 | if (se->se_port == IPC_PORT_NULL) { |
b0d623f7 A |
1272 | bcopy(&cred->cr_audit.as_mask, &se->se_mask, |
1273 | sizeof(se->se_mask)); | |
0a7de745 | 1274 | } |
b0d623f7 | 1275 | |
b0d623f7 | 1276 | /* |
6d2010ae A |
1277 | * Get a send right to the session's Mach port and insert it in the |
1278 | * process' mach port namespace. | |
b0d623f7 | 1279 | */ |
6d2010ae A |
1280 | sendport = audit_session_mksend(aia_p, &se->se_port); |
1281 | *ret_port = ipc_port_copyout_send(sendport, get_task_ipcspace(p->task)); | |
b0d623f7 A |
1282 | |
1283 | done: | |
0a7de745 A |
1284 | if (cred != NULL) { |
1285 | kauth_cred_unref(&cred); | |
1286 | } | |
1287 | if (err != 0) { | |
b0d623f7 | 1288 | *ret_port = MACH_PORT_NULL; |
0a7de745 A |
1289 | } |
1290 | return err; | |
b0d623f7 A |
1291 | } |
1292 | ||
6d2010ae A |
1293 | /* |
1294 | * audit_session_port (system call) | |
1295 | * | |
1296 | * Description: Obtain a Mach send right for the given session ID. | |
1297 | * | |
1298 | * Parameters: p Process calling audit_session_port(). | |
1299 | * uap->asid The target audit session ID. The special | |
0a7de745 A |
1300 | * value -1 can be used to target the process's |
1301 | * own session. | |
6d2010ae A |
1302 | * uap->portnamep User address at which to place port name. |
1303 | * | |
1304 | * Returns: 0 Success | |
0a7de745 A |
1305 | * EINVAL The calling process' session has not be set. |
1306 | * EINVAL The given session ID could not be found. | |
1307 | * EINVAL The Mach port right could not be copied out. | |
1308 | * ESRCH Bad process, can't get valid cred for process. | |
1309 | * EPERM Only the superuser can reference sessions other | |
1310 | * than the process's own. | |
1311 | * ENOMEM Port allocation failed due to no free memory. | |
6d2010ae A |
1312 | */ |
1313 | int | |
1314 | audit_session_port(proc_t p, struct audit_session_port_args *uap, | |
1315 | __unused int *retval) | |
b0d623f7 | 1316 | { |
6d2010ae A |
1317 | ipc_port_t sendport = IPC_PORT_NULL; |
1318 | mach_port_name_t portname = MACH_PORT_NULL; | |
1319 | kauth_cred_t cred = NULL; | |
1320 | auditinfo_addr_t *aia_p = NULL; | |
1321 | au_sentry_t *se = NULL; | |
1322 | int err = 0; | |
1323 | ||
1324 | /* Note: Currently this test will never be true, because | |
1325 | * ASSIGNED_ASID_MAX is effectively (uint32_t)-2. | |
1326 | */ | |
1327 | if (uap->asid != -1 && (uint32_t)uap->asid > ASSIGNED_ASID_MAX) { | |
1328 | err = EINVAL; | |
1329 | goto done; | |
1330 | } | |
1331 | cred = kauth_cred_proc_ref(p); | |
1332 | if (!IS_VALID_CRED(cred)) { | |
1333 | err = ESRCH; | |
1334 | goto done; | |
1335 | } | |
1336 | aia_p = cred->cr_audit.as_aia_p; | |
b0d623f7 | 1337 | |
6d2010ae A |
1338 | /* Find the session corresponding to the requested audit |
1339 | * session ID. If found, take a reference on it so that | |
1340 | * the session is not dropped until the join is later done. | |
1341 | */ | |
1342 | if (uap->asid == (au_asid_t)-1 || | |
1343 | uap->asid == aia_p->ai_asid) { | |
6d2010ae A |
1344 | if (!IS_VALID_SESSION(aia_p)) { |
1345 | /* Can't join the default session. */ | |
1346 | err = EINVAL; | |
1347 | goto done; | |
1348 | } | |
1349 | ||
1350 | /* No privilege is required to obtain a port for our | |
1351 | * own session. | |
1352 | */ | |
1353 | se = AU_SENTRY_PTR(aia_p); | |
1354 | audit_ref_session(se); | |
cb323159 A |
1355 | } else { |
1356 | /* | |
1357 | * Only privileged processes may obtain a port for | |
1358 | * any existing session. | |
6d2010ae | 1359 | */ |
cb323159 A |
1360 | err = priv_check_cred(cred, PRIV_AUDIT_SESSION_PORT, 0); |
1361 | if (err != 0) { | |
1362 | goto done; | |
1363 | } | |
6d2010ae A |
1364 | AUDIT_SENTRY_RLOCK(); |
1365 | se = audit_session_find(uap->asid); | |
1366 | AUDIT_SENTRY_RUNLOCK(); | |
1367 | if (NULL == se) { | |
1368 | err = EINVAL; | |
1369 | goto done; | |
1370 | } | |
1371 | aia_p = &se->se_auinfo; | |
6d2010ae | 1372 | } |
b0d623f7 A |
1373 | |
1374 | /* | |
6d2010ae A |
1375 | * Processes that join using this mach port will inherit this process' |
1376 | * pre-selection masks. | |
b0d623f7 | 1377 | */ |
0a7de745 | 1378 | if (se->se_port == IPC_PORT_NULL) { |
6d2010ae A |
1379 | bcopy(&cred->cr_audit.as_mask, &se->se_mask, |
1380 | sizeof(se->se_mask)); | |
0a7de745 | 1381 | } |
6d2010ae A |
1382 | |
1383 | /* | |
1384 | * Use the session reference to create a mach port reference for the | |
1385 | * session (at which point we are free to drop the session reference) | |
1386 | * and then copy out the mach port to the process' mach port namespace. | |
1387 | */ | |
1388 | sendport = audit_session_mksend(aia_p, &se->se_port); | |
1389 | portname = ipc_port_copyout_send(sendport, get_task_ipcspace(p->task)); | |
1390 | if (!MACH_PORT_VALID(portname)) { | |
1391 | err = EINVAL; | |
1392 | goto done; | |
b0d623f7 | 1393 | } |
6d2010ae A |
1394 | err = copyout(&portname, uap->portnamep, sizeof(mach_port_name_t)); |
1395 | done: | |
0a7de745 | 1396 | if (cred != NULL) { |
6d2010ae | 1397 | kauth_cred_unref(&cred); |
0a7de745 A |
1398 | } |
1399 | if (NULL != se) { | |
6d2010ae | 1400 | audit_unref_session(se); |
0a7de745 A |
1401 | } |
1402 | if (MACH_PORT_VALID(portname) && 0 != err) { | |
1403 | (void)mach_port_deallocate(get_task_ipcspace(p->task), | |
6d2010ae | 1404 | portname); |
0a7de745 | 1405 | } |
b0d623f7 | 1406 | |
0a7de745 | 1407 | return err; |
b0d623f7 A |
1408 | } |
1409 | ||
1410 | static int | |
743345f9 | 1411 | audit_session_join_internal(proc_t p, task_t task, ipc_port_t port, au_asid_t *new_asid) |
b0d623f7 | 1412 | { |
6d2010ae A |
1413 | auditinfo_addr_t *new_aia_p, *old_aia_p; |
1414 | kauth_cred_t my_cred = NULL; | |
b0d623f7 A |
1415 | au_asid_t old_asid; |
1416 | int err = 0; | |
1417 | ||
1418 | *new_asid = AU_DEFAUDITSID; | |
1419 | ||
6d2010ae | 1420 | if ((new_aia_p = audit_session_porttoaia(port)) == NULL) { |
b0d623f7 A |
1421 | err = EINVAL; |
1422 | goto done; | |
1423 | } | |
b0d623f7 | 1424 | |
4bd07ac2 | 1425 | proc_ucred_lock(p); |
6d2010ae A |
1426 | kauth_cred_ref(p->p_ucred); |
1427 | my_cred = p->p_ucred; | |
1428 | if (!IS_VALID_CRED(my_cred)) { | |
0a7de745 | 1429 | kauth_cred_unref(&my_cred); |
4bd07ac2 | 1430 | proc_ucred_unlock(p); |
b0d623f7 A |
1431 | err = ESRCH; |
1432 | goto done; | |
1433 | } | |
6d2010ae | 1434 | old_aia_p = my_cred->cr_audit.as_aia_p; |
b0d623f7 | 1435 | old_asid = old_aia_p->ai_asid; |
6d2010ae | 1436 | *new_asid = new_aia_p->ai_asid; |
b0d623f7 A |
1437 | |
1438 | /* | |
1439 | * Add process in if not already in the session. | |
1440 | */ | |
1441 | if (*new_asid != old_asid) { | |
6d2010ae A |
1442 | kauth_cred_t my_new_cred; |
1443 | struct au_session new_as; | |
1444 | ||
1445 | bcopy(&new_aia_p->ai_mask, &new_as.as_mask, | |
0a7de745 | 1446 | sizeof(new_as.as_mask)); |
6d2010ae A |
1447 | new_as.as_aia_p = new_aia_p; |
1448 | ||
1449 | my_new_cred = kauth_cred_setauditinfo(my_cred, &new_as); | |
1450 | p->p_ucred = my_new_cred; | |
1451 | PROC_UPDATE_CREDS_ONPROC(p); | |
1452 | ||
1453 | /* Increment the proc count of new session */ | |
1454 | audit_inc_procount(AU_SENTRY_PTR(new_aia_p)); | |
1455 | ||
4bd07ac2 | 1456 | proc_ucred_unlock(p); |
6d2010ae A |
1457 | |
1458 | /* Propagate the change from the process to the Mach task. */ | |
743345f9 | 1459 | set_security_token_task_internal(p, task); |
6d2010ae A |
1460 | |
1461 | /* Decrement the process count of the former session. */ | |
1462 | audit_dec_procount(AU_SENTRY_PTR(old_aia_p)); | |
0a7de745 | 1463 | } else { |
4bd07ac2 | 1464 | proc_ucred_unlock(p); |
b0d623f7 | 1465 | } |
6d2010ae | 1466 | kauth_cred_unref(&my_cred); |
b0d623f7 A |
1467 | |
1468 | done: | |
0a7de745 | 1469 | if (port != IPC_PORT_NULL) { |
b0d623f7 | 1470 | ipc_port_release_send(port); |
0a7de745 | 1471 | } |
b0d623f7 | 1472 | |
0a7de745 | 1473 | return err; |
b0d623f7 A |
1474 | } |
1475 | ||
1476 | /* | |
1477 | * audit_session_spawnjoin | |
1478 | * | |
1479 | * Description: posix_spawn() interface to audit_session_join_internal(). | |
1480 | * | |
1481 | * Returns: 0 Success | |
0a7de745 A |
1482 | * EINVAL Invalid Mach port name. |
1483 | * ESRCH Invalid calling process/cred. | |
b0d623f7 A |
1484 | */ |
1485 | int | |
743345f9 | 1486 | audit_session_spawnjoin(proc_t p, task_t task, ipc_port_t port) |
b0d623f7 A |
1487 | { |
1488 | au_asid_t new_asid; | |
0a7de745 A |
1489 | |
1490 | return audit_session_join_internal(p, task, port, &new_asid); | |
b0d623f7 A |
1491 | } |
1492 | ||
1493 | /* | |
1494 | * audit_session_join (system call) | |
1495 | * | |
1496 | * Description: Join the session for a given Mach port send right. | |
0a7de745 | 1497 | * |
b0d623f7 | 1498 | * Parameters: p Process calling session join. |
0a7de745 | 1499 | * uap->port A Mach send right. |
b0d623f7 | 1500 | * |
6d2010ae A |
1501 | * Returns: *ret_asid Audit session ID of new session. |
1502 | * In the failure case the return value will be -1 | |
1503 | * and 'errno' will be set to a non-zero value | |
1504 | * described below. | |
b0d623f7 | 1505 | * |
0a7de745 A |
1506 | * Errno: 0 Success |
1507 | * EINVAL Invalid Mach port name. | |
1508 | * ESRCH Invalid calling process/cred. | |
b0d623f7 A |
1509 | */ |
1510 | int | |
1511 | audit_session_join(proc_t p, struct audit_session_join_args *uap, | |
1512 | au_asid_t *ret_asid) | |
1513 | { | |
1514 | ipc_port_t port = IPC_PORT_NULL; | |
1515 | mach_port_name_t send = uap->port; | |
1516 | int err = 0; | |
1517 | ||
0a7de745 | 1518 | |
b0d623f7 | 1519 | if (ipc_object_copyin(get_task_ipcspace(p->task), send, |
c3c9b80d | 1520 | MACH_MSG_TYPE_COPY_SEND, &port, 0, NULL, IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND) != KERN_SUCCESS) { |
b0d623f7 A |
1521 | *ret_asid = AU_DEFAUDITSID; |
1522 | err = EINVAL; | |
0a7de745 | 1523 | } else { |
743345f9 | 1524 | err = audit_session_join_internal(p, p->task, port, ret_asid); |
0a7de745 | 1525 | } |
b0d623f7 | 1526 | |
0a7de745 | 1527 | return err; |
b0d623f7 A |
1528 | } |
1529 | ||
6d2010ae A |
1530 | /* |
1531 | * Audit session device. | |
1532 | */ | |
1533 | ||
1534 | /* | |
1535 | * Free an audit sdev entry. | |
1536 | */ | |
1537 | static void | |
1538 | audit_sdev_entry_free(struct audit_sdev_entry *ase) | |
1539 | { | |
6d2010ae A |
1540 | free(ase->ase_record, M_AUDIT_SDEV_ENTRY); |
1541 | free(ase, M_AUDIT_SDEV_ENTRY); | |
1542 | } | |
1543 | ||
1544 | /* | |
1545 | * Append individual record to a queue. Allocate queue-local buffer and | |
1546 | * add to the queue. If the queue is full or we can't allocate memory, | |
1547 | * drop the newest record. | |
1548 | */ | |
1549 | static void | |
1550 | audit_sdev_append(struct audit_sdev *asdev, void *record, u_int record_len) | |
1551 | { | |
1552 | struct audit_sdev_entry *ase; | |
1553 | ||
1554 | AUDIT_SDEV_LOCK_ASSERT(asdev); | |
1555 | ||
1556 | if (asdev->asdev_qlen >= asdev->asdev_qlimit) { | |
1557 | asdev->asdev_drops++; | |
1558 | audit_sdev_drops++; | |
1559 | return; | |
1560 | } | |
1561 | ||
0a7de745 | 1562 | ase = malloc(sizeof(*ase), M_AUDIT_SDEV_ENTRY, M_NOWAIT | M_ZERO); |
6d2010ae A |
1563 | if (NULL == ase) { |
1564 | asdev->asdev_drops++; | |
1565 | audit_sdev_drops++; | |
1566 | return; | |
1567 | } | |
1568 | ||
1569 | ase->ase_record = malloc(record_len, M_AUDIT_SDEV_ENTRY, M_NOWAIT); | |
1570 | if (NULL == ase->ase_record) { | |
1571 | free(ase, M_AUDIT_SDEV_ENTRY); | |
1572 | asdev->asdev_drops++; | |
1573 | audit_sdev_drops++; | |
1574 | return; | |
1575 | } | |
1576 | ||
1577 | bcopy(record, ase->ase_record, record_len); | |
1578 | ase->ase_record_len = record_len; | |
1579 | ||
1580 | TAILQ_INSERT_TAIL(&asdev->asdev_queue, ase, ase_queue); | |
1581 | asdev->asdev_inserts++; | |
1582 | asdev->asdev_qlen++; | |
1583 | asdev->asdev_qbyteslen += ase->ase_record_len; | |
1584 | selwakeup(&asdev->asdev_selinfo); | |
0a7de745 | 1585 | if (asdev->asdev_flags & AUDIT_SDEV_ASYNC) { |
6d2010ae | 1586 | pgsigio(asdev->asdev_sigio, SIGIO); |
0a7de745 | 1587 | } |
6d2010ae A |
1588 | |
1589 | cv_broadcast(&asdev->asdev_cv); | |
1590 | } | |
1591 | ||
1592 | /* | |
1593 | * Submit an audit record to be queued in the audit session device. | |
1594 | */ | |
1595 | void | |
1596 | audit_sdev_submit(__unused au_id_t auid, __unused au_asid_t asid, void *record, | |
1597 | u_int record_len) | |
1598 | { | |
1599 | struct audit_sdev *asdev; | |
1600 | ||
1601 | /* | |
1602 | * Lockless read to avoid lock overhead if sessio devices are not in | |
1603 | * use. | |
1604 | */ | |
0a7de745 | 1605 | if (NULL == TAILQ_FIRST(&audit_sdev_list)) { |
6d2010ae | 1606 | return; |
0a7de745 | 1607 | } |
6d2010ae A |
1608 | |
1609 | AUDIT_SDEV_LIST_RLOCK(); | |
1610 | TAILQ_FOREACH(asdev, &audit_sdev_list, asdev_list) { | |
1611 | AUDIT_SDEV_LOCK(asdev); | |
0a7de745 A |
1612 | |
1613 | /* | |
6d2010ae A |
1614 | * Only append to the sdev queue if the AUID and ASID match that |
1615 | * of the process that opened this session device or if the | |
1616 | * ALLSESSIONS flag is set. | |
1617 | */ | |
1618 | if ((/* XXXss auid == asdev->asdev_auid && */ | |
0a7de745 A |
1619 | asid == asdev->asdev_asid) || |
1620 | (asdev->asdev_flags & AUDIT_SDEV_ALLSESSIONS) != 0) { | |
6d2010ae | 1621 | audit_sdev_append(asdev, record, record_len); |
0a7de745 | 1622 | } |
6d2010ae A |
1623 | AUDIT_SDEV_UNLOCK(asdev); |
1624 | } | |
1625 | AUDIT_SDEV_LIST_RUNLOCK(); | |
1626 | ||
1627 | /* Unlocked increment. */ | |
1628 | audit_sdev_records++; | |
1629 | } | |
1630 | ||
1631 | /* | |
1632 | * Allocate a new audit sdev. Connects the sdev, on succes, to the global | |
1633 | * list and updates statistics. | |
1634 | */ | |
1635 | static struct audit_sdev * | |
1636 | audit_sdev_alloc(void) | |
1637 | { | |
1638 | struct audit_sdev *asdev; | |
1639 | ||
1640 | AUDIT_SDEV_LIST_WLOCK_ASSERT(); | |
1641 | ||
0a7de745 A |
1642 | asdev = malloc(sizeof(*asdev), M_AUDIT_SDEV, M_WAITOK | M_ZERO); |
1643 | if (NULL == asdev) { | |
1644 | return NULL; | |
1645 | } | |
6d2010ae A |
1646 | |
1647 | asdev->asdev_qlimit = AUDIT_SDEV_QLIMIT_DEFAULT; | |
1648 | TAILQ_INIT(&asdev->asdev_queue); | |
1649 | AUDIT_SDEV_LOCK_INIT(asdev); | |
1650 | AUDIT_SDEV_SX_LOCK_INIT(asdev); | |
1651 | cv_init(&asdev->asdev_cv, "audit_sdev_cv"); | |
1652 | ||
1653 | /* | |
1654 | * Add to global list and update global statistics. | |
1655 | */ | |
1656 | TAILQ_INSERT_HEAD(&audit_sdev_list, asdev, asdev_list); | |
1657 | audit_sdev_count++; | |
1658 | audit_sdev_ever++; | |
1659 | ||
0a7de745 | 1660 | return asdev; |
6d2010ae A |
1661 | } |
1662 | ||
1663 | /* | |
1664 | * Flush all records currently present in an audit sdev. | |
1665 | */ | |
1666 | static void | |
1667 | audit_sdev_flush(struct audit_sdev *asdev) | |
1668 | { | |
1669 | struct audit_sdev_entry *ase; | |
1670 | ||
1671 | AUDIT_SDEV_LOCK_ASSERT(asdev); | |
1672 | ||
1673 | while ((ase = TAILQ_FIRST(&asdev->asdev_queue)) != NULL) { | |
1674 | TAILQ_REMOVE(&asdev->asdev_queue, ase, ase_queue); | |
1675 | asdev->asdev_qbyteslen -= ase->ase_record_len; | |
1676 | audit_sdev_entry_free(ase); | |
1677 | asdev->asdev_qlen--; | |
1678 | } | |
1679 | asdev->asdev_qoffset = 0; | |
1680 | ||
1681 | KASSERT(0 == asdev->asdev_qlen, ("audit_sdev_flush: asdev_qlen")); | |
1682 | KASSERT(0 == asdev->asdev_qbyteslen, | |
1683 | ("audit_sdev_flush: asdev_qbyteslen")); | |
1684 | } | |
1685 | ||
1686 | /* | |
1687 | * Free an audit sdev. | |
1688 | */ | |
1689 | static void | |
1690 | audit_sdev_free(struct audit_sdev *asdev) | |
1691 | { | |
6d2010ae A |
1692 | AUDIT_SDEV_LIST_WLOCK_ASSERT(); |
1693 | AUDIT_SDEV_LOCK_ASSERT(asdev); | |
1694 | ||
1695 | /* XXXss - preselect hook here */ | |
1696 | audit_sdev_flush(asdev); | |
1697 | cv_destroy(&asdev->asdev_cv); | |
1698 | AUDIT_SDEV_SX_LOCK_DESTROY(asdev); | |
39236c6e | 1699 | AUDIT_SDEV_UNLOCK(asdev); |
6d2010ae A |
1700 | AUDIT_SDEV_LOCK_DESTROY(asdev); |
1701 | ||
1702 | TAILQ_REMOVE(&audit_sdev_list, asdev, asdev_list); | |
1703 | free(asdev, M_AUDIT_SDEV); | |
1704 | audit_sdev_count--; | |
1705 | } | |
1706 | ||
1707 | /* | |
1708 | * Get the auditinfo_addr of the proc and check to see if suser. Will return | |
1709 | * non-zero if not suser. | |
1710 | */ | |
1711 | static int | |
1712 | audit_sdev_get_aia(proc_t p, struct auditinfo_addr *aia_p) | |
1713 | { | |
1714 | int error; | |
1715 | kauth_cred_t scred; | |
1716 | ||
1717 | scred = kauth_cred_proc_ref(p); | |
1718 | error = suser(scred, &p->p_acflag); | |
1719 | ||
0a7de745 A |
1720 | if (NULL != aia_p) { |
1721 | bcopy(scred->cr_audit.as_aia_p, aia_p, sizeof(*aia_p)); | |
1722 | } | |
6d2010ae A |
1723 | kauth_cred_unref(&scred); |
1724 | ||
0a7de745 | 1725 | return error; |
6d2010ae A |
1726 | } |
1727 | ||
1728 | /* | |
1729 | * Audit session dev open method. | |
1730 | */ | |
1731 | static int | |
0a7de745 | 1732 | audit_sdev_open(dev_t dev, __unused int flags, __unused int devtype, proc_t p) |
6d2010ae A |
1733 | { |
1734 | struct audit_sdev *asdev; | |
1735 | struct auditinfo_addr aia; | |
1736 | int u; | |
1737 | ||
1738 | u = minor(dev); | |
0a7de745 A |
1739 | if (u < 0 || u >= MAX_AUDIT_SDEVS) { |
1740 | return ENXIO; | |
1741 | } | |
6d2010ae A |
1742 | |
1743 | (void) audit_sdev_get_aia(p, &aia); | |
1744 | ||
1745 | AUDIT_SDEV_LIST_WLOCK(); | |
1746 | asdev = audit_sdev_dtab[u]; | |
1747 | if (NULL == asdev) { | |
1748 | asdev = audit_sdev_alloc(); | |
1749 | if (NULL == asdev) { | |
1750 | AUDIT_SDEV_LIST_WUNLOCK(); | |
0a7de745 | 1751 | return ENOMEM; |
6d2010ae A |
1752 | } |
1753 | audit_sdev_dtab[u] = asdev; | |
1754 | } else { | |
1755 | KASSERT(asdev->asdev_open, ("audit_sdev_open: Already open")); | |
1756 | AUDIT_SDEV_LIST_WUNLOCK(); | |
0a7de745 | 1757 | return EBUSY; |
6d2010ae A |
1758 | } |
1759 | asdev->asdev_open = 1; | |
1760 | asdev->asdev_auid = aia.ai_auid; | |
1761 | asdev->asdev_asid = aia.ai_asid; | |
0a7de745 | 1762 | asdev->asdev_flags = 0; |
6d2010ae A |
1763 | |
1764 | AUDIT_SDEV_LIST_WUNLOCK(); | |
1765 | ||
0a7de745 | 1766 | return 0; |
6d2010ae A |
1767 | } |
1768 | ||
1769 | /* | |
1770 | * Audit session dev close method. | |
1771 | */ | |
1772 | static int | |
1773 | audit_sdev_close(dev_t dev, __unused int flags, __unused int devtype, | |
1774 | __unused proc_t p) | |
1775 | { | |
1776 | struct audit_sdev *asdev; | |
1777 | int u; | |
1778 | ||
1779 | u = minor(dev); | |
1780 | asdev = audit_sdev_dtab[u]; | |
1781 | ||
1782 | KASSERT(asdev != NULL, ("audit_sdev_close: asdev == NULL")); | |
1783 | KASSERT(asdev->asdev_open, ("audit_sdev_close: !asdev_open")); | |
1784 | ||
1785 | AUDIT_SDEV_LIST_WLOCK(); | |
1786 | AUDIT_SDEV_LOCK(asdev); | |
1787 | asdev->asdev_open = 0; | |
1788 | audit_sdev_free(asdev); /* sdev lock unlocked in audit_sdev_free() */ | |
1789 | audit_sdev_dtab[u] = NULL; | |
1790 | AUDIT_SDEV_LIST_WUNLOCK(); | |
1791 | ||
0a7de745 | 1792 | return 0; |
6d2010ae A |
1793 | } |
1794 | ||
1795 | /* | |
1796 | * Audit session dev ioctl method. | |
1797 | */ | |
1798 | static int | |
1799 | audit_sdev_ioctl(dev_t dev, u_long cmd, caddr_t data, | |
1800 | __unused int flag, proc_t p) | |
1801 | { | |
1802 | struct audit_sdev *asdev; | |
1803 | int error; | |
1804 | ||
1805 | asdev = audit_sdev_dtab[minor(dev)]; | |
1806 | KASSERT(asdev != NULL, ("audit_sdev_ioctl: asdev == NULL")); | |
1807 | ||
1808 | error = 0; | |
1809 | ||
1810 | switch (cmd) { | |
1811 | case FIONBIO: | |
1812 | AUDIT_SDEV_LOCK(asdev); | |
0a7de745 | 1813 | if (*(int *)data) { |
6d2010ae | 1814 | asdev->asdev_flags |= AUDIT_SDEV_NBIO; |
0a7de745 | 1815 | } else { |
6d2010ae | 1816 | asdev->asdev_flags &= ~AUDIT_SDEV_NBIO; |
0a7de745 | 1817 | } |
6d2010ae A |
1818 | AUDIT_SDEV_UNLOCK(asdev); |
1819 | break; | |
1820 | ||
1821 | case FIONREAD: | |
1822 | AUDIT_SDEV_LOCK(asdev); | |
1823 | *(int *)data = asdev->asdev_qbyteslen - asdev->asdev_qoffset; | |
1824 | AUDIT_SDEV_UNLOCK(asdev); | |
1825 | break; | |
1826 | ||
1827 | case AUDITSDEV_GET_QLEN: | |
1828 | *(u_int *)data = asdev->asdev_qlen; | |
1829 | break; | |
1830 | ||
1831 | case AUDITSDEV_GET_QLIMIT: | |
1832 | *(u_int *)data = asdev->asdev_qlimit; | |
1833 | break; | |
1834 | ||
1835 | case AUDITSDEV_SET_QLIMIT: | |
1836 | if (*(u_int *)data >= AUDIT_SDEV_QLIMIT_MIN || | |
1837 | *(u_int *)data <= AUDIT_SDEV_QLIMIT_MAX) { | |
1838 | asdev->asdev_qlimit = *(u_int *)data; | |
0a7de745 | 1839 | } else { |
6d2010ae | 1840 | error = EINVAL; |
0a7de745 | 1841 | } |
6d2010ae A |
1842 | break; |
1843 | ||
1844 | case AUDITSDEV_GET_QLIMIT_MIN: | |
1845 | *(u_int *)data = AUDIT_SDEV_QLIMIT_MIN; | |
1846 | break; | |
1847 | ||
1848 | case AUDITSDEV_GET_QLIMIT_MAX: | |
1849 | *(u_int *)data = AUDIT_SDEV_QLIMIT_MAX; | |
1850 | break; | |
1851 | ||
1852 | case AUDITSDEV_FLUSH: | |
0a7de745 A |
1853 | if (AUDIT_SDEV_SX_XLOCK_SIG(asdev) != 0) { |
1854 | return EINTR; | |
1855 | } | |
6d2010ae A |
1856 | AUDIT_SDEV_LOCK(asdev); |
1857 | audit_sdev_flush(asdev); | |
1858 | AUDIT_SDEV_UNLOCK(asdev); | |
1859 | AUDIT_SDEV_SX_XUNLOCK(asdev); | |
1860 | break; | |
1861 | ||
1862 | case AUDITSDEV_GET_MAXDATA: | |
1863 | *(u_int *)data = MAXAUDITDATA; | |
1864 | break; | |
1865 | ||
1866 | /* XXXss these should be 64 bit, maybe. */ | |
1867 | case AUDITSDEV_GET_INSERTS: | |
1868 | *(u_int *)data = asdev->asdev_inserts; | |
1869 | break; | |
1870 | ||
1871 | case AUDITSDEV_GET_READS: | |
1872 | *(u_int *)data = asdev->asdev_reads; | |
1873 | break; | |
1874 | ||
1875 | case AUDITSDEV_GET_DROPS: | |
1876 | *(u_int *)data = asdev->asdev_drops; | |
1877 | break; | |
1878 | ||
1879 | case AUDITSDEV_GET_ALLSESSIONS: | |
1880 | error = audit_sdev_get_aia(p, NULL); | |
0a7de745 | 1881 | if (error) { |
6d2010ae | 1882 | break; |
0a7de745 | 1883 | } |
6d2010ae A |
1884 | *(u_int *)data = (asdev->asdev_flags & AUDIT_SDEV_ALLSESSIONS) ? |
1885 | 1 : 0; | |
1886 | break; | |
1887 | ||
1888 | case AUDITSDEV_SET_ALLSESSIONS: | |
1889 | error = audit_sdev_get_aia(p, NULL); | |
0a7de745 | 1890 | if (error) { |
6d2010ae | 1891 | break; |
0a7de745 | 1892 | } |
6d2010ae A |
1893 | |
1894 | AUDIT_SDEV_LOCK(asdev); | |
0a7de745 | 1895 | if (*(int *)data) { |
6d2010ae | 1896 | asdev->asdev_flags |= AUDIT_SDEV_ALLSESSIONS; |
0a7de745 | 1897 | } else { |
6d2010ae | 1898 | asdev->asdev_flags &= ~AUDIT_SDEV_ALLSESSIONS; |
0a7de745 | 1899 | } |
6d2010ae A |
1900 | AUDIT_SDEV_UNLOCK(asdev); |
1901 | break; | |
1902 | ||
1903 | default: | |
1904 | error = ENOTTY; | |
1905 | } | |
1906 | ||
0a7de745 | 1907 | return error; |
6d2010ae A |
1908 | } |
1909 | ||
1910 | /* | |
0a7de745 | 1911 | * Audit session dev read method. |
6d2010ae A |
1912 | */ |
1913 | static int | |
1914 | audit_sdev_read(dev_t dev, struct uio *uio, __unused int flag) | |
1915 | { | |
1916 | struct audit_sdev_entry *ase; | |
1917 | struct audit_sdev *asdev; | |
1918 | u_int toread; | |
1919 | int error; | |
1920 | ||
1921 | asdev = audit_sdev_dtab[minor(dev)]; | |
1922 | KASSERT(NULL != asdev, ("audit_sdev_read: asdev == NULL")); | |
1923 | ||
1924 | /* | |
1925 | * We hold a sleep lock over read and flush because we rely on the | |
1926 | * stability of a record in the queue during uiomove. | |
1927 | */ | |
0a7de745 A |
1928 | if (0 != AUDIT_SDEV_SX_XLOCK_SIG(asdev)) { |
1929 | return EINTR; | |
1930 | } | |
6d2010ae A |
1931 | AUDIT_SDEV_LOCK(asdev); |
1932 | while (TAILQ_EMPTY(&asdev->asdev_queue)) { | |
1933 | if (asdev->asdev_flags & AUDIT_SDEV_NBIO) { | |
1934 | AUDIT_SDEV_UNLOCK(asdev); | |
1935 | AUDIT_SDEV_SX_XUNLOCK(asdev); | |
0a7de745 | 1936 | return EAGAIN; |
6d2010ae A |
1937 | } |
1938 | error = cv_wait_sig(&asdev->asdev_cv, AUDIT_SDEV_MTX(asdev)); | |
1939 | if (error) { | |
1940 | AUDIT_SDEV_UNLOCK(asdev); | |
1941 | AUDIT_SDEV_SX_XUNLOCK(asdev); | |
0a7de745 | 1942 | return error; |
6d2010ae A |
1943 | } |
1944 | } | |
1945 | ||
1946 | /* | |
1947 | * Copy as many remaining bytes from the current record to userspace | |
1948 | * as we can. Keep processing records until we run out of records in | |
1949 | * the queue or until the user buffer runs out of space. | |
1950 | * | |
1951 | * We rely on the sleep lock to maintain ase's stability here. | |
1952 | */ | |
1953 | asdev->asdev_reads++; | |
1954 | while ((ase = TAILQ_FIRST(&asdev->asdev_queue)) != NULL && | |
1955 | uio_resid(uio) > 0) { | |
1956 | AUDIT_SDEV_LOCK_ASSERT(asdev); | |
1957 | ||
1958 | KASSERT(ase->ase_record_len > asdev->asdev_qoffset, | |
1959 | ("audit_sdev_read: record_len > qoffset (1)")); | |
39236c6e | 1960 | toread = MIN((int)(ase->ase_record_len - asdev->asdev_qoffset), |
6d2010ae A |
1961 | uio_resid(uio)); |
1962 | AUDIT_SDEV_UNLOCK(asdev); | |
1963 | error = uiomove((char *) ase->ase_record + asdev->asdev_qoffset, | |
1964 | toread, uio); | |
1965 | if (error) { | |
1966 | AUDIT_SDEV_SX_XUNLOCK(asdev); | |
0a7de745 | 1967 | return error; |
6d2010ae A |
1968 | } |
1969 | ||
1970 | /* | |
1971 | * If the copy succeeded then update book-keeping, and if no | |
1972 | * bytes remain in the current record then free it. | |
1973 | */ | |
1974 | AUDIT_SDEV_LOCK(asdev); | |
1975 | KASSERT(TAILQ_FIRST(&asdev->asdev_queue) == ase, | |
1976 | ("audit_sdev_read: queue out of sync after uiomove")); | |
1977 | asdev->asdev_qoffset += toread; | |
1978 | KASSERT(ase->ase_record_len >= asdev->asdev_qoffset, | |
0a7de745 A |
1979 | ("audit_sdev_read: record_len >= qoffset (2)")); |
1980 | if (asdev->asdev_qoffset == ase->ase_record_len) { | |
1981 | TAILQ_REMOVE(&asdev->asdev_queue, ase, ase_queue); | |
1982 | asdev->asdev_qbyteslen -= ase->ase_record_len; | |
1983 | audit_sdev_entry_free(ase); | |
1984 | asdev->asdev_qlen--; | |
1985 | asdev->asdev_qoffset = 0; | |
1986 | } | |
6d2010ae A |
1987 | } |
1988 | AUDIT_SDEV_UNLOCK(asdev); | |
1989 | AUDIT_SDEV_SX_XUNLOCK(asdev); | |
0a7de745 | 1990 | return 0; |
6d2010ae A |
1991 | } |
1992 | ||
1993 | /* | |
1994 | * Audit session device poll method. | |
1995 | */ | |
1996 | static int | |
1997 | audit_sdev_poll(dev_t dev, int events, void *wql, struct proc *p) | |
1998 | { | |
1999 | struct audit_sdev *asdev; | |
2000 | int revents; | |
2001 | ||
2002 | revents = 0; | |
2003 | asdev = audit_sdev_dtab[minor(dev)]; | |
2004 | KASSERT(NULL != asdev, ("audit_sdev_poll: asdev == NULL")); | |
2005 | ||
2006 | if (events & (POLLIN | POLLRDNORM)) { | |
2007 | AUDIT_SDEV_LOCK(asdev); | |
0a7de745 | 2008 | if (NULL != TAILQ_FIRST(&asdev->asdev_queue)) { |
6d2010ae | 2009 | revents |= events & (POLLIN | POLLRDNORM); |
0a7de745 | 2010 | } else { |
6d2010ae | 2011 | selrecord(p, &asdev->asdev_selinfo, wql); |
0a7de745 | 2012 | } |
6d2010ae A |
2013 | AUDIT_SDEV_UNLOCK(asdev); |
2014 | } | |
0a7de745 | 2015 | return revents; |
6d2010ae A |
2016 | } |
2017 | ||
2018 | /* | |
2019 | * Audit sdev clone routine. Provides a new minor number or returns -1. | |
2020 | * This called with DEVFS_LOCK held. | |
2021 | */ | |
2022 | static int | |
2023 | audit_sdev_clone(__unused dev_t dev, int action) | |
2024 | { | |
2025 | int i; | |
2026 | ||
2027 | if (DEVFS_CLONE_ALLOC == action) { | |
0a7de745 A |
2028 | for (i = 0; i < MAX_AUDIT_SDEVS; i++) { |
2029 | if (NULL == audit_sdev_dtab[i]) { | |
2030 | return i; | |
2031 | } | |
2032 | } | |
6d2010ae | 2033 | |
0a7de745 | 2034 | /* |
6d2010ae A |
2035 | * This really should return -1 here but that seems to |
2036 | * hang things in devfs. We instead return 0 and let | |
2037 | * audit_sdev_open tell userland the bad news. | |
2038 | */ | |
0a7de745 | 2039 | return 0; |
6d2010ae A |
2040 | } |
2041 | ||
0a7de745 | 2042 | return -1; |
6d2010ae A |
2043 | } |
2044 | ||
2045 | static int | |
2046 | audit_sdev_init(void) | |
2047 | { | |
2048 | dev_t dev; | |
2049 | ||
2050 | TAILQ_INIT(&audit_sdev_list); | |
2051 | AUDIT_SDEV_LIST_LOCK_INIT(); | |
2052 | ||
2053 | audit_sdev_major = cdevsw_add(-1, &audit_sdev_cdevsw); | |
0a7de745 A |
2054 | if (audit_sdev_major < 0) { |
2055 | return KERN_FAILURE; | |
2056 | } | |
6d2010ae A |
2057 | |
2058 | dev = makedev(audit_sdev_major, 0); | |
2059 | devnode = devfs_make_node_clone(dev, DEVFS_CHAR, UID_ROOT, GID_WHEEL, | |
2060 | 0644, audit_sdev_clone, AUDIT_SDEV_NAME, 0); | |
2061 | ||
0a7de745 A |
2062 | if (NULL == devnode) { |
2063 | return KERN_FAILURE; | |
2064 | } | |
6d2010ae | 2065 | |
0a7de745 | 2066 | return KERN_SUCCESS; |
6d2010ae A |
2067 | } |
2068 | ||
2069 | /* XXXss | |
0a7de745 A |
2070 | * static int |
2071 | * audit_sdev_shutdown(void) | |
2072 | * { | |
2073 | * | |
2074 | * devfs_remove(devnode); | |
2075 | * (void) cdevsw_remove(audit_sdev_major, &audit_sdev_cdevsw); | |
2076 | * | |
2077 | * return (KERN_SUCCESS); | |
2078 | * } | |
2079 | */ | |
6d2010ae | 2080 | |
b0d623f7 A |
2081 | #else |
2082 | ||
2083 | int | |
2084 | audit_session_self(proc_t p, struct audit_session_self_args *uap, | |
2085 | mach_port_name_t *ret_port) | |
2086 | { | |
2087 | #pragma unused(p, uap, ret_port) | |
2088 | ||
0a7de745 | 2089 | return ENOSYS; |
b0d623f7 A |
2090 | } |
2091 | ||
2092 | int | |
2093 | audit_session_join(proc_t p, struct audit_session_join_args *uap, | |
2094 | au_asid_t *ret_asid) | |
2095 | { | |
2096 | #pragma unused(p, uap, ret_asid) | |
2097 | ||
0a7de745 | 2098 | return ENOSYS; |
b0d623f7 A |
2099 | } |
2100 | ||
6d2010ae A |
2101 | int |
2102 | audit_session_port(proc_t p, struct audit_session_port_args *uap, int *retval) | |
2103 | { | |
2104 | #pragma unused(p, uap, retval) | |
2105 | ||
0a7de745 | 2106 | return ENOSYS; |
6d2010ae A |
2107 | } |
2108 | ||
b0d623f7 | 2109 | #endif /* CONFIG_AUDIT */ |