]> git.saurik.com Git - apple/xnu.git/blob - bsd/bsm/audit_kernel.h
ba94c3e177685ef98efce1ff6443459198ee2f40
[apple/xnu.git] / bsd / bsm / audit_kernel.h
1 /*
2 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
3 *
4 * This file contains Original Code and/or Modifications of Original Code
5 * as defined in and that are subject to the Apple Public Source License
6 * Version 2.0 (the 'License'). You may not use this file except in
7 * compliance with the License. The rights granted to you under the
8 * License may not be used to create, or enable the creation or
9 * redistribution of, unlawful or unlicensed copies of an Apple operating
10 * system, or to circumvent, violate, or enable the circumvention or
11 * violation of, any terms of an Apple operating system software license
12 * agreement.
13 *
14 * Please obtain a copy of the License at
15 * http://www.opensource.apple.com/apsl/ and read it before using this
16 * file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
27 */
28
29 #ifndef _BSM_AUDIT_KERNEL_H
30 #define _BSM_AUDIT_KERNEL_H
31
32 #ifdef KERNEL
33
34 #include <bsm/audit.h>
35
36 #include <sys/sysctl.h>
37 #include <sys/user.h>
38 #include <sys/ipc.h>
39
40 /*
41 * Audit subsystem condition flags. The audit_enabled flag is set and
42 * removed automatically as a result of configuring log files, and
43 * can be observed but should not be directly manipulated. The audit
44 * suspension flag permits audit to be temporarily disabled without
45 * reconfiguring the audit target.
46 */
47 extern int audit_enabled;
48 extern int audit_suspended;
49
50 #define BSM_SUCCESS 0
51 #define BSM_FAILURE 1
52 #define BSM_NOAUDIT 2
53
54 /*
55 * Define the masks for the audited arguments.
56 */
57 #define ARG_EUID 0x0000000000000001ULL
58 #define ARG_RUID 0x0000000000000002ULL
59 #define ARG_SUID 0x0000000000000004ULL
60 #define ARG_EGID 0x0000000000000008ULL
61 #define ARG_RGID 0x0000000000000010ULL
62 #define ARG_SGID 0x0000000000000020ULL
63 #define ARG_PID 0x0000000000000040ULL
64 #define ARG_UID 0x0000000000000080ULL
65 #define ARG_AUID 0x0000000000000100ULL
66 #define ARG_GID 0x0000000000000200ULL
67 #define ARG_FD 0x0000000000000400ULL
68 #define ARG_POSIX_IPC_PERM 0x0000000000000800ULL
69 #define ARG_FFLAGS 0x0000000000001000ULL
70 #define ARG_MODE 0x0000000000002000ULL
71 #define ARG_DEV 0x0000000000004000ULL
72 #define ARG_ADDR 0x0000000000008000ULL
73 #define ARG_LEN 0x0000000000010000ULL
74 #define ARG_MASK 0x0000000000020000ULL
75 #define ARG_SIGNUM 0x0000000000040000ULL
76 #define ARG_LOGIN 0x0000000000080000ULL
77 #define ARG_SADDRINET 0x0000000000100000ULL
78 #define ARG_SADDRINET6 0x0000000000200000ULL
79 #define ARG_SADDRUNIX 0x0000000000400000ULL
80 #define ARG_KPATH1 0x0000000000800000ULL
81 #define ARG_KPATH2 0x0000000001000000ULL
82 #define ARG_UPATH1 0x0000000002000000ULL
83 #define ARG_UPATH2 0x0000000004000000ULL
84 #define ARG_TEXT 0x0000000008000000ULL
85 #define ARG_VNODE1 0x0000000010000000ULL
86 #define ARG_VNODE2 0x0000000020000000ULL
87 #define ARG_SVIPC_CMD 0x0000000040000000ULL
88 #define ARG_SVIPC_PERM 0x0000000080000000ULL
89 #define ARG_SVIPC_ID 0x0000000100000000ULL
90 #define ARG_SVIPC_ADDR 0x0000000200000000ULL
91 #define ARG_GROUPSET 0x0000000400000000ULL
92 #define ARG_CMD 0x0000000800000000ULL
93 #define ARG_SOCKINFO 0x0000001000000000ULL
94 #define ARG_ASID 0x0000002000000000ULL
95 #define ARG_TERMID 0x0000004000000000ULL
96 #define ARG_AUDITON 0x0000008000000000ULL
97 #define ARG_VALUE 0x0000010000000000ULL
98 #define ARG_AMASK 0x0000020000000000ULL
99 #define ARG_CTLNAME 0x0000040000000000ULL
100 #define ARG_PROCESS 0x0000080000000000ULL
101 #define ARG_MACHPORT1 0x0000100000000000ULL
102 #define ARG_MACHPORT2 0x0000200000000000ULL
103 #define ARG_NONE 0x0000000000000000ULL
104 #define ARG_ALL 0xFFFFFFFFFFFFFFFFULL
105
106 /* Defines for the kernel audit record k_ar_commit field */
107 #define AR_COMMIT_KERNEL 0x00000001U
108 #define AR_COMMIT_USER 0x00000010U
109
110 struct vnode_au_info {
111 mode_t vn_mode;
112 uid_t vn_uid;
113 gid_t vn_gid;
114 dev_t vn_dev;
115 long vn_fsid;
116 long vn_fileid;
117 long vn_gen;
118 };
119
120 struct groupset {
121 gid_t gidset[NGROUPS];
122 u_int gidset_size;
123 };
124
125 struct socket_au_info {
126 int so_domain;
127 int so_type;
128 int so_protocol;
129 in_addr_t so_raddr; /* remote address if INET socket */
130 in_addr_t so_laddr; /* local address if INET socket */
131 u_short so_rport; /* remote port */
132 u_short so_lport; /* local port */
133 };
134
135 union auditon_udata {
136 char au_path[MAXPATHLEN];
137 long au_cond;
138 long au_flags;
139 long au_policy;
140 au_evclass_map_t au_evclass;
141 au_mask_t au_mask;
142 auditinfo_t au_auinfo;
143 auditpinfo_t au_aupinfo;
144 auditpinfo_addr_t au_aupinfo_addr;
145 au_qctrl_t au_qctrl;
146 au_stat_t au_stat;
147 au_fstat_t au_fstat;
148 };
149
150 struct posix_ipc_perm {
151 uid_t pipc_uid;
152 gid_t pipc_gid;
153 mode_t pipc_mode;
154 };
155
156 struct audit_record {
157 /* Audit record header. */
158 u_int32_t ar_magic;
159 int ar_event;
160 int ar_retval; /* value returned to the process */
161 int ar_errno; /* return status of system call */
162 struct timespec ar_starttime;
163 struct timespec ar_endtime;
164 u_int64_t ar_valid_arg; /* Bitmask of valid arguments */
165
166 /* Audit subject information. */
167 struct xucred ar_subj_cred;
168 uid_t ar_subj_ruid;
169 gid_t ar_subj_rgid;
170 gid_t ar_subj_egid;
171 uid_t ar_subj_auid; /* Audit user ID */
172 pid_t ar_subj_asid; /* Audit session ID */
173 pid_t ar_subj_pid;
174 struct au_tid ar_subj_term;
175 char ar_subj_comm[MAXCOMLEN + 1];
176 struct au_mask ar_subj_amask;
177
178 /* Operation arguments. */
179 uid_t ar_arg_euid;
180 uid_t ar_arg_ruid;
181 uid_t ar_arg_suid;
182 gid_t ar_arg_egid;
183 gid_t ar_arg_rgid;
184 gid_t ar_arg_sgid;
185 pid_t ar_arg_pid;
186 pid_t ar_arg_asid;
187 struct au_tid ar_arg_termid;
188 uid_t ar_arg_uid;
189 uid_t ar_arg_auid;
190 gid_t ar_arg_gid;
191 struct groupset ar_arg_groups;
192 int ar_arg_fd;
193 int ar_arg_fflags;
194 mode_t ar_arg_mode;
195 int ar_arg_dev;
196 long ar_arg_value;
197 void * ar_arg_addr;
198 int ar_arg_len;
199 int ar_arg_mask;
200 u_int ar_arg_signum;
201 char ar_arg_login[MAXLOGNAME];
202 int ar_arg_ctlname[CTL_MAXNAME];
203 struct sockaddr ar_arg_sockaddr;
204 struct socket_au_info ar_arg_sockinfo;
205 char *ar_arg_upath1;
206 char *ar_arg_upath2;
207 char *ar_arg_kpath1;
208 char *ar_arg_kpath2;
209 char *ar_arg_text;
210 struct au_mask ar_arg_amask;
211 struct vnode_au_info ar_arg_vnode1;
212 struct vnode_au_info ar_arg_vnode2;
213 int ar_arg_cmd;
214 int ar_arg_svipc_cmd;
215 struct ipc_perm ar_arg_svipc_perm;
216 int ar_arg_svipc_id;
217 void * ar_arg_svipc_addr;
218 struct posix_ipc_perm ar_arg_pipc_perm;
219 mach_port_name_t ar_arg_mach_port1;
220 mach_port_name_t ar_arg_mach_port2;
221 union auditon_udata ar_arg_auditon;
222 };
223
224 /*
225 * In-kernel version of audit record; the basic record plus queue meta-data.
226 * This record can also have a pointer set to some opaque data that will
227 * be passed through to the audit writing mechanism.
228 */
229 struct kaudit_record {
230 struct audit_record k_ar;
231 u_int32_t k_ar_commit;
232 void *k_udata; /* user data */
233 u_int k_ulen; /* user data length */
234 struct uthread *k_uthread; /* thread we are auditing */
235 TAILQ_ENTRY(kaudit_record) k_q;
236 };
237
238 struct proc;
239 struct vnode;
240 struct componentname;
241
242 void audit_abort(struct kaudit_record *ar);
243 void audit_commit(struct kaudit_record *ar, int error,
244 int retval);
245 void audit_init(void);
246 void audit_shutdown(void);
247
248 struct kaudit_record *audit_new(int event, struct proc *p,
249 struct uthread *uthread);
250
251 void audit_syscall_enter(unsigned short code,
252 struct proc *proc, struct uthread *uthread);
253 void audit_syscall_exit(int error, struct proc *proc,
254 struct uthread *uthread);
255 void audit_mach_syscall_enter(unsigned short audit_event);
256 void audit_mach_syscall_exit(int retval,
257 struct uthread *uthread);
258
259 int kaudit_to_bsm(struct kaudit_record *kar,
260 struct au_record **pau);
261
262 int bsm_rec_verify(void *rec);
263
264 /*
265 * Kernel versions of the BSM audit record functions.
266 */
267 struct au_record *kau_open(void);
268 int kau_write(struct au_record *rec, token_t *m);
269 int kau_close(struct au_record *rec,
270 struct timespec *endtime, short event);
271 void kau_free(struct au_record *rec);
272 void kau_init(void);
273 token_t *kau_to_file(const char *file, const struct timeval *tv);
274 token_t *kau_to_header(const struct timespec *ctime, int rec_size,
275 au_event_t e_type, au_emod_t e_mod);
276 token_t *kau_to_header32(const struct timespec *ctime, int rec_size,
277 au_event_t e_type, au_emod_t e_mod);
278 token_t *kau_to_header64(const struct timespec *ctime, int rec_size,
279 au_event_t e_type, au_emod_t e_mod);
280 /*
281 * The remaining kernel functions are conditionally compiled in as they
282 * are wrapped by a macro, and the macro should be the only place in
283 * the source tree where these functions are referenced.
284 */
285 #ifdef AUDIT
286 void audit_arg_addr(user_addr_t addr);
287 void audit_arg_len(user_size_t len);
288 void audit_arg_fd(int fd);
289 void audit_arg_fflags(int fflags);
290 void audit_arg_gid(gid_t gid, gid_t egid, gid_t rgid,
291 gid_t sgid);
292 void audit_arg_uid(uid_t uid, uid_t euid, uid_t ruid,
293 uid_t suid);
294 void audit_arg_groupset(const gid_t *gidset, u_int gidset_size);
295 void audit_arg_login(const char *login);
296 void audit_arg_ctlname(const int *name, int namelen);
297 void audit_arg_mask(int mask);
298 void audit_arg_mode(mode_t mode);
299 void audit_arg_dev(int dev);
300 void audit_arg_value(long value);
301 void audit_arg_owner(uid_t uid, gid_t gid);
302 void audit_arg_pid(pid_t pid);
303 void audit_arg_process(struct proc *p);
304 void audit_arg_signum(u_int signum);
305 void audit_arg_socket(int sodomain, int sotype,
306 int soprotocol);
307 void audit_arg_sockaddr(struct proc *p,
308 struct sockaddr *so);
309 void audit_arg_auid(uid_t auid);
310 void audit_arg_auditinfo(const struct auditinfo *au_info);
311 void audit_arg_upath(struct proc *p, char *upath,
312 u_int64_t flags);
313 void audit_arg_vnpath(struct vnode *vp, u_int64_t flags);
314 void audit_arg_vnpath_withref(struct vnode *vp, u_int64_t flags);
315 void audit_arg_text(const char *text);
316 void audit_arg_cmd(int cmd);
317 void audit_arg_svipc_cmd(int cmd);
318 void audit_arg_svipc_perm(const struct ipc_perm *perm);
319 void audit_arg_svipc_id(int id);
320 void audit_arg_svipc_addr(void *addr);
321 void audit_arg_posix_ipc_perm(uid_t uid, gid_t gid,
322 mode_t mode);
323 void audit_arg_auditon(const union auditon_udata *udata);
324 void audit_arg_file(struct proc *p, const struct fileproc *fp);
325 void audit_arg_mach_port1(mach_port_name_t port);
326 void audit_arg_mach_port2(mach_port_name_t port);
327
328 void audit_sysclose(struct proc *p, int fd);
329
330 void audit_proc_init(struct proc *p);
331 void audit_proc_fork(struct proc *parent,
332 struct proc *child);
333 void audit_proc_free(struct proc *p);
334
335 /*
336 * Define a macro to wrap the audit_arg_* calls by checking the global
337 * audit_enabled flag before performing the actual call.
338 */
339 #define AUDIT_ARG(op, args...) do { \
340 if (audit_enabled) \
341 audit_arg_ ## op (args); \
342 } while (0)
343
344 #define AUDIT_SYSCALL_ENTER(args...) do { \
345 if (audit_enabled) { \
346 audit_syscall_enter(args); \
347 } \
348 } while (0)
349
350 /*
351 * Wrap the audit_syscall_exit() function so that it is called only when
352 * auditing is enabled, or we have a audit record on the thread. It is
353 * possible that an audit record was begun before auditing was turned off.
354 */
355 #define AUDIT_SYSCALL_EXIT(error, proc, uthread) do { \
356 if (audit_enabled || (uthread->uu_ar != NULL)) { \
357 audit_syscall_exit(error, proc, uthread); \
358 } \
359 } while (0)
360
361 /*
362 * Wrap the audit_mach_syscall_enter() and audit_mach_syscall_exit()
363 * functions in a manner similar to other system call enter/exit functions.
364 */
365 #define AUDIT_MACH_SYSCALL_ENTER(args...) do { \
366 if (audit_enabled) { \
367 audit_mach_syscall_enter(args); \
368 } \
369 } while (0)
370
371 #define AUDIT_MACH_SYSCALL_EXIT(retval) do { \
372 struct uthread *__uthread = get_bsdthread_info(current_thread()); \
373 if (audit_enabled || (__uthread->uu_ar != NULL)) { \
374 audit_mach_syscall_exit(retval, __uthread); \
375 } \
376 } while (0)
377
378 /*
379 * A Macro to wrap the audit_sysclose() function.
380 */
381 #define AUDIT_SYSCLOSE(args...) do { \
382 if (audit_enabled) \
383 audit_sysclose(args); \
384 } while (0)
385
386 #else /* !AUDIT */
387 #define AUDIT_SYSCALL_ENTER(args...) do { \
388 } while (0)
389
390 #define AUDIT_SYSCALL_EXIT(error, proc, uthread) do { \
391 } while (0)
392
393 #define AUDIT_MACH_SYSCALL_ENTER(args...) do { \
394 } while (0)
395
396 #define AUDIT_MACH_SYSCALL_EXIT(retval) do { \
397 } while (0)
398
399 #define AUDIT_SYSCLOSE(op, args...) do { \
400 } while (0)
401
402 #endif /* AUDIT */
403
404 #endif /* KERNEL */
405
406 #endif /* !_BSM_AUDIT_KERNEL_H */