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 */
56extern int	audit_enabled;
57extern int	audit_suspended;
58
59void	 audit_syscall_enter(unsigned short code, struct thread *td);
60void	 audit_syscall_exit(int error, struct thread *td);
61
62/*
63 * The remaining kernel functions are conditionally compiled in as they are
64 * wrapped by a macro, and the macro should be the only place in the source
65 * tree where these functions are referenced.
66 */
67#ifdef AUDIT
68struct ipc_perm;
69struct sockaddr;
70union auditon_udata;
71void	 audit_arg_addr(void * addr);
72void	 audit_arg_exit(int status, int retval);
73void	 audit_arg_len(int len);
74void	 audit_arg_atfd1(int atfd);
75void	 audit_arg_atfd2(int atfd);
76void	 audit_arg_fd(int fd);
77void	 audit_arg_fflags(int fflags);
78void	 audit_arg_gid(gid_t gid);
79void	 audit_arg_uid(uid_t uid);
80void	 audit_arg_egid(gid_t egid);
81void	 audit_arg_euid(uid_t euid);
82void	 audit_arg_rgid(gid_t rgid);
83void	 audit_arg_ruid(uid_t ruid);
84void	 audit_arg_sgid(gid_t sgid);
85void	 audit_arg_suid(uid_t suid);
86void	 audit_arg_groupset(gid_t *gidset, u_int gidset_size);
87void	 audit_arg_login(char *login);
88void	 audit_arg_ctlname(int *name, int namelen);
89void	 audit_arg_mask(int mask);
90void	 audit_arg_mode(mode_t mode);
91void	 audit_arg_dev(int dev);
92void	 audit_arg_value(long value);
93void	 audit_arg_owner(uid_t uid, gid_t gid);
94void	 audit_arg_pid(pid_t pid);
95void	 audit_arg_process(struct proc *p);
96void	 audit_arg_signum(u_int signum);
97void	 audit_arg_socket(int sodomain, int sotype, int soprotocol);
98void	 audit_arg_sockaddr(struct thread *td, int dirfd, struct sockaddr *sa);
99void	 audit_arg_auid(uid_t auid);
100void	 audit_arg_auditinfo(struct auditinfo *au_info);
101void	 audit_arg_auditinfo_addr(struct auditinfo_addr *au_info);
102void	 audit_arg_upath1(struct thread *td, int dirfd, char *upath);
103void	 audit_arg_upath2(struct thread *td, int dirfd, char *upath);
104void	 audit_arg_vnode1(struct vnode *vp);
105void	 audit_arg_vnode2(struct vnode *vp);
106void	 audit_arg_text(char *text);
107void	 audit_arg_cmd(int cmd);
108void	 audit_arg_svipc_cmd(int cmd);
109void	 audit_arg_svipc_perm(struct ipc_perm *perm);
110void	 audit_arg_svipc_id(int id);
111void	 audit_arg_svipc_addr(void *addr);
112void	 audit_arg_posix_ipc_perm(uid_t uid, gid_t gid, mode_t mode);
113void	 audit_arg_auditon(union auditon_udata *udata);
114void	 audit_arg_file(struct proc *p, struct file *fp);
115void	 audit_arg_argv(char *argv, int argc, int length);
116void	 audit_arg_envv(char *envv, int envc, int length);
117void	 audit_arg_rights(cap_rights_t *rightsp);
118void	 audit_arg_fcntl_rights(uint32_t fcntlrights);
119void	 audit_sysclose(struct thread *td, int fd);
120void	 audit_cred_copy(struct ucred *src, struct ucred *dest);
121void	 audit_cred_destroy(struct ucred *cred);
122void	 audit_cred_init(struct ucred *cred);
123void	 audit_cred_kproc0(struct ucred *cred);
124void	 audit_cred_proc1(struct ucred *cred);
125void	 audit_proc_coredump(struct thread *td, char *path, int errcode);
126void	 audit_thread_alloc(struct thread *td);
127void	 audit_thread_free(struct thread *td);
128
129/*
130 * Define macros to wrap the audit_arg_* calls by checking the global
131 * audit_enabled flag before performing the actual call.
132 */
133#define	AUDITING_TD(td)		((td)->td_pflags & TDP_AUDITREC)
134
135#define	AUDIT_ARG_ADDR(addr) do {					\
136	if (AUDITING_TD(curthread))					\
137		audit_arg_addr((addr));					\
138} while (0)
139
140#define	AUDIT_ARG_ARGV(argv, argc, length) do {				\
141	if (AUDITING_TD(curthread))					\
142		audit_arg_argv((argv), (argc), (length));		\
143} while (0)
144
145#define	AUDIT_ARG_ATFD1(atfd) do {					\
146	if (AUDITING_TD(curthread))					\
147		audit_arg_atfd1((atfd));				\
148} while (0)
149
150#define	AUDIT_ARG_ATFD2(atfd) do {					\
151	if (AUDITING_TD(curthread))					\
152		audit_arg_atfd2((atfd));				\
153} while (0)
154
155#define	AUDIT_ARG_AUDITON(udata) do {					\
156	if (AUDITING_TD(curthread))					\
157		audit_arg_auditon((udata));				\
158} while (0)
159
160#define	AUDIT_ARG_CMD(cmd) do {						\
161	if (AUDITING_TD(curthread))					\
162		audit_arg_cmd((cmd));					\
163} while (0)
164
165#define	AUDIT_ARG_DEV(dev) do {						\
166	if (AUDITING_TD(curthread))					\
167		audit_arg_dev((dev));					\
168} while (0)
169
170#define	AUDIT_ARG_EGID(egid) do {					\
171	if (AUDITING_TD(curthread))					\
172		audit_arg_egid((egid));					\
173} while (0)
174
175#define	AUDIT_ARG_ENVV(envv, envc, length) do {				\
176	if (AUDITING_TD(curthread))					\
177		audit_arg_envv((envv), (envc), (length));		\
178} while (0)
179
180#define	AUDIT_ARG_EXIT(status, retval) do {				\
181	if (AUDITING_TD(curthread))					\
182		audit_arg_exit((status), (retval));			\
183} while (0)
184
185#define	AUDIT_ARG_EUID(euid) do {					\
186	if (AUDITING_TD(curthread))					\
187		audit_arg_euid((euid));					\
188} while (0)
189
190#define	AUDIT_ARG_FD(fd) do {						\
191	if (AUDITING_TD(curthread))					\
192		audit_arg_fd((fd));					\
193} while (0)
194
195#define	AUDIT_ARG_FILE(p, fp) do {					\
196	if (AUDITING_TD(curthread))					\
197		audit_arg_file((p), (fp));				\
198} while (0)
199
200#define	AUDIT_ARG_FFLAGS(fflags) do {					\
201	if (AUDITING_TD(curthread))					\
202		audit_arg_fflags((fflags));				\
203} while (0)
204
205#define	AUDIT_ARG_GID(gid) do {						\
206	if (AUDITING_TD(curthread))					\
207		audit_arg_gid((gid));					\
208} while (0)
209
210#define	AUDIT_ARG_GROUPSET(gidset, gidset_size) do {			\
211	if (AUDITING_TD(curthread))					\
212		audit_arg_groupset((gidset), (gidset_size));		\
213} while (0)
214
215#define	AUDIT_ARG_MODE(mode) do {					\
216	if (AUDITING_TD(curthread))					\
217		audit_arg_mode((mode));					\
218} while (0)
219
220#define	AUDIT_ARG_OWNER(uid, gid) do {					\
221	if (AUDITING_TD(curthread))					\
222		audit_arg_owner((uid), (gid));				\
223} while (0)
224
225#define	AUDIT_ARG_PID(pid) do {						\
226	if (AUDITING_TD(curthread))					\
227		audit_arg_pid((pid));					\
228} while (0)
229
230#define	AUDIT_ARG_PROCESS(p) do {					\
231	if (AUDITING_TD(curthread))					\
232		audit_arg_process((p));					\
233} while (0)
234
235#define	AUDIT_ARG_RGID(rgid) do {					\
236	if (AUDITING_TD(curthread))					\
237		audit_arg_rgid((rgid));					\
238} while (0)
239
240#define	AUDIT_ARG_RIGHTS(rights) do {					\
241	if (AUDITING_TD(curthread))					\
242		audit_arg_rights((rights));				\
243} while (0)
244
245#define	AUDIT_ARG_FCNTL_RIGHTS(fcntlrights) do {			\
246	if (AUDITING_TD(curthread))					\
247		audit_arg_fcntl_rights((fcntlrights));			\
248} while (0)
249
250#define	AUDIT_ARG_RUID(ruid) do {					\
251	if (AUDITING_TD(curthread))					\
252		audit_arg_ruid((ruid));					\
253} while (0)
254
255#define	AUDIT_ARG_SIGNUM(signum) do {					\
256	if (AUDITING_TD(curthread))					\
257		audit_arg_signum((signum));				\
258} while (0)
259
260#define	AUDIT_ARG_SGID(sgid) do {					\
261	if (AUDITING_TD(curthread))					\
262		audit_arg_sgid((sgid));					\
263} while (0)
264
265#define	AUDIT_ARG_SOCKET(sodomain, sotype, soprotocol) do {		\
266	if (AUDITING_TD(curthread))					\
267		audit_arg_socket((sodomain), (sotype), (soprotocol));	\
268} while (0)
269
270#define	AUDIT_ARG_SOCKADDR(td, dirfd, sa) do {				\
271	if (AUDITING_TD(curthread))					\
272		audit_arg_sockaddr((td), (dirfd), (sa));		\
273} while (0)
274
275#define	AUDIT_ARG_SUID(suid) do {					\
276	if (AUDITING_TD(curthread))					\
277		audit_arg_suid((suid));					\
278} while (0)
279
280#define	AUDIT_ARG_TEXT(text) do {					\
281	if (AUDITING_TD(curthread))					\
282		audit_arg_text((text));					\
283} while (0)
284
285#define	AUDIT_ARG_UID(uid) do {						\
286	if (AUDITING_TD(curthread))					\
287		audit_arg_uid((uid));					\
288} while (0)
289
290#define	AUDIT_ARG_UPATH1(td, dirfd, upath) do {				\
291	if (AUDITING_TD(curthread))					\
292		audit_arg_upath1((td), (dirfd), (upath));		\
293} while (0)
294
295#define	AUDIT_ARG_UPATH2(td, dirfd, upath) do {				\
296	if (AUDITING_TD(curthread))					\
297		audit_arg_upath2((td), (dirfd), (upath));		\
298} while (0)
299
300#define	AUDIT_ARG_VALUE(value) do {					\
301	if (AUDITING_TD(curthread))					\
302		audit_arg_value((value));				\
303} while (0)
304
305#define	AUDIT_ARG_VNODE1(vp) do {					\
306	if (AUDITING_TD(curthread))					\
307		audit_arg_vnode1((vp));					\
308} while (0)
309
310#define	AUDIT_ARG_VNODE2(vp) do {					\
311	if (AUDITING_TD(curthread))					\
312		audit_arg_vnode2((vp));					\
313} while (0)
314
315#define	AUDIT_SYSCALL_ENTER(code, td)	do {				\
316	if (audit_enabled) {						\
317		audit_syscall_enter(code, td);				\
318	}								\
319} while (0)
320
321/*
322 * Wrap the audit_syscall_exit() function so that it is called only when
323 * we have a audit record on the thread.  Audit records can persist after
324 * auditing is disabled, so we don't just check audit_enabled here.
325 */
326#define	AUDIT_SYSCALL_EXIT(error, td)	do {				\
327	if (td->td_pflags & TDP_AUDITREC)				\
328		audit_syscall_exit(error, td);				\
329} while (0)
330
331/*
332 * A Macro to wrap the audit_sysclose() function.
333 */
334#define	AUDIT_SYSCLOSE(td, fd)	do {					\
335	if (td->td_pflags & TDP_AUDITREC)				\
336		audit_sysclose(td, fd);					\
337} while (0)
338
339#else /* !AUDIT */
340
341#define	AUDIT_ARG_ADDR(addr)
342#define	AUDIT_ARG_ARGV(argv, argc, length)
343#define	AUDIT_ARG_ATFD1(atfd)
344#define	AUDIT_ARG_ATFD2(atfd)
345#define	AUDIT_ARG_AUDITON(udata)
346#define	AUDIT_ARG_CMD(cmd)
347#define	AUDIT_ARG_DEV(dev)
348#define	AUDIT_ARG_EGID(egid)
349#define	AUDIT_ARG_ENVV(envv, envc, length)
350#define	AUDIT_ARG_EXIT(status, retval)
351#define	AUDIT_ARG_EUID(euid)
352#define	AUDIT_ARG_FD(fd)
353#define	AUDIT_ARG_FILE(p, fp)
354#define	AUDIT_ARG_FFLAGS(fflags)
355#define	AUDIT_ARG_GID(gid)
356#define	AUDIT_ARG_GROUPSET(gidset, gidset_size)
357#define	AUDIT_ARG_MODE(mode)
358#define	AUDIT_ARG_OWNER(uid, gid)
359#define	AUDIT_ARG_PID(pid)
360#define	AUDIT_ARG_PROCESS(p)
361#define	AUDIT_ARG_RGID(rgid)
362#define	AUDIT_ARG_RIGHTS(rights)
363#define	AUDIT_ARG_FCNTL_RIGHTS(fcntlrights)
364#define	AUDIT_ARG_RUID(ruid)
365#define	AUDIT_ARG_SIGNUM(signum)
366#define	AUDIT_ARG_SGID(sgid)
367#define	AUDIT_ARG_SOCKET(sodomain, sotype, soprotocol)
368#define	AUDIT_ARG_SOCKADDR(td, dirfd, sa)
369#define	AUDIT_ARG_SUID(suid)
370#define	AUDIT_ARG_TEXT(text)
371#define	AUDIT_ARG_UID(uid)
372#define	AUDIT_ARG_UPATH1(td, dirfd, upath)
373#define	AUDIT_ARG_UPATH2(td, dirfd, upath)
374#define	AUDIT_ARG_VALUE(value)
375#define	AUDIT_ARG_VNODE1(vp)
376#define	AUDIT_ARG_VNODE2(vp)
377
378#define	AUDIT_SYSCALL_ENTER(code, td)
379#define	AUDIT_SYSCALL_EXIT(error, td)
380
381#define	AUDIT_SYSCLOSE(p, fd)
382
383#endif /* AUDIT */
384
385#endif /* !_SECURITY_AUDIT_KERNEL_H_ */
386