xref: /freebsd/sys/security/audit/audit.h (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1999-2005 Apple Inc.
5  * Copyright (c) 2016-2018 Robert N. M. Watson
6  * All rights reserved.
7  *
8  * This software was developed by BAE Systems, the University of Cambridge
9  * Computer Laboratory, and Memorial University under DARPA/AFRL contract
10  * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
11  * (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 header includes function prototypes and type definitions that are
42  * necessary for the kernel as a whole to interact with the audit subsystem.
43  */
44 
45 #ifndef _SECURITY_AUDIT_KERNEL_H_
46 #define	_SECURITY_AUDIT_KERNEL_H_
47 
48 #ifndef _KERNEL
49 #error "no user-serviceable parts inside"
50 #endif
51 
52 #include <bsm/audit.h>
53 
54 #include <sys/file.h>
55 #include <sys/sysctl.h>
56 
57 /*
58  * Audit subsystem condition flags.  The audit_trail_enabled flag is set and
59  * removed automatically as a result of configuring log files, and can be
60  * observed but should not be directly manipulated.  The audit suspension
61  * flag permits audit to be temporarily disabled without reconfiguring the
62  * audit target.
63  *
64  * As DTrace can also request system-call auditing, a further
65  * audit_syscalls_enabled flag tracks whether newly entering system calls
66  * should be considered for auditing or not.
67  *
68  * XXXRW: Move trail flags to audit_private.h, as they no longer need to be
69  * visible outside the audit code...?
70  */
71 extern u_int	audit_dtrace_enabled;
72 extern int	audit_trail_enabled;
73 extern int	audit_trail_suspended;
74 extern bool	audit_syscalls_enabled;
75 
76 void	 audit_syscall_enter(unsigned short code, struct thread *td);
77 void	 audit_syscall_exit(int error, struct thread *td);
78 
79 /*
80  * The remaining kernel functions are conditionally compiled in as they are
81  * wrapped by a macro, and the macro should be the only place in the source
82  * tree where these functions are referenced.
83  */
84 #ifdef AUDIT
85 struct ipc_perm;
86 struct sockaddr;
87 union auditon_udata;
88 void	 audit_arg_addr(void * addr);
89 void	 audit_arg_exit(int status, int retval);
90 void	 audit_arg_len(int len);
91 void	 audit_arg_atfd1(int atfd);
92 void	 audit_arg_atfd2(int atfd);
93 void	 audit_arg_fd(int fd);
94 void	 audit_arg_fflags(int fflags);
95 void	 audit_arg_gid(gid_t gid);
96 void	 audit_arg_uid(uid_t uid);
97 void	 audit_arg_egid(gid_t egid);
98 void	 audit_arg_euid(uid_t euid);
99 void	 audit_arg_rgid(gid_t rgid);
100 void	 audit_arg_ruid(uid_t ruid);
101 void	 audit_arg_sgid(gid_t sgid);
102 void	 audit_arg_suid(uid_t suid);
103 void	 audit_arg_groupset(gid_t *gidset, u_int gidset_size);
104 void	 audit_arg_login(char *login);
105 void	 audit_arg_ctlname(int *name, int namelen);
106 void	 audit_arg_mask(int mask);
107 void	 audit_arg_mode(mode_t mode);
108 void	 audit_arg_dev(int dev);
109 void	 audit_arg_value(long value);
110 void	 audit_arg_owner(uid_t uid, gid_t gid);
111 void	 audit_arg_pid(pid_t pid);
112 void	 audit_arg_process(struct proc *p);
113 void	 audit_arg_signum(u_int signum);
114 void	 audit_arg_socket(int sodomain, int sotype, int soprotocol);
115 void	 audit_arg_sockaddr(struct thread *td, int dirfd, struct sockaddr *sa);
116 void	 audit_arg_auid(uid_t auid);
117 void	 audit_arg_auditinfo(struct auditinfo *au_info);
118 void	 audit_arg_auditinfo_addr(struct auditinfo_addr *au_info);
119 void	 audit_arg_upath1(struct thread *td, int dirfd, char *upath);
120 void	 audit_arg_upath1_canon(char *upath);
121 void	 audit_arg_upath2(struct thread *td, int dirfd, char *upath);
122 void	 audit_arg_upath2_canon(char *upath);
123 void	 audit_arg_vnode1(struct vnode *vp);
124 void	 audit_arg_vnode2(struct vnode *vp);
125 void	 audit_arg_text(const char *text);
126 void	 audit_arg_cmd(int cmd);
127 void	 audit_arg_svipc_cmd(int cmd);
128 void	 audit_arg_svipc_perm(struct ipc_perm *perm);
129 void	 audit_arg_svipc_id(int id);
130 void	 audit_arg_svipc_addr(void *addr);
131 void	 audit_arg_svipc_which(int which);
132 void	 audit_arg_posix_ipc_perm(uid_t uid, gid_t gid, mode_t mode);
133 void	 audit_arg_auditon(union auditon_udata *udata);
134 void	 audit_arg_file(struct proc *p, struct file *fp);
135 void	 audit_arg_argv(char *argv, int argc, int length);
136 void	 audit_arg_envv(char *envv, int envc, int length);
137 void	 audit_arg_rights(cap_rights_t *rightsp);
138 void	 audit_arg_fcntl_rights(uint32_t fcntlrights);
139 void	 audit_sysclose(struct thread *td, int fd);
140 void	 audit_cred_copy(struct ucred *src, struct ucred *dest);
141 void	 audit_cred_destroy(struct ucred *cred);
142 void	 audit_cred_init(struct ucred *cred);
143 void	 audit_cred_kproc0(struct ucred *cred);
144 void	 audit_cred_proc1(struct ucred *cred);
145 void	 audit_proc_coredump(struct thread *td, char *path, int errcode);
146 void	 audit_thread_alloc(struct thread *td);
147 void	 audit_thread_free(struct thread *td);
148 
149 /*
150  * Define macros to wrap the audit_arg_* calls by checking the global
151  * audit_syscalls_enabled flag before performing the actual call.
152  */
153 #define	AUDITING_TD(td)		(__predict_false((td)->td_pflags & TDP_AUDITREC))
154 
155 #define	AUDIT_ARG_ADDR(addr) do {					\
156 	if (AUDITING_TD(curthread))					\
157 		audit_arg_addr((addr));					\
158 } while (0)
159 
160 #define	AUDIT_ARG_ARGV(argv, argc, length) do {				\
161 	if (AUDITING_TD(curthread))					\
162 		audit_arg_argv((argv), (argc), (length));		\
163 } while (0)
164 
165 #define	AUDIT_ARG_ATFD1(atfd) do {					\
166 	if (AUDITING_TD(curthread))					\
167 		audit_arg_atfd1((atfd));				\
168 } while (0)
169 
170 #define	AUDIT_ARG_ATFD2(atfd) do {					\
171 	if (AUDITING_TD(curthread))					\
172 		audit_arg_atfd2((atfd));				\
173 } while (0)
174 
175 #define	AUDIT_ARG_AUDITON(udata) do {					\
176 	if (AUDITING_TD(curthread))					\
177 		audit_arg_auditon((udata));				\
178 } while (0)
179 
180 #define	AUDIT_ARG_CMD(cmd) do {						\
181 	if (AUDITING_TD(curthread))					\
182 		audit_arg_cmd((cmd));					\
183 } while (0)
184 
185 #define	AUDIT_ARG_DEV(dev) do {						\
186 	if (AUDITING_TD(curthread))					\
187 		audit_arg_dev((dev));					\
188 } while (0)
189 
190 #define	AUDIT_ARG_EGID(egid) do {					\
191 	if (AUDITING_TD(curthread))					\
192 		audit_arg_egid((egid));					\
193 } while (0)
194 
195 #define	AUDIT_ARG_ENVV(envv, envc, length) do {				\
196 	if (AUDITING_TD(curthread))					\
197 		audit_arg_envv((envv), (envc), (length));		\
198 } while (0)
199 
200 #define	AUDIT_ARG_EXIT(status, retval) do {				\
201 	if (AUDITING_TD(curthread))					\
202 		audit_arg_exit((status), (retval));			\
203 } while (0)
204 
205 #define	AUDIT_ARG_EUID(euid) do {					\
206 	if (AUDITING_TD(curthread))					\
207 		audit_arg_euid((euid));					\
208 } while (0)
209 
210 #define	AUDIT_ARG_FD(fd) do {						\
211 	if (AUDITING_TD(curthread))					\
212 		audit_arg_fd((fd));					\
213 } while (0)
214 
215 #define	AUDIT_ARG_FILE(p, fp) do {					\
216 	if (AUDITING_TD(curthread))					\
217 		audit_arg_file((p), (fp));				\
218 } while (0)
219 
220 #define	AUDIT_ARG_FFLAGS(fflags) do {					\
221 	if (AUDITING_TD(curthread))					\
222 		audit_arg_fflags((fflags));				\
223 } while (0)
224 
225 #define	AUDIT_ARG_GID(gid) do {						\
226 	if (AUDITING_TD(curthread))					\
227 		audit_arg_gid((gid));					\
228 } while (0)
229 
230 #define	AUDIT_ARG_GROUPSET(gidset, gidset_size) do {			\
231 	if (AUDITING_TD(curthread))					\
232 		audit_arg_groupset((gidset), (gidset_size));		\
233 } while (0)
234 
235 #define	AUDIT_ARG_LOGIN(login) do {					\
236 	if (AUDITING_TD(curthread))					\
237 		audit_arg_login((login));				\
238 } while (0)
239 
240 #define	AUDIT_ARG_MODE(mode) do {					\
241 	if (AUDITING_TD(curthread))					\
242 		audit_arg_mode((mode));					\
243 } while (0)
244 
245 #define	AUDIT_ARG_OWNER(uid, gid) do {					\
246 	if (AUDITING_TD(curthread))					\
247 		audit_arg_owner((uid), (gid));				\
248 } while (0)
249 
250 #define	AUDIT_ARG_PID(pid) do {						\
251 	if (AUDITING_TD(curthread))					\
252 		audit_arg_pid((pid));					\
253 } while (0)
254 
255 #define	AUDIT_ARG_POSIX_IPC_PERM(uid, gid, mode) do {			\
256 	if (AUDITING_TD(curthread))					\
257 		audit_arg_posix_ipc_perm((uid), (gid), (mod));		\
258 } while (0)
259 
260 #define	AUDIT_ARG_PROCESS(p) do {					\
261 	if (AUDITING_TD(curthread))					\
262 		audit_arg_process((p));					\
263 } while (0)
264 
265 #define	AUDIT_ARG_RGID(rgid) do {					\
266 	if (AUDITING_TD(curthread))					\
267 		audit_arg_rgid((rgid));					\
268 } while (0)
269 
270 #define	AUDIT_ARG_RIGHTS(rights) do {					\
271 	if (AUDITING_TD(curthread))					\
272 		audit_arg_rights((rights));				\
273 } while (0)
274 
275 #define	AUDIT_ARG_FCNTL_RIGHTS(fcntlrights) do {			\
276 	if (AUDITING_TD(curthread))					\
277 		audit_arg_fcntl_rights((fcntlrights));			\
278 } while (0)
279 
280 #define	AUDIT_ARG_RUID(ruid) do {					\
281 	if (AUDITING_TD(curthread))					\
282 		audit_arg_ruid((ruid));					\
283 } while (0)
284 
285 #define	AUDIT_ARG_SIGNUM(signum) do {					\
286 	if (AUDITING_TD(curthread))					\
287 		audit_arg_signum((signum));				\
288 } while (0)
289 
290 #define	AUDIT_ARG_SGID(sgid) do {					\
291 	if (AUDITING_TD(curthread))					\
292 		audit_arg_sgid((sgid));					\
293 } while (0)
294 
295 #define	AUDIT_ARG_SOCKET(sodomain, sotype, soprotocol) do {		\
296 	if (AUDITING_TD(curthread))					\
297 		audit_arg_socket((sodomain), (sotype), (soprotocol));	\
298 } while (0)
299 
300 #define	AUDIT_ARG_SOCKADDR(td, dirfd, sa) do {				\
301 	if (AUDITING_TD(curthread))					\
302 		audit_arg_sockaddr((td), (dirfd), (sa));		\
303 } while (0)
304 
305 #define	AUDIT_ARG_SUID(suid) do {					\
306 	if (AUDITING_TD(curthread))					\
307 		audit_arg_suid((suid));					\
308 } while (0)
309 
310 #define	AUDIT_ARG_SVIPC_CMD(cmd) do {					\
311 	if (AUDITING_TD(curthread))					\
312 		audit_arg_svipc_cmd((cmd));				\
313 } while (0)
314 
315 #define	AUDIT_ARG_SVIPC_PERM(perm) do {					\
316 	if (AUDITING_TD(curthread))					\
317 		audit_arg_svipc_perm((perm));				\
318 } while (0)
319 
320 #define	AUDIT_ARG_SVIPC_ID(id) do {					\
321 	if (AUDITING_TD(curthread))					\
322 		audit_arg_svipc_id((id));				\
323 } while (0)
324 
325 #define	AUDIT_ARG_SVIPC_ADDR(addr) do {					\
326 	if (AUDITING_TD(curthread))					\
327 		audit_arg_svipc_addr((addr));				\
328 } while (0)
329 
330 #define	AUDIT_ARG_SVIPC_WHICH(which) do {				\
331 	if (AUDITING_TD(curthread))					\
332 		audit_arg_svipc_which((which));				\
333 } while (0)
334 
335 #define	AUDIT_ARG_TEXT(text) do {					\
336 	if (AUDITING_TD(curthread))					\
337 		audit_arg_text((text));					\
338 } while (0)
339 
340 #define	AUDIT_ARG_UID(uid) do {						\
341 	if (AUDITING_TD(curthread))					\
342 		audit_arg_uid((uid));					\
343 } while (0)
344 
345 #define	AUDIT_ARG_UPATH1(td, dirfd, upath) do {				\
346 	if (AUDITING_TD(curthread))					\
347 		audit_arg_upath1((td), (dirfd), (upath));		\
348 } while (0)
349 
350 #define	AUDIT_ARG_UPATH1_CANON(upath) do {				\
351 	if (AUDITING_TD(curthread))					\
352 		audit_arg_upath1_canon((upath));			\
353 } while (0)
354 
355 #define	AUDIT_ARG_UPATH2(td, dirfd, upath) do {				\
356 	if (AUDITING_TD(curthread))					\
357 		audit_arg_upath2((td), (dirfd), (upath));		\
358 } while (0)
359 
360 #define	AUDIT_ARG_UPATH2_CANON(upath) do {				\
361 	if (AUDITING_TD(curthread))					\
362 		audit_arg_upath2_canon((upath));			\
363 } while (0)
364 
365 #define	AUDIT_ARG_VALUE(value) do {					\
366 	if (AUDITING_TD(curthread))					\
367 		audit_arg_value((value));				\
368 } while (0)
369 
370 #define	AUDIT_ARG_VNODE1(vp) do {					\
371 	if (AUDITING_TD(curthread))					\
372 		audit_arg_vnode1((vp));					\
373 } while (0)
374 
375 #define	AUDIT_ARG_VNODE2(vp) do {					\
376 	if (AUDITING_TD(curthread))					\
377 		audit_arg_vnode2((vp));					\
378 } while (0)
379 
380 #define	AUDIT_SYSCALL_ENTER(code, td)	do {				\
381 	if (audit_syscalls_enabled) {					\
382 		audit_syscall_enter(code, td);				\
383 	}								\
384 } while (0)
385 
386 /*
387  * Wrap the audit_syscall_exit() function so that it is called only when
388  * we have a audit record on the thread.  Audit records can persist after
389  * auditing is disabled, so we don't just check audit_syscalls_enabled here.
390  */
391 #define	AUDIT_SYSCALL_EXIT(error, td)	do {				\
392 	if (AUDITING_TD(td))						\
393 		audit_syscall_exit(error, td);				\
394 } while (0)
395 
396 /*
397  * A Macro to wrap the audit_sysclose() function.
398  */
399 #define	AUDIT_SYSCLOSE(td, fd)	do {					\
400 	if (AUDITING_TD(td))						\
401 		audit_sysclose(td, fd);					\
402 } while (0)
403 
404 #else /* !AUDIT */
405 
406 #define	AUDIT_ARG_ADDR(addr)
407 #define	AUDIT_ARG_ARGV(argv, argc, length)
408 #define	AUDIT_ARG_ATFD1(atfd)
409 #define	AUDIT_ARG_ATFD2(atfd)
410 #define	AUDIT_ARG_AUDITON(udata)
411 #define	AUDIT_ARG_CMD(cmd)
412 #define	AUDIT_ARG_DEV(dev)
413 #define	AUDIT_ARG_EGID(egid)
414 #define	AUDIT_ARG_ENVV(envv, envc, length)
415 #define	AUDIT_ARG_EXIT(status, retval)
416 #define	AUDIT_ARG_EUID(euid)
417 #define	AUDIT_ARG_FD(fd)
418 #define	AUDIT_ARG_FILE(p, fp)
419 #define	AUDIT_ARG_FFLAGS(fflags)
420 #define	AUDIT_ARG_GID(gid)
421 #define	AUDIT_ARG_GROUPSET(gidset, gidset_size)
422 #define	AUDIT_ARG_LOGIN(login)
423 #define	AUDIT_ARG_MODE(mode)
424 #define	AUDIT_ARG_OWNER(uid, gid)
425 #define	AUDIT_ARG_PID(pid)
426 #define	AUDIT_ARG_POSIX_IPC_PERM(uid, gid, mode)
427 #define	AUDIT_ARG_PROCESS(p)
428 #define	AUDIT_ARG_RGID(rgid)
429 #define	AUDIT_ARG_RIGHTS(rights)
430 #define	AUDIT_ARG_FCNTL_RIGHTS(fcntlrights)
431 #define	AUDIT_ARG_RUID(ruid)
432 #define	AUDIT_ARG_SIGNUM(signum)
433 #define	AUDIT_ARG_SGID(sgid)
434 #define	AUDIT_ARG_SOCKET(sodomain, sotype, soprotocol)
435 #define	AUDIT_ARG_SOCKADDR(td, dirfd, sa)
436 #define	AUDIT_ARG_SUID(suid)
437 #define	AUDIT_ARG_SVIPC_CMD(cmd)
438 #define	AUDIT_ARG_SVIPC_PERM(perm)
439 #define	AUDIT_ARG_SVIPC_ID(id)
440 #define	AUDIT_ARG_SVIPC_ADDR(addr)
441 #define	AUDIT_ARG_SVIPC_WHICH(which)
442 #define	AUDIT_ARG_TEXT(text)
443 #define	AUDIT_ARG_UID(uid)
444 #define	AUDIT_ARG_UPATH1(td, dirfd, upath)
445 #define	AUDIT_ARG_UPATH1_CANON(upath)
446 #define	AUDIT_ARG_UPATH2(td, dirfd, upath)
447 #define	AUDIT_ARG_UPATH2_CANON(upath)
448 #define	AUDIT_ARG_VALUE(value)
449 #define	AUDIT_ARG_VNODE1(vp)
450 #define	AUDIT_ARG_VNODE2(vp)
451 
452 #define	AUDIT_SYSCALL_ENTER(code, td)
453 #define	AUDIT_SYSCALL_EXIT(error, td)
454 
455 #define	AUDIT_SYSCLOSE(p, fd)
456 
457 #endif /* AUDIT */
458 
459 #endif /* !_SECURITY_AUDIT_KERNEL_H_ */
460