]> git.saurik.com Git - apple/xnu.git/blame - bsd/security/audit/audit.h
xnu-6153.141.1.tar.gz
[apple/xnu.git] / bsd / security / audit / audit.h
CommitLineData
b0d623f7 1/*-
94ff46dc 2 * Copyright (c) 2004-2019 Apple Inc.
b0d623f7
A
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 */
30/*
31 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
32 * support for mandatory and extensible security protections. This notice
33 * is included in support of clause 2.2 (b) of the Apple Public License,
34 * Version 2.0.
35 */
36
37/*
38 * This header includes function prototypes and type definitions that are
39 * necessary for the kernel as a whole to interact with the audit subsystem.
40 */
41
42#ifndef _SECURITY_AUDIT_AUDIT_H
0a7de745 43#define _SECURITY_AUDIT_AUDIT_H
b0d623f7
A
44
45#if defined(_KERNEL) || defined(KERNEL)
46
b0d623f7
A
47#include <bsm/audit.h>
48
49#include <sys/sysctl.h>
50#include <sys/user.h>
51#include <sys/ipc.h>
52
53/*
54 * Audit subsystem condition flags. The audit_enabled flag is set and
55 * removed automatically as a result of configuring log files, and can be
56 * observed but should not be directly manipulated. The audit suspension
57 * flag permits audit to be temporarily disabled without reconfiguring the
58 * audit target. The audit syscalls flag is set at the first hint that kernel
59 * events (system and mach calls) need to be audited. It is used for
60 * performance so an event class map table lookup doesn't have be done for
61 * every system call if only user events are being audited.
62 */
0a7de745
A
63extern int audit_enabled;
64extern int audit_suspended;
65extern int audit_syscalls;
b0d623f7
A
66
67/*
68 * Define the masks for the audited arguments.
69 *
70 * XXXRW: These need to remain in audit.h for now because our vnode and name
71 * lookup audit calls rely on passing in flags to indicate which name or
72 * vnode is being logged. These should move to audit_private.h when that is
73 * fixed.
74 */
0a7de745
A
75#define ARG_EUID 0x0000000000000001ULL
76#define ARG_RUID 0x0000000000000002ULL
77#define ARG_SUID 0x0000000000000004ULL
78#define ARG_EGID 0x0000000000000008ULL
79#define ARG_RGID 0x0000000000000010ULL
80#define ARG_SGID 0x0000000000000020ULL
81#define ARG_PID 0x0000000000000040ULL
82#define ARG_UID 0x0000000000000080ULL
83#define ARG_AUID 0x0000000000000100ULL
84#define ARG_GID 0x0000000000000200ULL
85#define ARG_FD 0x0000000000000400ULL
86#define ARG_FD1 ARG_FD
87#define ARG_POSIX_IPC_PERM 0x0000000000000800ULL
88#define ARG_FFLAGS 0x0000000000001000ULL
89#define ARG_MODE 0x0000000000002000ULL
90#define ARG_VALUE32 0x0000000000004000ULL
91#define ARG_ADDR32 0x0000000000008000ULL
92#define ARG_ADDR ARG_ADDR32
93#define ARG_LEN 0x0000000000010000ULL
94#define ARG_MASK 0x0000000000020000ULL
95#define ARG_SIGNUM 0x0000000000040000ULL
96#define ARG_LOGIN 0x0000000000080000ULL
97#define ARG_SADDRINET 0x0000000000100000ULL
98#define ARG_SADDRINET6 0x0000000000200000ULL
99#define ARG_SADDRUNIX 0x0000000000400000ULL
100#define ARG_TERMID_ADDR ARG_SADDRUNIX
101#define ARG_KPATH1 0x0000000000800000ULL /* darwin-only */
102#define ARG_KPATH2 0x0000000001000000ULL /* darwin-only */
103#define ARG_UPATH1 0x0000000002000000ULL
104#define ARG_UPATH2 0x0000000004000000ULL
105#define ARG_TEXT 0x0000000008000000ULL
106#define ARG_VNODE1 0x0000000010000000ULL
107#define ARG_VNODE2 0x0000000020000000ULL
108#define ARG_SVIPC_CMD 0x0000000040000000ULL
109#define ARG_SVIPC_PERM 0x0000000080000000ULL
110#define ARG_SVIPC_ID 0x0000000100000000ULL
111#define ARG_SVIPC_ADDR 0x0000000200000000ULL
112#define ARG_GROUPSET 0x0000000400000000ULL
113#define ARG_CMD 0x0000000800000000ULL
114#define ARG_SOCKINFO 0x0000001000000000ULL
115#define ARG_ASID 0x0000002000000000ULL
116#define ARG_TERMID 0x0000004000000000ULL
117#define ARG_AUDITON 0x0000008000000000ULL
118#define ARG_VALUE64 0x0000010000000000ULL /* darwin-only */
119#define ARG_AMASK 0x0000020000000000ULL
120#define ARG_CTLNAME 0x0000040000000000ULL
121#define ARG_PROCESS 0x0000080000000000ULL
122#define ARG_MACHPORT1 0x0000100000000000ULL
123#define ARG_MACHPORT2 0x0000200000000000ULL
124#define ARG_MAC_STRING 0x0000400000000000ULL
125#define ARG_EXIT 0x0000800000000000ULL
126#define ARG_IOVECSTR 0x0001000000000000ULL
127#define ARG_ARGV 0x0002000000000000ULL
128#define ARG_ENVV 0x0004000000000000ULL
129#define ARG_OPAQUE 0x0008000000000000ULL /* darwin-only */
130#define ARG_DATA 0x0010000000000000ULL /* darwin-only */
131#define ARG_ADDR64 0x0020000000000000ULL /* darwin-only */
132#define ARG_FD2 0x0040000000000000ULL /* darwin-only */
133#define ARG_IDENTITY 0x0080000000000000ULL /* darwin-only */
134#define ARG_NONE 0x0000000000000000ULL
135#define ARG_ALL 0xFFFFFFFFFFFFFFFFULL
b0d623f7
A
136
137#if CONFIG_MACF
138
139#define MAC_AUDIT_LABEL_LEN 1024
140#define MAC_AUDIT_DATA_TYPE 0
141#define MAC_AUDIT_TEXT_TYPE 1
142
143struct mac_audit_record {
0a7de745
A
144 int type; /* one of the types defined above */
145 int length; /* byte length of the data field */
146 u_char *data; /* the payload */
147 LIST_ENTRY(mac_audit_record) records;
b0d623f7
A
148};
149
150#endif
151
152struct proc;
153struct vnode;
154struct componentname;
155
0a7de745
A
156int kau_will_audit(void);
157void audit_init(void);
158void audit_shutdown(void);
159void audit_syscall_enter(unsigned int code,
160 struct proc *proc, struct uthread *uthread);
b0d623f7
A
161#if CONFIG_MACF
162/*
163 * The parameter list of audit_syscall_exit() was modified to also take the
164 * Darwin syscall number, which is required by mac_audit_check_postselect().
165 */
0a7de745
A
166void audit_syscall_exit(unsigned int code, int error,
167 struct proc *proc, struct uthread *uthread);
b0d623f7 168#else
0a7de745
A
169void audit_syscall_exit(int error, struct proc *proc,
170 struct uthread *uthread);
b0d623f7 171#endif
0a7de745
A
172void audit_mach_syscall_enter(unsigned short audit_event);
173void audit_mach_syscall_exit(int retval, struct uthread *uthread);
b0d623f7 174
94ff46dc
A
175void audit_subcall_enter(au_event_t event,
176 struct proc *proc, struct uthread *uthread);
177void audit_subcall_exit(int error,
178 struct uthread *uthread);
179
6d2010ae 180extern struct auditinfo_addr *audit_default_aia_p;
b0d623f7
A
181
182/*
183 * The remaining kernel functions are conditionally compiled in as they are
184 * wrapped by a macro, and the macro should be the only place in the source
185 * tree where these functions are referenced.
186 */
187#if CONFIG_AUDIT
188struct ipc_perm;
189struct sockaddr;
190union auditon_udata;
0a7de745
A
191void audit_arg_addr(struct kaudit_record *ar, user_addr_t addr);
192void audit_arg_exit(struct kaudit_record *ar, int status, int retval);
193void audit_arg_len(struct kaudit_record *ar, user_size_t len);
194void audit_arg_fd(struct kaudit_record *ar, int fd);
195void audit_arg_fd2(struct kaudit_record *ar, int fd);
196void audit_arg_fflags(struct kaudit_record *ar, int fflags);
197void audit_arg_gid(struct kaudit_record *ar, gid_t gid);
198void audit_arg_uid(struct kaudit_record *ar, uid_t uid);
199void audit_arg_egid(struct kaudit_record *ar, gid_t egid);
200void audit_arg_euid(struct kaudit_record *ar, uid_t euid);
201void audit_arg_rgid(struct kaudit_record *ar, gid_t rgid);
202void audit_arg_ruid(struct kaudit_record *ar, uid_t ruid);
203void audit_arg_sgid(struct kaudit_record *ar, gid_t sgid);
204void audit_arg_suid(struct kaudit_record *ar, uid_t suid);
205void audit_arg_groupset(struct kaudit_record *ar, gid_t *gidset,
206 u_int gidset_size);
207void audit_arg_login(struct kaudit_record *ar, char *login);
208void audit_arg_ctlname(struct kaudit_record *ar, int *name, int namelen);
209void audit_arg_mask(struct kaudit_record *ar, int mask);
210void audit_arg_mode(struct kaudit_record *ar, mode_t mode);
211void audit_arg_value32(struct kaudit_record *ar, uint32_t value32);
212void audit_arg_value64(struct kaudit_record *ar, uint64_t value64);
213void audit_arg_owner(struct kaudit_record *ar, uid_t uid, gid_t gid);
214void audit_arg_pid(struct kaudit_record *ar, pid_t pid);
215void audit_arg_process(struct kaudit_record *ar, proc_t p);
216void audit_arg_signum(struct kaudit_record *ar, u_int signum);
217void audit_arg_socket(struct kaudit_record *ar, int sodomain, int sotype,
218 int soprotocol);
219void audit_arg_sockaddr(struct kaudit_record *ar, struct vnode *cwd_vp,
220 struct sockaddr *so);
221void audit_arg_auid(struct kaudit_record *ar, uid_t auid);
222void audit_arg_auditinfo(struct kaudit_record *ar,
223 struct auditinfo *au_info);
224void audit_arg_auditinfo_addr(struct kaudit_record *ar,
225 struct auditinfo_addr *au_info);
226void audit_arg_upath(struct kaudit_record *ar, struct vnode *cwd_vp,
227 char *upath, u_int64_t flags);
228void audit_arg_kpath(struct kaudit_record *ar,
229 char *kpath, u_int64_t flags);
230void audit_arg_vnpath(struct kaudit_record *ar, struct vnode *vp,
231 u_int64_t flags);
232void audit_arg_vnpath_withref(struct kaudit_record *ar, struct vnode *vp,
233 u_int64_t flags);
234void audit_arg_text(struct kaudit_record *ar, char *text);
235void audit_arg_opaque(struct kaudit_record *ar, void *data, size_t size);
236void audit_arg_data(struct kaudit_record *ar, void *data, size_t size,
237 size_t number);
238void audit_arg_cmd(struct kaudit_record *ar, int cmd);
239void audit_arg_svipc_cmd(struct kaudit_record *ar, int cmd);
240void audit_arg_svipc_perm(struct kaudit_record *ar, struct ipc_perm *perm);
241void audit_arg_svipc_id(struct kaudit_record *ar, int id);
242void audit_arg_svipc_addr(struct kaudit_record *ar, user_addr_t addr);
243void audit_arg_posix_ipc_perm(struct kaudit_record *ar, uid_t uid,
244 gid_t gid, mode_t mode);
245void audit_arg_auditon(struct kaudit_record *ar,
246 union auditon_udata *udata);
247void audit_arg_file(struct kaudit_record *ar, struct proc *p,
248 struct fileproc *fp);
249void audit_arg_argv(struct kaudit_record *ar, char *argv, int argc,
250 int length);
251void audit_arg_envv(struct kaudit_record *ar, char *envv, int envc,
252 int length);
253void audit_arg_identity(struct kaudit_record *ar);
254
255void audit_arg_mach_port1(struct kaudit_record *ar, mach_port_name_t port);
256void audit_arg_mach_port2(struct kaudit_record *ar, mach_port_name_t port);
257void audit_sysclose(struct kaudit_record *ar, struct proc *p, int fd);
b0d623f7
A
258
259void audit_proc_coredump(proc_t proc, char *path, int errcode);
0a7de745
A
260void audit_proc_init(struct proc *p);
261void audit_proc_fork(struct proc *parent, struct proc *child);
262void audit_proc_free(struct proc *p);
b0d623f7
A
263
264#ifndef _KAUTH_CRED_T
0a7de745 265#define _KAUTH_CRED_T
b0d623f7
A
266struct ucred;
267typedef struct ucred *kauth_cred_t;
268#endif /* !_KAUTH_CRED_T */
269
0a7de745
A
270void audit_session_ref(kauth_cred_t cred);
271void audit_session_unref(kauth_cred_t cred);
272void audit_session_procnew(proc_t p);
273void audit_session_procexit(proc_t p);
274int audit_session_spawnjoin(proc_t p, task_t task, ipc_port_t port);
b0d623f7 275
0a7de745
A
276void audit_sdev_submit(au_id_t auid, au_asid_t asid, void *record,
277 u_int record_len);
6d2010ae 278
b0d623f7 279/*
0a7de745 280 * Audit session macros.
b0d623f7 281 */
0a7de745 282#define IS_VALID_SESSION(a) ((a) != NULL && (a) != audit_default_aia_p)
b0d623f7 283
0a7de745
A
284#define AUDIT_SESSION_REF(cred) audit_session_ref(cred)
285#define AUDIT_SESSION_UNREF(cred) audit_session_unref(cred)
b0d623f7 286
0a7de745
A
287#define AUDIT_SESSION_PROCNEW(p) audit_session_procnew(p)
288#define AUDIT_SESSION_PROCEXIT(p) audit_session_procexit(p)
b0d623f7
A
289
290#if CONFIG_MACF
0a7de745 291/*
b0d623f7
A
292 * audit_mac_data() is the MAC Framework's entry point to the audit subsystem.
293 * It currently creates only text and data audit tokens.
294 */
0a7de745
A
295int audit_mac_data(int type, int len, u_char *data);
296void audit_arg_mac_string(struct kaudit_record *ar, char *string);
b0d623f7
A
297
298#endif
299
300extern au_event_t sys_au_event[];
301
0a7de745 302#define AUDIT_RECORD() \
b0d623f7
A
303 ((struct uthread*)get_bsdthread_info(current_thread()))->uu_ar
304
0a7de745
A
305#ifndef AUDIT_USE_BUILTIN_EXPECT
306#define AUDIT_USE_BUILTIN_EXPECT
b0d623f7
A
307#endif
308
0a7de745 309#ifdef AUDIT_USE_BUILTIN_EXPECT
b0d623f7 310/*
0a7de745 311 * Use branch prediction for the case of auditing enabled but not
b0d623f7
A
312 * auditing system calls.
313 */
0a7de745
A
314#define AUDIT_SYSCALLS() __builtin_expect(audit_syscalls, 0)
315#define AUDIT_ENABLED() __builtin_expect(audit_syscalls && \
316 audit_enabled, 0)
317#define AUDIT_AUDITING(x) __builtin_expect(NULL != (x), 0)
b0d623f7
A
318
319#else
320
0a7de745
A
321#define AUDIT_SYSCALLS() (audit_syscalls)
322#define AUDIT_ENABLED() (audit_syscalls && audit_enabled)
323#define AUDIT_AUDITING(x) (NULL != (x))
b0d623f7
A
324
325#endif /* AUDIT_USE_BUILTIN_EXPECT */
326
b226f5e5
A
327#define AUDIT_RECORD_EXISTS() (AUDIT_ENABLED() && AUDIT_AUDITING(AUDIT_RECORD()))
328
b0d623f7
A
329/*
330 * Define a macro to wrap the audit_arg_* calls by checking the global
331 * audit_enabled flag before performing the actual call.
332 */
0a7de745
A
333#define AUDIT_ARG(op, args...) do { \
334 if (AUDIT_SYSCALLS()) { \
335 struct kaudit_record *__ar = AUDIT_RECORD(); \
336 if (AUDIT_AUDITING(__ar)) \
337 audit_arg_ ## op (__ar, ## args); \
338 } \
b0d623f7
A
339} while (0)
340
0a7de745
A
341#define AUDIT_SYSCALL_ENTER(args...) do { \
342 if (AUDIT_ENABLED()) { \
343 audit_syscall_enter(args); \
344 } \
b0d623f7
A
345} while (0)
346
347/*
348 * Wrap the audit_syscall_exit() function so that it is called only when
0a7de745
A
349 * we have a audit record on the thread. Audit records can persist after
350 * auditing is disabled, so we don't just check audit_enabled here.
b0d623f7 351 */
0a7de745
A
352#define AUDIT_SYSCALL_EXIT(code, proc, uthread, error) do { \
353 if (AUDIT_AUDITING(uthread->uu_ar)) \
354 audit_syscall_exit(code, error, proc, uthread); \
b0d623f7
A
355} while (0)
356
94ff46dc
A
357#define AUDIT_SUBCALL_ENTER(event, proc, uthread) do { \
358 if (AUDIT_ENABLED()) \
359 audit_subcall_enter(AUE_ ## event, proc, uthread); \
360} while (0)
361
362#define AUDIT_SUBCALL_EXIT(uthread, error) do { \
363 if (AUDIT_AUDITING(uthread->uu_ar)) \
364 audit_subcall_exit(error, uthread); \
365} while (0)
366
b0d623f7
A
367/*
368 * Wrap the audit_mach_syscall_enter() and audit_mach_syscall_exit()
369 * functions in a manner similar to other system call enter/exit functions.
370 */
0a7de745
A
371#define AUDIT_MACH_SYSCALL_ENTER(args...) do { \
372 if (AUDIT_ENABLED()) { \
373 audit_mach_syscall_enter(args); \
374 } \
b0d623f7
A
375} while (0)
376
0a7de745
A
377#define AUDIT_MACH_SYSCALL_EXIT(retval) do { \
378 if (AUDIT_SYSCALLS()) { \
379 struct uthread *__uthread = \
380 get_bsdthread_info(current_thread()); \
381 if (AUDIT_AUDITING(__uthread->uu_ar)) \
382 audit_mach_syscall_exit(retval, __uthread); \
383 } \
b0d623f7
A
384} while (0)
385
386/*
387 * A Macro to wrap the audit_sysclose() function.
388 */
0a7de745
A
389#define AUDIT_SYSCLOSE(args...) do { \
390 if (AUDIT_SYSCALLS()) { \
391 struct kaudit_record *__ar = AUDIT_RECORD(); \
392 if (AUDIT_AUDITING(__ar)) \
393 audit_sysclose(__ar, args); \
394 } \
b0d623f7
A
395} while (0)
396
397#else /* !CONFIG_AUDIT */
398
0a7de745 399#define AUDIT_ARG(op, args...) do { \
b0d623f7
A
400} while (0)
401
0a7de745 402#define AUDIT_SYSCALL_ENTER(args...) do { \
b0d623f7
A
403} while (0)
404
0a7de745 405#define AUDIT_SYSCALL_EXIT(code, proc, uthread, error) do { \
b0d623f7
A
406} while (0)
407
94ff46dc
A
408#define AUDIT_SUBCALL_ENTER(event, proc, uthread) do { \
409} while (0)
410
411#define AUDIT_SUBCALL_EXIT(uthread, error) do { \
412} while (0)
413
0a7de745 414#define AUDIT_MACH_SYSCALL_ENTER(args...) do { \
b0d623f7
A
415} while (0)
416
0a7de745 417#define AUDIT_MACH_SYSCALL_EXIT(retval) do { \
b0d623f7
A
418} while (0)
419
0a7de745 420#define AUDIT_SYSCLOSE(op, args...) do { \
b0d623f7
A
421} while (0)
422
0a7de745 423#define AUDIT_SESSION_REF(cred) do { \
b0d623f7
A
424} while (0)
425
0a7de745 426#define AUDIT_SESSION_UNREF(cred) do { \
b0d623f7
A
427} while (0)
428
0a7de745 429#define AUDIT_SESSION_PROCNEW(cred) do { \
b0d623f7
A
430} while (0)
431
0a7de745 432#define AUDIT_SESSION_PROCEXIT(cred) do { \
b0d623f7
A
433} while (0)
434
0a7de745 435#define AUDIT_SESSION_REF(cred) do { \
b0d623f7
A
436} while (0)
437
0a7de745 438#define AUDIT_SESSION_UNREF(cred) do { \
b0d623f7
A
439} while (0)
440
0a7de745 441#define AUDIT_SESSION_PROCNEW(cred) do { \
b0d623f7
A
442} while (0)
443
0a7de745 444#define AUDIT_SESSION_PROCEXIT(cred) do { \
b0d623f7
A
445} while (0)
446
447#endif /* CONFIG_AUDIT */
448
449#endif /* KERNEL */
450
451#endif /* !_SECURITY_AUDIT_ADUIT_H */