audit_private.h revision 156882
1155192Srwatson/*
2155192Srwatson * Copyright (c) 1999-2005 Apple Computer, 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.
13155192Srwatson * 3.  Neither the name of Apple Computer, 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 156882 2006-03-19 15:37:04Z 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/*
56155192Srwatson * Audit control variables that are usually set/read via system calls
57155192Srwatson * and 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;
64155192Srwatson
65155192Srwatson/*
66155192Srwatson * Success/failure conditions for the conversion of a kernel audit record to
67155192Srwatson * BSM format.
68155192Srwatson */
69155192Srwatson#define	BSM_SUCCESS		0
70155192Srwatson#define	BSM_FAILURE		1
71155192Srwatson#define	BSM_NOAUDIT		2
72155192Srwatson
73155192Srwatson/*
74155192Srwatson * Defines for the kernel audit record k_ar_commit field.
75155192Srwatson */
76155192Srwatson#define	AR_COMMIT_KERNEL	0x00000001U
77155192Srwatson#define	AR_COMMIT_USER		0x00000010U
78155192Srwatson
79155192Srwatson/*
80155192Srwatson * Audit data is generated as a stream of struct audit_record structures,
81155192Srwatson * linked by struct kaudit_record, and contain storage for possible audit so
82155192Srwatson * that it will not need to be allocated during the processing of a system
83155192Srwatson * call, both improving efficiency and avoiding sleeping at untimely moments.
84155192Srwatson * This structure is converted to BSM format before being written to disk.
85155192Srwatson */
86155192Srwatsonstruct vnode_au_info {
87155192Srwatson	mode_t		vn_mode;
88155192Srwatson	uid_t		vn_uid;
89155192Srwatson	gid_t		vn_gid;
90155192Srwatson	dev_t		vn_dev;
91155192Srwatson	long		vn_fsid;
92155192Srwatson	long		vn_fileid;
93155192Srwatson	long		vn_gen;
94155192Srwatson};
95155192Srwatson
96155192Srwatsonstruct groupset {
97155192Srwatson	gid_t	gidset[NGROUPS];
98155192Srwatson	u_int	gidset_size;
99155192Srwatson};
100155192Srwatson
101155192Srwatsonstruct socket_au_info {
102155192Srwatson	int 		so_domain;
103155192Srwatson	int		so_type;
104155192Srwatson	int		so_protocol;
105155192Srwatson	in_addr_t	so_raddr;	/* remote address if INET socket */
106155192Srwatson	in_addr_t	so_laddr;	/* local address if INET socket */
107155192Srwatson	u_short		so_rport;	/* remote port */
108155192Srwatson	u_short		so_lport;	/* local port */
109155192Srwatson};
110155192Srwatson
111155192Srwatsonunion auditon_udata {
112155192Srwatson	char			*au_path;
113155192Srwatson	long			au_cond;
114155192Srwatson	long			au_flags;
115155192Srwatson	long			au_policy;
116155192Srwatson	int			au_trigger;
117155192Srwatson	au_evclass_map_t	au_evclass;
118155192Srwatson	au_mask_t		au_mask;
119155192Srwatson	auditinfo_t		au_auinfo;
120155192Srwatson	auditpinfo_t		au_aupinfo;
121155192Srwatson	auditpinfo_addr_t	au_aupinfo_addr;
122155192Srwatson	au_qctrl_t		au_qctrl;
123155192Srwatson	au_stat_t		au_stat;
124155192Srwatson	au_fstat_t		au_fstat;
125155192Srwatson};
126155192Srwatson
127155192Srwatsonstruct posix_ipc_perm {
128155192Srwatson	uid_t			pipc_uid;
129155192Srwatson	gid_t			pipc_gid;
130155192Srwatson	mode_t			pipc_mode;
131155192Srwatson};
132155192Srwatson
133155192Srwatsonstruct audit_record {
134155192Srwatson	/* Audit record header. */
135155192Srwatson	u_int32_t		ar_magic;
136155192Srwatson	int			ar_event;
137155192Srwatson	int			ar_retval; /* value returned to the process */
138155192Srwatson	int			ar_errno;  /* return status of system call */
139155192Srwatson	struct timespec		ar_starttime;
140155192Srwatson	struct timespec		ar_endtime;
141155192Srwatson	u_int64_t		ar_valid_arg;  /* Bitmask of valid arguments */
142155192Srwatson
143155192Srwatson	/* Audit subject information. */
144155192Srwatson	struct xucred			ar_subj_cred;
145155192Srwatson	uid_t				ar_subj_ruid;
146155192Srwatson	gid_t				ar_subj_rgid;
147155192Srwatson	gid_t				ar_subj_egid;
148155192Srwatson	uid_t				ar_subj_auid; /* Audit user ID */
149155192Srwatson	pid_t				ar_subj_asid; /* Audit session ID */
150155192Srwatson	pid_t				ar_subj_pid;
151155192Srwatson	struct au_tid			ar_subj_term;
152155192Srwatson	char				ar_subj_comm[MAXCOMLEN + 1];
153155192Srwatson	struct au_mask			ar_subj_amask;
154155192Srwatson
155155192Srwatson	/* Operation arguments. */
156155192Srwatson	uid_t				ar_arg_euid;
157155192Srwatson	uid_t				ar_arg_ruid;
158155192Srwatson	uid_t				ar_arg_suid;
159155192Srwatson	gid_t				ar_arg_egid;
160155192Srwatson	gid_t				ar_arg_rgid;
161155192Srwatson	gid_t				ar_arg_sgid;
162155192Srwatson	pid_t				ar_arg_pid;
163155192Srwatson	pid_t				ar_arg_asid;
164155192Srwatson	struct au_tid			ar_arg_termid;
165155192Srwatson	uid_t				ar_arg_uid;
166155192Srwatson	uid_t				ar_arg_auid;
167155192Srwatson	gid_t				ar_arg_gid;
168155192Srwatson	struct groupset			ar_arg_groups;
169155192Srwatson	int				ar_arg_fd;
170155192Srwatson	int				ar_arg_fflags;
171155192Srwatson	mode_t				ar_arg_mode;
172155192Srwatson	int				ar_arg_dev;
173155192Srwatson	long				ar_arg_value;
174155192Srwatson	void *				ar_arg_addr;
175155192Srwatson	int				ar_arg_len;
176155192Srwatson	int				ar_arg_mask;
177155192Srwatson	u_int				ar_arg_signum;
178155192Srwatson	char				ar_arg_login[MAXLOGNAME];
179155192Srwatson	int				ar_arg_ctlname[CTL_MAXNAME];
180155192Srwatson	struct sockaddr			ar_arg_sockaddr;
181155192Srwatson	struct socket_au_info		ar_arg_sockinfo;
182155192Srwatson	char				*ar_arg_upath1;
183155192Srwatson	char				*ar_arg_upath2;
184155192Srwatson	char				*ar_arg_text;
185155192Srwatson	struct au_mask			ar_arg_amask;
186155192Srwatson	struct vnode_au_info		ar_arg_vnode1;
187155192Srwatson	struct vnode_au_info		ar_arg_vnode2;
188155192Srwatson	int				ar_arg_cmd;
189155192Srwatson	int				ar_arg_svipc_cmd;
190155192Srwatson	struct ipc_perm			ar_arg_svipc_perm;
191155192Srwatson	int				ar_arg_svipc_id;
192155192Srwatson	void *				ar_arg_svipc_addr;
193155192Srwatson	struct posix_ipc_perm		ar_arg_pipc_perm;
194155192Srwatson	union auditon_udata		ar_arg_auditon;
195155192Srwatson	int				ar_arg_exitstatus;
196155192Srwatson	int				ar_arg_exitretval;
197155192Srwatson};
198155192Srwatson
199155192Srwatson/*
200155192Srwatson * Arguments in the audit record are initially not defined; flags are set to
201155192Srwatson * indicate if they are present so they can be included in the audit log
202155192Srwatson * stream only if defined.
203155192Srwatson */
204155192Srwatson#define	ARG_IS_VALID(kar, arg)	((kar)->k_ar.ar_valid_arg & (arg))
205155192Srwatson#define	ARG_SET_VALID(kar, arg) do {					\
206155192Srwatson	(kar)->k_ar.ar_valid_arg |= (arg);				\
207155192Srwatson} while (0)
208155192Srwatson
209155192Srwatson/*
210155192Srwatson * In-kernel version of audit record; the basic record plus queue meta-data.
211155192Srwatson * This record can also have a pointer set to some opaque data that will
212155192Srwatson * be passed through to the audit writing mechanism.
213155192Srwatson */
214155192Srwatsonstruct kaudit_record {
215155192Srwatson	struct audit_record		k_ar;
216155192Srwatson	u_int32_t			k_ar_commit;
217155192Srwatson	void 				*k_udata;    /* user data */
218155192Srwatson	u_int				k_ulen;     /* user data length */
219155192Srwatson	struct uthread			*k_uthread; /* thread we are auditing */
220155192Srwatson	TAILQ_ENTRY(kaudit_record)	k_q;
221155192Srwatson};
222155192Srwatson
223155192Srwatson/*
224155192Srwatson * Functions to manage the allocation, release, and commit of kernel audit
225155192Srwatson * records.
226155192Srwatson */
227155192Srwatsonvoid			 audit_abort(struct kaudit_record *ar);
228155192Srwatsonvoid			 audit_commit(struct kaudit_record *ar, int error,
229155192Srwatson			    int retval);
230155192Srwatsonstruct kaudit_record	*audit_new(int event, struct thread *td);
231155192Srwatson
232155192Srwatson/*
233155192Srwatson * Functions relating to the conversion of internal kernel audit records to
234155192Srwatson * the BSM file format.
235155192Srwatson */
236156291Srwatsonstruct au_record;
237155192Srwatsonint			 kaudit_to_bsm(struct kaudit_record *kar,
238155192Srwatson			    struct au_record **pau);
239155192Srwatsonint			 bsm_rec_verify(void *rec);
240155192Srwatson
241155192Srwatson/*
242155192Srwatson * Kernel versions of the libbsm audit record functions.
243155192Srwatson */
244155192Srwatsonvoid			 kau_free(struct au_record *rec);
245155192Srwatsonvoid			 kau_init(void);
246155192Srwatson
247155192Srwatson/*
248155192Srwatson * Return values for pre-selection and post-selection decisions.
249155192Srwatson */
250155192Srwatson#define	AU_PRS_SUCCESS		1
251155192Srwatson#define	AU_PRS_FAILURE		2
252155192Srwatson#define	AU_PRS_BOTH		(AU_PRS_SUCCESS|AU_PRS_FAILURE)
253155192Srwatson
254155192Srwatson/*
255155192Srwatson * Flags to use on audit files when opening and closing.
256155192Srwatson */
257155192Srwatson#define	AUDIT_OPEN_FLAGS	(FWRITE | O_APPEND)
258155192Srwatson#define	AUDIT_CLOSE_FLAGS	(FWRITE | O_APPEND)
259155192Srwatson
260155192Srwatson#include <sys/fcntl.h>
261155192Srwatson#include <sys/kernel.h>
262155192Srwatson#include <sys/malloc.h>
263155192Srwatson
264155192Srwatson/*
265155192Srwatson * Some of the BSM tokenizer functions take different parameters in the
266155192Srwatson * kernel implementations in order to save the copying of large kernel
267155192Srwatson * data structures. The prototypes of these functions are declared here.
268155192Srwatson */
269155192Srwatsontoken_t		*kau_to_socket(struct socket_au_info *soi);
270155192Srwatson
271155192Srwatson/*
272155192Srwatson * audit_klib prototypes
273155192Srwatson */
274155192Srwatsonint		 au_preselect(au_event_t event, au_mask_t *mask_p, int sorf);
275155192Srwatsonau_event_t	 flags_and_error_to_openevent(int oflags, int error);
276155192Srwatsonvoid		 au_evclassmap_init(void);
277155192Srwatsonvoid		 au_evclassmap_insert(au_event_t event, au_class_t class);
278155192Srwatsonau_class_t	 au_event_class(au_event_t event);
279155192Srwatsonau_event_t	 ctlname_to_sysctlevent(int name[], uint64_t valid_arg);
280155192Srwatsonint		 auditon_command_event(int cmd);
281155192Srwatsonint		 msgctl_to_event(int cmd);
282155192Srwatsonint		 semctl_to_event(int cmr);
283155192Srwatsonvoid		 canon_path(struct thread *td, char *path, char *cpath);
284155192Srwatson
285155192Srwatson/*
286155192Srwatson * Audit trigger events notify user space of kernel audit conditions
287155192Srwatson * asynchronously.
288155192Srwatson */
289155192Srwatsonvoid		 audit_trigger_init(void);
290156845Srwatsonint		 send_trigger(unsigned int trigger);
291155192Srwatson
292155192Srwatson/*
293155192Srwatson * General audit related functions.
294155192Srwatson */
295155192Srwatsonstruct kaudit_record	*currecord(void);
296155192Srwatsonvoid			 audit_shutdown(void *arg, int howto);
297155192Srwatsonvoid			 audit_rotate_vnode(struct ucred *cred,
298155192Srwatson			    struct vnode *vp);
299155192Srwatson
300155408Srwatson/*
301155408Srwatson * Audit pipe functions.
302155408Srwatson */
303155408Srwatsonvoid			 audit_pipe_submit(void *record, u_int record_len);
304155408Srwatson
305156882Srwatson#endif /* ! _SECURITY_AUDIT_PRIVATE_H_ */
306