xref: /freebsd/sys/security/audit/audit.h (revision ea825d02749f382c3f7e17f28247f20a48733eab)
1 /*-
2  * Copyright (c) 1999-2005 Apple Inc.
3  * Copyright (c) 2016-2017 Robert N. M. Watson
4  * All rights reserved.
5  *
6  * This software was developed by BAE Systems, the University of Cambridge
7  * Computer Laboratory, and Memorial University under DARPA/AFRL contract
8  * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
9  * (TC) research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1.  Redistributions of source code must retain the above copyright
15  *     notice, this list of conditions and the following disclaimer.
16  * 2.  Redistributions in binary form must reproduce the above copyright
17  *     notice, this list of conditions and the following disclaimer in the
18  *     documentation and/or other materials provided with the distribution.
19  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
20  *     its contributors may be used to endorse or promote products derived
21  *     from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $FreeBSD$
36  */
37 
38 /*
39  * This header includes function prototypes and type definitions that are
40  * necessary for the kernel as a whole to interact with the audit subsystem.
41  */
42 
43 #ifndef _SECURITY_AUDIT_KERNEL_H_
44 #define	_SECURITY_AUDIT_KERNEL_H_
45 
46 #ifndef _KERNEL
47 #error "no user-serviceable parts inside"
48 #endif
49 
50 #include <bsm/audit.h>
51 
52 #include <sys/file.h>
53 #include <sys/sysctl.h>
54 
55 /*
56  * Audit subsystem condition flags.  The audit_enabled flag is set and
57  * removed automatically as a result of configuring log files, and can be
58  * observed but should not be directly manipulated.  The audit suspension
59  * flag permits audit to be temporarily disabled without reconfiguring the
60  * audit target.
61  */
62 extern int	audit_enabled;
63 extern int	audit_suspended;
64 
65 void	 audit_syscall_enter(unsigned short code, struct thread *td);
66 void	 audit_syscall_exit(int error, struct thread *td);
67 
68 /*
69  * The remaining kernel functions are conditionally compiled in as they are
70  * wrapped by a macro, and the macro should be the only place in the source
71  * tree where these functions are referenced.
72  */
73 #ifdef AUDIT
74 struct ipc_perm;
75 struct sockaddr;
76 union auditon_udata;
77 void	 audit_arg_addr(void * addr);
78 void	 audit_arg_exit(int status, int retval);
79 void	 audit_arg_len(int len);
80 void	 audit_arg_atfd1(int atfd);
81 void	 audit_arg_atfd2(int atfd);
82 void	 audit_arg_fd(int fd);
83 void	 audit_arg_fflags(int fflags);
84 void	 audit_arg_gid(gid_t gid);
85 void	 audit_arg_uid(uid_t uid);
86 void	 audit_arg_egid(gid_t egid);
87 void	 audit_arg_euid(uid_t euid);
88 void	 audit_arg_rgid(gid_t rgid);
89 void	 audit_arg_ruid(uid_t ruid);
90 void	 audit_arg_sgid(gid_t sgid);
91 void	 audit_arg_suid(uid_t suid);
92 void	 audit_arg_groupset(gid_t *gidset, u_int gidset_size);
93 void	 audit_arg_login(char *login);
94 void	 audit_arg_ctlname(int *name, int namelen);
95 void	 audit_arg_mask(int mask);
96 void	 audit_arg_mode(mode_t mode);
97 void	 audit_arg_dev(int dev);
98 void	 audit_arg_value(long value);
99 void	 audit_arg_owner(uid_t uid, gid_t gid);
100 void	 audit_arg_pid(pid_t pid);
101 void	 audit_arg_process(struct proc *p);
102 void	 audit_arg_signum(u_int signum);
103 void	 audit_arg_socket(int sodomain, int sotype, int soprotocol);
104 void	 audit_arg_sockaddr(struct thread *td, int dirfd, struct sockaddr *sa);
105 void	 audit_arg_auid(uid_t auid);
106 void	 audit_arg_auditinfo(struct auditinfo *au_info);
107 void	 audit_arg_auditinfo_addr(struct auditinfo_addr *au_info);
108 void	 audit_arg_upath1(struct thread *td, int dirfd, char *upath);
109 void	 audit_arg_upath1_canon(char *upath);
110 void	 audit_arg_upath2(struct thread *td, int dirfd, char *upath);
111 void	 audit_arg_upath2_canon(char *upath);
112 void	 audit_arg_vnode1(struct vnode *vp);
113 void	 audit_arg_vnode2(struct vnode *vp);
114 void	 audit_arg_text(char *text);
115 void	 audit_arg_cmd(int cmd);
116 void	 audit_arg_svipc_cmd(int cmd);
117 void	 audit_arg_svipc_perm(struct ipc_perm *perm);
118 void	 audit_arg_svipc_id(int id);
119 void	 audit_arg_svipc_addr(void *addr);
120 void	 audit_arg_svipc_which(int which);
121 void	 audit_arg_posix_ipc_perm(uid_t uid, gid_t gid, mode_t mode);
122 void	 audit_arg_auditon(union auditon_udata *udata);
123 void	 audit_arg_file(struct proc *p, struct file *fp);
124 void	 audit_arg_argv(char *argv, int argc, int length);
125 void	 audit_arg_envv(char *envv, int envc, int length);
126 void	 audit_arg_rights(cap_rights_t *rightsp);
127 void	 audit_arg_fcntl_rights(uint32_t fcntlrights);
128 void	 audit_sysclose(struct thread *td, int fd);
129 void	 audit_cred_copy(struct ucred *src, struct ucred *dest);
130 void	 audit_cred_destroy(struct ucred *cred);
131 void	 audit_cred_init(struct ucred *cred);
132 void	 audit_cred_kproc0(struct ucred *cred);
133 void	 audit_cred_proc1(struct ucred *cred);
134 void	 audit_proc_coredump(struct thread *td, char *path, int errcode);
135 void	 audit_thread_alloc(struct thread *td);
136 void	 audit_thread_free(struct thread *td);
137 
138 /*
139  * Define macros to wrap the audit_arg_* calls by checking the global
140  * audit_enabled flag before performing the actual call.
141  */
142 #define	AUDITING_TD(td)		((td)->td_pflags & TDP_AUDITREC)
143 
144 #define	AUDIT_ARG_ADDR(addr) do {					\
145 	if (AUDITING_TD(curthread))					\
146 		audit_arg_addr((addr));					\
147 } while (0)
148 
149 #define	AUDIT_ARG_ARGV(argv, argc, length) do {				\
150 	if (AUDITING_TD(curthread))					\
151 		audit_arg_argv((argv), (argc), (length));		\
152 } while (0)
153 
154 #define	AUDIT_ARG_ATFD1(atfd) do {					\
155 	if (AUDITING_TD(curthread))					\
156 		audit_arg_atfd1((atfd));				\
157 } while (0)
158 
159 #define	AUDIT_ARG_ATFD2(atfd) do {					\
160 	if (AUDITING_TD(curthread))					\
161 		audit_arg_atfd2((atfd));				\
162 } while (0)
163 
164 #define	AUDIT_ARG_AUDITON(udata) do {					\
165 	if (AUDITING_TD(curthread))					\
166 		audit_arg_auditon((udata));				\
167 } while (0)
168 
169 #define	AUDIT_ARG_CMD(cmd) do {						\
170 	if (AUDITING_TD(curthread))					\
171 		audit_arg_cmd((cmd));					\
172 } while (0)
173 
174 #define	AUDIT_ARG_DEV(dev) do {						\
175 	if (AUDITING_TD(curthread))					\
176 		audit_arg_dev((dev));					\
177 } while (0)
178 
179 #define	AUDIT_ARG_EGID(egid) do {					\
180 	if (AUDITING_TD(curthread))					\
181 		audit_arg_egid((egid));					\
182 } while (0)
183 
184 #define	AUDIT_ARG_ENVV(envv, envc, length) do {				\
185 	if (AUDITING_TD(curthread))					\
186 		audit_arg_envv((envv), (envc), (length));		\
187 } while (0)
188 
189 #define	AUDIT_ARG_EXIT(status, retval) do {				\
190 	if (AUDITING_TD(curthread))					\
191 		audit_arg_exit((status), (retval));			\
192 } while (0)
193 
194 #define	AUDIT_ARG_EUID(euid) do {					\
195 	if (AUDITING_TD(curthread))					\
196 		audit_arg_euid((euid));					\
197 } while (0)
198 
199 #define	AUDIT_ARG_FD(fd) do {						\
200 	if (AUDITING_TD(curthread))					\
201 		audit_arg_fd((fd));					\
202 } while (0)
203 
204 #define	AUDIT_ARG_FILE(p, fp) do {					\
205 	if (AUDITING_TD(curthread))					\
206 		audit_arg_file((p), (fp));				\
207 } while (0)
208 
209 #define	AUDIT_ARG_FFLAGS(fflags) do {					\
210 	if (AUDITING_TD(curthread))					\
211 		audit_arg_fflags((fflags));				\
212 } while (0)
213 
214 #define	AUDIT_ARG_GID(gid) do {						\
215 	if (AUDITING_TD(curthread))					\
216 		audit_arg_gid((gid));					\
217 } while (0)
218 
219 #define	AUDIT_ARG_GROUPSET(gidset, gidset_size) do {			\
220 	if (AUDITING_TD(curthread))					\
221 		audit_arg_groupset((gidset), (gidset_size));		\
222 } while (0)
223 
224 #define	AUDIT_ARG_LOGIN(login) do {					\
225 	if (AUDITING_TD(curthread))					\
226 		audit_arg_login((login));				\
227 } while (0)
228 
229 #define	AUDIT_ARG_MODE(mode) do {					\
230 	if (AUDITING_TD(curthread))					\
231 		audit_arg_mode((mode));					\
232 } while (0)
233 
234 #define	AUDIT_ARG_OWNER(uid, gid) do {					\
235 	if (AUDITING_TD(curthread))					\
236 		audit_arg_owner((uid), (gid));				\
237 } while (0)
238 
239 #define	AUDIT_ARG_PID(pid) do {						\
240 	if (AUDITING_TD(curthread))					\
241 		audit_arg_pid((pid));					\
242 } while (0)
243 
244 #define	AUDIT_ARG_POSIX_IPC_PERM(uid, gid, mode) do {			\
245 	if (AUDITING_TD(curthread))					\
246 		audit_arg_posix_ipc_perm((uid), (gid), (mod));		\
247 } while (0)
248 
249 #define	AUDIT_ARG_PROCESS(p) do {					\
250 	if (AUDITING_TD(curthread))					\
251 		audit_arg_process((p));					\
252 } while (0)
253 
254 #define	AUDIT_ARG_RGID(rgid) do {					\
255 	if (AUDITING_TD(curthread))					\
256 		audit_arg_rgid((rgid));					\
257 } while (0)
258 
259 #define	AUDIT_ARG_RIGHTS(rights) do {					\
260 	if (AUDITING_TD(curthread))					\
261 		audit_arg_rights((rights));				\
262 } while (0)
263 
264 #define	AUDIT_ARG_FCNTL_RIGHTS(fcntlrights) do {			\
265 	if (AUDITING_TD(curthread))					\
266 		audit_arg_fcntl_rights((fcntlrights));			\
267 } while (0)
268 
269 #define	AUDIT_ARG_RUID(ruid) do {					\
270 	if (AUDITING_TD(curthread))					\
271 		audit_arg_ruid((ruid));					\
272 } while (0)
273 
274 #define	AUDIT_ARG_SIGNUM(signum) do {					\
275 	if (AUDITING_TD(curthread))					\
276 		audit_arg_signum((signum));				\
277 } while (0)
278 
279 #define	AUDIT_ARG_SGID(sgid) do {					\
280 	if (AUDITING_TD(curthread))					\
281 		audit_arg_sgid((sgid));					\
282 } while (0)
283 
284 #define	AUDIT_ARG_SOCKET(sodomain, sotype, soprotocol) do {		\
285 	if (AUDITING_TD(curthread))					\
286 		audit_arg_socket((sodomain), (sotype), (soprotocol));	\
287 } while (0)
288 
289 #define	AUDIT_ARG_SOCKADDR(td, dirfd, sa) do {				\
290 	if (AUDITING_TD(curthread))					\
291 		audit_arg_sockaddr((td), (dirfd), (sa));		\
292 } while (0)
293 
294 #define	AUDIT_ARG_SUID(suid) do {					\
295 	if (AUDITING_TD(curthread))					\
296 		audit_arg_suid((suid));					\
297 } while (0)
298 
299 #define	AUDIT_ARG_SVIPC_CMD(cmd) do {					\
300 	if (AUDITING_TD(curthread))					\
301 		audit_arg_svipc_cmd((cmd));				\
302 } while (0)
303 
304 #define	AUDIT_ARG_SVIPC_PERM(perm) do {					\
305 	if (AUDITING_TD(curthread))					\
306 		audit_arg_svipc_perm((perm));				\
307 } while (0)
308 
309 #define	AUDIT_ARG_SVIPC_ID(id) do {					\
310 	if (AUDITING_TD(curthread))					\
311 		audit_arg_svipc_id((id));				\
312 } while (0)
313 
314 #define	AUDIT_ARG_SVIPC_ADDR(addr) do {					\
315 	if (AUDITING_TD(curthread))					\
316 		audit_arg_svipc_addr((addr));				\
317 } while (0)
318 
319 #define	AUDIT_ARG_SVIPC_WHICH(which) do {				\
320 	if (AUDITING_TD(curthread))					\
321 		audit_arg_svipc_which((which));				\
322 } while (0)
323 
324 #define	AUDIT_ARG_TEXT(text) do {					\
325 	if (AUDITING_TD(curthread))					\
326 		audit_arg_text((text));					\
327 } while (0)
328 
329 #define	AUDIT_ARG_UID(uid) do {						\
330 	if (AUDITING_TD(curthread))					\
331 		audit_arg_uid((uid));					\
332 } while (0)
333 
334 #define	AUDIT_ARG_UPATH1(td, dirfd, upath) do {				\
335 	if (AUDITING_TD(curthread))					\
336 		audit_arg_upath1((td), (dirfd), (upath));		\
337 } while (0)
338 
339 #define	AUDIT_ARG_UPATH1_CANON(upath) do {				\
340 	if (AUDITING_TD(curthread))					\
341 		audit_arg_upath1_canon((upath));			\
342 } while (0)
343 
344 #define	AUDIT_ARG_UPATH2(td, dirfd, upath) do {				\
345 	if (AUDITING_TD(curthread))					\
346 		audit_arg_upath2((td), (dirfd), (upath));		\
347 } while (0)
348 
349 #define	AUDIT_ARG_UPATH2_CANON(upath) do {				\
350 	if (AUDITING_TD(curthread))					\
351 		audit_arg_upath2_canon((upath));			\
352 } while (0)
353 
354 #define	AUDIT_ARG_VALUE(value) do {					\
355 	if (AUDITING_TD(curthread))					\
356 		audit_arg_value((value));				\
357 } while (0)
358 
359 #define	AUDIT_ARG_VNODE1(vp) do {					\
360 	if (AUDITING_TD(curthread))					\
361 		audit_arg_vnode1((vp));					\
362 } while (0)
363 
364 #define	AUDIT_ARG_VNODE2(vp) do {					\
365 	if (AUDITING_TD(curthread))					\
366 		audit_arg_vnode2((vp));					\
367 } while (0)
368 
369 #define	AUDIT_SYSCALL_ENTER(code, td)	do {				\
370 	if (audit_enabled) {						\
371 		audit_syscall_enter(code, td);				\
372 	}								\
373 } while (0)
374 
375 /*
376  * Wrap the audit_syscall_exit() function so that it is called only when
377  * we have a audit record on the thread.  Audit records can persist after
378  * auditing is disabled, so we don't just check audit_enabled here.
379  */
380 #define	AUDIT_SYSCALL_EXIT(error, td)	do {				\
381 	if (td->td_pflags & TDP_AUDITREC)				\
382 		audit_syscall_exit(error, td);				\
383 } while (0)
384 
385 /*
386  * A Macro to wrap the audit_sysclose() function.
387  */
388 #define	AUDIT_SYSCLOSE(td, fd)	do {					\
389 	if (td->td_pflags & TDP_AUDITREC)				\
390 		audit_sysclose(td, fd);					\
391 } while (0)
392 
393 #else /* !AUDIT */
394 
395 #define	AUDIT_ARG_ADDR(addr)
396 #define	AUDIT_ARG_ARGV(argv, argc, length)
397 #define	AUDIT_ARG_ATFD1(atfd)
398 #define	AUDIT_ARG_ATFD2(atfd)
399 #define	AUDIT_ARG_AUDITON(udata)
400 #define	AUDIT_ARG_CMD(cmd)
401 #define	AUDIT_ARG_DEV(dev)
402 #define	AUDIT_ARG_EGID(egid)
403 #define	AUDIT_ARG_ENVV(envv, envc, length)
404 #define	AUDIT_ARG_EXIT(status, retval)
405 #define	AUDIT_ARG_EUID(euid)
406 #define	AUDIT_ARG_FD(fd)
407 #define	AUDIT_ARG_FILE(p, fp)
408 #define	AUDIT_ARG_FFLAGS(fflags)
409 #define	AUDIT_ARG_GID(gid)
410 #define	AUDIT_ARG_GROUPSET(gidset, gidset_size)
411 #define	AUDIT_ARG_LOGIN(login)
412 #define	AUDIT_ARG_MODE(mode)
413 #define	AUDIT_ARG_OWNER(uid, gid)
414 #define	AUDIT_ARG_PID(pid)
415 #define	AUDIT_ARG_POSIX_IPC_PERM(uid, gid, mode)
416 #define	AUDIT_ARG_PROCESS(p)
417 #define	AUDIT_ARG_RGID(rgid)
418 #define	AUDIT_ARG_RIGHTS(rights)
419 #define	AUDIT_ARG_FCNTL_RIGHTS(fcntlrights)
420 #define	AUDIT_ARG_RUID(ruid)
421 #define	AUDIT_ARG_SIGNUM(signum)
422 #define	AUDIT_ARG_SGID(sgid)
423 #define	AUDIT_ARG_SOCKET(sodomain, sotype, soprotocol)
424 #define	AUDIT_ARG_SOCKADDR(td, dirfd, sa)
425 #define	AUDIT_ARG_SUID(suid)
426 #define	AUDIT_ARG_SVIPC_CMD(cmd)
427 #define	AUDIT_ARG_SVIPC_PERM(perm)
428 #define	AUDIT_ARG_SVIPC_ID(id)
429 #define	AUDIT_ARG_SVIPC_ADDR(addr)
430 #define	AUDIT_ARG_SVIPC_WHICH(which)
431 #define	AUDIT_ARG_TEXT(text)
432 #define	AUDIT_ARG_UID(uid)
433 #define	AUDIT_ARG_UPATH1(td, dirfd, upath)
434 #define	AUDIT_ARG_UPATH1_CANON(upath)
435 #define	AUDIT_ARG_UPATH2(td, dirfd, upath)
436 #define	AUDIT_ARG_UPATH2_CANON(upath)
437 #define	AUDIT_ARG_VALUE(value)
438 #define	AUDIT_ARG_VNODE1(vp)
439 #define	AUDIT_ARG_VNODE2(vp)
440 
441 #define	AUDIT_SYSCALL_ENTER(code, td)
442 #define	AUDIT_SYSCALL_EXIT(error, td)
443 
444 #define	AUDIT_SYSCLOSE(p, fd)
445 
446 #endif /* AUDIT */
447 
448 #endif /* !_SECURITY_AUDIT_KERNEL_H_ */
449