audit_private.h revision 195925
1/*-
2 * Copyright (c) 1999-2009 Apple Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/security/audit/audit_private.h 195925 2009-07-28 21:39:58Z rwatson $
30 */
31
32/*
33 * This include file contains function prototypes and type definitions used
34 * within the audit implementation.
35 */
36
37#ifndef _SECURITY_AUDIT_PRIVATE_H_
38#define	_SECURITY_AUDIT_PRIVATE_H_
39
40#ifndef _KERNEL
41#error "no user-serviceable parts inside"
42#endif
43
44#include <sys/ipc.h>
45#include <sys/socket.h>
46#include <sys/ucred.h>
47
48#ifdef MALLOC_DECLARE
49MALLOC_DECLARE(M_AUDITBSM);
50MALLOC_DECLARE(M_AUDITDATA);
51MALLOC_DECLARE(M_AUDITPATH);
52MALLOC_DECLARE(M_AUDITTEXT);
53MALLOC_DECLARE(M_AUDITGIDSET);
54#endif
55
56/*
57 * Audit control variables that are usually set/read via system calls and
58 * used to control various aspects of auditing.
59 */
60extern struct au_qctrl		audit_qctrl;
61extern struct audit_fstat	audit_fstat;
62extern struct au_mask		audit_nae_mask;
63extern int			audit_panic_on_write_fail;
64extern int			audit_fail_stop;
65extern int			audit_argv;
66extern int			audit_arge;
67
68/*
69 * Success/failure conditions for the conversion of a kernel audit record to
70 * BSM format.
71 */
72#define	BSM_SUCCESS	0
73#define	BSM_FAILURE	1
74#define	BSM_NOAUDIT	2
75
76/*
77 * Defines for the kernel audit record k_ar_commit field.  Flags are set to
78 * indicate what sort of record it is, and which preselection mechanism
79 * selected it.
80 */
81#define	AR_COMMIT_KERNEL	0x00000001U
82#define	AR_COMMIT_USER		0x00000010U
83
84#define	AR_PRESELECT_TRAIL	0x00001000U
85#define	AR_PRESELECT_PIPE	0x00002000U
86
87#define	AR_PRESELECT_USER_TRAIL	0x00004000U
88#define	AR_PRESELECT_USER_PIPE	0x00008000U
89
90/*
91 * Audit data is generated as a stream of struct audit_record structures,
92 * linked by struct kaudit_record, and contain storage for possible audit so
93 * that it will not need to be allocated during the processing of a system
94 * call, both improving efficiency and avoiding sleeping at untimely moments.
95 * This structure is converted to BSM format before being written to disk.
96 */
97struct vnode_au_info {
98	mode_t	vn_mode;
99	uid_t	vn_uid;
100	gid_t	vn_gid;
101	dev_t	vn_dev;
102	long	vn_fsid;
103	long	vn_fileid;
104	long	vn_gen;
105};
106
107struct groupset {
108	gid_t	*gidset;
109	u_int	 gidset_size;
110};
111
112struct socket_au_info {
113	int		so_domain;
114	int		so_type;
115	int		so_protocol;
116	in_addr_t	so_raddr;	/* Remote address if INET socket. */
117	in_addr_t	so_laddr;	/* Local address if INET socket. */
118	u_short		so_rport;	/* Remote port. */
119	u_short		so_lport;	/* Local port. */
120};
121
122/*
123 * The following is used for A_OLDSETQCTRL and AU_OLDGETQCTRL and a 64-bit
124 * userland.
125 */
126struct au_qctrl64 {
127	u_int64_t	aq64_hiwater;
128	u_int64_t	aq64_lowater;
129	u_int64_t	aq64_bufsz;
130	u_int64_t	aq64_delay;
131	u_int64_t	aq64_minfree;
132};
133typedef	struct au_qctrl64	au_qctrl64_t;
134
135union auditon_udata {
136	char			*au_path;
137	int			au_cond;
138	int			au_flags;
139	int			au_policy;
140	int			au_trigger;
141	int64_t			au_cond64;
142	int64_t			au_policy64;
143	au_evclass_map_t	au_evclass;
144	au_mask_t		au_mask;
145	auditinfo_t		au_auinfo;
146	auditpinfo_t		au_aupinfo;
147	auditpinfo_addr_t	au_aupinfo_addr;
148	au_qctrl_t		au_qctrl;
149	au_qctrl64_t		au_qctrl64;
150	au_stat_t		au_stat;
151	au_fstat_t		au_fstat;
152	auditinfo_addr_t	au_kau_info;
153};
154
155struct posix_ipc_perm {
156	uid_t	pipc_uid;
157	gid_t	pipc_gid;
158	mode_t	pipc_mode;
159};
160
161struct audit_record {
162	/* Audit record header. */
163	u_int32_t		ar_magic;
164	int			ar_event;
165	int			ar_retval; /* value returned to the process */
166	int			ar_errno;  /* return status of system call */
167	struct timespec		ar_starttime;
168	struct timespec		ar_endtime;
169	u_int64_t		ar_valid_arg;  /* Bitmask of valid arguments */
170
171	/* Audit subject information. */
172	struct xucred		ar_subj_cred;
173	uid_t			ar_subj_ruid;
174	gid_t			ar_subj_rgid;
175	gid_t			ar_subj_egid;
176	uid_t			ar_subj_auid; /* Audit user ID */
177	pid_t			ar_subj_asid; /* Audit session ID */
178	pid_t			ar_subj_pid;
179	struct au_tid		ar_subj_term;
180	struct au_tid_addr	ar_subj_term_addr;
181	struct au_mask		ar_subj_amask;
182
183	/* Operation arguments. */
184	uid_t			ar_arg_euid;
185	uid_t			ar_arg_ruid;
186	uid_t			ar_arg_suid;
187	gid_t			ar_arg_egid;
188	gid_t			ar_arg_rgid;
189	gid_t			ar_arg_sgid;
190	pid_t			ar_arg_pid;
191	pid_t			ar_arg_asid;
192	struct au_tid		ar_arg_termid;
193	struct au_tid_addr	ar_arg_termid_addr;
194	uid_t			ar_arg_uid;
195	uid_t			ar_arg_auid;
196	gid_t			ar_arg_gid;
197	struct groupset		ar_arg_groups;
198	int			ar_arg_fd;
199	int			ar_arg_atfd1;
200	int			ar_arg_atfd2;
201	int			ar_arg_fflags;
202	mode_t			ar_arg_mode;
203	int			ar_arg_dev;
204	long			ar_arg_value;
205	void *			ar_arg_addr;
206	int			ar_arg_len;
207	int			ar_arg_mask;
208	u_int			ar_arg_signum;
209	char			ar_arg_login[MAXLOGNAME];
210	int			ar_arg_ctlname[CTL_MAXNAME];
211	struct socket_au_info	ar_arg_sockinfo;
212	char			*ar_arg_upath1;
213	char			*ar_arg_upath2;
214	char			*ar_arg_text;
215	struct au_mask		ar_arg_amask;
216	struct vnode_au_info	ar_arg_vnode1;
217	struct vnode_au_info	ar_arg_vnode2;
218	int			ar_arg_cmd;
219	int			ar_arg_svipc_cmd;
220	struct ipc_perm		ar_arg_svipc_perm;
221	int			ar_arg_svipc_id;
222	void *			ar_arg_svipc_addr;
223	struct posix_ipc_perm	ar_arg_pipc_perm;
224	union auditon_udata	ar_arg_auditon;
225	char			*ar_arg_argv;
226	int			ar_arg_argc;
227	char			*ar_arg_envv;
228	int			ar_arg_envc;
229	int			ar_arg_exitstatus;
230	int			ar_arg_exitretval;
231	struct sockaddr_storage ar_arg_sockaddr;
232};
233
234/*
235 * Arguments in the audit record are initially not defined; flags are set to
236 * indicate if they are present so they can be included in the audit log
237 * stream only if defined.
238 */
239#define	ARG_IS_VALID(kar, arg)	((kar)->k_ar.ar_valid_arg & (arg))
240#define	ARG_SET_VALID(kar, arg) do {					\
241	(kar)->k_ar.ar_valid_arg |= (arg);				\
242} while (0)
243
244/*
245 * In-kernel version of audit record; the basic record plus queue meta-data.
246 * This record can also have a pointer set to some opaque data that will be
247 * passed through to the audit writing mechanism.
248 */
249struct kaudit_record {
250	struct audit_record		 k_ar;
251	u_int32_t			 k_ar_commit;
252	void				*k_udata;	/* User data. */
253	u_int				 k_ulen;	/* User data length. */
254	struct uthread			*k_uthread;	/* Audited thread. */
255	TAILQ_ENTRY(kaudit_record)	 k_q;
256};
257TAILQ_HEAD(kaudit_queue, kaudit_record);
258
259/*
260 * Functions to manage the allocation, release, and commit of kernel audit
261 * records.
262 */
263void			 audit_abort(struct kaudit_record *ar);
264void			 audit_commit(struct kaudit_record *ar, int error,
265			    int retval);
266struct kaudit_record	*audit_new(int event, struct thread *td);
267
268/*
269 * Functions relating to the conversion of internal kernel audit records to
270 * the BSM file format.
271 */
272struct au_record;
273int	 kaudit_to_bsm(struct kaudit_record *kar, struct au_record **pau);
274int	 bsm_rec_verify(void *rec);
275
276/*
277 * Kernel versions of the libbsm audit record functions.
278 */
279void	 kau_free(struct au_record *rec);
280void	 kau_init(void);
281
282/*
283 * Return values for pre-selection and post-selection decisions.
284 */
285#define	AU_PRS_SUCCESS	1
286#define	AU_PRS_FAILURE	2
287#define	AU_PRS_BOTH	(AU_PRS_SUCCESS|AU_PRS_FAILURE)
288
289/*
290 * Data structures relating to the kernel audit queue.  Ideally, these might
291 * be abstracted so that only accessor methods are exposed.
292 */
293extern struct mtx		audit_mtx;
294extern struct cv		audit_watermark_cv;
295extern struct cv		audit_worker_cv;
296extern struct kaudit_queue	audit_q;
297extern int			audit_q_len;
298extern int			audit_pre_q_len;
299extern int			audit_in_failure;
300
301/*
302 * Flags to use on audit files when opening and closing.
303 */
304#define	AUDIT_OPEN_FLAGS	(FWRITE | O_APPEND)
305#define	AUDIT_CLOSE_FLAGS	(FWRITE | O_APPEND)
306
307#include <sys/fcntl.h>
308#include <sys/kernel.h>
309#include <sys/malloc.h>
310
311/*
312 * Some of the BSM tokenizer functions take different parameters in the
313 * kernel implementations in order to save the copying of large kernel data
314 * structures.  The prototypes of these functions are declared here.
315 */
316token_t		*kau_to_socket(struct socket_au_info *soi);
317
318/*
319 * audit_klib prototypes
320 */
321int		 au_preselect(au_event_t event, au_class_t class,
322		    au_mask_t *mask_p, int sorf);
323void		 au_evclassmap_init(void);
324void		 au_evclassmap_insert(au_event_t event, au_class_t class);
325au_class_t	 au_event_class(au_event_t event);
326au_event_t	 audit_ctlname_to_sysctlevent(int name[], uint64_t valid_arg);
327au_event_t	 audit_flags_and_error_to_openevent(int oflags, int error);
328au_event_t	 audit_flags_and_error_to_openatevent(int oflags, int error);
329au_event_t	 audit_msgctl_to_event(int cmd);
330au_event_t	 audit_semctl_to_event(int cmr);
331void		 audit_canon_path(struct thread *td, char *path, char *cpath);
332au_event_t	 auditon_command_event(int cmd);
333
334/*
335 * Audit trigger events notify user space of kernel audit conditions
336 * asynchronously.
337 */
338void		 audit_trigger_init(void);
339int		 audit_send_trigger(unsigned int trigger);
340
341/*
342 * Accessor functions to manage global audit state.
343 */
344void	 audit_set_kinfo(struct auditinfo_addr *);
345void	 audit_get_kinfo(struct auditinfo_addr *);
346
347/*
348 * General audit related functions.
349 */
350struct kaudit_record	*currecord(void);
351void			 audit_free(struct kaudit_record *ar);
352void			 audit_shutdown(void *arg, int howto);
353void			 audit_rotate_vnode(struct ucred *cred,
354			    struct vnode *vp);
355void			 audit_worker_init(void);
356
357/*
358 * Audit pipe functions.
359 */
360int	 audit_pipe_preselect(au_id_t auid, au_event_t event,
361	    au_class_t class, int sorf, int trail_select);
362void	 audit_pipe_submit(au_id_t auid, au_event_t event, au_class_t class,
363	    int sorf, int trail_select, void *record, u_int record_len);
364void	 audit_pipe_submit_user(void *record, u_int record_len);
365
366#endif /* ! _SECURITY_AUDIT_PRIVATE_H_ */
367