#include <sys/param.h>
#include <sys/condvar.h>
#include <sys/conf.h>
#include <sys/eventhandler.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/fcntl.h>
#include <sys/ipc.h>
#include <sys/jail.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/namei.h>
#include <sys/priv.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/protosw.h>
#include <sys/domain.h>
#include <sys/sysctl.h>
#include <sys/sysproto.h>
#include <sys/sysent.h>
#include <sys/systm.h>
#include <sys/ucred.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <sys/unistd.h>
#include <sys/vnode.h>
#include <bsm/audit.h>
#include <bsm/audit_internal.h>
#include <bsm/audit_kevents.h>
#include <netinet/in.h>
#include <netinet/in_pcb.h>
#include <security/audit/audit.h>
#include <security/audit/audit_private.h>
#include <vm/uma.h>
FEATURE(audit, "BSM audit support");
static uma_zone_t audit_record_zone;
static MALLOC_DEFINE(M_AUDITCRED, "audit_cred", "Audit cred storage");
MALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage");
MALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage");
MALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage");
MALLOC_DEFINE(M_AUDITGIDSET, "audit_gidset", "Audit GID set storage");
static SYSCTL_NODE(_security, OID_AUTO, audit, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
"TrustedBSD audit controls");
int audit_trail_enabled;
int audit_trail_suspended;
#ifdef KDTRACE_HOOKS
u_int audit_dtrace_enabled;
#endif
bool __read_frequently audit_syscalls_enabled;
int audit_panic_on_write_fail;
int audit_fail_stop;
int audit_argv;
int audit_arge;
int audit_in_failure;
struct audit_fstat audit_fstat;
struct au_mask audit_nae_mask;
struct mtx audit_mtx;
struct kaudit_queue audit_q;
int audit_q_len;
int audit_pre_q_len;
struct au_qctrl audit_qctrl;
struct cv audit_worker_cv;
struct cv audit_watermark_cv;
static struct cv audit_fail_cv;
#ifdef KDTRACE_HOOKS
void *(*dtaudit_hook_preselect)(au_id_t auid, au_event_t event,
au_class_t class);
int (*dtaudit_hook_commit)(struct kaudit_record *kar, au_id_t auid,
au_event_t event, au_class_t class, int sorf);
void (*dtaudit_hook_bsm)(struct kaudit_record *kar, au_id_t auid,
au_event_t event, au_class_t class, int sorf,
void *bsm_data, size_t bsm_lenlen);
#endif
static struct auditinfo_addr audit_kinfo;
static struct rwlock audit_kinfo_lock;
#define KINFO_LOCK_INIT() rw_init(&audit_kinfo_lock, \
"audit_kinfo_lock")
#define KINFO_RLOCK() rw_rlock(&audit_kinfo_lock)
#define KINFO_WLOCK() rw_wlock(&audit_kinfo_lock)
#define KINFO_RUNLOCK() rw_runlock(&audit_kinfo_lock)
#define KINFO_WUNLOCK() rw_wunlock(&audit_kinfo_lock)
void
audit_syscalls_enabled_update(void)
{
mtx_lock(&audit_mtx);
#ifdef KDTRACE_HOOKS
if (audit_dtrace_enabled)
audit_syscalls_enabled = true;
else {
#endif
if (audit_trail_enabled && !audit_trail_suspended)
audit_syscalls_enabled = true;
else
audit_syscalls_enabled = false;
#ifdef KDTRACE_HOOKS
}
#endif
mtx_unlock(&audit_mtx);
}
void
audit_set_kinfo(struct auditinfo_addr *ak)
{
KASSERT(ak->ai_termid.at_type == AU_IPv4 ||
ak->ai_termid.at_type == AU_IPv6,
("audit_set_kinfo: invalid address type"));
KINFO_WLOCK();
audit_kinfo = *ak;
KINFO_WUNLOCK();
}
void
audit_get_kinfo(struct auditinfo_addr *ak)
{
KASSERT(audit_kinfo.ai_termid.at_type == AU_IPv4 ||
audit_kinfo.ai_termid.at_type == AU_IPv6,
("audit_set_kinfo: invalid address type"));
KINFO_RLOCK();
*ak = audit_kinfo;
KINFO_RUNLOCK();
}
static int
audit_record_ctor(void *mem, int size, void *arg, int flags)
{
struct kaudit_record *ar;
struct thread *td;
struct ucred *cred;
struct prison *pr;
KASSERT(sizeof(*ar) == size, ("audit_record_ctor: wrong size"));
td = arg;
ar = mem;
bzero(ar, sizeof(*ar));
ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC;
nanotime(&ar->k_ar.ar_starttime);
cred = td->td_ucred;
cru2x(cred, &ar->k_ar.ar_subj_cred);
ar->k_ar.ar_subj_ruid = cred->cr_ruid;
ar->k_ar.ar_subj_rgid = cred->cr_rgid;
ar->k_ar.ar_subj_egid = cred->cr_gid;
ar->k_ar.ar_subj_auid = cred->cr_audit.ai_auid;
ar->k_ar.ar_subj_asid = cred->cr_audit.ai_asid;
ar->k_ar.ar_subj_pid = td->td_proc->p_pid;
ar->k_ar.ar_subj_amask = cred->cr_audit.ai_mask;
ar->k_ar.ar_subj_term_addr = cred->cr_audit.ai_termid;
if (jailed(cred)) {
pr = cred->cr_prison;
(void) strlcpy(ar->k_ar.ar_jailname, pr->pr_name,
sizeof(ar->k_ar.ar_jailname));
} else
ar->k_ar.ar_jailname[0] = '\0';
return (0);
}
static void
audit_record_dtor(void *mem, int size, void *arg)
{
struct kaudit_record *ar;
KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size"));
ar = mem;
if (ar->k_ar.ar_arg_upath1 != NULL)
free(ar->k_ar.ar_arg_upath1, M_AUDITPATH);
if (ar->k_ar.ar_arg_upath2 != NULL)
free(ar->k_ar.ar_arg_upath2, M_AUDITPATH);
if (ar->k_ar.ar_arg_text != NULL)
free(ar->k_ar.ar_arg_text, M_AUDITTEXT);
if (ar->k_udata != NULL)
free(ar->k_udata, M_AUDITDATA);
if (ar->k_ar.ar_arg_argv != NULL)
free(ar->k_ar.ar_arg_argv, M_AUDITTEXT);
if (ar->k_ar.ar_arg_envv != NULL)
free(ar->k_ar.ar_arg_envv, M_AUDITTEXT);
if (ar->k_ar.ar_arg_groups.gidset != NULL)
free(ar->k_ar.ar_arg_groups.gidset, M_AUDITGIDSET);
}
static void
audit_init(void *dummy __unused)
{
audit_trail_enabled = 0;
audit_trail_suspended = 0;
audit_syscalls_enabled = false;
audit_panic_on_write_fail = 0;
audit_fail_stop = 0;
audit_in_failure = 0;
audit_argv = 0;
audit_arge = 0;
audit_fstat.af_filesz = 0;
audit_fstat.af_currsz = 0;
audit_nae_mask.am_success = 0;
audit_nae_mask.am_failure = 0;
TAILQ_INIT(&audit_q);
audit_q_len = 0;
audit_pre_q_len = 0;
audit_qctrl.aq_hiwater = AQ_HIWATER;
audit_qctrl.aq_lowater = AQ_LOWATER;
audit_qctrl.aq_bufsz = AQ_BUFSZ;
audit_qctrl.aq_minfree = AU_FS_MINFREE;
audit_kinfo.ai_termid.at_type = AU_IPv4;
audit_kinfo.ai_termid.at_addr[0] = INADDR_ANY;
mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF);
KINFO_LOCK_INIT();
cv_init(&audit_worker_cv, "audit_worker_cv");
cv_init(&audit_watermark_cv, "audit_watermark_cv");
cv_init(&audit_fail_cv, "audit_fail_cv");
audit_record_zone = uma_zcreate("audit_record",
sizeof(struct kaudit_record), audit_record_ctor,
audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
audit_syscalls_enabled_update();
kau_init();
audit_trigger_init();
EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL,
SHUTDOWN_PRI_FIRST);
audit_worker_init();
}
SYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL);
void
audit_shutdown(void *arg, int howto)
{
if (KERNEL_PANICKED())
return;
audit_rotate_vnode(NULL, NULL);
}
struct kaudit_record *
currecord(void)
{
return (curthread->td_ar);
}
struct kaudit_record *
audit_new(int event, struct thread *td)
{
struct kaudit_record *ar;
mtx_lock(&audit_mtx);
audit_pre_q_len++;
while (audit_q_len >= audit_qctrl.aq_hiwater)
cv_wait(&audit_watermark_cv, &audit_mtx);
mtx_unlock(&audit_mtx);
ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK);
ar->k_ar.ar_event = event;
return (ar);
}
void
audit_free(struct kaudit_record *ar)
{
uma_zfree(audit_record_zone, ar);
}
void
audit_commit(struct kaudit_record *ar, int error, int retval)
{
au_event_t event;
au_class_t class;
au_id_t auid;
int sorf;
struct au_mask *aumask;
if (ar == NULL)
return;
ar->k_ar.ar_errno = error;
ar->k_ar.ar_retval = retval;
nanotime(&ar->k_ar.ar_endtime);
if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID)
aumask = &audit_nae_mask;
else
aumask = &ar->k_ar.ar_subj_amask;
if (error)
sorf = AU_PRS_FAILURE;
else
sorf = AU_PRS_SUCCESS;
switch(ar->k_ar.ar_event) {
case AUE_OPEN_RWTC:
ar->k_ar.ar_event = audit_flags_and_error_to_openevent(
ar->k_ar.ar_arg_fflags, error);
break;
case AUE_OPENAT_RWTC:
ar->k_ar.ar_event = audit_flags_and_error_to_openatevent(
ar->k_ar.ar_arg_fflags, error);
break;
case AUE_SYSCTL:
ar->k_ar.ar_event = audit_ctlname_to_sysctlevent(
ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg);
break;
case AUE_AUDITON:
ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd);
break;
case AUE_MSGSYS:
if (ARG_IS_VALID(ar, ARG_SVIPC_WHICH))
ar->k_ar.ar_event =
audit_msgsys_to_event(ar->k_ar.ar_arg_svipc_which);
break;
case AUE_SEMSYS:
if (ARG_IS_VALID(ar, ARG_SVIPC_WHICH))
ar->k_ar.ar_event =
audit_semsys_to_event(ar->k_ar.ar_arg_svipc_which);
break;
case AUE_SHMSYS:
if (ARG_IS_VALID(ar, ARG_SVIPC_WHICH))
ar->k_ar.ar_event =
audit_shmsys_to_event(ar->k_ar.ar_arg_svipc_which);
break;
}
auid = ar->k_ar.ar_subj_auid;
event = ar->k_ar.ar_event;
class = au_event_class(event);
ar->k_ar_commit |= AR_COMMIT_KERNEL;
if (au_preselect(event, class, aumask, sorf) != 0)
ar->k_ar_commit |= AR_PRESELECT_TRAIL;
if (audit_pipe_preselect(auid, event, class, sorf,
ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0)
ar->k_ar_commit |= AR_PRESELECT_PIPE;
#ifdef KDTRACE_HOOKS
if (dtaudit_hook_commit != NULL) {
if (dtaudit_hook_commit(ar, auid, event, class, sorf) != 0)
ar->k_ar_commit |= AR_PRESELECT_DTRACE;
}
#endif
if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE |
AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE |
AR_PRESELECT_DTRACE)) == 0) {
mtx_lock(&audit_mtx);
audit_pre_q_len--;
mtx_unlock(&audit_mtx);
audit_free(ar);
return;
}
mtx_lock(&audit_mtx);
if (audit_trail_suspended || !audit_trail_enabled) {
audit_pre_q_len--;
mtx_unlock(&audit_mtx);
audit_free(ar);
return;
}
TAILQ_INSERT_TAIL(&audit_q, ar, k_q);
audit_q_len++;
audit_pre_q_len--;
cv_signal(&audit_worker_cv);
mtx_unlock(&audit_mtx);
}
void
audit_syscall_enter(unsigned short code, struct thread *td)
{
struct au_mask *aumask;
#ifdef KDTRACE_HOOKS
void *dtaudit_state;
#endif
au_class_t class;
au_event_t event;
au_id_t auid;
int record_needed;
KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL"));
KASSERT((td->td_pflags & TDP_AUDITREC) == 0,
("audit_syscall_enter: TDP_AUDITREC set"));
if (code >= td->td_proc->p_sysent->sv_size)
return;
event = td->td_proc->p_sysent->sv_table[code].sy_auevent;
if (event == AUE_NULL)
return;
auid = td->td_ucred->cr_audit.ai_auid;
if (auid == AU_DEFAUDITID)
aumask = &audit_nae_mask;
else
aumask = &td->td_ucred->cr_audit.ai_mask;
class = au_event_class(event);
if (au_preselect(event, class, aumask, AU_PRS_BOTH)) {
if (audit_in_failure &&
priv_check(td, PRIV_AUDIT_FAILSTOP) != 0) {
cv_wait(&audit_fail_cv, &audit_mtx);
panic("audit_failing_stop: thread continued");
}
record_needed = 1;
} else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) {
record_needed = 1;
} else {
record_needed = 0;
}
#ifdef KDTRACE_HOOKS
dtaudit_state = NULL;
if (dtaudit_hook_preselect != NULL) {
dtaudit_state = dtaudit_hook_preselect(auid, event, class);
if (dtaudit_state != NULL)
record_needed = 1;
}
#endif
if (record_needed) {
td->td_ar = audit_new(event, td);
if (td->td_ar != NULL) {
td->td_pflags |= TDP_AUDITREC;
#ifdef KDTRACE_HOOKS
td->td_ar->k_dtaudit_state = dtaudit_state;
#endif
}
} else
td->td_ar = NULL;
}
void
audit_syscall_exit(int error, struct thread *td)
{
int retval;
if (error)
retval = -1;
else
retval = td->td_retval[0];
audit_commit(td->td_ar, error, retval);
td->td_ar = NULL;
td->td_pflags &= ~TDP_AUDITREC;
}
void
audit_cred_copy(struct ucred *src, struct ucred *dest)
{
bcopy(&src->cr_audit, &dest->cr_audit, sizeof(dest->cr_audit));
}
void
audit_cred_destroy(struct ucred *cred)
{
}
void
audit_cred_init(struct ucred *cred)
{
bzero(&cred->cr_audit, sizeof(cred->cr_audit));
}
void
audit_cred_kproc0(struct ucred *cred)
{
cred->cr_audit.ai_auid = AU_DEFAUDITID;
cred->cr_audit.ai_termid.at_type = AU_IPv4;
}
void
audit_cred_proc1(struct ucred *cred)
{
cred->cr_audit.ai_auid = AU_DEFAUDITID;
cred->cr_audit.ai_termid.at_type = AU_IPv4;
}
void
audit_thread_alloc(struct thread *td)
{
td->td_ar = NULL;
}
void
audit_thread_free(struct thread *td)
{
KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL"));
KASSERT((td->td_pflags & TDP_AUDITREC) == 0,
("audit_thread_free: TDP_AUDITREC set"));
}
void
audit_proc_coredump(struct thread *td, char *path, int errcode)
{
struct kaudit_record *ar;
struct au_mask *aumask;
struct ucred *cred;
au_class_t class;
int ret, sorf;
char **pathp;
au_id_t auid;
ret = 0;
cred = td->td_ucred;
auid = cred->cr_audit.ai_auid;
if (auid == AU_DEFAUDITID)
aumask = &audit_nae_mask;
else
aumask = &cred->cr_audit.ai_mask;
if (errcode != 0)
sorf = AU_PRS_FAILURE;
else
sorf = AU_PRS_SUCCESS;
class = au_event_class(AUE_CORE);
if (au_preselect(AUE_CORE, class, aumask, sorf) == 0 &&
audit_pipe_preselect(auid, AUE_CORE, class, sorf, 0) == 0)
return;
ar = audit_new(AUE_CORE, td);
if (ar == NULL)
return;
if (path != NULL) {
pathp = &ar->k_ar.ar_arg_upath1;
*pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK);
audit_canon_path(td, AT_FDCWD, path, *pathp);
ARG_SET_VALID(ar, ARG_UPATH1);
}
ar->k_ar.ar_arg_signum = td->td_proc->p_sig;
ARG_SET_VALID(ar, ARG_SIGNUM);
if (errcode != 0)
ret = 1;
audit_commit(ar, errcode, ret);
}