]> git.saurik.com Git - apple/xnu.git/blob - bsd/security/audit/audit_private.h
xnu-1486.2.11.tar.gz
[apple/xnu.git] / bsd / security / audit / audit_private.h
1 /*-
2 * Copyright (c) 1999-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 /*
30 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
31 * support for mandatory and extensible security protections. This notice
32 * is included in support of clause 2.2 (b) of the Apple Public License,
33 * Version 2.0.
34 */
35
36 /*
37 * This include file contains function prototypes and type definitions used
38 * within the audit implementation.
39 */
40
41 #ifndef _SECURITY_AUDIT_PRIVATE_H_
42 #define _SECURITY_AUDIT_PRIVATE_H_
43
44 #if defined(_KERNEL) || defined(KERNEL)
45
46 #if CONFIG_MACF
47 #include <sys/queue.h>
48 #include <security/mac_framework.h>
49 #endif
50
51 #include <sys/ipc.h>
52 #include <sys/proc_internal.h> /* for PID_MAX */
53 #include <sys/socket.h>
54 #include <sys/ucred.h>
55
56 #ifdef MALLOC_DECLARE
57 MALLOC_DECLARE(M_AUDITBSM);
58 MALLOC_DECLARE(M_AUDITDATA);
59 MALLOC_DECLARE(M_AUDITPATH);
60 MALLOC_DECLARE(M_AUDITTEXT);
61 #endif
62
63 /*
64 * Audit control variables that are usually set/read via system calls and
65 * used to control various aspects of auditing.
66 */
67 extern struct au_qctrl audit_qctrl;
68 extern struct audit_fstat audit_fstat;
69 extern struct au_mask audit_nae_mask;
70 extern int audit_panic_on_write_fail;
71 extern int audit_fail_stop;
72 extern int audit_argv;
73 extern int audit_arge;
74
75 /*
76 * Kernel mask that is used to check to see if system calls need to be audited.
77 */
78 extern au_class_t audit_kevent_mask;
79
80 /*
81 * The macro used to check to see if the system calls need to be auditing.
82 * This will pessimisticly set the audit syscalls flag if the audit kevent
83 * mask has not been created yet. User code should build the event/class
84 * mapping table before setting preselection masks to avoid this.
85 */
86 #define AUDIT_CHECK_IF_KEVENTS_MASK(m) do { \
87 if ((m).am_success || (m).am_failure) \
88 if (!audit_kevent_mask || \
89 (audit_kevent_mask & (m).am_success) || \
90 (audit_kevent_mask & (m).am_failure)) \
91 audit_syscalls = 1; \
92 } while (0)
93
94 /*
95 * Success/failure conditions for the conversion of a kernel audit record to
96 * BSM format.
97 */
98 #define BSM_SUCCESS 0
99 #define BSM_FAILURE 1
100 #define BSM_NOAUDIT 2
101
102 /*
103 * Defines for the kernel audit record k_ar_commit field. Flags are set to
104 * indicate what sort of record it is, and which preselection mechanism
105 * selected it.
106 */
107 #define AR_COMMIT_KERNEL 0x00000001U
108 #define AR_COMMIT_USER 0x00000010U
109
110 #define AR_PRESELECT_TRAIL 0x00001000U
111 #define AR_PRESELECT_PIPE 0x00002000U
112
113 #define AR_PRESELECT_USER_TRAIL 0x00004000U
114 #define AR_PRESELECT_USER_PIPE 0x00008000U
115
116 #define AR_DRAIN_QUEUE 0x80000000U
117
118 /*
119 * Audit data is generated as a stream of struct audit_record structures,
120 * linked by struct kaudit_record, and contain storage for possible audit so
121 * that it will not need to be allocated during the processing of a system
122 * call, both improving efficiency and avoiding sleeping at untimely moments.
123 * This structure is converted to BSM format before being written to disk.
124 */
125 struct vnode_au_info {
126 mode_t vn_mode;
127 uid_t vn_uid;
128 gid_t vn_gid;
129 dev_t vn_dev;
130 long vn_fsid;
131 long vn_fileid;
132 long vn_gen;
133 };
134
135 struct groupset {
136 gid_t gidset[NGROUPS];
137 u_int gidset_size;
138 };
139
140 struct socket_au_info {
141 int sai_domain;
142 int sai_type;
143 int sai_protocol;
144
145 /* Foreign (remote) address/port. */
146 struct sockaddr_storage sai_faddr;
147
148 /* Local address/port. */
149 struct sockaddr_storage sai_laddr;
150 };
151
152 /*
153 * The following is used for A_OLDSETQCTRL and A_OLDGETQCTRL and a 64-bit
154 * userland.
155 */
156 struct au_qctrl64 {
157 u_int64_t aq64_hiwater;
158 u_int64_t aq64_lowater;
159 u_int64_t aq64_bufsz;
160 u_int64_t aq64_delay;
161 int64_t aq64_minfree;
162 };
163 typedef struct au_qctrl64 au_qctrl64_t;
164
165 union auditon_udata {
166 char *au_path;
167 int au_cond;
168 int au_policy;
169 int64_t au_cond64;
170 int64_t au_policy64;
171 int au_trigger;
172 au_evclass_map_t au_evclass;
173 au_mask_t au_mask;
174 auditinfo_t au_auinfo;
175 auditpinfo_t au_aupinfo;
176 auditpinfo_addr_t au_aupinfo_addr;
177 au_qctrl_t au_qctrl;
178 au_qctrl64_t au_qctrl64;
179 au_stat_t au_stat;
180 au_fstat_t au_fstat;
181 auditinfo_addr_t au_kau_info;
182 };
183
184 struct posix_ipc_perm {
185 uid_t pipc_uid;
186 gid_t pipc_gid;
187 mode_t pipc_mode;
188 };
189
190 struct audit_record {
191 /* Audit record header. */
192 u_int32_t ar_magic;
193 int ar_event;
194 int ar_retval; /* value returned to the process */
195 int ar_errno; /* return status of system call */
196 struct timespec ar_starttime;
197 struct timespec ar_endtime;
198 u_int64_t ar_valid_arg; /* Bitmask of valid arguments */
199
200 /* Audit subject information. */
201 struct xucred ar_subj_cred;
202 uid_t ar_subj_ruid;
203 gid_t ar_subj_rgid;
204 gid_t ar_subj_egid;
205 uid_t ar_subj_auid; /* Audit user ID */
206 pid_t ar_subj_asid; /* Audit session ID */
207 pid_t ar_subj_pid;
208 struct au_tid ar_subj_term;
209 struct au_tid_addr ar_subj_term_addr;
210 struct au_mask ar_subj_amask;
211
212 /* Operation arguments. */
213 uid_t ar_arg_euid;
214 uid_t ar_arg_ruid;
215 uid_t ar_arg_suid;
216 gid_t ar_arg_egid;
217 gid_t ar_arg_rgid;
218 gid_t ar_arg_sgid;
219 pid_t ar_arg_pid;
220 pid_t ar_arg_asid;
221 struct au_tid ar_arg_termid;
222 struct au_tid_addr ar_arg_termid_addr;
223 uid_t ar_arg_uid;
224 uid_t ar_arg_auid;
225 gid_t ar_arg_gid;
226 struct groupset ar_arg_groups;
227 int ar_arg_fd;
228 int ar_arg_fflags;
229 mode_t ar_arg_mode;
230 uint32_t ar_arg_value32;
231 uint64_t ar_arg_value64;
232 user_addr_t ar_arg_addr;
233 user_size_t ar_arg_len;
234 int ar_arg_mask;
235 u_int ar_arg_signum;
236 char ar_arg_login[MAXLOGNAME];
237 int ar_arg_ctlname[CTL_MAXNAME];
238 struct socket_au_info ar_arg_sockinfo;
239 char *ar_arg_upath1;
240 char *ar_arg_upath2;
241 char *ar_arg_kpath1; /* darwin-only */
242 char *ar_arg_kpath2; /* darwin-only */
243 #if CONFIG_MACF
244 char *ar_vnode1_mac_labels;
245 char *ar_vnode2_mac_labels;
246 char *ar_cred_mac_labels;
247 char *ar_arg_mac_string;
248 #endif
249 char *ar_arg_text;
250 void *ar_arg_opaque; /* darwin-only */
251 void *ar_arg_data; /* darwin-only */
252 u_int16_t ar_arg_opq_size; /* darwin-only */
253 u_char ar_arg_data_type; /* darwin-only */
254 u_char ar_arg_data_count; /* darwin-only */
255 struct au_mask ar_arg_amask;
256 struct vnode_au_info ar_arg_vnode1;
257 struct vnode_au_info ar_arg_vnode2;
258 int ar_arg_cmd;
259 int ar_arg_svipc_cmd;
260 struct ipc_perm ar_arg_svipc_perm;
261 int ar_arg_svipc_id;
262 user_addr_t ar_arg_svipc_addr;
263 struct posix_ipc_perm ar_arg_pipc_perm;
264 mach_port_name_t ar_arg_mach_port1; /* darwin-only */
265 mach_port_name_t ar_arg_mach_port2; /* darwin-only */
266 union auditon_udata ar_arg_auditon;
267 char *ar_arg_argv;
268 int ar_arg_argc;
269 char *ar_arg_envv;
270 int ar_arg_envc;
271 int ar_arg_exitstatus;
272 int ar_arg_exitretval;
273 struct sockaddr_storage ar_arg_sockaddr;
274
275 #if CONFIG_MACF
276 /*
277 * MAC security related fields added by MAC policies ar_forced_by_mac
278 * is 1 if mac_audit_check_preselect() forced this call to be audited,
279 * 0 otherwise.
280 */
281 LIST_HEAD(mac_audit_record_list_t, mac_audit_record) *ar_mac_records;
282 int ar_forced_by_mac;
283 #endif
284 };
285
286 /*
287 * Arguments in the audit record are initially not defined; flags are set to
288 * indicate if they are present so they can be included in the audit log
289 * stream only if defined.
290 */
291 #define ARG_IS_VALID(kar, arg) ((kar)->k_ar.ar_valid_arg & (arg))
292 #define ARG_SET_VALID(kar, arg) do { \
293 (kar)->k_ar.ar_valid_arg |= (arg); \
294 } while (0)
295
296 /*
297 * Current thread macro. get_bsdthread_info() returns a void ptr for some
298 * reason.
299 */
300 #define curthread() ((struct uthread *)get_bsdthread_info(current_thread()))
301
302 /*
303 * In-kernel version of audit record; the basic record plus queue meta-data.
304 * This record can also have a pointer set to some opaque data that will be
305 * passed through to the audit writing mechanism.
306 */
307 struct kaudit_record {
308 struct audit_record k_ar;
309 u_int32_t k_ar_commit;
310 void *k_udata; /* User data. */
311 u_int k_ulen; /* User data length. */
312 struct uthread *k_uthread; /* Audited thread. */
313 TAILQ_ENTRY(kaudit_record) k_q;
314 };
315 TAILQ_HEAD(kaudit_queue, kaudit_record);
316
317 /*
318 * Functions to manage the allocation, release, and commit of kernel audit
319 * records.
320 */
321 void audit_abort(struct kaudit_record *ar);
322 void audit_commit(struct kaudit_record *ar, int error,
323 int retval);
324 struct kaudit_record *audit_new(int event, proc_t p, struct uthread *td);
325
326 /*
327 * Functions relating to the conversion of internal kernel audit records to
328 * the BSM file format.
329 */
330 struct au_record;
331 int kaudit_to_bsm(struct kaudit_record *kar, struct au_record **pau);
332 int bsm_rec_verify(void *rec);
333
334 /*
335 * Kernel versions of the libbsm audit record functions.
336 */
337 void kau_free(struct au_record *rec);
338 void kau_init(void);
339
340 /*
341 * Return values for pre-selection and post-selection decisions.
342 */
343 #define AU_PRS_SUCCESS 1
344 #define AU_PRS_FAILURE 2
345 #define AU_PRS_BOTH (AU_PRS_SUCCESS|AU_PRS_FAILURE)
346
347 /*
348 * Data structures relating to the kernel audit queue. Ideally, these might
349 * be abstracted so that only accessor methods are exposed.
350 */
351 extern struct mtx audit_mtx;
352 extern struct cv audit_watermark_cv;
353 extern struct cv audit_worker_cv;
354 extern struct cv audit_drain_cv;
355 extern struct kaudit_queue audit_q;
356 extern int audit_q_len;
357 extern int audit_pre_q_len;
358 extern int audit_in_failure;
359
360 /*
361 * Flags to use on audit files when opening and closing.
362 */
363 #define AUDIT_OPEN_FLAGS (FWRITE | O_APPEND)
364 #define AUDIT_CLOSE_FLAGS (FWRITE | O_APPEND)
365
366 #include <sys/fcntl.h>
367 #include <sys/kernel.h>
368 #include <sys/malloc.h>
369
370 /*
371 * Some of the BSM tokenizer functions take different parameters in the
372 * kernel implementations in order to save the copying of large kernel data
373 * structures. The prototypes of these functions are declared here.
374 */
375 token_t *kau_to_socket(struct socket_au_info *soi);
376
377 /*
378 * audit_klib prototypes
379 */
380 int au_preselect(au_event_t event, au_class_t class,
381 au_mask_t *mask_p, int sorf);
382 void au_evclassmap_init(void);
383 void au_evclassmap_insert(au_event_t event, au_class_t class);
384 au_class_t au_event_class(au_event_t event);
385 au_event_t audit_ctlname_to_sysctlevent(int name[], uint64_t valid_arg);
386 au_event_t audit_flags_and_error_to_openevent(int oflags, int error);
387 au_event_t audit_flags_and_error_to_openextendedevent(int oflags,
388 int error);
389 au_event_t audit_msgctl_to_event(int cmd);
390 au_event_t audit_semctl_to_event(int cmr);
391 int audit_canon_path(struct vnode *cwd_vp, char *path,
392 char *cpath);
393 au_event_t auditon_command_event(int cmd);
394 au_event_t audit_fcntl_command_event(int cmd, int oflags, int error);
395
396 /*
397 * Audit trigger events notify user space of kernel audit conditions
398 * asynchronously.
399 */
400 int audit_send_trigger(unsigned int trigger);
401
402 /*
403 * Accessor functions to manage global audit state.
404 */
405 void audit_set_kinfo(struct auditinfo_addr *);
406 void audit_get_kinfo(struct auditinfo_addr *);
407
408 /*
409 * General audit related functions.
410 */
411 struct kaudit_record *currecord(void);
412 void audit_free(struct kaudit_record *ar);
413 void audit_rotate_vnode(struct ucred *cred,
414 struct vnode *vp);
415 void audit_worker_init(void);
416
417 /*
418 * Audit pipe functions.
419 */
420 int audit_pipe_init(void);
421 int audit_pipe_shutdown(void);
422 int audit_pipe_preselect(au_id_t auid, au_event_t event,
423 au_class_t class, int sorf, int trail_select);
424 void audit_pipe_submit(au_id_t auid, au_event_t event, au_class_t class,
425 int sorf, int trail_select, void *record, u_int record_len);
426 void audit_pipe_submit_user(void *record, u_int record_len);
427
428 /*
429 * Audit MAC prototypes.
430 */
431 void audit_mac_init(void);
432 int audit_mac_new(proc_t p, struct kaudit_record *ar);
433 void audit_mac_free(struct kaudit_record *ar);
434 int audit_mac_syscall_enter(unsigned short code, proc_t p,
435 struct uthread *uthread, kauth_cred_t my_cred, au_event_t event);
436 int audit_mac_syscall_exit(unsigned short code, struct uthread *uthread,
437 int error, int retval);
438
439 /*
440 * Audit Session.
441 */
442 void audit_session_init(void);
443 int audit_session_setaia(proc_t p, auditinfo_addr_t *aia_p, int newprocess);
444 auditinfo_addr_t *audit_session_update(auditinfo_addr_t *new_aia);
445 int audit_session_lookup(au_asid_t asid, auditinfo_addr_t *ret_aia);
446
447 /*
448 * Kernel assigned audit session IDs start at PID_MAX + 1 and ends at
449 * ASSIGNED_ASID_MAX.
450 */
451 #define ASSIGNED_ASID_MIN (PID_MAX + 1)
452 #define ASSIGNED_ASID_MAX (0xFFFFFFFF - 1)
453
454 #endif /* defined(KERNEL) || defined(_KERNEL) */
455
456 #endif /* ! _SECURITY_AUDIT_PRIVATE_H_ */