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