audit_private.h revision 155192
1112158Sdas/* 2112158Sdas * Copyright (c) 1999-2005 Apple Computer, Inc. 3112158Sdas * All rights reserved. 4112158Sdas * 5112158Sdas * Redistribution and use in source and binary forms, with or without 6112158Sdas * modification, are permitted provided that the following conditions 7112158Sdas * are met: 8112158Sdas * 1. Redistributions of source code must retain the above copyright 9112158Sdas * notice, this list of conditions and the following disclaimer. 10112158Sdas * 2. Redistributions in binary form must reproduce the above copyright 11112158Sdas * notice, this list of conditions and the following disclaimer in the 12112158Sdas * documentation and/or other materials provided with the distribution. 13112158Sdas * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 14112158Sdas * its contributors may be used to endorse or promote products derived 15112158Sdas * from this software without specific prior written permission. 16112158Sdas * 17112158Sdas * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND 18112158Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19112158Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20112158Sdas * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR 21112158Sdas * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22112158Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23112158Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24112158Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 25112158Sdas * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26112158Sdas * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27112158Sdas * POSSIBILITY OF SUCH DAMAGE. 28112158Sdas * 29112161Sdas * $FreeBSD: head/sys/security/audit/audit_private.h 155192 2006-02-01 20:01:18Z rwatson $ 30112161Sdas */ 31112158Sdas 32112158Sdas/* 33112158Sdas * This include file contains function prototypes and type definitions used 34112158Sdas * within the audit implementation. 35112158Sdas */ 36112158Sdas 37112158Sdas#ifndef _BSM_AUDIT_PRIVATE_H 38165746Sdas#define _BSM_AUDIT_PRIVATE_H 39165746Sdas 40112158Sdas#ifndef _KERNEL 41112158Sdas#error "no user-serviceable parts inside" 42112158Sdas#endif 43112158Sdas 44112158Sdas#include <sys/ipc.h> 45112158Sdas#include <sys/socket.h> 46112158Sdas#include <sys/ucred.h> 47112158Sdas 48112158Sdas#ifdef MALLOC_DECLARE 49112158SdasMALLOC_DECLARE(M_AUDITBSM); 50112158SdasMALLOC_DECLARE(M_AUDITDATA); 51112158SdasMALLOC_DECLARE(M_AUDITPATH); 52112158SdasMALLOC_DECLARE(M_AUDITTEXT); 53112158Sdas#endif 54112158Sdas 55112158Sdas/* 56112158Sdas * Audit control variables that are usually set/read via system calls 57112158Sdas * and used to control various aspects of auditing. 58112158Sdas */ 59112158Sdasextern struct au_qctrl audit_qctrl; 60112158Sdasextern struct audit_fstat audit_fstat; 61165746Sdasextern struct au_mask audit_nae_mask; 62112158Sdasextern int audit_panic_on_write_fail; 63112158Sdasextern int audit_fail_stop; 64112158Sdas 65112158Sdas/* 66112158Sdas * Success/failure conditions for the conversion of a kernel audit record to 67112158Sdas * BSM format. 68112158Sdas */ 69112158Sdas#define BSM_SUCCESS 0 70112158Sdas#define BSM_FAILURE 1 71112158Sdas#define BSM_NOAUDIT 2 72112158Sdas 73112158Sdas/* 74112158Sdas * Defines for the kernel audit record k_ar_commit field. 75112158Sdas */ 76112158Sdas#define AR_COMMIT_KERNEL 0x00000001U 77112158Sdas#define AR_COMMIT_USER 0x00000010U 78112158Sdas 79112158Sdas/* 80112158Sdas * Audit data is generated as a stream of struct audit_record structures, 81112158Sdas * linked by struct kaudit_record, and contain storage for possible audit so 82112158Sdas * that it will not need to be allocated during the processing of a system 83112158Sdas * call, both improving efficiency and avoiding sleeping at untimely moments. 84112158Sdas * This structure is converted to BSM format before being written to disk. 85112158Sdas */ 86112158Sdasstruct vnode_au_info { 87112158Sdas mode_t vn_mode; 88112158Sdas uid_t vn_uid; 89112158Sdas gid_t vn_gid; 90112158Sdas dev_t vn_dev; 91112158Sdas long vn_fsid; 92112158Sdas long vn_fileid; 93112158Sdas long vn_gen; 94112158Sdas}; 95112158Sdas 96112158Sdasstruct groupset { 97112158Sdas gid_t gidset[NGROUPS]; 98112158Sdas u_int gidset_size; 99112158Sdas}; 100112158Sdas 101112158Sdasstruct socket_au_info { 102112158Sdas int so_domain; 103112158Sdas int so_type; 104112158Sdas int so_protocol; 105112158Sdas in_addr_t so_raddr; /* remote address if INET socket */ 106112158Sdas in_addr_t so_laddr; /* local address if INET socket */ 107112158Sdas u_short so_rport; /* remote port */ 108112158Sdas u_short so_lport; /* local port */ 109112158Sdas}; 110112158Sdas 111112158Sdasunion auditon_udata { 112112158Sdas char *au_path; 113112158Sdas long au_cond; 114112158Sdas long au_flags; 115112158Sdas long au_policy; 116112158Sdas int au_trigger; 117112158Sdas au_evclass_map_t au_evclass; 118112158Sdas au_mask_t au_mask; 119112158Sdas auditinfo_t au_auinfo; 120112158Sdas auditpinfo_t au_aupinfo; 121112158Sdas auditpinfo_addr_t au_aupinfo_addr; 122112158Sdas au_qctrl_t au_qctrl; 123112158Sdas au_stat_t au_stat; 124112158Sdas au_fstat_t au_fstat; 125112158Sdas}; 126112158Sdas 127112158Sdasstruct posix_ipc_perm { 128112158Sdas uid_t pipc_uid; 129112158Sdas gid_t pipc_gid; 130112158Sdas mode_t pipc_mode; 131112158Sdas}; 132165746Sdas 133112158Sdasstruct audit_record { 134112158Sdas /* Audit record header. */ 135182709Sdas u_int32_t ar_magic; 136182709Sdas int ar_event; 137182709Sdas int ar_retval; /* value returned to the process */ 138182709Sdas int ar_errno; /* return status of system call */ 139182709Sdas struct timespec ar_starttime; 140182709Sdas struct timespec ar_endtime; 141182709Sdas u_int64_t ar_valid_arg; /* Bitmask of valid arguments */ 142182709Sdas 143182709Sdas /* Audit subject information. */ 144182709Sdas struct xucred ar_subj_cred; 145112158Sdas uid_t ar_subj_ruid; 146112158Sdas gid_t ar_subj_rgid; 147112158Sdas gid_t ar_subj_egid; 148112158Sdas uid_t ar_subj_auid; /* Audit user ID */ 149112158Sdas pid_t ar_subj_asid; /* Audit session ID */ 150112158Sdas pid_t ar_subj_pid; 151112158Sdas struct au_tid ar_subj_term; 152112158Sdas char ar_subj_comm[MAXCOMLEN + 1]; 153112158Sdas struct au_mask ar_subj_amask; 154112158Sdas 155112158Sdas /* Operation arguments. */ 156182709Sdas uid_t ar_arg_euid; 157112158Sdas uid_t ar_arg_ruid; 158112158Sdas uid_t ar_arg_suid; 159112158Sdas gid_t ar_arg_egid; 160112158Sdas gid_t ar_arg_rgid; 161112158Sdas gid_t ar_arg_sgid; 162112158Sdas pid_t ar_arg_pid; 163112158Sdas pid_t ar_arg_asid; 164112158Sdas struct au_tid ar_arg_termid; 165112158Sdas uid_t ar_arg_uid; 166112158Sdas uid_t ar_arg_auid; 167112158Sdas gid_t ar_arg_gid; 168112158Sdas struct groupset ar_arg_groups; 169112158Sdas int ar_arg_fd; 170112158Sdas int ar_arg_fflags; 171112158Sdas mode_t ar_arg_mode; 172112158Sdas int ar_arg_dev; 173112158Sdas long ar_arg_value; 174112158Sdas void * ar_arg_addr; 175174693Sdas int ar_arg_len; 176174693Sdas int ar_arg_mask; 177174693Sdas u_int ar_arg_signum; 178112158Sdas char ar_arg_login[MAXLOGNAME]; 179165746Sdas int ar_arg_ctlname[CTL_MAXNAME]; 180112158Sdas struct sockaddr ar_arg_sockaddr; 181112158Sdas struct socket_au_info ar_arg_sockinfo; 182112158Sdas char *ar_arg_upath1; 183112158Sdas char *ar_arg_upath2; 184112158Sdas char *ar_arg_text; 185112158Sdas struct au_mask ar_arg_amask; 186112161Sdas struct vnode_au_info ar_arg_vnode1; 187112158Sdas struct vnode_au_info ar_arg_vnode2; 188112158Sdas int ar_arg_cmd; 189112161Sdas int ar_arg_svipc_cmd; 190112158Sdas struct ipc_perm ar_arg_svipc_perm; 191116652Sdas int ar_arg_svipc_id; 192116652Sdas void * ar_arg_svipc_addr; 193116652Sdas struct posix_ipc_perm ar_arg_pipc_perm; 194116652Sdas union auditon_udata ar_arg_auditon; 195112158Sdas int ar_arg_exitstatus; 196112158Sdas int ar_arg_exitretval; 197112158Sdas}; 198112158Sdas 199112158Sdas/* 200112158Sdas * Arguments in the audit record are initially not defined; flags are set to 201112158Sdas * indicate if they are present so they can be included in the audit log 202112158Sdas * stream only if defined. 203112158Sdas */ 204112158Sdas#define ARG_IS_VALID(kar, arg) ((kar)->k_ar.ar_valid_arg & (arg)) 205112158Sdas#define ARG_SET_VALID(kar, arg) do { \ 206112158Sdas (kar)->k_ar.ar_valid_arg |= (arg); \ 207112161Sdas} while (0) 208112418Sdas 209140431Sdas/* 210182709Sdas * In-kernel version of audit record; the basic record plus queue meta-data. 211112161Sdas * This record can also have a pointer set to some opaque data that will 212112158Sdas * be passed through to the audit writing mechanism. 213112158Sdas */ 214112158Sdasstruct kaudit_record { 215112158Sdas struct audit_record k_ar; 216112158Sdas u_int32_t k_ar_commit; 217112158Sdas void *k_udata; /* user data */ 218112158Sdas u_int k_ulen; /* user data length */ 219112158Sdas struct uthread *k_uthread; /* thread we are auditing */ 220112158Sdas TAILQ_ENTRY(kaudit_record) k_q; 221112158Sdas}; 222112158Sdas 223112158Sdas/* 224112158Sdas * Functions to manage the allocation, release, and commit of kernel audit 225112158Sdas * records. 226112158Sdas */ 227112158Sdasvoid audit_abort(struct kaudit_record *ar); 228112158Sdasvoid audit_commit(struct kaudit_record *ar, int error, 229112158Sdas int retval); 230112158Sdasstruct kaudit_record *audit_new(int event, struct thread *td); 231112158Sdas 232112158Sdas/* 233112158Sdas * Functions relating to the conversion of internal kernel audit records to 234112158Sdas * the BSM file format. 235112158Sdas */ 236112158Sdasint kaudit_to_bsm(struct kaudit_record *kar, 237112158Sdas struct au_record **pau); 238112158Sdasint bsm_rec_verify(void *rec); 239112158Sdas 240112158Sdas/* 241112158Sdas * Kernel versions of the libbsm audit record functions. 242112158Sdas */ 243112158Sdasvoid kau_free(struct au_record *rec); 244112158Sdasvoid kau_init(void); 245112158Sdas 246112158Sdas/* 247112158Sdas * Return values for pre-selection and post-selection decisions. 248112158Sdas */ 249112158Sdas#define AU_PRS_SUCCESS 1 250112158Sdas#define AU_PRS_FAILURE 2 251112158Sdas#define AU_PRS_BOTH (AU_PRS_SUCCESS|AU_PRS_FAILURE) 252112158Sdas 253112158Sdas/* 254112158Sdas * Flags to use on audit files when opening and closing. 255112158Sdas */ 256112158Sdas#define AUDIT_OPEN_FLAGS (FWRITE | O_APPEND) 257112158Sdas#define AUDIT_CLOSE_FLAGS (FWRITE | O_APPEND) 258112158Sdas 259112158Sdas#include <sys/fcntl.h> 260112158Sdas#include <sys/kernel.h> 261112158Sdas#include <sys/malloc.h> 262112158Sdas#include <security/audit/audit.h> 263112158Sdas 264112158Sdas/* 265112158Sdas * Some of the BSM tokenizer functions take different parameters in the 266112158Sdas * kernel implementations in order to save the copying of large kernel 267112158Sdas * data structures. The prototypes of these functions are declared here. 268112158Sdas */ 269112158Sdastoken_t *kau_to_socket(struct socket_au_info *soi); 270112158Sdas 271112158Sdas/* 272112158Sdas * audit_klib prototypes 273112158Sdas */ 274112158Sdasint au_preselect(au_event_t event, au_mask_t *mask_p, int sorf); 275112158Sdasau_event_t flags_and_error_to_openevent(int oflags, int error); 276112158Sdasvoid au_evclassmap_init(void); 277112158Sdasvoid au_evclassmap_insert(au_event_t event, au_class_t class); 278112158Sdasau_class_t au_event_class(au_event_t event); 279112158Sdasau_event_t ctlname_to_sysctlevent(int name[], uint64_t valid_arg); 280112158Sdasint auditon_command_event(int cmd); 281112158Sdasint msgctl_to_event(int cmd); 282112158Sdasint semctl_to_event(int cmr); 283112158Sdasvoid canon_path(struct thread *td, char *path, char *cpath); 284112158Sdas 285112158Sdas/* 286112158Sdas * Audit trigger events notify user space of kernel audit conditions 287112158Sdas * asynchronously. 288112158Sdas */ 289112158Sdasvoid audit_trigger_init(void); 290112158Sdasvoid send_trigger(unsigned int trigger); 291112158Sdas 292112158Sdas/* 293112158Sdas * General audit related functions. 294112158Sdas */ 295112158Sdasstruct kaudit_record *currecord(void); 296112158Sdasvoid audit_shutdown(void *arg, int howto); 297112158Sdasvoid audit_rotate_vnode(struct ucred *cred, 298112158Sdas struct vnode *vp); 299112158Sdas 300112158Sdas#endif /* ! _BSM_AUDIT_PRIVATE_H */ 301112158Sdas