xref: /freebsd/sys/security/audit/audit.h (revision e4b4bbb665e36adb8e79b1c5c7946be0ba80b9f1)
1 /*-
2  * Copyright (c) 1999-2005 Apple Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 /*
33  * This header includes function prototypes and type definitions that are
34  * necessary for the kernel as a whole to interact with the audit subsystem.
35  */
36 
37 #ifndef _SECURITY_AUDIT_KERNEL_H_
38 #define	_SECURITY_AUDIT_KERNEL_H_
39 
40 #ifndef _KERNEL
41 #error "no user-serviceable parts inside"
42 #endif
43 
44 #include <bsm/audit.h>
45 
46 #include <sys/file.h>
47 #include <sys/sysctl.h>
48 
49 /*
50  * Audit subsystem condition flags.  The audit_enabled flag is set and
51  * removed automatically as a result of configuring log files, and can be
52  * observed but should not be directly manipulated.  The audit suspension
53  * flag permits audit to be temporarily disabled without reconfiguring the
54  * audit target.
55  */
56 extern int	audit_enabled;
57 extern int	audit_suspended;
58 
59 /*
60  * Define the masks for the audited arguments.
61  *
62  * XXXRW: These need to remain in audit.h for now because our vnode and name
63  * lookup audit calls rely on passing in flags to indicate which name or
64  * vnode is being logged.  These should move to audit_private.h when that is
65  * fixed.
66  */
67 #define	ARG_EUID		0x0000000000000001ULL
68 #define	ARG_RUID		0x0000000000000002ULL
69 #define	ARG_SUID		0x0000000000000004ULL
70 #define	ARG_EGID		0x0000000000000008ULL
71 #define	ARG_RGID		0x0000000000000010ULL
72 #define	ARG_SGID		0x0000000000000020ULL
73 #define	ARG_PID			0x0000000000000040ULL
74 #define	ARG_UID			0x0000000000000080ULL
75 #define	ARG_AUID		0x0000000000000100ULL
76 #define	ARG_GID			0x0000000000000200ULL
77 #define	ARG_FD			0x0000000000000400ULL
78 #define	ARG_POSIX_IPC_PERM	0x0000000000000800ULL
79 #define	ARG_FFLAGS		0x0000000000001000ULL
80 #define	ARG_MODE		0x0000000000002000ULL
81 #define	ARG_DEV			0x0000000000004000ULL
82 #define	ARG_ADDR		0x0000000000008000ULL
83 #define	ARG_LEN			0x0000000000010000ULL
84 #define	ARG_MASK		0x0000000000020000ULL
85 #define	ARG_SIGNUM		0x0000000000040000ULL
86 #define	ARG_LOGIN		0x0000000000080000ULL
87 #define	ARG_SADDRINET		0x0000000000100000ULL
88 #define	ARG_SADDRINET6		0x0000000000200000ULL
89 #define	ARG_SADDRUNIX		0x0000000000400000ULL
90 #define	ARG_TERMID_ADDR		0x0000000000400000ULL
91 #define	ARG_UNUSED2		0x0000000001000000ULL
92 #define	ARG_UPATH1		0x0000000002000000ULL
93 #define	ARG_UPATH2		0x0000000004000000ULL
94 #define	ARG_TEXT		0x0000000008000000ULL
95 #define	ARG_VNODE1		0x0000000010000000ULL
96 #define	ARG_VNODE2		0x0000000020000000ULL
97 #define	ARG_SVIPC_CMD		0x0000000040000000ULL
98 #define	ARG_SVIPC_PERM		0x0000000080000000ULL
99 #define	ARG_SVIPC_ID		0x0000000100000000ULL
100 #define	ARG_SVIPC_ADDR		0x0000000200000000ULL
101 #define	ARG_GROUPSET		0x0000000400000000ULL
102 #define	ARG_CMD			0x0000000800000000ULL
103 #define	ARG_SOCKINFO		0x0000001000000000ULL
104 #define	ARG_ASID		0x0000002000000000ULL
105 #define	ARG_TERMID		0x0000004000000000ULL
106 #define	ARG_AUDITON		0x0000008000000000ULL
107 #define	ARG_VALUE		0x0000010000000000ULL
108 #define	ARG_AMASK		0x0000020000000000ULL
109 #define	ARG_CTLNAME		0x0000040000000000ULL
110 #define	ARG_PROCESS		0x0000080000000000ULL
111 #define	ARG_MACHPORT1		0x0000100000000000ULL
112 #define	ARG_MACHPORT2		0x0000200000000000ULL
113 #define	ARG_EXIT		0x0000400000000000ULL
114 #define	ARG_IOVECSTR		0x0000800000000000ULL
115 #define	ARG_ARGV		0x0001000000000000ULL
116 #define	ARG_ENVV		0x0002000000000000ULL
117 #define	ARG_ATFD1		0x0004000000000000ULL
118 #define	ARG_ATFD2		0x0008000000000000ULL
119 #define	ARG_NONE		0x0000000000000000ULL
120 #define	ARG_ALL			0xFFFFFFFFFFFFFFFFULL
121 
122 void	 audit_syscall_enter(unsigned short code, struct thread *td);
123 void	 audit_syscall_exit(int error, struct thread *td);
124 
125 /*
126  * The remaining kernel functions are conditionally compiled in as they are
127  * wrapped by a macro, and the macro should be the only place in the source
128  * tree where these functions are referenced.
129  */
130 #ifdef AUDIT
131 struct ipc_perm;
132 struct sockaddr;
133 union auditon_udata;
134 void	 audit_arg_addr(void * addr);
135 void	 audit_arg_exit(int status, int retval);
136 void	 audit_arg_len(int len);
137 void	 audit_arg_atfd1(int atfd);
138 void	 audit_arg_atfd2(int atfd);
139 void	 audit_arg_fd(int fd);
140 void	 audit_arg_fflags(int fflags);
141 void	 audit_arg_gid(gid_t gid);
142 void	 audit_arg_uid(uid_t uid);
143 void	 audit_arg_egid(gid_t egid);
144 void	 audit_arg_euid(uid_t euid);
145 void	 audit_arg_rgid(gid_t rgid);
146 void	 audit_arg_ruid(uid_t ruid);
147 void	 audit_arg_sgid(gid_t sgid);
148 void	 audit_arg_suid(uid_t suid);
149 void	 audit_arg_groupset(gid_t *gidset, u_int gidset_size);
150 void	 audit_arg_login(char *login);
151 void	 audit_arg_ctlname(int *name, int namelen);
152 void	 audit_arg_mask(int mask);
153 void	 audit_arg_mode(mode_t mode);
154 void	 audit_arg_dev(int dev);
155 void	 audit_arg_value(long value);
156 void	 audit_arg_owner(uid_t uid, gid_t gid);
157 void	 audit_arg_pid(pid_t pid);
158 void	 audit_arg_process(struct proc *p);
159 void	 audit_arg_signum(u_int signum);
160 void	 audit_arg_socket(int sodomain, int sotype, int soprotocol);
161 void	 audit_arg_sockaddr(struct thread *td, struct sockaddr *sa);
162 void	 audit_arg_auid(uid_t auid);
163 void	 audit_arg_auditinfo(struct auditinfo *au_info);
164 void	 audit_arg_auditinfo_addr(struct auditinfo_addr *au_info);
165 void	 audit_arg_upath(struct thread *td, char *upath, u_int64_t flags);
166 void	 audit_arg_vnode(struct vnode *vp, u_int64_t flags);
167 void	 audit_arg_text(char *text);
168 void	 audit_arg_cmd(int cmd);
169 void	 audit_arg_svipc_cmd(int cmd);
170 void	 audit_arg_svipc_perm(struct ipc_perm *perm);
171 void	 audit_arg_svipc_id(int id);
172 void	 audit_arg_svipc_addr(void *addr);
173 void	 audit_arg_posix_ipc_perm(uid_t uid, gid_t gid, mode_t mode);
174 void	 audit_arg_auditon(union auditon_udata *udata);
175 void	 audit_arg_file(struct proc *p, struct file *fp);
176 void	 audit_arg_argv(char *argv, int argc, int length);
177 void	 audit_arg_envv(char *envv, int envc, int length);
178 void	 audit_sysclose(struct thread *td, int fd);
179 void	 audit_cred_copy(struct ucred *src, struct ucred *dest);
180 void	 audit_cred_destroy(struct ucred *cred);
181 void	 audit_cred_init(struct ucred *cred);
182 void	 audit_cred_kproc0(struct ucred *cred);
183 void	 audit_cred_proc1(struct ucred *cred);
184 void	 audit_proc_coredump(struct thread *td, char *path, int errcode);
185 void	 audit_thread_alloc(struct thread *td);
186 void	 audit_thread_free(struct thread *td);
187 
188 /*
189  * Define macros to wrap the audit_arg_* calls by checking the global
190  * audit_enabled flag before performing the actual call.
191  */
192 #define	AUDITING_TD(td)		((td)->td_pflags & TDP_AUDITREC)
193 
194 #define	AUDIT_ARG_ADDR(addr) do {					\
195 	if (AUDITING_TD(curthread))					\
196 		audit_arg_addr((addr));					\
197 } while (0)
198 
199 #define	AUDIT_ARG_ARGV(argv, argc, length) do {				\
200 	if (AUDITING_TD(curthread))					\
201 		audit_arg_argv((argv), (argc), (length));		\
202 } while (0)
203 
204 #define	AUDIT_ARG_ATFD1(atfd) do {					\
205 	if (AUDITING_TD(curthread))					\
206 		audit_arg_atfd1((atfd));				\
207 } while (0)
208 
209 #define	AUDIT_ARG_ATFD2(atfd) do {					\
210 	if (AUDITING_TD(curthread))					\
211 		audit_arg_atfd2((atfd));				\
212 } while (0)
213 
214 #define	AUDIT_ARG_AUDITON(udata) do {					\
215 	if (AUDITING_TD(curthread))					\
216 		audit_arg_auditon((udata));				\
217 } while (0)
218 
219 #define	AUDIT_ARG_CMD(cmd) do {						\
220 	if (AUDITING_TD(curthread))					\
221 		audit_arg_cmd((cmd));					\
222 } while (0)
223 
224 #define	AUDIT_ARG_DEV(dev) do {						\
225 	if (AUDITING_TD(curthread))					\
226 		audit_arg_dev((dev));					\
227 } while (0)
228 
229 #define	AUDIT_ARG_EGID(egid) do {					\
230 	if (AUDITING_TD(curthread))					\
231 		audit_arg_egid((egid));					\
232 } while (0)
233 
234 #define	AUDIT_ARG_ENVV(envv, envc, length) do {				\
235 	if (AUDITING_TD(curthread))					\
236 		audit_arg_envv((envv), (envc), (length));		\
237 } while (0)
238 
239 #define	AUDIT_ARG_EXIT(status, retval) do {				\
240 	if (AUDITING_TD(curthread))					\
241 		audit_arg_exit((status), (retval));			\
242 } while (0)
243 
244 #define	AUDIT_ARG_EUID(euid) do {					\
245 	if (AUDITING_TD(curthread))					\
246 		audit_arg_euid((euid));					\
247 } while (0)
248 
249 #define	AUDIT_ARG_FD(fd) do {						\
250 	if (AUDITING_TD(curthread))					\
251 		audit_arg_fd((fd));					\
252 } while (0)
253 
254 #define	AUDIT_ARG_FILE(p, fp) do {					\
255 	if (AUDITING_TD(curthread))					\
256 		audit_arg_file((p), (fp));				\
257 } while (0)
258 
259 #define	AUDIT_ARG_FFLAGS(fflags) do {					\
260 	if (AUDITING_TD(curthread))					\
261 		audit_arg_fflags((fflags));				\
262 } while (0)
263 
264 #define	AUDIT_ARG_GID(gid) do {						\
265 	if (AUDITING_TD(curthread))					\
266 		audit_arg_gid((gid));					\
267 } while (0)
268 
269 #define	AUDIT_ARG_GROUPSET(gidset, gidset_size) do {			\
270 	if (AUDITING_TD(curthread))					\
271 		audit_arg_groupset((gidset), (gidset_size));		\
272 } while (0)
273 
274 #define	AUDIT_ARG_MODE(mode) do {					\
275 	if (AUDITING_TD(curthread))					\
276 		audit_arg_mode((mode));					\
277 } while (0)
278 
279 #define	AUDIT_ARG_OWNER(uid, gid) do {					\
280 	if (AUDITING_TD(curthread))					\
281 		audit_arg_owner((uid), (gid));				\
282 } while (0)
283 
284 #define	AUDIT_ARG_PID(pid) do {						\
285 	if (AUDITING_TD(curthread))					\
286 		audit_arg_pid((pid));					\
287 } while (0)
288 
289 #define	AUDIT_ARG_PROCESS(p) do {					\
290 	if (AUDITING_TD(curthread))					\
291 		audit_arg_process((p));					\
292 } while (0)
293 
294 #define	AUDIT_ARG_RGID(rgid) do {					\
295 	if (AUDITING_TD(curthread))					\
296 		audit_arg_gid((rgid));					\
297 } while (0)
298 
299 #define	AUDIT_ARG_RUID(ruid) do {					\
300 	if (AUDITING_TD(curthread))					\
301 		audit_arg_ruid((ruid));					\
302 } while (0)
303 
304 #define	AUDIT_ARG_SIGNUM(signum) do {					\
305 	if (AUDITING_TD(curthread))					\
306 		audit_arg_signum((signum));				\
307 } while (0)
308 
309 #define	AUDIT_ARG_SGID(sgid) do {					\
310 	if (AUDITING_TD(curthread))					\
311 		audit_arg_sgid((sgid));					\
312 } while (0)
313 
314 #define	AUDIT_ARG_SOCKET(sodomain, sotype, soprotocol) do {		\
315 	if (AUDITING_TD(curthread))					\
316 		audit_arg_socket((sodomain), (sotype), (soprotocol));	\
317 } while (0)
318 
319 #define	AUDIT_ARG_SUID(suid) do {					\
320 	if (AUDITING_TD(curthread))					\
321 		audit_arg_suid((suid));					\
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_UPATH(td, upath, flags) do {				\
335 	if (AUDITING_TD(curthread))					\
336 		audit_arg_upath((td), (upath), (flags));		\
337 } while (0)
338 
339 #define	AUDIT_ARG_VALUE(value) do {					\
340 	if (AUDITING_TD(curthread))					\
341 		audit_arg_value((value));				\
342 } while (0)
343 
344 #define	AUDIT_ARG_VNODE(vp, flags) do {					\
345 	if (AUDITING_TD(curthread))					\
346 		audit_arg_vnode((vp), (flags));				\
347 } while (0)
348 
349 #define	AUDIT_SYSCALL_ENTER(code, td)	do {				\
350 	if (audit_enabled) {						\
351 		audit_syscall_enter(code, td);				\
352 	}								\
353 } while (0)
354 
355 /*
356  * Wrap the audit_syscall_exit() function so that it is called only when
357  * we have a audit record on the thread.  Audit records can persist after
358  * auditing is disabled, so we don't just check audit_enabled here.
359  */
360 #define	AUDIT_SYSCALL_EXIT(error, td)	do {				\
361 	if (td->td_pflags & TDP_AUDITREC)				\
362 		audit_syscall_exit(error, td);				\
363 } while (0)
364 
365 /*
366  * A Macro to wrap the audit_sysclose() function.
367  */
368 #define	AUDIT_SYSCLOSE(td, fd)	do {					\
369 	if (td->td_pflags & TDP_AUDITREC)				\
370 		audit_sysclose(td, fd);					\
371 } while (0)
372 
373 #else /* !AUDIT */
374 
375 #define	AUDIT_ARG_ADDR(addr)
376 #define	AUDIT_ARG_ARGV(argv, argc, length)
377 #define	AUDIT_ARG_ATFD1(atfd)
378 #define	AUDIT_ARG_ATFD2(atfd)
379 #define	AUDIT_ARG_AUDITON(udata)
380 #define	AUDIT_ARG_CMD(cmd)
381 #define	AUDIT_ARG_DEV(dev)
382 #define	AUDIT_ARG_EGID(egid)
383 #define	AUDIT_ARG_ENVV(envv, envc, length)
384 #define	AUDIT_ARG_EXIT(status, retval)
385 #define	AUDIT_ARG_EUID(euid)
386 #define	AUDIT_ARG_FD(fd)
387 #define	AUDIT_ARG_FILE(p, fp)
388 #define	AUDIT_ARG_FFLAGS(fflags)
389 #define	AUDIT_ARG_GID(gid)
390 #define	AUDIT_ARG_GROUPSET(gidset, gidset_size)
391 #define	AUDIT_ARG_MODE(mode)
392 #define	AUDIT_ARG_OWNER(uid, gid)
393 #define	AUDIT_ARG_PID(pid)
394 #define	AUDIT_ARG_PROCESS(p)
395 #define	AUDIT_ARG_RGID(rgid)
396 #define	AUDIT_ARG_RUID(ruid)
397 #define	AUDIT_ARG_SIGNUM(signum)
398 #define	AUDIT_ARG_SGID(sgid)
399 #define	AUDIT_ARG_SOCKET(sodomain, sotype, soprotocol)
400 #define	AUDIT_ARG_SUID(suid)
401 #define	AUDIT_ARG_TEXT(text)
402 #define	AUDIT_ARG_UID(uid)
403 #define	AUDIT_ARG_UPATH(td, upath, flags)
404 #define	AUDIT_ARG_VALUE(value)
405 #define	AUDIT_ARG_VNODE(vp, flags)
406 
407 #define	AUDIT_SYSCALL_ENTER(code, td)
408 #define	AUDIT_SYSCALL_EXIT(error, td)
409 
410 #define	AUDIT_SYSCLOSE(p, fd)
411 
412 #endif /* AUDIT */
413 
414 #endif /* !_SECURITY_AUDIT_KERNEL_H_ */
415