2 * Copyright (c) 1999-2016 Apple Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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,
37 * This include file contains function prototypes and type definitions used
38 * within the audit implementation.
41 #ifndef _SECURITY_AUDIT_PRIVATE_H_
42 #define _SECURITY_AUDIT_PRIVATE_H_
44 #if defined(_KERNEL) || defined(KERNEL)
47 #include <sys/queue.h>
48 #include <security/mac_framework.h>
52 #include <sys/proc_internal.h> /* for PID_MAX */
53 #include <sys/socket.h>
54 #include <sys/ucred.h>
57 MALLOC_DECLARE(M_AUDITBSM
);
58 MALLOC_DECLARE(M_AUDITDATA
);
59 MALLOC_DECLARE(M_AUDITPATH
);
60 MALLOC_DECLARE(M_AUDITTEXT
);
64 * Audit control variables that are usually set/read via system calls and
65 * used to control various aspects of auditing.
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
;
76 * Kernel mask that is used to check to see if system calls need to be audited.
78 extern au_class_t audit_kevent_mask
;
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.
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)) \
95 * Success/failure conditions for the conversion of a kernel audit record to
100 #define BSM_NOAUDIT 2
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
107 #define AR_COMMIT_KERNEL 0x00000001U
108 #define AR_COMMIT_USER 0x00000010U
110 #define AR_PRESELECT_TRAIL 0x00001000U
111 #define AR_PRESELECT_PIPE 0x00002000U
113 #define AR_PRESELECT_USER_TRAIL 0x00004000U
114 #define AR_PRESELECT_USER_PIPE 0x00008000U
116 #define AR_PRESELECT_FILTER 0x00010000U
118 #define AR_DRAIN_QUEUE 0x80000000U
121 * Audit data is generated as a stream of struct audit_record structures,
122 * linked by struct kaudit_record, and contain storage for possible audit so
123 * that it will not need to be allocated during the processing of a system
124 * call, both improving efficiency and avoiding sleeping at untimely moments.
125 * This structure is converted to BSM format before being written to disk.
127 struct vnode_au_info
{
138 gid_t gidset
[NGROUPS
];
142 struct socket_au_info
{
147 /* Foreign (remote) address/port. */
148 struct sockaddr_storage sai_faddr
;
150 /* Local address/port. */
151 struct sockaddr_storage sai_laddr
;
155 * The following is used for A_OLDSETQCTRL and A_OLDGETQCTRL and a 64-bit
159 u_int64_t aq64_hiwater
;
160 u_int64_t aq64_lowater
;
161 u_int64_t aq64_bufsz
;
162 u_int64_t aq64_delay
;
163 int64_t aq64_minfree
;
165 typedef struct au_qctrl64 au_qctrl64_t
;
167 union auditon_udata
{
174 au_evclass_map_t au_evclass
;
176 au_asflgs_t au_flags
;
177 auditinfo_t au_auinfo
;
178 auditpinfo_t au_aupinfo
;
179 auditpinfo_addr_t au_aupinfo_addr
;
181 au_qctrl64_t au_qctrl64
;
184 auditinfo_addr_t au_kau_info
;
187 struct posix_ipc_perm
{
193 struct audit_record
{
194 /* Audit record header. */
197 int ar_retval
; /* value returned to the process */
198 int ar_errno
; /* return status of system call */
199 struct timespec ar_starttime
;
200 struct timespec ar_endtime
;
201 u_int64_t ar_valid_arg
; /* Bitmask of valid arguments */
203 /* Audit subject information. */
204 struct xucred ar_subj_cred
;
208 uid_t ar_subj_auid
; /* Audit user ID */
209 pid_t ar_subj_asid
; /* Audit session ID */
211 struct au_tid ar_subj_term
;
212 struct au_tid_addr ar_subj_term_addr
;
213 struct au_mask ar_subj_amask
;
215 /* Operation arguments. */
224 struct au_tid ar_arg_termid
;
225 struct au_tid_addr ar_arg_termid_addr
;
229 struct groupset ar_arg_groups
;
233 uint32_t ar_arg_value32
;
234 uint64_t ar_arg_value64
;
235 user_addr_t ar_arg_addr
;
236 user_size_t ar_arg_len
;
239 char ar_arg_login
[MAXLOGNAME
];
240 int ar_arg_ctlname
[CTL_MAXNAME
];
241 struct socket_au_info ar_arg_sockinfo
;
244 char *ar_arg_kpath1
; /* darwin-only */
245 char *ar_arg_kpath2
; /* darwin-only */
247 char *ar_vnode1_mac_labels
;
248 char *ar_vnode2_mac_labels
;
249 char *ar_cred_mac_labels
;
250 char *ar_arg_mac_string
;
253 void *ar_arg_opaque
; /* darwin-only */
254 void *ar_arg_data
; /* darwin-only */
255 u_int16_t ar_arg_opq_size
; /* darwin-only */
256 u_char ar_arg_data_type
; /* darwin-only */
257 u_char ar_arg_data_count
; /* darwin-only */
258 struct au_mask ar_arg_amask
;
259 struct vnode_au_info ar_arg_vnode1
;
260 struct vnode_au_info ar_arg_vnode2
;
262 int ar_arg_svipc_cmd
;
263 struct ipc_perm ar_arg_svipc_perm
;
265 user_addr_t ar_arg_svipc_addr
;
266 struct posix_ipc_perm ar_arg_pipc_perm
;
267 mach_port_name_t ar_arg_mach_port1
; /* darwin-only */
268 mach_port_name_t ar_arg_mach_port2
; /* darwin-only */
269 union auditon_udata ar_arg_auditon
;
274 int ar_arg_exitstatus
;
275 int ar_arg_exitretval
;
276 struct sockaddr_storage ar_arg_sockaddr
;
281 * MAC security related fields added by MAC policies ar_forced_by_mac
282 * is 1 if mac_audit_check_preselect() forced this call to be audited,
285 LIST_HEAD(mac_audit_record_list_t
, mac_audit_record
) *ar_mac_records
;
286 int ar_forced_by_mac
;
291 * Arguments in the audit record are initially not defined; flags are set to
292 * indicate if they are present so they can be included in the audit log
293 * stream only if defined.
295 #define ARG_IS_VALID(kar, arg) ((kar)->k_ar.ar_valid_arg & (arg))
296 #define ARG_SET_VALID(kar, arg) do { \
297 (kar)->k_ar.ar_valid_arg |= (arg); \
301 * Current thread macro. get_bsdthread_info() returns a void ptr for some
304 #define curthread() ((struct uthread *)get_bsdthread_info(current_thread()))
307 * In-kernel version of audit record; the basic record plus queue meta-data.
308 * This record can also have a pointer set to some opaque data that will be
309 * passed through to the audit writing mechanism.
311 struct kaudit_record
{
312 struct audit_record k_ar
;
313 u_int32_t k_ar_commit
;
314 void *k_udata
; /* User data. */
315 u_int k_ulen
; /* User data length. */
316 struct uthread
*k_uthread
; /* Audited thread. */
317 TAILQ_ENTRY(kaudit_record
) k_q
;
319 TAILQ_HEAD(kaudit_queue
, kaudit_record
);
322 * Functions to manage the allocation, release, and commit of kernel audit
325 void audit_abort(struct kaudit_record
*ar
);
326 void audit_commit(struct kaudit_record
*ar
, int error
,
328 struct kaudit_record
*audit_new(int event
, proc_t p
, struct uthread
*td
);
331 * Functions relating to the conversion of internal kernel audit records to
332 * the BSM file format.
335 int kaudit_to_bsm(struct kaudit_record
*kar
, struct au_record
**pau
);
336 int bsm_rec_verify(void *rec
);
339 * Kernel versions of the libbsm audit record functions.
341 void kau_free(struct au_record
*rec
);
345 * Return values for pre-selection and post-selection decisions.
347 #define AU_PRS_SUCCESS 1
348 #define AU_PRS_FAILURE 2
349 #define AU_PRS_BOTH (AU_PRS_SUCCESS|AU_PRS_FAILURE)
352 * Data structures relating to the kernel audit queue. Ideally, these might
353 * be abstracted so that only accessor methods are exposed.
355 extern struct mtx audit_mtx
;
356 extern struct cv audit_watermark_cv
;
357 extern struct cv audit_worker_cv
;
358 extern struct cv audit_drain_cv
;
359 extern struct kaudit_queue audit_q
;
360 extern int audit_q_len
;
361 extern int audit_pre_q_len
;
362 extern int audit_in_failure
;
365 * Flags to use on audit files when opening and closing.
367 #define AUDIT_OPEN_FLAGS (FWRITE | O_APPEND)
368 #define AUDIT_CLOSE_FLAGS (FWRITE | O_APPEND)
370 #include <sys/fcntl.h>
371 #include <sys/kernel.h>
372 #include <sys/malloc.h>
375 * Some of the BSM tokenizer functions take different parameters in the
376 * kernel implementations in order to save the copying of large kernel data
377 * structures. The prototypes of these functions are declared here.
379 token_t
*kau_to_socket(struct socket_au_info
*soi
);
382 * audit_klib prototypes
384 int au_preselect(au_event_t event
, au_class_t
class,
385 au_mask_t
*mask_p
, int sorf
);
386 void au_evclassmap_init(void);
387 void au_evclassmap_insert(au_event_t event
, au_class_t
class);
388 au_class_t
au_event_class(au_event_t event
);
389 au_event_t
audit_ctlname_to_sysctlevent(int name
[], uint64_t valid_arg
);
390 au_event_t
audit_flags_and_error_to_openevent(int oflags
, int error
);
391 au_event_t
audit_flags_and_error_to_openextendedevent(int oflags
,
393 au_event_t
audit_flags_and_error_to_openatevent(int oflags
,
395 au_event_t
audit_flags_and_error_to_openbyidevent(int oflags
,
397 au_event_t
audit_msgctl_to_event(int cmd
);
398 au_event_t
audit_semctl_to_event(int cmr
);
399 int audit_canon_path(struct vnode
*cwd_vp
, char *path
,
401 au_event_t
auditon_command_event(int cmd
);
402 au_event_t
audit_fcntl_command_event(int cmd
, int oflags
, int error
);
405 * Audit trigger events notify user space of kernel audit conditions
408 int audit_send_trigger(unsigned int trigger
);
411 * Accessor functions to manage global audit state.
413 void audit_set_kinfo(struct auditinfo_addr
*);
414 void audit_get_kinfo(struct auditinfo_addr
*);
417 * General audit related functions.
419 struct kaudit_record
*currecord(void);
420 void audit_free(struct kaudit_record
*ar
);
421 void audit_rotate_vnode(struct ucred
*cred
,
423 void audit_worker_init(void);
426 * Audit pipe functions.
428 int audit_pipe_init(void);
429 int audit_pipe_shutdown(void);
430 int audit_pipe_preselect(au_id_t auid
, au_event_t event
,
431 au_class_t
class, int sorf
, int trail_select
);
432 void audit_pipe_submit(au_id_t auid
, au_event_t event
, au_class_t
class,
433 int sorf
, int trail_select
, void *record
, u_int record_len
);
434 void audit_pipe_submit_user(void *record
, u_int record_len
);
437 * Audit MAC prototypes.
439 void audit_mac_init(void);
440 int audit_mac_new(proc_t p
, struct kaudit_record
*ar
);
441 void audit_mac_free(struct kaudit_record
*ar
);
442 int audit_mac_syscall_enter(unsigned short code
, proc_t p
,
443 struct uthread
*uthread
, kauth_cred_t my_cred
, au_event_t event
);
444 int audit_mac_syscall_exit(unsigned short code
, struct uthread
*uthread
,
445 int error
, int retval
);
450 void audit_session_init(void);
451 int audit_session_setaia(proc_t p
, auditinfo_addr_t
*aia_p
);
452 auditinfo_addr_t
*audit_session_update(auditinfo_addr_t
*new_aia
);
453 int audit_session_lookup(au_asid_t asid
, auditinfo_addr_t
*ret_aia
);
456 * Kernel assigned audit session IDs start at PID_MAX + 1 and ends at
459 #define ASSIGNED_ASID_MIN (PID_MAX + 1)
460 #define ASSIGNED_ASID_MAX (0xFFFFFFFF - 1)
462 #endif /* defined(KERNEL) || defined(_KERNEL) */
464 #endif /* ! _SECURITY_AUDIT_PRIVATE_H_ */