xref: /freebsd/sys/bsm/audit.h (revision 926ce35a7e33457153b56be1ba51a31aea674000)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2005-2009 Apple Inc.
5  * Copyright (c) 2016 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  *
17  * 1.  Redistributions of source code must retain the above copyright
18  *     notice, this list of conditions and the following disclaimer.
19  * 2.  Redistributions in binary form must reproduce the above copyright
20  *     notice, this list of conditions and the following disclaimer in the
21  *     documentation and/or other materials provided with the distribution.
22  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
23  *     its contributors may be used to endorse or promote products derived
24  *     from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
27  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
30  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $FreeBSD$
38  */
39 
40 #ifndef	_BSM_AUDIT_H
41 #define	_BSM_AUDIT_H
42 
43 #include <sys/param.h>
44 #include <sys/types.h>
45 
46 #define	AUDIT_RECORD_MAGIC	0x828a0f1b
47 #define	MAX_AUDIT_RECORDS	20
48 #define	MAXAUDITDATA		(0x8000 - 1)
49 #define	MAX_AUDIT_RECORD_SIZE	MAXAUDITDATA
50 #define	MIN_AUDIT_FILE_SIZE	(512 * 1024)
51 
52 /*
53  * Minimum noumber of free blocks on the filesystem containing the audit
54  * log necessary to avoid a hard log rotation. DO NOT SET THIS VALUE TO 0
55  * as the kernel does an unsigned compare, plus we want to leave a few blocks
56  * free so userspace can terminate the log, etc.
57  */
58 #define	AUDIT_HARD_LIMIT_FREE_BLOCKS	4
59 
60 /*
61  * Triggers for the audit daemon.
62  */
63 #define	AUDIT_TRIGGER_MIN		1
64 #define	AUDIT_TRIGGER_LOW_SPACE		1	/* Below low watermark. */
65 #define	AUDIT_TRIGGER_ROTATE_KERNEL	2	/* Kernel requests rotate. */
66 #define	AUDIT_TRIGGER_READ_FILE		3	/* Re-read config file. */
67 #define	AUDIT_TRIGGER_CLOSE_AND_DIE	4	/* Terminate audit. */
68 #define	AUDIT_TRIGGER_NO_SPACE		5	/* Below min free space. */
69 #define	AUDIT_TRIGGER_ROTATE_USER	6	/* User requests rotate. */
70 #define	AUDIT_TRIGGER_INITIALIZE	7	/* User initialize of auditd. */
71 #define	AUDIT_TRIGGER_EXPIRE_TRAILS	8	/* User expiration of trails. */
72 #define	AUDIT_TRIGGER_MAX		8
73 
74 /*
75  * The special device filename (FreeBSD).
76  */
77 #define	AUDITDEV_FILENAME	"audit"
78 #define	AUDIT_TRIGGER_FILE	("/dev/" AUDITDEV_FILENAME)
79 
80 /*
81  * Pre-defined audit IDs
82  */
83 #define	AU_DEFAUDITID	(uid_t)(-1)
84 #define	AU_DEFAUDITSID	 0
85 #define	AU_ASSIGN_ASID	-1
86 
87 /*
88  * IPC types.
89  */
90 #define	AT_IPC_MSG	((u_char)1)	/* Message IPC id. */
91 #define	AT_IPC_SEM	((u_char)2)	/* Semaphore IPC id. */
92 #define	AT_IPC_SHM	((u_char)3)	/* Shared mem IPC id. */
93 
94 /*
95  * Audit conditions.
96  */
97 #define	AUC_UNSET		0
98 #define	AUC_AUDITING		1
99 #define	AUC_NOAUDIT		2
100 #define	AUC_DISABLED		-1
101 
102 /*
103  * auditon(2) commands.
104  */
105 #define	A_OLDGETPOLICY	2
106 #define	A_OLDSETPOLICY	3
107 #define	A_GETKMASK	4
108 #define	A_SETKMASK	5
109 #define	A_OLDGETQCTRL	6
110 #define	A_OLDSETQCTRL	7
111 #define	A_GETCWD	8
112 #define	A_GETCAR	9
113 #define	A_GETSTAT	12
114 #define	A_SETSTAT	13
115 #define	A_SETUMASK	14
116 #define	A_SETSMASK	15
117 #define	A_OLDGETCOND	20
118 #define	A_OLDSETCOND	21
119 #define	A_GETCLASS	22
120 #define	A_SETCLASS	23
121 #define	A_GETPINFO	24
122 #define	A_SETPMASK	25
123 #define	A_SETFSIZE	26
124 #define	A_GETFSIZE	27
125 #define	A_GETPINFO_ADDR	28
126 #define	A_GETKAUDIT	29
127 #define	A_SETKAUDIT	30
128 #define	A_SENDTRIGGER	31
129 #define	A_GETSINFO_ADDR	32
130 #define	A_GETPOLICY	33
131 #define	A_SETPOLICY	34
132 #define	A_GETQCTRL	35
133 #define	A_SETQCTRL	36
134 #define	A_GETCOND	37
135 #define	A_SETCOND	38
136 #define	A_GETEVENT	39	/* Get audit event-to-name mapping. */
137 #define	A_SETEVENT	40	/* Set audit event-to-name mapping. */
138 
139 /*
140  * Audit policy controls.
141  */
142 #define	AUDIT_CNT	0x0001
143 #define	AUDIT_AHLT	0x0002
144 #define	AUDIT_ARGV	0x0004
145 #define	AUDIT_ARGE	0x0008
146 #define	AUDIT_SEQ	0x0010
147 #define	AUDIT_WINDATA	0x0020
148 #define	AUDIT_USER	0x0040
149 #define	AUDIT_GROUP	0x0080
150 #define	AUDIT_TRAIL	0x0100
151 #define	AUDIT_PATH	0x0200
152 #define	AUDIT_SCNT	0x0400
153 #define	AUDIT_PUBLIC	0x0800
154 #define	AUDIT_ZONENAME	0x1000
155 #define	AUDIT_PERZONE	0x2000
156 
157 /*
158  * Default audit queue control parameters.
159  */
160 #define	AQ_HIWATER	100
161 #define	AQ_MAXHIGH	10000
162 #define	AQ_LOWATER	10
163 #define	AQ_BUFSZ	MAXAUDITDATA
164 #define	AQ_MAXBUFSZ	1048576
165 
166 /*
167  * Default minimum percentage free space on file system.
168  */
169 #define	AU_FS_MINFREE	20
170 
171 /*
172  * Type definitions used indicating the length of variable length addresses
173  * in tokens containing addresses, such as header fields.
174  */
175 #define	AU_IPv4		4
176 #define	AU_IPv6		16
177 
178 __BEGIN_DECLS
179 
180 typedef	uid_t		au_id_t;
181 typedef	pid_t		au_asid_t;
182 typedef	u_int16_t	au_event_t;
183 typedef	u_int16_t	au_emod_t;
184 typedef	u_int32_t	au_class_t;
185 typedef	u_int64_t	au_asflgs_t __attribute__ ((aligned (8)));
186 
187 struct au_tid {
188 	u_int32_t	port;		/* XXX dev_t compatibility */
189 	u_int32_t	machine;
190 };
191 typedef	struct au_tid	au_tid_t;
192 
193 struct au_tid_addr {
194 	u_int32_t	at_port;	/* XXX dev_t compatibility */
195 	u_int32_t	at_type;
196 	u_int32_t	at_addr[4];
197 };
198 typedef	struct au_tid_addr	au_tid_addr_t;
199 
200 struct au_mask {
201 	unsigned int    am_success;     /* Success bits. */
202 	unsigned int    am_failure;     /* Failure bits. */
203 };
204 typedef	struct au_mask	au_mask_t;
205 
206 struct auditinfo {
207 	au_id_t		ai_auid;	/* Audit user ID. */
208 	au_mask_t	ai_mask;	/* Audit masks. */
209 	au_tid_t	ai_termid;	/* Terminal ID. */
210 	au_asid_t	ai_asid;	/* Audit session ID. */
211 };
212 typedef	struct auditinfo	auditinfo_t;
213 
214 struct auditinfo_addr {
215 	au_id_t		ai_auid;	/* Audit user ID. */
216 	au_mask_t	ai_mask;	/* Audit masks. */
217 	au_tid_addr_t	ai_termid;	/* Terminal ID. */
218 	au_asid_t	ai_asid;	/* Audit session ID. */
219 	au_asflgs_t	ai_flags;	/* Audit session flags. */
220 };
221 typedef	struct auditinfo_addr	auditinfo_addr_t;
222 
223 struct auditpinfo {
224 	pid_t		ap_pid;		/* ID of target process. */
225 	au_id_t		ap_auid;	/* Audit user ID. */
226 	au_mask_t	ap_mask;	/* Audit masks. */
227 	au_tid_t	ap_termid;	/* Terminal ID. */
228 	au_asid_t	ap_asid;	/* Audit session ID. */
229 };
230 typedef	struct auditpinfo	auditpinfo_t;
231 
232 struct auditpinfo_addr {
233 	pid_t		ap_pid;		/* ID of target process. */
234 	au_id_t		ap_auid;	/* Audit user ID. */
235 	au_mask_t	ap_mask;	/* Audit masks. */
236 	au_tid_addr_t	ap_termid;	/* Terminal ID. */
237 	au_asid_t	ap_asid;	/* Audit session ID. */
238 	au_asflgs_t	ap_flags;	/* Audit session flags. */
239 };
240 typedef	struct auditpinfo_addr	auditpinfo_addr_t;
241 
242 struct au_session {
243 	auditinfo_addr_t	*as_aia_p;	/* Ptr to full audit info. */
244 	au_mask_t		 as_mask;	/* Process Audit Masks. */
245 };
246 typedef struct au_session       au_session_t;
247 
248 /*
249  * Contents of token_t are opaque outside of libbsm.
250  */
251 typedef	struct au_token	token_t;
252 
253 /*
254  * Kernel audit queue control parameters:
255  * 			Default:		Maximum:
256  * 	aq_hiwater:	AQ_HIWATER (100)	AQ_MAXHIGH (10000)
257  * 	aq_lowater:	AQ_LOWATER (10)		<aq_hiwater
258  * 	aq_bufsz:	AQ_BUFSZ (32767)	AQ_MAXBUFSZ (1048576)
259  * 	aq_delay:	20			20000 (not used)
260  */
261 struct au_qctrl {
262 	int	aq_hiwater;	/* Max # of audit recs in queue when */
263 				/* threads with new ARs get blocked. */
264 
265 	int	aq_lowater;	/* # of audit recs in queue when */
266 				/* blocked threads get unblocked. */
267 
268 	int	aq_bufsz;	/* Max size of audit record for audit(2). */
269 	int	aq_delay;	/* Queue delay (not used). */
270 	int	aq_minfree;	/* Minimum filesystem percent free space. */
271 };
272 typedef	struct au_qctrl	au_qctrl_t;
273 
274 /*
275  * Structure for the audit statistics.
276  */
277 struct audit_stat {
278 	unsigned int	as_version;
279 	unsigned int	as_numevent;
280 	int		as_generated;
281 	int		as_nonattrib;
282 	int		as_kernel;
283 	int		as_audit;
284 	int		as_auditctl;
285 	int		as_enqueue;
286 	int		as_written;
287 	int		as_wblocked;
288 	int		as_rblocked;
289 	int		as_dropped;
290 	int		as_totalsize;
291 	unsigned int	as_memused;
292 };
293 typedef	struct audit_stat	au_stat_t;
294 
295 /*
296  * Structure for the audit file statistics.
297  */
298 struct audit_fstat {
299 	u_int64_t	af_filesz;
300 	u_int64_t	af_currsz;
301 };
302 typedef	struct audit_fstat	au_fstat_t;
303 
304 /*
305  * Audit to event class mapping.
306  */
307 struct au_evclass_map {
308 	au_event_t	ec_number;
309 	au_class_t	ec_class;
310 };
311 typedef	struct au_evclass_map	au_evclass_map_t;
312 
313 /*
314  * Event-to-name mapping.
315  */
316 #define	EVNAMEMAP_NAME_SIZE	64
317 struct au_evname_map {
318 	au_event_t	en_number;
319 	char		en_name[EVNAMEMAP_NAME_SIZE];
320 };
321 typedef struct au_evname_map	au_evname_map_t;
322 
323 /*
324  * Audit system calls.
325  */
326 #if !defined(_KERNEL) && !defined(KERNEL)
327 int	audit(const void *, int);
328 int	auditon(int, void *, int);
329 int	auditctl(const char *);
330 int	getauid(au_id_t *);
331 int	setauid(const au_id_t *);
332 int	getaudit(struct auditinfo *);
333 int	setaudit(const struct auditinfo *);
334 int	getaudit_addr(struct auditinfo_addr *, int);
335 int	setaudit_addr(const struct auditinfo_addr *, int);
336 
337 #ifdef __APPLE_API_PRIVATE
338 #include <mach/port.h>
339 mach_port_name_t audit_session_self(void);
340 au_asid_t	 audit_session_join(mach_port_name_t port);
341 #endif /* __APPLE_API_PRIVATE */
342 
343 #endif /* defined(_KERNEL) || defined(KERNEL) */
344 
345 __END_DECLS
346 
347 #endif /* !_BSM_AUDIT_H */
348