audit_private.h revision 180709
1155192Srwatson/*
2180701Srwatson * Copyright (c) 1999-2005 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 180709 2008-07-22 16:44:48Z 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);
53155192Srwatson#endif
54155192Srwatson
55155192Srwatson/*
56156889Srwatson * Audit control variables that are usually set/read via system calls and
57156889Srwatson * used to control various aspects of auditing.
58155192Srwatson */
59155192Srwatsonextern struct au_qctrl		audit_qctrl;
60155192Srwatsonextern struct audit_fstat	audit_fstat;
61155192Srwatsonextern struct au_mask		audit_nae_mask;
62155192Srwatsonextern int			audit_panic_on_write_fail;
63155192Srwatsonextern int			audit_fail_stop;
64161813Swsalamonextern int			audit_argv;
65161813Swsalamonextern int			audit_arge;
66155192Srwatson
67155192Srwatson/*
68155192Srwatson * Success/failure conditions for the conversion of a kernel audit record to
69155192Srwatson * BSM format.
70155192Srwatson */
71156889Srwatson#define	BSM_SUCCESS	0
72156889Srwatson#define	BSM_FAILURE	1
73156889Srwatson#define	BSM_NOAUDIT	2
74155192Srwatson
75155192Srwatson/*
76159269Srwatson * Defines for the kernel audit record k_ar_commit field.  Flags are set to
77159269Srwatson * indicate what sort of record it is, and which preselection mechanism
78159269Srwatson * selected it.
79155192Srwatson */
80155192Srwatson#define	AR_COMMIT_KERNEL	0x00000001U
81155192Srwatson#define	AR_COMMIT_USER		0x00000010U
82155192Srwatson
83159269Srwatson#define	AR_PRESELECT_TRAIL	0x00001000U
84159269Srwatson#define	AR_PRESELECT_PIPE	0x00002000U
85159269Srwatson
86162380Scsjp#define	AR_PRESELECT_USER_TRAIL	0x00004000U
87162380Scsjp#define	AR_PRESELECT_USER_PIPE	0x00008000U
88162380Scsjp
89155192Srwatson/*
90155192Srwatson * Audit data is generated as a stream of struct audit_record structures,
91155192Srwatson * linked by struct kaudit_record, and contain storage for possible audit so
92155192Srwatson * that it will not need to be allocated during the processing of a system
93155192Srwatson * call, both improving efficiency and avoiding sleeping at untimely moments.
94155192Srwatson * This structure is converted to BSM format before being written to disk.
95155192Srwatson */
96155192Srwatsonstruct vnode_au_info {
97156889Srwatson	mode_t	vn_mode;
98156889Srwatson	uid_t	vn_uid;
99156889Srwatson	gid_t	vn_gid;
100156889Srwatson	dev_t	vn_dev;
101156889Srwatson	long	vn_fsid;
102156889Srwatson	long	vn_fileid;
103156889Srwatson	long	vn_gen;
104155192Srwatson};
105155192Srwatson
106155192Srwatsonstruct groupset {
107155192Srwatson	gid_t	gidset[NGROUPS];
108155192Srwatson	u_int	gidset_size;
109155192Srwatson};
110155192Srwatson
111155192Srwatsonstruct socket_au_info {
112180709Srwatson	int		so_domain;
113155192Srwatson	int		so_type;
114155192Srwatson	int		so_protocol;
115156889Srwatson	in_addr_t	so_raddr;	/* Remote address if INET socket. */
116156889Srwatson	in_addr_t	so_laddr;	/* Local address if INET socket. */
117156889Srwatson	u_short		so_rport;	/* Remote port. */
118156889Srwatson	u_short		so_lport;	/* Local port. */
119155192Srwatson};
120155192Srwatson
121155192Srwatsonunion auditon_udata {
122155192Srwatson	char			*au_path;
123155192Srwatson	long			au_cond;
124155192Srwatson	long			au_flags;
125155192Srwatson	long			au_policy;
126155192Srwatson	int			au_trigger;
127155192Srwatson	au_evclass_map_t	au_evclass;
128155192Srwatson	au_mask_t		au_mask;
129155192Srwatson	auditinfo_t		au_auinfo;
130155192Srwatson	auditpinfo_t		au_aupinfo;
131155192Srwatson	auditpinfo_addr_t	au_aupinfo_addr;
132155192Srwatson	au_qctrl_t		au_qctrl;
133155192Srwatson	au_stat_t		au_stat;
134155192Srwatson	au_fstat_t		au_fstat;
135155192Srwatson};
136155192Srwatson
137155192Srwatsonstruct posix_ipc_perm {
138156889Srwatson	uid_t	pipc_uid;
139156889Srwatson	gid_t	pipc_gid;
140156889Srwatson	mode_t	pipc_mode;
141155192Srwatson};
142155192Srwatson
143155192Srwatsonstruct audit_record {
144155192Srwatson	/* Audit record header. */
145155192Srwatson	u_int32_t		ar_magic;
146155192Srwatson	int			ar_event;
147155192Srwatson	int			ar_retval; /* value returned to the process */
148155192Srwatson	int			ar_errno;  /* return status of system call */
149155192Srwatson	struct timespec		ar_starttime;
150155192Srwatson	struct timespec		ar_endtime;
151155192Srwatson	u_int64_t		ar_valid_arg;  /* Bitmask of valid arguments */
152155192Srwatson
153155192Srwatson	/* Audit subject information. */
154156889Srwatson	struct xucred		ar_subj_cred;
155156889Srwatson	uid_t			ar_subj_ruid;
156156889Srwatson	gid_t			ar_subj_rgid;
157156889Srwatson	gid_t			ar_subj_egid;
158156889Srwatson	uid_t			ar_subj_auid; /* Audit user ID */
159156889Srwatson	pid_t			ar_subj_asid; /* Audit session ID */
160156889Srwatson	pid_t			ar_subj_pid;
161156889Srwatson	struct au_tid		ar_subj_term;
162168688Scsjp	struct au_tid_addr	ar_subj_term_addr;
163156889Srwatson	struct au_mask		ar_subj_amask;
164155192Srwatson
165155192Srwatson	/* Operation arguments. */
166156889Srwatson	uid_t			ar_arg_euid;
167156889Srwatson	uid_t			ar_arg_ruid;
168156889Srwatson	uid_t			ar_arg_suid;
169156889Srwatson	gid_t			ar_arg_egid;
170156889Srwatson	gid_t			ar_arg_rgid;
171156889Srwatson	gid_t			ar_arg_sgid;
172156889Srwatson	pid_t			ar_arg_pid;
173156889Srwatson	pid_t			ar_arg_asid;
174156889Srwatson	struct au_tid		ar_arg_termid;
175168688Scsjp	struct au_tid_addr	ar_arg_termid_addr;
176156889Srwatson	uid_t			ar_arg_uid;
177156889Srwatson	uid_t			ar_arg_auid;
178156889Srwatson	gid_t			ar_arg_gid;
179156889Srwatson	struct groupset		ar_arg_groups;
180156889Srwatson	int			ar_arg_fd;
181156889Srwatson	int			ar_arg_fflags;
182156889Srwatson	mode_t			ar_arg_mode;
183156889Srwatson	int			ar_arg_dev;
184156889Srwatson	long			ar_arg_value;
185156889Srwatson	void *			ar_arg_addr;
186156889Srwatson	int			ar_arg_len;
187156889Srwatson	int			ar_arg_mask;
188156889Srwatson	u_int			ar_arg_signum;
189156889Srwatson	char			ar_arg_login[MAXLOGNAME];
190156889Srwatson	int			ar_arg_ctlname[CTL_MAXNAME];
191156889Srwatson	struct socket_au_info	ar_arg_sockinfo;
192156889Srwatson	char			*ar_arg_upath1;
193156889Srwatson	char			*ar_arg_upath2;
194156889Srwatson	char			*ar_arg_text;
195156889Srwatson	struct au_mask		ar_arg_amask;
196156889Srwatson	struct vnode_au_info	ar_arg_vnode1;
197156889Srwatson	struct vnode_au_info	ar_arg_vnode2;
198156889Srwatson	int			ar_arg_cmd;
199156889Srwatson	int			ar_arg_svipc_cmd;
200156889Srwatson	struct ipc_perm		ar_arg_svipc_perm;
201156889Srwatson	int			ar_arg_svipc_id;
202156889Srwatson	void *			ar_arg_svipc_addr;
203156889Srwatson	struct posix_ipc_perm	ar_arg_pipc_perm;
204156889Srwatson	union auditon_udata	ar_arg_auditon;
205161813Swsalamon	char			*ar_arg_argv;
206161813Swsalamon	int			ar_arg_argc;
207161813Swsalamon	char			*ar_arg_envv;
208161813Swsalamon	int			ar_arg_envc;
209156889Srwatson	int			ar_arg_exitstatus;
210156889Srwatson	int			ar_arg_exitretval;
211164011Scsjp	struct sockaddr_storage ar_arg_sockaddr;
212155192Srwatson};
213155192Srwatson
214155192Srwatson/*
215155192Srwatson * Arguments in the audit record are initially not defined; flags are set to
216155192Srwatson * indicate if they are present so they can be included in the audit log
217155192Srwatson * stream only if defined.
218155192Srwatson */
219155192Srwatson#define	ARG_IS_VALID(kar, arg)	((kar)->k_ar.ar_valid_arg & (arg))
220155192Srwatson#define	ARG_SET_VALID(kar, arg) do {					\
221155192Srwatson	(kar)->k_ar.ar_valid_arg |= (arg);				\
222155192Srwatson} while (0)
223155192Srwatson
224155192Srwatson/*
225155192Srwatson * In-kernel version of audit record; the basic record plus queue meta-data.
226156889Srwatson * This record can also have a pointer set to some opaque data that will be
227156889Srwatson * passed through to the audit writing mechanism.
228155192Srwatson */
229155192Srwatsonstruct kaudit_record {
230156889Srwatson	struct audit_record		 k_ar;
231156889Srwatson	u_int32_t			 k_ar_commit;
232156889Srwatson	void				*k_udata;	/* User data. */
233156889Srwatson	u_int				 k_ulen;	/* User data length. */
234156889Srwatson	struct uthread			*k_uthread;	/* Audited thread. */
235156889Srwatson	TAILQ_ENTRY(kaudit_record)	 k_q;
236155192Srwatson};
237156888SrwatsonTAILQ_HEAD(kaudit_queue, kaudit_record);
238155192Srwatson
239155192Srwatson/*
240155192Srwatson * Functions to manage the allocation, release, and commit of kernel audit
241155192Srwatson * records.
242155192Srwatson */
243155192Srwatsonvoid			 audit_abort(struct kaudit_record *ar);
244156888Srwatsonvoid			 audit_commit(struct kaudit_record *ar, int error,
245155192Srwatson			    int retval);
246155192Srwatsonstruct kaudit_record	*audit_new(int event, struct thread *td);
247155192Srwatson
248155192Srwatson/*
249155192Srwatson * Functions relating to the conversion of internal kernel audit records to
250155192Srwatson * the BSM file format.
251155192Srwatson */
252156291Srwatsonstruct au_record;
253156889Srwatsonint	 kaudit_to_bsm(struct kaudit_record *kar, struct au_record **pau);
254156889Srwatsonint	 bsm_rec_verify(void *rec);
255155192Srwatson
256155192Srwatson/*
257155192Srwatson * Kernel versions of the libbsm audit record functions.
258155192Srwatson */
259156889Srwatsonvoid	 kau_free(struct au_record *rec);
260156889Srwatsonvoid	 kau_init(void);
261155192Srwatson
262155192Srwatson/*
263155192Srwatson * Return values for pre-selection and post-selection decisions.
264155192Srwatson */
265156889Srwatson#define	AU_PRS_SUCCESS	1
266156889Srwatson#define	AU_PRS_FAILURE	2
267156889Srwatson#define	AU_PRS_BOTH	(AU_PRS_SUCCESS|AU_PRS_FAILURE)
268155192Srwatson
269156888Srwatson/*
270156888Srwatson * Data structures relating to the kernel audit queue.  Ideally, these might
271156888Srwatson * be abstracted so that only accessor methods are exposed.
272156888Srwatson */
273156889Srwatsonextern struct mtx		audit_mtx;
274159261Srwatsonextern struct cv		audit_watermark_cv;
275159261Srwatsonextern struct cv		audit_worker_cv;
276156889Srwatsonextern struct kaudit_queue	audit_q;
277156889Srwatsonextern int			audit_q_len;
278156889Srwatsonextern int			audit_pre_q_len;
279156889Srwatsonextern int			audit_in_failure;
280156888Srwatson
281156888Srwatson/*
282155192Srwatson * Flags to use on audit files when opening and closing.
283155192Srwatson */
284155192Srwatson#define	AUDIT_OPEN_FLAGS	(FWRITE | O_APPEND)
285155192Srwatson#define	AUDIT_CLOSE_FLAGS	(FWRITE | O_APPEND)
286155192Srwatson
287155192Srwatson#include <sys/fcntl.h>
288155192Srwatson#include <sys/kernel.h>
289155192Srwatson#include <sys/malloc.h>
290155192Srwatson
291155192Srwatson/*
292155192Srwatson * Some of the BSM tokenizer functions take different parameters in the
293156889Srwatson * kernel implementations in order to save the copying of large kernel data
294170196Srwatson * structures.  The prototypes of these functions are declared here.
295155192Srwatson */
296155192Srwatsontoken_t		*kau_to_socket(struct socket_au_info *soi);
297155192Srwatson
298155192Srwatson/*
299155192Srwatson * audit_klib prototypes
300155192Srwatson */
301159269Srwatsonint		 au_preselect(au_event_t event, au_class_t class,
302159269Srwatson		    au_mask_t *mask_p, int sorf);
303155192Srwatsonvoid		 au_evclassmap_init(void);
304155192Srwatsonvoid		 au_evclassmap_insert(au_event_t event, au_class_t class);
305155192Srwatsonau_class_t	 au_event_class(au_event_t event);
306176690Srwatsonau_event_t	 audit_ctlname_to_sysctlevent(int name[], uint64_t valid_arg);
307176690Srwatsonau_event_t	 audit_flags_and_error_to_openevent(int oflags, int error);
308176565Srwatsonint		 audit_msgctl_to_event(int cmd);
309176565Srwatsonint		 audit_semctl_to_event(int cmr);
310176565Srwatsonvoid		 audit_canon_path(struct thread *td, char *path, char *cpath);
311176690Srwatsonint		 auditon_command_event(int cmd);
312155192Srwatson
313155192Srwatson/*
314155192Srwatson * Audit trigger events notify user space of kernel audit conditions
315155192Srwatson * asynchronously.
316155192Srwatson */
317155192Srwatsonvoid		 audit_trigger_init(void);
318176686Srwatsonint		 audit_send_trigger(unsigned int trigger);
319155192Srwatson
320155192Srwatson/*
321155192Srwatson * General audit related functions.
322155192Srwatson */
323155192Srwatsonstruct kaudit_record	*currecord(void);
324156888Srwatsonvoid			 audit_free(struct kaudit_record *ar);
325155192Srwatsonvoid			 audit_shutdown(void *arg, int howto);
326155192Srwatsonvoid			 audit_rotate_vnode(struct ucred *cred,
327155192Srwatson			    struct vnode *vp);
328156888Srwatsonvoid			 audit_worker_init(void);
329155192Srwatson
330155408Srwatson/*
331155408Srwatson * Audit pipe functions.
332155408Srwatson */
333159269Srwatsonint	 audit_pipe_preselect(au_id_t auid, au_event_t event,
334159269Srwatson	    au_class_t class, int sorf, int trail_select);
335159269Srwatsonvoid	 audit_pipe_submit(au_id_t auid, au_event_t event, au_class_t class,
336159269Srwatson	    int sorf, int trail_select, void *record, u_int record_len);
337159269Srwatsonvoid	 audit_pipe_submit_user(void *record, u_int record_len);
338155408Srwatson
339156882Srwatson#endif /* ! _SECURITY_AUDIT_PRIVATE_H_ */
340