audit_private.h revision 195926
1181053Srwatson/*-
2188313Srwatson * Copyright (c) 1999-2009 Apple Inc.
3155192Srwatson * All rights reserved.
4155192Srwatson *
5155192Srwatson * Redistribution and use in source and binary forms, with or without
6155192Srwatson * modification, are permitted provided that the following conditions
7155192Srwatson * are met:
8155192Srwatson * 1.  Redistributions of source code must retain the above copyright
9155192Srwatson *     notice, this list of conditions and the following disclaimer.
10155192Srwatson * 2.  Redistributions in binary form must reproduce the above copyright
11155192Srwatson *     notice, this list of conditions and the following disclaimer in the
12155192Srwatson *     documentation and/or other materials provided with the distribution.
13180701Srwatson * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14155192Srwatson *     its contributors may be used to endorse or promote products derived
15155192Srwatson *     from this software without specific prior written permission.
16155192Srwatson *
17155192Srwatson * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18155192Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19155192Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20155192Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21155192Srwatson * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22155192Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23155192Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24155192Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25155192Srwatson * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26155192Srwatson * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27155192Srwatson * POSSIBILITY OF SUCH DAMAGE.
28155192Srwatson *
29155192Srwatson * $FreeBSD: head/sys/security/audit/audit_private.h 195926 2009-07-28 21:52:24Z rwatson $
30155192Srwatson */
31155192Srwatson
32155192Srwatson/*
33155192Srwatson * This include file contains function prototypes and type definitions used
34155192Srwatson * within the audit implementation.
35155192Srwatson */
36155192Srwatson
37156882Srwatson#ifndef _SECURITY_AUDIT_PRIVATE_H_
38156882Srwatson#define	_SECURITY_AUDIT_PRIVATE_H_
39155192Srwatson
40155192Srwatson#ifndef _KERNEL
41155192Srwatson#error "no user-serviceable parts inside"
42155192Srwatson#endif
43155192Srwatson
44155192Srwatson#include <sys/ipc.h>
45155192Srwatson#include <sys/socket.h>
46155192Srwatson#include <sys/ucred.h>
47155192Srwatson
48155192Srwatson#ifdef MALLOC_DECLARE
49155192SrwatsonMALLOC_DECLARE(M_AUDITBSM);
50155192SrwatsonMALLOC_DECLARE(M_AUDITDATA);
51155192SrwatsonMALLOC_DECLARE(M_AUDITPATH);
52155192SrwatsonMALLOC_DECLARE(M_AUDITTEXT);
53195177SssonMALLOC_DECLARE(M_AUDITGIDSET);
54155192Srwatson#endif
55155192Srwatson
56155192Srwatson/*
57156889Srwatson * Audit control variables that are usually set/read via system calls and
58156889Srwatson * used to control various aspects of auditing.
59155192Srwatson */
60155192Srwatsonextern struct au_qctrl		audit_qctrl;
61155192Srwatsonextern struct audit_fstat	audit_fstat;
62155192Srwatsonextern struct au_mask		audit_nae_mask;
63155192Srwatsonextern int			audit_panic_on_write_fail;
64155192Srwatsonextern int			audit_fail_stop;
65161813Swsalamonextern int			audit_argv;
66161813Swsalamonextern int			audit_arge;
67155192Srwatson
68155192Srwatson/*
69155192Srwatson * Success/failure conditions for the conversion of a kernel audit record to
70155192Srwatson * BSM format.
71155192Srwatson */
72156889Srwatson#define	BSM_SUCCESS	0
73156889Srwatson#define	BSM_FAILURE	1
74156889Srwatson#define	BSM_NOAUDIT	2
75155192Srwatson
76155192Srwatson/*
77159269Srwatson * Defines for the kernel audit record k_ar_commit field.  Flags are set to
78159269Srwatson * indicate what sort of record it is, and which preselection mechanism
79159269Srwatson * selected it.
80155192Srwatson */
81155192Srwatson#define	AR_COMMIT_KERNEL	0x00000001U
82155192Srwatson#define	AR_COMMIT_USER		0x00000010U
83155192Srwatson
84159269Srwatson#define	AR_PRESELECT_TRAIL	0x00001000U
85159269Srwatson#define	AR_PRESELECT_PIPE	0x00002000U
86159269Srwatson
87162380Scsjp#define	AR_PRESELECT_USER_TRAIL	0x00004000U
88162380Scsjp#define	AR_PRESELECT_USER_PIPE	0x00008000U
89162380Scsjp
90155192Srwatson/*
91155192Srwatson * Audit data is generated as a stream of struct audit_record structures,
92155192Srwatson * linked by struct kaudit_record, and contain storage for possible audit so
93155192Srwatson * that it will not need to be allocated during the processing of a system
94155192Srwatson * call, both improving efficiency and avoiding sleeping at untimely moments.
95155192Srwatson * This structure is converted to BSM format before being written to disk.
96155192Srwatson */
97155192Srwatsonstruct vnode_au_info {
98156889Srwatson	mode_t	vn_mode;
99156889Srwatson	uid_t	vn_uid;
100156889Srwatson	gid_t	vn_gid;
101156889Srwatson	dev_t	vn_dev;
102156889Srwatson	long	vn_fsid;
103156889Srwatson	long	vn_fileid;
104156889Srwatson	long	vn_gen;
105155192Srwatson};
106155192Srwatson
107155192Srwatsonstruct groupset {
108195177Ssson	gid_t	*gidset;
109195177Ssson	u_int	 gidset_size;
110155192Srwatson};
111155192Srwatson
112155192Srwatsonstruct socket_au_info {
113180709Srwatson	int		so_domain;
114155192Srwatson	int		so_type;
115155192Srwatson	int		so_protocol;
116156889Srwatson	in_addr_t	so_raddr;	/* Remote address if INET socket. */
117156889Srwatson	in_addr_t	so_laddr;	/* Local address if INET socket. */
118156889Srwatson	u_short		so_rport;	/* Remote port. */
119156889Srwatson	u_short		so_lport;	/* Local port. */
120155192Srwatson};
121155192Srwatson
122191270Srwatson/*
123191270Srwatson * The following is used for A_OLDSETQCTRL and AU_OLDGETQCTRL and a 64-bit
124191270Srwatson * userland.
125191270Srwatson */
126191270Srwatsonstruct au_qctrl64 {
127191270Srwatson	u_int64_t	aq64_hiwater;
128191270Srwatson	u_int64_t	aq64_lowater;
129191270Srwatson	u_int64_t	aq64_bufsz;
130191270Srwatson	u_int64_t	aq64_delay;
131191270Srwatson	u_int64_t	aq64_minfree;
132191270Srwatson};
133191270Srwatsontypedef	struct au_qctrl64	au_qctrl64_t;
134191270Srwatson
135155192Srwatsonunion auditon_udata {
136155192Srwatson	char			*au_path;
137191270Srwatson	int			au_cond;
138191270Srwatson	int			au_flags;
139191270Srwatson	int			au_policy;
140155192Srwatson	int			au_trigger;
141191270Srwatson	int64_t			au_cond64;
142191270Srwatson	int64_t			au_policy64;
143155192Srwatson	au_evclass_map_t	au_evclass;
144155192Srwatson	au_mask_t		au_mask;
145155192Srwatson	auditinfo_t		au_auinfo;
146155192Srwatson	auditpinfo_t		au_aupinfo;
147155192Srwatson	auditpinfo_addr_t	au_aupinfo_addr;
148155192Srwatson	au_qctrl_t		au_qctrl;
149191270Srwatson	au_qctrl64_t		au_qctrl64;
150155192Srwatson	au_stat_t		au_stat;
151155192Srwatson	au_fstat_t		au_fstat;
152184856Scsjp	auditinfo_addr_t	au_kau_info;
153155192Srwatson};
154155192Srwatson
155155192Srwatsonstruct posix_ipc_perm {
156156889Srwatson	uid_t	pipc_uid;
157156889Srwatson	gid_t	pipc_gid;
158156889Srwatson	mode_t	pipc_mode;
159155192Srwatson};
160155192Srwatson
161155192Srwatsonstruct audit_record {
162155192Srwatson	/* Audit record header. */
163155192Srwatson	u_int32_t		ar_magic;
164155192Srwatson	int			ar_event;
165155192Srwatson	int			ar_retval; /* value returned to the process */
166155192Srwatson	int			ar_errno;  /* return status of system call */
167155192Srwatson	struct timespec		ar_starttime;
168155192Srwatson	struct timespec		ar_endtime;
169155192Srwatson	u_int64_t		ar_valid_arg;  /* Bitmask of valid arguments */
170155192Srwatson
171155192Srwatson	/* Audit subject information. */
172156889Srwatson	struct xucred		ar_subj_cred;
173156889Srwatson	uid_t			ar_subj_ruid;
174156889Srwatson	gid_t			ar_subj_rgid;
175156889Srwatson	gid_t			ar_subj_egid;
176156889Srwatson	uid_t			ar_subj_auid; /* Audit user ID */
177156889Srwatson	pid_t			ar_subj_asid; /* Audit session ID */
178156889Srwatson	pid_t			ar_subj_pid;
179156889Srwatson	struct au_tid		ar_subj_term;
180168688Scsjp	struct au_tid_addr	ar_subj_term_addr;
181156889Srwatson	struct au_mask		ar_subj_amask;
182155192Srwatson
183155192Srwatson	/* Operation arguments. */
184156889Srwatson	uid_t			ar_arg_euid;
185156889Srwatson	uid_t			ar_arg_ruid;
186156889Srwatson	uid_t			ar_arg_suid;
187156889Srwatson	gid_t			ar_arg_egid;
188156889Srwatson	gid_t			ar_arg_rgid;
189156889Srwatson	gid_t			ar_arg_sgid;
190156889Srwatson	pid_t			ar_arg_pid;
191156889Srwatson	pid_t			ar_arg_asid;
192156889Srwatson	struct au_tid		ar_arg_termid;
193168688Scsjp	struct au_tid_addr	ar_arg_termid_addr;
194156889Srwatson	uid_t			ar_arg_uid;
195156889Srwatson	uid_t			ar_arg_auid;
196156889Srwatson	gid_t			ar_arg_gid;
197156889Srwatson	struct groupset		ar_arg_groups;
198156889Srwatson	int			ar_arg_fd;
199195925Srwatson	int			ar_arg_atfd1;
200195925Srwatson	int			ar_arg_atfd2;
201156889Srwatson	int			ar_arg_fflags;
202156889Srwatson	mode_t			ar_arg_mode;
203156889Srwatson	int			ar_arg_dev;
204156889Srwatson	long			ar_arg_value;
205156889Srwatson	void *			ar_arg_addr;
206156889Srwatson	int			ar_arg_len;
207156889Srwatson	int			ar_arg_mask;
208156889Srwatson	u_int			ar_arg_signum;
209156889Srwatson	char			ar_arg_login[MAXLOGNAME];
210156889Srwatson	int			ar_arg_ctlname[CTL_MAXNAME];
211156889Srwatson	struct socket_au_info	ar_arg_sockinfo;
212156889Srwatson	char			*ar_arg_upath1;
213156889Srwatson	char			*ar_arg_upath2;
214156889Srwatson	char			*ar_arg_text;
215156889Srwatson	struct au_mask		ar_arg_amask;
216156889Srwatson	struct vnode_au_info	ar_arg_vnode1;
217156889Srwatson	struct vnode_au_info	ar_arg_vnode2;
218156889Srwatson	int			ar_arg_cmd;
219156889Srwatson	int			ar_arg_svipc_cmd;
220156889Srwatson	struct ipc_perm		ar_arg_svipc_perm;
221156889Srwatson	int			ar_arg_svipc_id;
222156889Srwatson	void *			ar_arg_svipc_addr;
223156889Srwatson	struct posix_ipc_perm	ar_arg_pipc_perm;
224156889Srwatson	union auditon_udata	ar_arg_auditon;
225161813Swsalamon	char			*ar_arg_argv;
226161813Swsalamon	int			ar_arg_argc;
227161813Swsalamon	char			*ar_arg_envv;
228161813Swsalamon	int			ar_arg_envc;
229156889Srwatson	int			ar_arg_exitstatus;
230156889Srwatson	int			ar_arg_exitretval;
231164011Scsjp	struct sockaddr_storage ar_arg_sockaddr;
232155192Srwatson};
233155192Srwatson
234155192Srwatson/*
235155192Srwatson * Arguments in the audit record are initially not defined; flags are set to
236155192Srwatson * indicate if they are present so they can be included in the audit log
237155192Srwatson * stream only if defined.
238155192Srwatson */
239155192Srwatson#define	ARG_IS_VALID(kar, arg)	((kar)->k_ar.ar_valid_arg & (arg))
240155192Srwatson#define	ARG_SET_VALID(kar, arg) do {					\
241155192Srwatson	(kar)->k_ar.ar_valid_arg |= (arg);				\
242155192Srwatson} while (0)
243195926Srwatson#define	ARG_CLEAR_VALID(kar, arg) do {					\
244195926Srwatson	(kar)->k_ar.ar_valid_arg &= ~(arg);				\
245195926Srwatson} while (0)
246155192Srwatson
247155192Srwatson/*
248155192Srwatson * In-kernel version of audit record; the basic record plus queue meta-data.
249156889Srwatson * This record can also have a pointer set to some opaque data that will be
250156889Srwatson * passed through to the audit writing mechanism.
251155192Srwatson */
252155192Srwatsonstruct kaudit_record {
253156889Srwatson	struct audit_record		 k_ar;
254156889Srwatson	u_int32_t			 k_ar_commit;
255156889Srwatson	void				*k_udata;	/* User data. */
256156889Srwatson	u_int				 k_ulen;	/* User data length. */
257156889Srwatson	struct uthread			*k_uthread;	/* Audited thread. */
258156889Srwatson	TAILQ_ENTRY(kaudit_record)	 k_q;
259155192Srwatson};
260156888SrwatsonTAILQ_HEAD(kaudit_queue, kaudit_record);
261155192Srwatson
262155192Srwatson/*
263155192Srwatson * Functions to manage the allocation, release, and commit of kernel audit
264155192Srwatson * records.
265155192Srwatson */
266155192Srwatsonvoid			 audit_abort(struct kaudit_record *ar);
267156888Srwatsonvoid			 audit_commit(struct kaudit_record *ar, int error,
268155192Srwatson			    int retval);
269155192Srwatsonstruct kaudit_record	*audit_new(int event, struct thread *td);
270155192Srwatson
271155192Srwatson/*
272155192Srwatson * Functions relating to the conversion of internal kernel audit records to
273155192Srwatson * the BSM file format.
274155192Srwatson */
275156291Srwatsonstruct au_record;
276156889Srwatsonint	 kaudit_to_bsm(struct kaudit_record *kar, struct au_record **pau);
277156889Srwatsonint	 bsm_rec_verify(void *rec);
278155192Srwatson
279155192Srwatson/*
280155192Srwatson * Kernel versions of the libbsm audit record functions.
281155192Srwatson */
282156889Srwatsonvoid	 kau_free(struct au_record *rec);
283156889Srwatsonvoid	 kau_init(void);
284155192Srwatson
285155192Srwatson/*
286155192Srwatson * Return values for pre-selection and post-selection decisions.
287155192Srwatson */
288156889Srwatson#define	AU_PRS_SUCCESS	1
289156889Srwatson#define	AU_PRS_FAILURE	2
290156889Srwatson#define	AU_PRS_BOTH	(AU_PRS_SUCCESS|AU_PRS_FAILURE)
291155192Srwatson
292156888Srwatson/*
293156888Srwatson * Data structures relating to the kernel audit queue.  Ideally, these might
294156888Srwatson * be abstracted so that only accessor methods are exposed.
295156888Srwatson */
296156889Srwatsonextern struct mtx		audit_mtx;
297159261Srwatsonextern struct cv		audit_watermark_cv;
298159261Srwatsonextern struct cv		audit_worker_cv;
299156889Srwatsonextern struct kaudit_queue	audit_q;
300191270Srwatsonextern int			audit_q_len;
301191270Srwatsonextern int			audit_pre_q_len;
302156889Srwatsonextern int			audit_in_failure;
303156888Srwatson
304156888Srwatson/*
305155192Srwatson * Flags to use on audit files when opening and closing.
306155192Srwatson */
307155192Srwatson#define	AUDIT_OPEN_FLAGS	(FWRITE | O_APPEND)
308155192Srwatson#define	AUDIT_CLOSE_FLAGS	(FWRITE | O_APPEND)
309155192Srwatson
310155192Srwatson#include <sys/fcntl.h>
311155192Srwatson#include <sys/kernel.h>
312155192Srwatson#include <sys/malloc.h>
313155192Srwatson
314155192Srwatson/*
315155192Srwatson * Some of the BSM tokenizer functions take different parameters in the
316156889Srwatson * kernel implementations in order to save the copying of large kernel data
317170196Srwatson * structures.  The prototypes of these functions are declared here.
318155192Srwatson */
319155192Srwatsontoken_t		*kau_to_socket(struct socket_au_info *soi);
320155192Srwatson
321155192Srwatson/*
322155192Srwatson * audit_klib prototypes
323155192Srwatson */
324159269Srwatsonint		 au_preselect(au_event_t event, au_class_t class,
325159269Srwatson		    au_mask_t *mask_p, int sorf);
326155192Srwatsonvoid		 au_evclassmap_init(void);
327155192Srwatsonvoid		 au_evclassmap_insert(au_event_t event, au_class_t class);
328155192Srwatsonau_class_t	 au_event_class(au_event_t event);
329176690Srwatsonau_event_t	 audit_ctlname_to_sysctlevent(int name[], uint64_t valid_arg);
330176690Srwatsonau_event_t	 audit_flags_and_error_to_openevent(int oflags, int error);
331195925Srwatsonau_event_t	 audit_flags_and_error_to_openatevent(int oflags, int error);
332188313Srwatsonau_event_t	 audit_msgctl_to_event(int cmd);
333188313Srwatsonau_event_t	 audit_semctl_to_event(int cmr);
334176565Srwatsonvoid		 audit_canon_path(struct thread *td, char *path, char *cpath);
335188313Srwatsonau_event_t	 auditon_command_event(int cmd);
336155192Srwatson
337155192Srwatson/*
338155192Srwatson * Audit trigger events notify user space of kernel audit conditions
339155192Srwatson * asynchronously.
340155192Srwatson */
341155192Srwatsonvoid		 audit_trigger_init(void);
342176686Srwatsonint		 audit_send_trigger(unsigned int trigger);
343155192Srwatson
344155192Srwatson/*
345184858Srwatson * Accessor functions to manage global audit state.
346184858Srwatson */
347184858Srwatsonvoid	 audit_set_kinfo(struct auditinfo_addr *);
348184858Srwatsonvoid	 audit_get_kinfo(struct auditinfo_addr *);
349184858Srwatson
350184858Srwatson/*
351155192Srwatson * General audit related functions.
352155192Srwatson */
353155192Srwatsonstruct kaudit_record	*currecord(void);
354156888Srwatsonvoid			 audit_free(struct kaudit_record *ar);
355155192Srwatsonvoid			 audit_shutdown(void *arg, int howto);
356155192Srwatsonvoid			 audit_rotate_vnode(struct ucred *cred,
357155192Srwatson			    struct vnode *vp);
358156888Srwatsonvoid			 audit_worker_init(void);
359155192Srwatson
360155408Srwatson/*
361155408Srwatson * Audit pipe functions.
362155408Srwatson */
363159269Srwatsonint	 audit_pipe_preselect(au_id_t auid, au_event_t event,
364159269Srwatson	    au_class_t class, int sorf, int trail_select);
365159269Srwatsonvoid	 audit_pipe_submit(au_id_t auid, au_event_t event, au_class_t class,
366159269Srwatson	    int sorf, int trail_select, void *record, u_int record_len);
367159269Srwatsonvoid	 audit_pipe_submit_user(void *record, u_int record_len);
368155408Srwatson
369156882Srwatson#endif /* ! _SECURITY_AUDIT_PRIVATE_H_ */
370