audit_private.h revision 162380
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1999-2005 Apple Computer, Inc.
31590Srgrimes * All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1.  Redistributions of source code must retain the above copyright
91590Srgrimes *     notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2.  Redistributions in binary form must reproduce the above copyright
111590Srgrimes *     notice, this list of conditions and the following disclaimer in the
121590Srgrimes *     documentation and/or other materials provided with the distribution.
131590Srgrimes * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
141590Srgrimes *     its contributors may be used to endorse or promote products derived
151590Srgrimes *     from this software without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
211590Srgrimes * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
251590Srgrimes * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
261590Srgrimes * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
271590Srgrimes * POSSIBILITY OF SUCH DAMAGE.
281590Srgrimes *
291590Srgrimes * $FreeBSD: head/sys/security/audit/audit_private.h 162380 2006-09-17 17:52:57Z csjp $
301590Srgrimes */
311590Srgrimes
321590Srgrimes/*
331590Srgrimes * This include file contains function prototypes and type definitions used
341590Srgrimes * within the audit implementation.
351590Srgrimes */
361590Srgrimes
371590Srgrimes#ifndef _SECURITY_AUDIT_PRIVATE_H_
381590Srgrimes#define	_SECURITY_AUDIT_PRIVATE_H_
391590Srgrimes
401590Srgrimes#ifndef _KERNEL
411590Srgrimes#error "no user-serviceable parts inside"
421590Srgrimes#endif
431590Srgrimes
441590Srgrimes#include <sys/ipc.h>
451590Srgrimes#include <sys/socket.h>
461590Srgrimes#include <sys/ucred.h>
4711914Sphk
481590Srgrimes#ifdef MALLOC_DECLARE
491590SrgrimesMALLOC_DECLARE(M_AUDITBSM);
501590SrgrimesMALLOC_DECLARE(M_AUDITDATA);
511590SrgrimesMALLOC_DECLARE(M_AUDITPATH);
521590SrgrimesMALLOC_DECLARE(M_AUDITTEXT);
531590Srgrimes#endif
541590Srgrimes
551590Srgrimes/*
561590Srgrimes * The AUDIT_EXCESSIVELY_VERBOSE define enables a number of gratuitously
571590Srgrimes * noisy printf's to the console.  Due to the volume, it should be left off
581590Srgrimes * unless you want your system to churn a lot whenever the audit record flow
591590Srgrimes * gets high.
601590Srgrimes */
611590Srgrimes//#define	AUDIT_EXCESSIVELY_VERBOSE
621590Srgrimes#ifdef AUDIT_EXCESSIVELY_VERBOSE
631590Srgrimes#define	AUDIT_PRINTF(x)	printf x
641590Srgrimes#else
651590Srgrimes#define	AUDIT_PRINTF(x)
661590Srgrimes#endif
671590Srgrimes
681590Srgrimes/*
691590Srgrimes * Audit control variables that are usually set/read via system calls and
701590Srgrimes * used to control various aspects of auditing.
711590Srgrimes */
721590Srgrimesextern struct au_qctrl		audit_qctrl;
731590Srgrimesextern struct audit_fstat	audit_fstat;
741590Srgrimesextern struct au_mask		audit_nae_mask;
751590Srgrimesextern int			audit_panic_on_write_fail;
761590Srgrimesextern int			audit_fail_stop;
771590Srgrimesextern int			audit_argv;
781590Srgrimesextern int			audit_arge;
791590Srgrimes
801590Srgrimes/*
811590Srgrimes * Success/failure conditions for the conversion of a kernel audit record to
821590Srgrimes * BSM format.
831590Srgrimes */
841590Srgrimes#define	BSM_SUCCESS	0
851590Srgrimes#define	BSM_FAILURE	1
861590Srgrimes#define	BSM_NOAUDIT	2
871590Srgrimes
881590Srgrimes/*
891590Srgrimes * Defines for the kernel audit record k_ar_commit field.  Flags are set to
901590Srgrimes * indicate what sort of record it is, and which preselection mechanism
911590Srgrimes * selected it.
921590Srgrimes */
931590Srgrimes#define	AR_COMMIT_KERNEL	0x00000001U
941590Srgrimes#define	AR_COMMIT_USER		0x00000010U
951590Srgrimes
961590Srgrimes#define	AR_PRESELECT_TRAIL	0x00001000U
971590Srgrimes#define	AR_PRESELECT_PIPE	0x00002000U
981590Srgrimes
991590Srgrimes#define	AR_PRESELECT_USER_TRAIL	0x00004000U
1001590Srgrimes#define	AR_PRESELECT_USER_PIPE	0x00008000U
1011590Srgrimes
1021590Srgrimes/*
1031590Srgrimes * Audit data is generated as a stream of struct audit_record structures,
1041590Srgrimes * linked by struct kaudit_record, and contain storage for possible audit so
1051590Srgrimes * that it will not need to be allocated during the processing of a system
1061590Srgrimes * call, both improving efficiency and avoiding sleeping at untimely moments.
1071590Srgrimes * This structure is converted to BSM format before being written to disk.
1081590Srgrimes */
1091590Srgrimesstruct vnode_au_info {
1101590Srgrimes	mode_t	vn_mode;
1111590Srgrimes	uid_t	vn_uid;
1121590Srgrimes	gid_t	vn_gid;
1131590Srgrimes	dev_t	vn_dev;
1141590Srgrimes	long	vn_fsid;
1151590Srgrimes	long	vn_fileid;
1161590Srgrimes	long	vn_gen;
1171590Srgrimes};
1181590Srgrimes
1191590Srgrimesstruct groupset {
1201590Srgrimes	gid_t	gidset[NGROUPS];
1211590Srgrimes	u_int	gidset_size;
1221590Srgrimes};
1231590Srgrimes
1241590Srgrimesstruct socket_au_info {
1251590Srgrimes	int 		so_domain;
1261590Srgrimes	int		so_type;
1271590Srgrimes	int		so_protocol;
1281590Srgrimes	in_addr_t	so_raddr;	/* Remote address if INET socket. */
1291590Srgrimes	in_addr_t	so_laddr;	/* Local address if INET socket. */
1301590Srgrimes	u_short		so_rport;	/* Remote port. */
1311590Srgrimes	u_short		so_lport;	/* Local port. */
1321590Srgrimes};
1331590Srgrimes
1341590Srgrimesunion auditon_udata {
1351590Srgrimes	char			*au_path;
1361590Srgrimes	long			au_cond;
1371590Srgrimes	long			au_flags;
1381590Srgrimes	long			au_policy;
1391590Srgrimes	int			au_trigger;
1401590Srgrimes	au_evclass_map_t	au_evclass;
1411590Srgrimes	au_mask_t		au_mask;
1421590Srgrimes	auditinfo_t		au_auinfo;
1431590Srgrimes	auditpinfo_t		au_aupinfo;
1441590Srgrimes	auditpinfo_addr_t	au_aupinfo_addr;
1451590Srgrimes	au_qctrl_t		au_qctrl;
1461590Srgrimes	au_stat_t		au_stat;
1471590Srgrimes	au_fstat_t		au_fstat;
1481590Srgrimes};
1491590Srgrimes
1501590Srgrimesstruct posix_ipc_perm {
1511590Srgrimes	uid_t	pipc_uid;
1521590Srgrimes	gid_t	pipc_gid;
1531590Srgrimes	mode_t	pipc_mode;
1541590Srgrimes};
1551590Srgrimes
1561590Srgrimesstruct audit_record {
1571590Srgrimes	/* Audit record header. */
1581590Srgrimes	u_int32_t		ar_magic;
1591590Srgrimes	int			ar_event;
1601590Srgrimes	int			ar_retval; /* value returned to the process */
1611590Srgrimes	int			ar_errno;  /* return status of system call */
1621590Srgrimes	struct timespec		ar_starttime;
1631590Srgrimes	struct timespec		ar_endtime;
1641590Srgrimes	u_int64_t		ar_valid_arg;  /* Bitmask of valid arguments */
1651590Srgrimes
1661590Srgrimes	/* Audit subject information. */
1671590Srgrimes	struct xucred		ar_subj_cred;
1681590Srgrimes	uid_t			ar_subj_ruid;
1691590Srgrimes	gid_t			ar_subj_rgid;
1701590Srgrimes	gid_t			ar_subj_egid;
1711590Srgrimes	uid_t			ar_subj_auid; /* Audit user ID */
1721590Srgrimes	pid_t			ar_subj_asid; /* Audit session ID */
1731590Srgrimes	pid_t			ar_subj_pid;
1741590Srgrimes	struct au_tid		ar_subj_term;
1751590Srgrimes	char			ar_subj_comm[MAXCOMLEN + 1];
1761590Srgrimes	struct au_mask		ar_subj_amask;
1771590Srgrimes
1781590Srgrimes	/* Operation arguments. */
1791590Srgrimes	uid_t			ar_arg_euid;
1801590Srgrimes	uid_t			ar_arg_ruid;
1811590Srgrimes	uid_t			ar_arg_suid;
1821590Srgrimes	gid_t			ar_arg_egid;
1831590Srgrimes	gid_t			ar_arg_rgid;
1841590Srgrimes	gid_t			ar_arg_sgid;
1851590Srgrimes	pid_t			ar_arg_pid;
1861590Srgrimes	pid_t			ar_arg_asid;
1871590Srgrimes	struct au_tid		ar_arg_termid;
1881590Srgrimes	uid_t			ar_arg_uid;
1891590Srgrimes	uid_t			ar_arg_auid;
1901590Srgrimes	gid_t			ar_arg_gid;
1911590Srgrimes	struct groupset		ar_arg_groups;
1921590Srgrimes	int			ar_arg_fd;
1931590Srgrimes	int			ar_arg_fflags;
1941590Srgrimes	mode_t			ar_arg_mode;
1951590Srgrimes	int			ar_arg_dev;
1961590Srgrimes	long			ar_arg_value;
1971590Srgrimes	void *			ar_arg_addr;
1981590Srgrimes	int			ar_arg_len;
1991590Srgrimes	int			ar_arg_mask;
2001590Srgrimes	u_int			ar_arg_signum;
2011590Srgrimes	char			ar_arg_login[MAXLOGNAME];
2021590Srgrimes	int			ar_arg_ctlname[CTL_MAXNAME];
2031590Srgrimes	struct sockaddr		ar_arg_sockaddr;
2041590Srgrimes	struct socket_au_info	ar_arg_sockinfo;
2051590Srgrimes	char			*ar_arg_upath1;
2061590Srgrimes	char			*ar_arg_upath2;
2071590Srgrimes	char			*ar_arg_text;
2081590Srgrimes	struct au_mask		ar_arg_amask;
2091590Srgrimes	struct vnode_au_info	ar_arg_vnode1;
2101590Srgrimes	struct vnode_au_info	ar_arg_vnode2;
2111590Srgrimes	int			ar_arg_cmd;
2121590Srgrimes	int			ar_arg_svipc_cmd;
2138874Srgrimes	struct ipc_perm		ar_arg_svipc_perm;
2141590Srgrimes	int			ar_arg_svipc_id;
2151590Srgrimes	void *			ar_arg_svipc_addr;
2161590Srgrimes	struct posix_ipc_perm	ar_arg_pipc_perm;
2171590Srgrimes	union auditon_udata	ar_arg_auditon;
2181590Srgrimes	char			*ar_arg_argv;
2191590Srgrimes	int			ar_arg_argc;
2201590Srgrimes	char			*ar_arg_envv;
2211590Srgrimes	int			ar_arg_envc;
2221590Srgrimes	int			ar_arg_exitstatus;
2231590Srgrimes	int			ar_arg_exitretval;
2241590Srgrimes};
2251590Srgrimes
2261590Srgrimes/*
2271590Srgrimes * Arguments in the audit record are initially not defined; flags are set to
2281590Srgrimes * indicate if they are present so they can be included in the audit log
2291590Srgrimes * stream only if defined.
2301590Srgrimes */
2311590Srgrimes#define	ARG_IS_VALID(kar, arg)	((kar)->k_ar.ar_valid_arg & (arg))
2321590Srgrimes#define	ARG_SET_VALID(kar, arg) do {					\
2331590Srgrimes	(kar)->k_ar.ar_valid_arg |= (arg);				\
2341590Srgrimes} while (0)
2351590Srgrimes
2361590Srgrimes/*
2371590Srgrimes * In-kernel version of audit record; the basic record plus queue meta-data.
2381590Srgrimes * This record can also have a pointer set to some opaque data that will be
2391590Srgrimes * passed through to the audit writing mechanism.
2401590Srgrimes */
2411590Srgrimesstruct kaudit_record {
2421590Srgrimes	struct audit_record		 k_ar;
2431590Srgrimes	u_int32_t			 k_ar_commit;
2441590Srgrimes	void				*k_udata;	/* User data. */
2451590Srgrimes	u_int				 k_ulen;	/* User data length. */
2461590Srgrimes	struct uthread			*k_uthread;	/* Audited thread. */
247	TAILQ_ENTRY(kaudit_record)	 k_q;
248};
249TAILQ_HEAD(kaudit_queue, kaudit_record);
250
251/*
252 * Functions to manage the allocation, release, and commit of kernel audit
253 * records.
254 */
255void			 audit_abort(struct kaudit_record *ar);
256void			 audit_commit(struct kaudit_record *ar, int error,
257			    int retval);
258struct kaudit_record	*audit_new(int event, struct thread *td);
259
260/*
261 * Functions relating to the conversion of internal kernel audit records to
262 * the BSM file format.
263 */
264struct au_record;
265int	 kaudit_to_bsm(struct kaudit_record *kar, struct au_record **pau);
266int	 bsm_rec_verify(void *rec);
267
268/*
269 * Kernel versions of the libbsm audit record functions.
270 */
271void	 kau_free(struct au_record *rec);
272void	 kau_init(void);
273
274/*
275 * Return values for pre-selection and post-selection decisions.
276 */
277#define	AU_PRS_SUCCESS	1
278#define	AU_PRS_FAILURE	2
279#define	AU_PRS_BOTH	(AU_PRS_SUCCESS|AU_PRS_FAILURE)
280
281/*
282 * Data structures relating to the kernel audit queue.  Ideally, these might
283 * be abstracted so that only accessor methods are exposed.
284 */
285extern struct mtx		audit_mtx;
286extern struct cv		audit_watermark_cv;
287extern struct cv		audit_worker_cv;
288extern struct kaudit_queue	audit_q;
289extern int			audit_q_len;
290extern int			audit_pre_q_len;
291extern int			audit_in_failure;
292
293/*
294 * Flags to use on audit files when opening and closing.
295 */
296#define	AUDIT_OPEN_FLAGS	(FWRITE | O_APPEND)
297#define	AUDIT_CLOSE_FLAGS	(FWRITE | O_APPEND)
298
299#include <sys/fcntl.h>
300#include <sys/kernel.h>
301#include <sys/malloc.h>
302
303/*
304 * Some of the BSM tokenizer functions take different parameters in the
305 * kernel implementations in order to save the copying of large kernel data
306 * structures. The prototypes of these functions are declared here.
307 */
308token_t		*kau_to_socket(struct socket_au_info *soi);
309
310/*
311 * audit_klib prototypes
312 */
313int		 au_preselect(au_event_t event, au_class_t class,
314		    au_mask_t *mask_p, int sorf);
315au_event_t	 flags_and_error_to_openevent(int oflags, int error);
316void		 au_evclassmap_init(void);
317void		 au_evclassmap_insert(au_event_t event, au_class_t class);
318au_class_t	 au_event_class(au_event_t event);
319au_event_t	 ctlname_to_sysctlevent(int name[], uint64_t valid_arg);
320int		 auditon_command_event(int cmd);
321int		 msgctl_to_event(int cmd);
322int		 semctl_to_event(int cmr);
323void		 canon_path(struct thread *td, char *path, char *cpath);
324
325/*
326 * Audit trigger events notify user space of kernel audit conditions
327 * asynchronously.
328 */
329void		 audit_trigger_init(void);
330int		 send_trigger(unsigned int trigger);
331
332/*
333 * General audit related functions.
334 */
335struct kaudit_record	*currecord(void);
336void			 audit_free(struct kaudit_record *ar);
337void			 audit_shutdown(void *arg, int howto);
338void			 audit_rotate_vnode(struct ucred *cred,
339			    struct vnode *vp);
340void			 audit_worker_init(void);
341
342/*
343 * Audit pipe functions.
344 */
345int	 audit_pipe_preselect(au_id_t auid, au_event_t event,
346	    au_class_t class, int sorf, int trail_select);
347void	 audit_pipe_submit(au_id_t auid, au_event_t event, au_class_t class,
348	    int sorf, int trail_select, void *record, u_int record_len);
349void	 audit_pipe_submit_user(void *record, u_int record_len);
350
351#endif /* ! _SECURITY_AUDIT_PRIVATE_H_ */
352