xref: /freebsd/sys/security/audit/audit_private.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1999-2009 Apple Inc.
5  * Copyright (c) 2016, 2018 Robert N. M. Watson
6  * All rights reserved.
7  *
8  * Portions of this software were developed by BAE Systems, the University of
9  * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
10  * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
11  * Computing (TC) research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1.  Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  * 2.  Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
22  *     its contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
29  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $FreeBSD$
38  */
39 
40 /*
41  * This include file contains function prototypes and type definitions used
42  * within the audit implementation.
43  */
44 
45 #ifndef _SECURITY_AUDIT_PRIVATE_H_
46 #define	_SECURITY_AUDIT_PRIVATE_H_
47 
48 #ifndef _KERNEL
49 #error "no user-serviceable parts inside"
50 #endif
51 
52 #include <sys/caprights.h>
53 #include <sys/ipc.h>
54 #include <sys/socket.h>
55 #include <sys/ucred.h>
56 
57 #ifdef MALLOC_DECLARE
58 MALLOC_DECLARE(M_AUDITBSM);
59 MALLOC_DECLARE(M_AUDITDATA);
60 MALLOC_DECLARE(M_AUDITPATH);
61 MALLOC_DECLARE(M_AUDITTEXT);
62 MALLOC_DECLARE(M_AUDITGIDSET);
63 #endif
64 
65 /*
66  * Audit control variables that are usually set/read via system calls and
67  * used to control various aspects of auditing.
68  */
69 extern struct au_qctrl		audit_qctrl;
70 extern struct audit_fstat	audit_fstat;
71 extern struct au_mask		audit_nae_mask;
72 extern int			audit_panic_on_write_fail;
73 extern int			audit_fail_stop;
74 extern int			audit_argv;
75 extern int			audit_arge;
76 
77 /*
78  * Success/failure conditions for the conversion of a kernel audit record to
79  * BSM format.
80  */
81 #define	BSM_SUCCESS	0
82 #define	BSM_FAILURE	1
83 #define	BSM_NOAUDIT	2
84 
85 /*
86  * Defines for the kernel audit record k_ar_commit field.  Flags are set to
87  * indicate what sort of record it is, and which preselection mechanism
88  * selected it.
89  */
90 #define	AR_COMMIT_KERNEL	0x00000001U
91 #define	AR_COMMIT_USER		0x00000010U
92 
93 #define	AR_PRESELECT_TRAIL	0x00001000U
94 #define	AR_PRESELECT_PIPE	0x00002000U
95 
96 #define	AR_PRESELECT_USER_TRAIL	0x00004000U
97 #define	AR_PRESELECT_USER_PIPE	0x00008000U
98 
99 #define	AR_PRESELECT_DTRACE	0x00010000U
100 
101 /*
102  * Audit data is generated as a stream of struct audit_record structures,
103  * linked by struct kaudit_record, and contain storage for possible audit so
104  * that it will not need to be allocated during the processing of a system
105  * call, both improving efficiency and avoiding sleeping at untimely moments.
106  * This structure is converted to BSM format before being written to disk.
107  */
108 struct vnode_au_info {
109 	mode_t	vn_mode;
110 	uid_t	vn_uid;
111 	gid_t	vn_gid;
112 	u_int32_t vn_dev;		/* XXX dev_t compatibility */
113 	long	vn_fsid;		/* XXX uint64_t compatibility */
114 	long	vn_fileid;		/* XXX ino_t compatibility */
115 	long	vn_gen;
116 };
117 
118 struct groupset {
119 	gid_t	*gidset;
120 	u_int	 gidset_size;
121 };
122 
123 struct socket_au_info {
124 	int		so_domain;
125 	int		so_type;
126 	int		so_protocol;
127 	in_addr_t	so_raddr;	/* Remote address if INET socket. */
128 	in_addr_t	so_laddr;	/* Local address if INET socket. */
129 	u_short		so_rport;	/* Remote port. */
130 	u_short		so_lport;	/* Local port. */
131 };
132 
133 /*
134  * The following is used for A_OLDSETQCTRL and AU_OLDGETQCTRL and a 64-bit
135  * userland.
136  */
137 struct au_qctrl64 {
138 	u_int64_t	aq64_hiwater;
139 	u_int64_t	aq64_lowater;
140 	u_int64_t	aq64_bufsz;
141 	u_int64_t	aq64_delay;
142 	u_int64_t	aq64_minfree;
143 };
144 typedef	struct au_qctrl64	au_qctrl64_t;
145 
146 union auditon_udata {
147 	char			*au_path;
148 	int			au_cond;
149 	int			au_flags;
150 	int			au_policy;
151 	int			au_trigger;
152 	int64_t			au_cond64;
153 	int64_t			au_policy64;
154 	au_evclass_map_t	au_evclass;
155 	au_mask_t		au_mask;
156 	auditinfo_t		au_auinfo;
157 	auditpinfo_t		au_aupinfo;
158 	auditpinfo_addr_t	au_aupinfo_addr;
159 	au_qctrl_t		au_qctrl;
160 	au_qctrl64_t		au_qctrl64;
161 	au_stat_t		au_stat;
162 	au_fstat_t		au_fstat;
163 	auditinfo_addr_t	au_kau_info;
164 	au_evname_map_t		au_evname;
165 };
166 
167 struct posix_ipc_perm {
168 	uid_t	pipc_uid;
169 	gid_t	pipc_gid;
170 	mode_t	pipc_mode;
171 };
172 
173 struct audit_record {
174 	/* Audit record header. */
175 	u_int32_t		ar_magic;
176 	int			ar_event;
177 	int			ar_retval; /* value returned to the process */
178 	int			ar_errno;  /* return status of system call */
179 	struct timespec		ar_starttime;
180 	struct timespec		ar_endtime;
181 	u_int64_t		ar_valid_arg;  /* Bitmask of valid arguments */
182 
183 	/* Audit subject information. */
184 	struct xucred		ar_subj_cred;
185 	uid_t			ar_subj_ruid;
186 	gid_t			ar_subj_rgid;
187 	gid_t			ar_subj_egid;
188 	uid_t			ar_subj_auid; /* Audit user ID */
189 	pid_t			ar_subj_asid; /* Audit session ID */
190 	pid_t			ar_subj_pid;
191 	struct au_tid		ar_subj_term;
192 	struct au_tid_addr	ar_subj_term_addr;
193 	struct au_mask		ar_subj_amask;
194 
195 	/* Operation arguments. */
196 	uid_t			ar_arg_euid;
197 	uid_t			ar_arg_ruid;
198 	uid_t			ar_arg_suid;
199 	gid_t			ar_arg_egid;
200 	gid_t			ar_arg_rgid;
201 	gid_t			ar_arg_sgid;
202 	pid_t			ar_arg_pid;
203 	pid_t			ar_arg_asid;
204 	struct au_tid		ar_arg_termid;
205 	struct au_tid_addr	ar_arg_termid_addr;
206 	uid_t			ar_arg_uid;
207 	uid_t			ar_arg_auid;
208 	gid_t			ar_arg_gid;
209 	struct groupset		ar_arg_groups;
210 	int			ar_arg_fd;
211 	int			ar_arg_atfd1;
212 	int			ar_arg_atfd2;
213 	int			ar_arg_fflags;
214 	mode_t			ar_arg_mode;
215 	int			ar_arg_dev;	/* XXX dev_t compatibility */
216 	long			ar_arg_value;
217 	void			*ar_arg_addr;
218 	int			ar_arg_len;
219 	int			ar_arg_mask;
220 	u_int			ar_arg_signum;
221 	char			ar_arg_login[MAXLOGNAME];
222 	int			ar_arg_ctlname[CTL_MAXNAME];
223 	struct socket_au_info	ar_arg_sockinfo;
224 	char			*ar_arg_upath1;
225 	char			*ar_arg_upath2;
226 	char			*ar_arg_text;
227 	struct au_mask		ar_arg_amask;
228 	struct vnode_au_info	ar_arg_vnode1;
229 	struct vnode_au_info	ar_arg_vnode2;
230 	int			ar_arg_cmd;
231 	int			ar_arg_svipc_which;
232 	int			ar_arg_svipc_cmd;
233 	struct ipc_perm		ar_arg_svipc_perm;
234 	int			ar_arg_svipc_id;
235 	void			*ar_arg_svipc_addr;
236 	struct posix_ipc_perm	ar_arg_pipc_perm;
237 	union auditon_udata	ar_arg_auditon;
238 	char			*ar_arg_argv;
239 	int			ar_arg_argc;
240 	char			*ar_arg_envv;
241 	int			ar_arg_envc;
242 	int			ar_arg_exitstatus;
243 	int			ar_arg_exitretval;
244 	struct sockaddr_storage ar_arg_sockaddr;
245 	cap_rights_t		ar_arg_rights;
246 	uint32_t		ar_arg_fcntl_rights;
247 	char			ar_jailname[MAXHOSTNAMELEN];
248 };
249 
250 /*
251  * Arguments in the audit record are initially not defined; flags are set to
252  * indicate if they are present so they can be included in the audit log
253  * stream only if defined.
254  */
255 #define	ARG_EUID		0x0000000000000001ULL
256 #define	ARG_RUID		0x0000000000000002ULL
257 #define	ARG_SUID		0x0000000000000004ULL
258 #define	ARG_EGID		0x0000000000000008ULL
259 #define	ARG_RGID		0x0000000000000010ULL
260 #define	ARG_SGID		0x0000000000000020ULL
261 #define	ARG_PID			0x0000000000000040ULL
262 #define	ARG_UID			0x0000000000000080ULL
263 #define	ARG_AUID		0x0000000000000100ULL
264 #define	ARG_GID			0x0000000000000200ULL
265 #define	ARG_FD			0x0000000000000400ULL
266 #define	ARG_POSIX_IPC_PERM	0x0000000000000800ULL
267 #define	ARG_FFLAGS		0x0000000000001000ULL
268 #define	ARG_MODE		0x0000000000002000ULL
269 #define	ARG_DEV			0x0000000000004000ULL
270 #define	ARG_ADDR		0x0000000000008000ULL
271 #define	ARG_LEN			0x0000000000010000ULL
272 #define	ARG_MASK		0x0000000000020000ULL
273 #define	ARG_SIGNUM		0x0000000000040000ULL
274 #define	ARG_LOGIN		0x0000000000080000ULL
275 #define	ARG_SADDRINET		0x0000000000100000ULL
276 #define	ARG_SADDRINET6		0x0000000000200000ULL
277 #define	ARG_SADDRUNIX		0x0000000000400000ULL
278 #define	ARG_TERMID_ADDR		0x0000000000800000ULL
279 #define	ARG_UNUSED2		0x0000000001000000ULL
280 #define	ARG_UPATH1		0x0000000002000000ULL
281 #define	ARG_UPATH2		0x0000000004000000ULL
282 #define	ARG_TEXT		0x0000000008000000ULL
283 #define	ARG_VNODE1		0x0000000010000000ULL
284 #define	ARG_VNODE2		0x0000000020000000ULL
285 #define	ARG_SVIPC_CMD		0x0000000040000000ULL
286 #define	ARG_SVIPC_PERM		0x0000000080000000ULL
287 #define	ARG_SVIPC_ID		0x0000000100000000ULL
288 #define	ARG_SVIPC_ADDR		0x0000000200000000ULL
289 #define	ARG_GROUPSET		0x0000000400000000ULL
290 #define	ARG_CMD			0x0000000800000000ULL
291 #define	ARG_SOCKINFO		0x0000001000000000ULL
292 #define	ARG_ASID		0x0000002000000000ULL
293 #define	ARG_TERMID		0x0000004000000000ULL
294 #define	ARG_AUDITON		0x0000008000000000ULL
295 #define	ARG_VALUE		0x0000010000000000ULL
296 #define	ARG_AMASK		0x0000020000000000ULL
297 #define	ARG_CTLNAME		0x0000040000000000ULL
298 #define	ARG_PROCESS		0x0000080000000000ULL
299 #define	ARG_MACHPORT1		0x0000100000000000ULL
300 #define	ARG_MACHPORT2		0x0000200000000000ULL
301 #define	ARG_EXIT		0x0000400000000000ULL
302 #define	ARG_IOVECSTR		0x0000800000000000ULL
303 #define	ARG_ARGV		0x0001000000000000ULL
304 #define	ARG_ENVV		0x0002000000000000ULL
305 #define	ARG_ATFD1		0x0004000000000000ULL
306 #define	ARG_ATFD2		0x0008000000000000ULL
307 #define	ARG_RIGHTS		0x0010000000000000ULL
308 #define	ARG_FCNTL_RIGHTS	0x0020000000000000ULL
309 #define	ARG_SVIPC_WHICH		0x0200000000000000ULL
310 #define	ARG_NONE		0x0000000000000000ULL
311 #define	ARG_ALL			0xFFFFFFFFFFFFFFFFULL
312 
313 #define	ARG_IS_VALID(kar, arg)	((kar)->k_ar.ar_valid_arg & (arg))
314 #define	ARG_SET_VALID(kar, arg) do {					\
315 	(kar)->k_ar.ar_valid_arg |= (arg);				\
316 } while (0)
317 #define	ARG_CLEAR_VALID(kar, arg) do {					\
318 	(kar)->k_ar.ar_valid_arg &= ~(arg);				\
319 } while (0)
320 
321 /*
322  * In-kernel version of audit record; the basic record plus queue meta-data.
323  * This record can also have a pointer set to some opaque data that will be
324  * passed through to the audit writing mechanism.
325  */
326 struct kaudit_record {
327 	struct audit_record		 k_ar;
328 	u_int32_t			 k_ar_commit;
329 	void				*k_udata;	/* User data. */
330 	u_int				 k_ulen;	/* User data length. */
331 	struct uthread			*k_uthread;	/* Audited thread. */
332 	void				*k_dtaudit_state;
333 	TAILQ_ENTRY(kaudit_record)	 k_q;
334 };
335 TAILQ_HEAD(kaudit_queue, kaudit_record);
336 
337 /*
338  * Functions to manage the allocation, release, and commit of kernel audit
339  * records.
340  */
341 void			 audit_abort(struct kaudit_record *ar);
342 void			 audit_commit(struct kaudit_record *ar, int error,
343 			    int retval);
344 struct kaudit_record	*audit_new(int event, struct thread *td);
345 
346 /*
347  * Function to update the audit_syscalls_enabled flag, whose value is affected
348  * by configuration of the audit trail/pipe mechanism and DTrace.  Call this
349  * function when any of the inputs to that policy change.
350  */
351 void	audit_syscalls_enabled_update(void);
352 
353 /*
354  * Functions relating to the conversion of internal kernel audit records to
355  * the BSM file format.
356  */
357 struct au_record;
358 int	 kaudit_to_bsm(struct kaudit_record *kar, struct au_record **pau);
359 int	 bsm_rec_verify(void *rec);
360 
361 /*
362  * Kernel versions of the libbsm audit record functions.
363  */
364 void	 kau_free(struct au_record *rec);
365 void	 kau_init(void);
366 
367 /*
368  * Return values for pre-selection and post-selection decisions.
369  */
370 #define	AU_PRS_SUCCESS	1
371 #define	AU_PRS_FAILURE	2
372 #define	AU_PRS_BOTH	(AU_PRS_SUCCESS|AU_PRS_FAILURE)
373 
374 /*
375  * Data structures relating to the kernel audit queue.  Ideally, these might
376  * be abstracted so that only accessor methods are exposed.
377  */
378 extern struct mtx		audit_mtx;
379 extern struct cv		audit_watermark_cv;
380 extern struct cv		audit_worker_cv;
381 extern struct kaudit_queue	audit_q;
382 extern int			audit_q_len;
383 extern int			audit_pre_q_len;
384 extern int			audit_in_failure;
385 
386 /*
387  * Flags to use on audit files when opening and closing.
388  */
389 #define	AUDIT_OPEN_FLAGS	(FWRITE | O_APPEND)
390 #define	AUDIT_CLOSE_FLAGS	(FWRITE | O_APPEND)
391 
392 /*
393  * Audit event-to-name mapping structure, maintained in audit_bsm_klib.c.  It
394  * appears in this header so that the DTrace audit provider can dereference
395  * instances passed back in the au_evname_foreach() callbacks.  Safe access to
396  * its fields requires holding ene_lock (after it is visible in the global
397  * table).
398  *
399  * Locking:
400  * (c) - Constant after inserted in the global table
401  * (l) - Protected by ene_lock
402  * (m) - Protected by evnamemap_lock (audit_bsm_klib.c)
403  * (M) - Writes protected by evnamemap_lock; reads unprotected.
404  */
405 struct evname_elem {
406 	au_event_t		ene_event;			/* (c) */
407 	char			ene_name[EVNAMEMAP_NAME_SIZE];	/* (l) */
408 	LIST_ENTRY(evname_elem)	ene_entry;			/* (m) */
409 	struct mtx		ene_lock;
410 
411 	/* DTrace probe IDs; 0 if not yet registered. */
412 	uint32_t		ene_commit_probe_id;		/* (M) */
413 	uint32_t		ene_bsm_probe_id;		/* (M) */
414 
415 	/* Flags indicating if the probes enabled or not. */
416 	int			ene_commit_probe_enabled;	/* (M) */
417 	int			ene_bsm_probe_enabled;		/* (M) */
418 };
419 
420 #define	EVNAME_LOCK(ene)	mtx_lock(&(ene)->ene_lock)
421 #define	EVNAME_UNLOCK(ene)	mtx_unlock(&(ene)->ene_lock)
422 
423 /*
424  * Callback function typedef for the same.
425  */
426 typedef	void	(*au_evnamemap_callback_t)(struct evname_elem *ene);
427 
428 /*
429  * DTrace audit provider (dtaudit) hooks -- to be set non-NULL when the audit
430  * provider is loaded and ready to be called into.
431  */
432 extern void	*(*dtaudit_hook_preselect)(au_id_t auid, au_event_t event,
433 		    au_class_t class);
434 extern int	(*dtaudit_hook_commit)(struct kaudit_record *kar,
435 		    au_id_t auid, au_event_t event, au_class_t class,
436 		    int sorf);
437 extern void	(*dtaudit_hook_bsm)(struct kaudit_record *kar, au_id_t auid,
438 		    au_event_t event, au_class_t class, int sorf,
439 		    void *bsm_data, size_t bsm_len);
440 
441 #include <sys/fcntl.h>
442 #include <sys/kernel.h>
443 #include <sys/malloc.h>
444 
445 /*
446  * Some of the BSM tokenizer functions take different parameters in the
447  * kernel implementations in order to save the copying of large kernel data
448  * structures.  The prototypes of these functions are declared here.
449  */
450 token_t		*kau_to_socket(struct socket_au_info *soi);
451 
452 /*
453  * audit_klib prototypes
454  */
455 int		 au_preselect(au_event_t event, au_class_t class,
456 		    au_mask_t *mask_p, int sorf);
457 void		 au_evclassmap_init(void);
458 void		 au_evclassmap_insert(au_event_t event, au_class_t class);
459 au_class_t	 au_event_class(au_event_t event);
460 void		 au_evnamemap_init(void);
461 void		 au_evnamemap_insert(au_event_t event, const char *name);
462 void		 au_evnamemap_foreach(au_evnamemap_callback_t callback);
463 struct evname_elem	*au_evnamemap_lookup(au_event_t event);
464 int		 au_event_name(au_event_t event, char *name);
465 au_event_t	 audit_ctlname_to_sysctlevent(int name[], uint64_t valid_arg);
466 au_event_t	 audit_flags_and_error_to_openevent(int oflags, int error);
467 au_event_t	 audit_flags_and_error_to_openatevent(int oflags, int error);
468 au_event_t	 audit_msgctl_to_event(int cmd);
469 au_event_t	 audit_msgsys_to_event(int which);
470 au_event_t	 audit_semctl_to_event(int cmd);
471 au_event_t	 audit_semsys_to_event(int which);
472 au_event_t	 audit_shmsys_to_event(int which);
473 void		 audit_canon_path(struct thread *td, int dirfd, char *path,
474 		    char *cpath);
475 void		 audit_canon_path_vp(struct thread *td, struct vnode *rdir,
476 		    struct vnode *cdir, char *path, char *cpath);
477 au_event_t	 auditon_command_event(int cmd);
478 
479 /*
480  * Audit trigger events notify user space of kernel audit conditions
481  * asynchronously.
482  */
483 void		 audit_trigger_init(void);
484 int		 audit_send_trigger(unsigned int trigger);
485 
486 /*
487  * Accessor functions to manage global audit state.
488  */
489 void	 audit_set_kinfo(struct auditinfo_addr *);
490 void	 audit_get_kinfo(struct auditinfo_addr *);
491 
492 /*
493  * General audit related functions.
494  */
495 struct kaudit_record	*currecord(void);
496 void			 audit_free(struct kaudit_record *ar);
497 void			 audit_shutdown(void *arg, int howto);
498 void			 audit_rotate_vnode(struct ucred *cred,
499 			    struct vnode *vp);
500 void			 audit_worker_init(void);
501 
502 /*
503  * Audit pipe functions.
504  */
505 int	 audit_pipe_preselect(au_id_t auid, au_event_t event,
506 	    au_class_t class, int sorf, int trail_select);
507 void	 audit_pipe_submit(au_id_t auid, au_event_t event, au_class_t class,
508 	    int sorf, int trail_select, void *record, u_int record_len);
509 void	 audit_pipe_submit_user(void *record, u_int record_len);
510 
511 #endif /* ! _SECURITY_AUDIT_PRIVATE_H_ */
512