audit_private.h revision 162380
1251877Speter/*
2251877Speter * Copyright (c) 1999-2005 Apple Computer, Inc.
3251877Speter * All rights reserved.
4251877Speter *
5251877Speter * Redistribution and use in source and binary forms, with or without
6251877Speter * modification, are permitted provided that the following conditions
7251877Speter * are met:
8251877Speter * 1.  Redistributions of source code must retain the above copyright
9251877Speter *     notice, this list of conditions and the following disclaimer.
10251877Speter * 2.  Redistributions in binary form must reproduce the above copyright
11251877Speter *     notice, this list of conditions and the following disclaimer in the
12251877Speter *     documentation and/or other materials provided with the distribution.
13251877Speter * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14251877Speter *     its contributors may be used to endorse or promote products derived
15251877Speter *     from this software without specific prior written permission.
16251877Speter *
17251877Speter * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18251877Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19251877Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20251877Speter * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21251877Speter * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22251877Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23251877Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24251877Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25251877Speter * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26253895Speter * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27253895Speter * POSSIBILITY OF SUCH DAMAGE.
28251877Speter *
29251877Speter * $FreeBSD: head/sys/security/audit/audit_private.h 162380 2006-09-17 17:52:57Z csjp $
30251877Speter */
31251877Speter
32251877Speter/*
33251877Speter * This include file contains function prototypes and type definitions used
34251877Speter * within the audit implementation.
35251877Speter */
36251877Speter
37251877Speter#ifndef _SECURITY_AUDIT_PRIVATE_H_
38251877Speter#define	_SECURITY_AUDIT_PRIVATE_H_
39251877Speter
40251877Speter#ifndef _KERNEL
41251877Speter#error "no user-serviceable parts inside"
42251877Speter#endif
43251877Speter
44251877Speter#include <sys/ipc.h>
45251877Speter#include <sys/socket.h>
46253895Speter#include <sys/ucred.h>
47253895Speter
48251877Speter#ifdef MALLOC_DECLARE
49251877SpeterMALLOC_DECLARE(M_AUDITBSM);
50253895SpeterMALLOC_DECLARE(M_AUDITDATA);
51262324SpeterMALLOC_DECLARE(M_AUDITPATH);
52251877SpeterMALLOC_DECLARE(M_AUDITTEXT);
53251877Speter#endif
54251877Speter
55251877Speter/*
56251877Speter * The AUDIT_EXCESSIVELY_VERBOSE define enables a number of gratuitously
57251877Speter * noisy printf's to the console.  Due to the volume, it should be left off
58251877Speter * unless you want your system to churn a lot whenever the audit record flow
59253895Speter * gets high.
60253895Speter */
61253895Speter//#define	AUDIT_EXCESSIVELY_VERBOSE
62253895Speter#ifdef AUDIT_EXCESSIVELY_VERBOSE
63253895Speter#define	AUDIT_PRINTF(x)	printf x
64253895Speter#else
65251877Speter#define	AUDIT_PRINTF(x)
66253895Speter#endif
67253895Speter
68251877Speter/*
69253895Speter * Audit control variables that are usually set/read via system calls and
70253895Speter * used to control various aspects of auditing.
71253895Speter */
72253895Speterextern struct au_qctrl		audit_qctrl;
73253895Speterextern struct audit_fstat	audit_fstat;
74253895Speterextern struct au_mask		audit_nae_mask;
75253895Speterextern int			audit_panic_on_write_fail;
76253895Speterextern int			audit_fail_stop;
77253895Speterextern int			audit_argv;
78251877Speterextern int			audit_arge;
79251877Speter
80251877Speter/*
81251877Speter * Success/failure conditions for the conversion of a kernel audit record to
82251877Speter * BSM format.
83251877Speter */
84251877Speter#define	BSM_SUCCESS	0
85253895Speter#define	BSM_FAILURE	1
86253895Speter#define	BSM_NOAUDIT	2
87253895Speter
88251877Speter/*
89251877Speter * Defines for the kernel audit record k_ar_commit field.  Flags are set to
90251877Speter * indicate what sort of record it is, and which preselection mechanism
91251877Speter * selected it.
92253895Speter */
93253895Speter#define	AR_COMMIT_KERNEL	0x00000001U
94253895Speter#define	AR_COMMIT_USER		0x00000010U
95253895Speter
96253895Speter#define	AR_PRESELECT_TRAIL	0x00001000U
97251877Speter#define	AR_PRESELECT_PIPE	0x00002000U
98251877Speter
99251877Speter#define	AR_PRESELECT_USER_TRAIL	0x00004000U
100251877Speter#define	AR_PRESELECT_USER_PIPE	0x00008000U
101251877Speter
102251877Speter/*
103251877Speter * Audit data is generated as a stream of struct audit_record structures,
104251877Speter * linked by struct kaudit_record, and contain storage for possible audit so
105251877Speter * that it will not need to be allocated during the processing of a system
106251877Speter * call, both improving efficiency and avoiding sleeping at untimely moments.
107251877Speter * This structure is converted to BSM format before being written to disk.
108251877Speter */
109251877Speterstruct vnode_au_info {
110251877Speter	mode_t	vn_mode;
111251877Speter	uid_t	vn_uid;
112251877Speter	gid_t	vn_gid;
113251877Speter	dev_t	vn_dev;
114251877Speter	long	vn_fsid;
115251877Speter	long	vn_fileid;
116251877Speter	long	vn_gen;
117251877Speter};
118251877Speter
119251877Speterstruct groupset {
120251877Speter	gid_t	gidset[NGROUPS];
121251877Speter	u_int	gidset_size;
122253895Speter};
123253895Speter
124253895Speterstruct socket_au_info {
125253895Speter	int 		so_domain;
126253895Speter	int		so_type;
127253895Speter	int		so_protocol;
128251877Speter	in_addr_t	so_raddr;	/* Remote address if INET socket. */
129253895Speter	in_addr_t	so_laddr;	/* Local address if INET socket. */
130253895Speter	u_short		so_rport;	/* Remote port. */
131251877Speter	u_short		so_lport;	/* Local port. */
132251877Speter};
133251877Speter
134253895Speterunion auditon_udata {
135253895Speter	char			*au_path;
136253895Speter	long			au_cond;
137253895Speter	long			au_flags;
138253895Speter	long			au_policy;
139253895Speter	int			au_trigger;
140253895Speter	au_evclass_map_t	au_evclass;
141253895Speter	au_mask_t		au_mask;
142253895Speter	auditinfo_t		au_auinfo;
143253895Speter	auditpinfo_t		au_aupinfo;
144253895Speter	auditpinfo_addr_t	au_aupinfo_addr;
145253895Speter	au_qctrl_t		au_qctrl;
146253895Speter	au_stat_t		au_stat;
147251877Speter	au_fstat_t		au_fstat;
148251877Speter};
149251877Speter
150251877Speterstruct posix_ipc_perm {
151251877Speter	uid_t	pipc_uid;
152251877Speter	gid_t	pipc_gid;
153251877Speter	mode_t	pipc_mode;
154251877Speter};
155251877Speter
156251877Speterstruct audit_record {
157251877Speter	/* Audit record header. */
158251877Speter	u_int32_t		ar_magic;
159251877Speter	int			ar_event;
160253895Speter	int			ar_retval; /* value returned to the process */
161253895Speter	int			ar_errno;  /* return status of system call */
162251877Speter	struct timespec		ar_starttime;
163251877Speter	struct timespec		ar_endtime;
164253895Speter	u_int64_t		ar_valid_arg;  /* Bitmask of valid arguments */
165251877Speter
166253895Speter	/* Audit subject information. */
167251877Speter	struct xucred		ar_subj_cred;
168253895Speter	uid_t			ar_subj_ruid;
169251877Speter	gid_t			ar_subj_rgid;
170253895Speter	gid_t			ar_subj_egid;
171253895Speter	uid_t			ar_subj_auid; /* Audit user ID */
172253895Speter	pid_t			ar_subj_asid; /* Audit session ID */
173251877Speter	pid_t			ar_subj_pid;
174251877Speter	struct au_tid		ar_subj_term;
175251877Speter	char			ar_subj_comm[MAXCOMLEN + 1];
176251877Speter	struct au_mask		ar_subj_amask;
177251877Speter
178	/* Operation arguments. */
179	uid_t			ar_arg_euid;
180	uid_t			ar_arg_ruid;
181	uid_t			ar_arg_suid;
182	gid_t			ar_arg_egid;
183	gid_t			ar_arg_rgid;
184	gid_t			ar_arg_sgid;
185	pid_t			ar_arg_pid;
186	pid_t			ar_arg_asid;
187	struct au_tid		ar_arg_termid;
188	uid_t			ar_arg_uid;
189	uid_t			ar_arg_auid;
190	gid_t			ar_arg_gid;
191	struct groupset		ar_arg_groups;
192	int			ar_arg_fd;
193	int			ar_arg_fflags;
194	mode_t			ar_arg_mode;
195	int			ar_arg_dev;
196	long			ar_arg_value;
197	void *			ar_arg_addr;
198	int			ar_arg_len;
199	int			ar_arg_mask;
200	u_int			ar_arg_signum;
201	char			ar_arg_login[MAXLOGNAME];
202	int			ar_arg_ctlname[CTL_MAXNAME];
203	struct sockaddr		ar_arg_sockaddr;
204	struct socket_au_info	ar_arg_sockinfo;
205	char			*ar_arg_upath1;
206	char			*ar_arg_upath2;
207	char			*ar_arg_text;
208	struct au_mask		ar_arg_amask;
209	struct vnode_au_info	ar_arg_vnode1;
210	struct vnode_au_info	ar_arg_vnode2;
211	int			ar_arg_cmd;
212	int			ar_arg_svipc_cmd;
213	struct ipc_perm		ar_arg_svipc_perm;
214	int			ar_arg_svipc_id;
215	void *			ar_arg_svipc_addr;
216	struct posix_ipc_perm	ar_arg_pipc_perm;
217	union auditon_udata	ar_arg_auditon;
218	char			*ar_arg_argv;
219	int			ar_arg_argc;
220	char			*ar_arg_envv;
221	int			ar_arg_envc;
222	int			ar_arg_exitstatus;
223	int			ar_arg_exitretval;
224};
225
226/*
227 * Arguments in the audit record are initially not defined; flags are set to
228 * indicate if they are present so they can be included in the audit log
229 * stream only if defined.
230 */
231#define	ARG_IS_VALID(kar, arg)	((kar)->k_ar.ar_valid_arg & (arg))
232#define	ARG_SET_VALID(kar, arg) do {					\
233	(kar)->k_ar.ar_valid_arg |= (arg);				\
234} while (0)
235
236/*
237 * In-kernel version of audit record; the basic record plus queue meta-data.
238 * This record can also have a pointer set to some opaque data that will be
239 * passed through to the audit writing mechanism.
240 */
241struct kaudit_record {
242	struct audit_record		 k_ar;
243	u_int32_t			 k_ar_commit;
244	void				*k_udata;	/* User data. */
245	u_int				 k_ulen;	/* User data length. */
246	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