audit_private.h revision 184856
113044Sasami/*-
213044Sasami * Copyright (c) 1999-2005 Apple Inc.
313044Sasami * All rights reserved.
413044Sasami *
513044Sasami * Redistribution and use in source and binary forms, with or without
613044Sasami * modification, are permitted provided that the following conditions
713044Sasami * are met:
813044Sasami * 1.  Redistributions of source code must retain the above copyright
913044Sasami *     notice, this list of conditions and the following disclaimer.
1013044Sasami * 2.  Redistributions in binary form must reproduce the above copyright
1113044Sasami *     notice, this list of conditions and the following disclaimer in the
1213044Sasami *     documentation and/or other materials provided with the distribution.
1313044Sasami * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
1413044Sasami *     its contributors may be used to endorse or promote products derived
1513044Sasami *     from this software without specific prior written permission.
1613044Sasami *
1713044Sasami * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
1813044Sasami * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1913044Sasami * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2013044Sasami * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
2113044Sasami * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2213044Sasami * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2313044Sasami * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2413044Sasami * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2513044Sasami * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2613044Sasami * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2713044Sasami * POSSIBILITY OF SUCH DAMAGE.
2813044Sasami *
2913044Sasami * $FreeBSD: head/sys/security/audit/audit_private.h 184856 2008-11-11 21:57:03Z csjp $
3013044Sasami */
3113044Sasami
3213044Sasami/*
3313044Sasami * This include file contains function prototypes and type definitions used
3413044Sasami * within the audit implementation.
35114589Sobrien */
36114589Sobrien
3736628Scharnier#ifndef _SECURITY_AUDIT_PRIVATE_H_
3813044Sasami#define	_SECURITY_AUDIT_PRIVATE_H_
3948568Sbillf
4013044Sasami#ifndef _KERNEL
4113044Sasami#error "no user-serviceable parts inside"
4245329Speter#endif
4313044Sasami
4413044Sasami#include <sys/ipc.h>
4513044Sasami#include <sys/socket.h>
4613044Sasami#include <sys/ucred.h>
4713044Sasami
4869793Sobrien#ifdef MALLOC_DECLARE
4913044SasamiMALLOC_DECLARE(M_AUDITBSM);
5013044SasamiMALLOC_DECLARE(M_AUDITDATA);
5113044SasamiMALLOC_DECLARE(M_AUDITPATH);
5213044SasamiMALLOC_DECLARE(M_AUDITTEXT);
53115730Sphk#endif
5413044Sasami
5539228Sgibbs/*
5613052Sasami * Audit control variables that are usually set/read via system calls and
5713044Sasami * used to control various aspects of auditing.
5813044Sasami */
5913044Sasamiextern struct au_qctrl		audit_qctrl;
6013044Sasamiextern struct audit_fstat	audit_fstat;
6113044Sasamiextern struct au_mask		audit_nae_mask;
62109417Sphkextern int			audit_panic_on_write_fail;
6313044Sasamiextern int			audit_fail_stop;
6413044Sasamiextern int			audit_argv;
65109417Sphkextern int			audit_arge;
6613044Sasami
6713044Sasami/*
6813044Sasami * Success/failure conditions for the conversion of a kernel audit record to
6913762Sasami * BSM format.
7013044Sasami */
7113044Sasami#define	BSM_SUCCESS	0
7213044Sasami#define	BSM_FAILURE	1
7313044Sasami#define	BSM_NOAUDIT	2
7413044Sasami
7513044Sasami/*
7613044Sasami * Defines for the kernel audit record k_ar_commit field.  Flags are set to
7713044Sasami * indicate what sort of record it is, and which preselection mechanism
7813044Sasami * selected it.
7992539Simp */
80109417Sphk#define	AR_COMMIT_KERNEL	0x00000001U
8192539Simp#define	AR_COMMIT_USER		0x00000010U
8292539Simp
8392539Simp#define	AR_PRESELECT_TRAIL	0x00001000U
8492539Simp#define	AR_PRESELECT_PIPE	0x00002000U
8592539Simp
86109417Sphk#define	AR_PRESELECT_USER_TRAIL	0x00004000U
8792539Simp#define	AR_PRESELECT_USER_PIPE	0x00008000U
8813044Sasami
8913044Sasami/*
9092539Simp * Audit data is generated as a stream of struct audit_record structures,
9113044Sasami * linked by struct kaudit_record, and contain storage for possible audit so
9213044Sasami * that it will not need to be allocated during the processing of a system
9313044Sasami * call, both improving efficiency and avoiding sleeping at untimely moments.
9483329Sru * This structure is converted to BSM format before being written to disk.
9513044Sasami */
9613044Sasamistruct vnode_au_info {
9713044Sasami	mode_t	vn_mode;
9813044Sasami	uid_t	vn_uid;
9913044Sasami	gid_t	vn_gid;
10013044Sasami	dev_t	vn_dev;
10113044Sasami	long	vn_fsid;
10213044Sasami	long	vn_fileid;
10313044Sasami	long	vn_gen;
10413044Sasami};
10513044Sasami
10613044Sasamistruct groupset {
10713044Sasami	gid_t	gidset[NGROUPS];
10813044Sasami	u_int	gidset_size;
10913044Sasami};
11013044Sasami
11113044Sasamistruct socket_au_info {
11213044Sasami	int		so_domain;
11313044Sasami	int		so_type;
11413044Sasami	int		so_protocol;
11513044Sasami	in_addr_t	so_raddr;	/* Remote address if INET socket. */
11613044Sasami	in_addr_t	so_laddr;	/* Local address if INET socket. */
11713044Sasami	u_short		so_rport;	/* Remote port. */
11813044Sasami	u_short		so_lport;	/* Local port. */
11913044Sasami};
12013044Sasami
12113044Sasamiunion auditon_udata {
12213044Sasami	char			*au_path;
12313044Sasami	long			au_cond;
12413044Sasami	long			au_flags;
12513044Sasami	long			au_policy;
12613044Sasami	int			au_trigger;
12713044Sasami	au_evclass_map_t	au_evclass;
12813044Sasami	au_mask_t		au_mask;
12913044Sasami	auditinfo_t		au_auinfo;
13013044Sasami	auditpinfo_t		au_aupinfo;
13113044Sasami	auditpinfo_addr_t	au_aupinfo_addr;
13213044Sasami	au_qctrl_t		au_qctrl;
13313044Sasami	au_stat_t		au_stat;
13413044Sasami	au_fstat_t		au_fstat;
13513044Sasami	auditinfo_addr_t	au_kau_info;
13613044Sasami};
13713044Sasami
13845329Speterstruct posix_ipc_perm {
13945329Speter	uid_t	pipc_uid;
14045329Speter	gid_t	pipc_gid;
14145329Speter	mode_t	pipc_mode;
14245329Speter};
14345329Speter
14413044Sasamistruct audit_record {
14513044Sasami	/* Audit record header. */
14613044Sasami	u_int32_t		ar_magic;
14713044Sasami	int			ar_event;
14813044Sasami	int			ar_retval; /* value returned to the process */
14913044Sasami	int			ar_errno;  /* return status of system call */
15013044Sasami	struct timespec		ar_starttime;
15113044Sasami	struct timespec		ar_endtime;
15213044Sasami	u_int64_t		ar_valid_arg;  /* Bitmask of valid arguments */
15313044Sasami
15413044Sasami	/* Audit subject information. */
15513044Sasami	struct xucred		ar_subj_cred;
15613044Sasami	uid_t			ar_subj_ruid;
15713044Sasami	gid_t			ar_subj_rgid;
15813044Sasami	gid_t			ar_subj_egid;
15913044Sasami	uid_t			ar_subj_auid; /* Audit user ID */
16036628Scharnier	pid_t			ar_subj_asid; /* Audit session ID */
16113044Sasami	pid_t			ar_subj_pid;
16213044Sasami	struct au_tid		ar_subj_term;
16313044Sasami	struct au_tid_addr	ar_subj_term_addr;
16492539Simp	struct au_mask		ar_subj_amask;
16513044Sasami
16613044Sasami	/* Operation arguments. */
167109417Sphk	uid_t			ar_arg_euid;
168109417Sphk	uid_t			ar_arg_ruid;
169109417Sphk	uid_t			ar_arg_suid;
17013044Sasami	gid_t			ar_arg_egid;
17113044Sasami	gid_t			ar_arg_rgid;
17213044Sasami	gid_t			ar_arg_sgid;
17313044Sasami	pid_t			ar_arg_pid;
17413044Sasami	pid_t			ar_arg_asid;
17513044Sasami	struct au_tid		ar_arg_termid;
17613044Sasami	struct au_tid_addr	ar_arg_termid_addr;
17713044Sasami	uid_t			ar_arg_uid;
17813044Sasami	uid_t			ar_arg_auid;
179109472Sphk	gid_t			ar_arg_gid;
18013044Sasami	struct groupset		ar_arg_groups;
18113044Sasami	int			ar_arg_fd;
18213044Sasami	int			ar_arg_fflags;
18313044Sasami	mode_t			ar_arg_mode;
184109421Sphk	int			ar_arg_dev;
18513044Sasami	long			ar_arg_value;
18613044Sasami	void *			ar_arg_addr;
18713044Sasami	int			ar_arg_len;
18813044Sasami	int			ar_arg_mask;
18913044Sasami	u_int			ar_arg_signum;
19013044Sasami	char			ar_arg_login[MAXLOGNAME];
19113044Sasami	int			ar_arg_ctlname[CTL_MAXNAME];
19213044Sasami	struct socket_au_info	ar_arg_sockinfo;
19313044Sasami	char			*ar_arg_upath1;
19413044Sasami	char			*ar_arg_upath2;
19548568Sbillf	char			*ar_arg_text;
19613044Sasami	struct au_mask		ar_arg_amask;
19713044Sasami	struct vnode_au_info	ar_arg_vnode1;
19813044Sasami	struct vnode_au_info	ar_arg_vnode2;
19913044Sasami	int			ar_arg_cmd;
20013044Sasami	int			ar_arg_svipc_cmd;
20113044Sasami	struct ipc_perm		ar_arg_svipc_perm;
20213044Sasami	int			ar_arg_svipc_id;
20313044Sasami	void *			ar_arg_svipc_addr;
20413044Sasami	struct posix_ipc_perm	ar_arg_pipc_perm;
20513044Sasami	union auditon_udata	ar_arg_auditon;
20648568Sbillf	char			*ar_arg_argv;
20713044Sasami	int			ar_arg_argc;
20813044Sasami	char			*ar_arg_envv;
20913044Sasami	int			ar_arg_envc;
210109472Sphk	int			ar_arg_exitstatus;
21113044Sasami	int			ar_arg_exitretval;
21213044Sasami	struct sockaddr_storage ar_arg_sockaddr;
21313044Sasami};
21413044Sasami
21513044Sasami/*
21613044Sasami * Arguments in the audit record are initially not defined; flags are set to
21713044Sasami * indicate if they are present so they can be included in the audit log
21813044Sasami * stream only if defined.
21913044Sasami */
22013044Sasami#define	ARG_IS_VALID(kar, arg)	((kar)->k_ar.ar_valid_arg & (arg))
22113044Sasami#define	ARG_SET_VALID(kar, arg) do {					\
22213044Sasami	(kar)->k_ar.ar_valid_arg |= (arg);				\
22313044Sasami} while (0)
22413044Sasami
22513044Sasami/*
22613044Sasami * In-kernel version of audit record; the basic record plus queue meta-data.
22713044Sasami * This record can also have a pointer set to some opaque data that will be
22813044Sasami * passed through to the audit writing mechanism.
22913044Sasami */
23013044Sasamistruct kaudit_record {
23113044Sasami	struct audit_record		 k_ar;
23213044Sasami	u_int32_t			 k_ar_commit;
23313044Sasami	void				*k_udata;	/* User data. */
23413044Sasami	u_int				 k_ulen;	/* User data length. */
23513044Sasami	struct uthread			*k_uthread;	/* Audited thread. */
23613044Sasami	TAILQ_ENTRY(kaudit_record)	 k_q;
23713044Sasami};
23813044SasamiTAILQ_HEAD(kaudit_queue, kaudit_record);
23913044Sasami
24013044Sasami/*
24113044Sasami * Functions to manage the allocation, release, and commit of kernel audit
24213044Sasami * records.
24313044Sasami */
24413044Sasamivoid			 audit_abort(struct kaudit_record *ar);
24513044Sasamivoid			 audit_commit(struct kaudit_record *ar, int error,
24613044Sasami			    int retval);
24713044Sasamistruct kaudit_record	*audit_new(int event, struct thread *td);
24813044Sasami
24913044Sasami/*
25013044Sasami * Functions relating to the conversion of internal kernel audit records to
25113044Sasami * the BSM file format.
25213044Sasami */
25313044Sasamistruct au_record;
254109421Sphkint	 kaudit_to_bsm(struct kaudit_record *kar, struct au_record **pau);
25513044Sasamiint	 bsm_rec_verify(void *rec);
25613044Sasami
25713044Sasami/*
25813044Sasami * Kernel versions of the libbsm audit record functions.
25913044Sasami */
26013044Sasamivoid	 kau_free(struct au_record *rec);
26113044Sasamivoid	 kau_init(void);
26213044Sasami
26313044Sasami/*
264109417Sphk * Return values for pre-selection and post-selection decisions.
265109417Sphk */
26613044Sasami#define	AU_PRS_SUCCESS	1
26713044Sasami#define	AU_PRS_FAILURE	2
268109417Sphk#define	AU_PRS_BOTH	(AU_PRS_SUCCESS|AU_PRS_FAILURE)
26913044Sasami
270109417Sphk/*
271109417Sphk * Data structures relating to the kernel audit queue.  Ideally, these might
27213044Sasami * be abstracted so that only accessor methods are exposed.
27348568Sbillf */
27413044Sasamiextern struct mtx		audit_mtx;
27513044Sasamiextern struct cv		audit_watermark_cv;
27613044Sasamiextern struct cv		audit_worker_cv;
27713044Sasamiextern struct kaudit_queue	audit_q;
27813044Sasamiextern int			audit_q_len;
27913044Sasamiextern int			audit_pre_q_len;
28013044Sasamiextern int			audit_in_failure;
28113044Sasami
28213044Sasami/*
28313044Sasami * Flags to use on audit files when opening and closing.
28413044Sasami */
28592539Simp#define	AUDIT_OPEN_FLAGS	(FWRITE | O_APPEND)
28613044Sasami#define	AUDIT_CLOSE_FLAGS	(FWRITE | O_APPEND)
28713044Sasami
28813044Sasami#include <sys/fcntl.h>
28913044Sasami#include <sys/kernel.h>
29013044Sasami#include <sys/malloc.h>
29132116Simp
29213044Sasami/*
29351690Sbillf * Some of the BSM tokenizer functions take different parameters in the
29432116Simp * kernel implementations in order to save the copying of large kernel data
29532116Simp * structures.  The prototypes of these functions are declared here.
29613044Sasami */
29732116Simptoken_t		*kau_to_socket(struct socket_au_info *soi);
29813044Sasami
29913044Sasami/*
30013044Sasami * audit_klib prototypes
30132116Simp */
30213044Sasamiint		 au_preselect(au_event_t event, au_class_t class,
30313044Sasami		    au_mask_t *mask_p, int sorf);
30413044Sasamivoid		 au_evclassmap_init(void);
30513044Sasamivoid		 au_evclassmap_insert(au_event_t event, au_class_t class);
30613044Sasamiau_class_t	 au_event_class(au_event_t event);
30713044Sasamiau_event_t	 audit_ctlname_to_sysctlevent(int name[], uint64_t valid_arg);
30813044Sasamiau_event_t	 audit_flags_and_error_to_openevent(int oflags, int error);
30913044Sasamiint		 audit_msgctl_to_event(int cmd);
31013044Sasamiint		 audit_semctl_to_event(int cmr);
31113044Sasamivoid		 audit_canon_path(struct thread *td, char *path, char *cpath);
31213044Sasamiint		 auditon_command_event(int cmd);
31313044Sasami
31413044Sasami/*
31513044Sasami * Audit trigger events notify user space of kernel audit conditions
31613044Sasami * asynchronously.
31713044Sasami */
31813044Sasamivoid		 audit_trigger_init(void);
31913044Sasamiint		 audit_send_trigger(unsigned int trigger);
32013044Sasami
32113044Sasami/*
32213044Sasami * General audit related functions.
32313044Sasami */
32413044Sasamistruct kaudit_record	*currecord(void);
32513044Sasamivoid			 audit_free(struct kaudit_record *ar);
32613044Sasamivoid			 audit_shutdown(void *arg, int howto);
32713044Sasamivoid			 audit_rotate_vnode(struct ucred *cred,
32813044Sasami			    struct vnode *vp);
32913044Sasamivoid			 audit_worker_init(void);
33013044Sasami
33113044Sasami/*
33213044Sasami * Audit pipe functions.
33313044Sasami */
33413044Sasamiint	 audit_pipe_preselect(au_id_t auid, au_event_t event,
33513044Sasami	    au_class_t class, int sorf, int trail_select);
33613044Sasamivoid	 audit_pipe_submit(au_id_t auid, au_event_t event, au_class_t class,
33713044Sasami	    int sorf, int trail_select, void *record, u_int record_len);
33813044Sasamivoid	 audit_pipe_submit_user(void *record, u_int record_len);
33913044Sasami
34013044Sasami#endif /* ! _SECURITY_AUDIT_PRIVATE_H_ */
34113044Sasami