1181053Srwatson/*-
2180701Srwatson * Copyright (c) 1999-2005 Apple Inc.
3155192Srwatson * All rights reserved.
4155192Srwatson *
5155192Srwatson * Redistribution and use in source and binary forms, with or without
6155192Srwatson * modification, are permitted provided that the following conditions
7155192Srwatson * are met:
8155192Srwatson * 1.  Redistributions of source code must retain the above copyright
9155192Srwatson *     notice, this list of conditions and the following disclaimer.
10155192Srwatson * 2.  Redistributions in binary form must reproduce the above copyright
11155192Srwatson *     notice, this list of conditions and the following disclaimer in the
12155192Srwatson *     documentation and/or other materials provided with the distribution.
13180701Srwatson * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14155192Srwatson *     its contributors may be used to endorse or promote products derived
15155192Srwatson *     from this software without specific prior written permission.
16155192Srwatson *
17155192Srwatson * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18155192Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19155192Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20155192Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21155192Srwatson * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22155192Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23155192Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24155192Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25155192Srwatson * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26155192Srwatson * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27155192Srwatson * POSSIBILITY OF SUCH DAMAGE.
28155192Srwatson *
29155192Srwatson * $FreeBSD$
30155192Srwatson */
31155192Srwatson
32155192Srwatson/*
33155192Srwatson * This header includes function prototypes and type definitions that are
34155192Srwatson * necessary for the kernel as a whole to interact with the audit subsystem.
35155192Srwatson */
36155192Srwatson
37156882Srwatson#ifndef _SECURITY_AUDIT_KERNEL_H_
38178617Srwatson#define	_SECURITY_AUDIT_KERNEL_H_
39155192Srwatson
40155192Srwatson#ifndef _KERNEL
41155192Srwatson#error "no user-serviceable parts inside"
42155192Srwatson#endif
43155192Srwatson
44155192Srwatson#include <bsm/audit.h>
45155192Srwatson
46155192Srwatson#include <sys/file.h>
47155192Srwatson#include <sys/sysctl.h>
48155192Srwatson
49155192Srwatson/*
50155192Srwatson * Audit subsystem condition flags.  The audit_enabled flag is set and
51170196Srwatson * removed automatically as a result of configuring log files, and can be
52170196Srwatson * observed but should not be directly manipulated.  The audit suspension
53170196Srwatson * flag permits audit to be temporarily disabled without reconfiguring the
54170196Srwatson * audit target.
55155192Srwatson */
56155192Srwatsonextern int	audit_enabled;
57155192Srwatsonextern int	audit_suspended;
58155192Srwatson
59156889Srwatsonvoid	 audit_syscall_enter(unsigned short code, struct thread *td);
60156889Srwatsonvoid	 audit_syscall_exit(int error, struct thread *td);
61155192Srwatson
62155192Srwatson/*
63170196Srwatson * The remaining kernel functions are conditionally compiled in as they are
64170196Srwatson * wrapped by a macro, and the macro should be the only place in the source
65170196Srwatson * tree where these functions are referenced.
66155192Srwatson */
67155192Srwatson#ifdef AUDIT
68155192Srwatsonstruct ipc_perm;
69155192Srwatsonstruct sockaddr;
70155192Srwatsonunion auditon_udata;
71156889Srwatsonvoid	 audit_arg_addr(void * addr);
72156889Srwatsonvoid	 audit_arg_exit(int status, int retval);
73156889Srwatsonvoid	 audit_arg_len(int len);
74195925Srwatsonvoid	 audit_arg_atfd1(int atfd);
75195925Srwatsonvoid	 audit_arg_atfd2(int atfd);
76156889Srwatsonvoid	 audit_arg_fd(int fd);
77156889Srwatsonvoid	 audit_arg_fflags(int fflags);
78156889Srwatsonvoid	 audit_arg_gid(gid_t gid);
79156889Srwatsonvoid	 audit_arg_uid(uid_t uid);
80156889Srwatsonvoid	 audit_arg_egid(gid_t egid);
81156889Srwatsonvoid	 audit_arg_euid(uid_t euid);
82156889Srwatsonvoid	 audit_arg_rgid(gid_t rgid);
83156889Srwatsonvoid	 audit_arg_ruid(uid_t ruid);
84156889Srwatsonvoid	 audit_arg_sgid(gid_t sgid);
85156889Srwatsonvoid	 audit_arg_suid(uid_t suid);
86156889Srwatsonvoid	 audit_arg_groupset(gid_t *gidset, u_int gidset_size);
87156889Srwatsonvoid	 audit_arg_login(char *login);
88156889Srwatsonvoid	 audit_arg_ctlname(int *name, int namelen);
89156889Srwatsonvoid	 audit_arg_mask(int mask);
90156889Srwatsonvoid	 audit_arg_mode(mode_t mode);
91156889Srwatsonvoid	 audit_arg_dev(int dev);
92156889Srwatsonvoid	 audit_arg_value(long value);
93156889Srwatsonvoid	 audit_arg_owner(uid_t uid, gid_t gid);
94156889Srwatsonvoid	 audit_arg_pid(pid_t pid);
95156889Srwatsonvoid	 audit_arg_process(struct proc *p);
96156889Srwatsonvoid	 audit_arg_signum(u_int signum);
97156889Srwatsonvoid	 audit_arg_socket(int sodomain, int sotype, int soprotocol);
98247667Spjdvoid	 audit_arg_sockaddr(struct thread *td, int dirfd, struct sockaddr *sa);
99156889Srwatsonvoid	 audit_arg_auid(uid_t auid);
100156889Srwatsonvoid	 audit_arg_auditinfo(struct auditinfo *au_info);
101171066Scsjpvoid	 audit_arg_auditinfo_addr(struct auditinfo_addr *au_info);
102243726Spjdvoid	 audit_arg_upath1(struct thread *td, int dirfd, char *upath);
103243726Spjdvoid	 audit_arg_upath2(struct thread *td, int dirfd, char *upath);
104195926Srwatsonvoid	 audit_arg_vnode1(struct vnode *vp);
105195926Srwatsonvoid	 audit_arg_vnode2(struct vnode *vp);
106156889Srwatsonvoid	 audit_arg_text(char *text);
107156889Srwatsonvoid	 audit_arg_cmd(int cmd);
108156889Srwatsonvoid	 audit_arg_svipc_cmd(int cmd);
109156889Srwatsonvoid	 audit_arg_svipc_perm(struct ipc_perm *perm);
110156889Srwatsonvoid	 audit_arg_svipc_id(int id);
111156889Srwatsonvoid	 audit_arg_svipc_addr(void *addr);
112156889Srwatsonvoid	 audit_arg_posix_ipc_perm(uid_t uid, gid_t gid, mode_t mode);
113156889Srwatsonvoid	 audit_arg_auditon(union auditon_udata *udata);
114156889Srwatsonvoid	 audit_arg_file(struct proc *p, struct file *fp);
115161813Swsalamonvoid	 audit_arg_argv(char *argv, int argc, int length);
116161813Swsalamonvoid	 audit_arg_envv(char *envv, int envc, int length);
117255219Spjdvoid	 audit_arg_rights(cap_rights_t *rightsp);
118247602Spjdvoid	 audit_arg_fcntl_rights(uint32_t fcntlrights);
119156889Srwatsonvoid	 audit_sysclose(struct thread *td, int fd);
120170407Srwatsonvoid	 audit_cred_copy(struct ucred *src, struct ucred *dest);
121170407Srwatsonvoid	 audit_cred_destroy(struct ucred *cred);
122170407Srwatsonvoid	 audit_cred_init(struct ucred *cred);
123170407Srwatsonvoid	 audit_cred_kproc0(struct ucred *cred);
124170407Srwatsonvoid	 audit_cred_proc1(struct ucred *cred);
125172995Scsjpvoid	 audit_proc_coredump(struct thread *td, char *path, int errcode);
126156889Srwatsonvoid	 audit_thread_alloc(struct thread *td);
127156889Srwatsonvoid	 audit_thread_free(struct thread *td);
128155192Srwatson
129155192Srwatson/*
130195104Srwatson * Define macros to wrap the audit_arg_* calls by checking the global
131155192Srwatson * audit_enabled flag before performing the actual call.
132155192Srwatson */
133195104Srwatson#define	AUDITING_TD(td)		((td)->td_pflags & TDP_AUDITREC)
134195104Srwatson
135195104Srwatson#define	AUDIT_ARG_ADDR(addr) do {					\
136195104Srwatson	if (AUDITING_TD(curthread))					\
137195104Srwatson		audit_arg_addr((addr));					\
138156889Srwatson} while (0)
139155192Srwatson
140195104Srwatson#define	AUDIT_ARG_ARGV(argv, argc, length) do {				\
141195104Srwatson	if (AUDITING_TD(curthread))					\
142195104Srwatson		audit_arg_argv((argv), (argc), (length));		\
143195104Srwatson} while (0)
144195104Srwatson
145195925Srwatson#define	AUDIT_ARG_ATFD1(atfd) do {					\
146195925Srwatson	if (AUDITING_TD(curthread))					\
147195925Srwatson		audit_arg_atfd1((atfd));				\
148195925Srwatson} while (0)
149195925Srwatson
150195925Srwatson#define	AUDIT_ARG_ATFD2(atfd) do {					\
151195925Srwatson	if (AUDITING_TD(curthread))					\
152195925Srwatson		audit_arg_atfd2((atfd));				\
153195925Srwatson} while (0)
154195925Srwatson
155195104Srwatson#define	AUDIT_ARG_AUDITON(udata) do {					\
156195104Srwatson	if (AUDITING_TD(curthread))					\
157195104Srwatson		audit_arg_auditon((udata));				\
158195104Srwatson} while (0)
159195104Srwatson
160195104Srwatson#define	AUDIT_ARG_CMD(cmd) do {						\
161195104Srwatson	if (AUDITING_TD(curthread))					\
162195104Srwatson		audit_arg_cmd((cmd));					\
163195104Srwatson} while (0)
164195104Srwatson
165195104Srwatson#define	AUDIT_ARG_DEV(dev) do {						\
166195104Srwatson	if (AUDITING_TD(curthread))					\
167195104Srwatson		audit_arg_dev((dev));					\
168195104Srwatson} while (0)
169195104Srwatson
170195104Srwatson#define	AUDIT_ARG_EGID(egid) do {					\
171195104Srwatson	if (AUDITING_TD(curthread))					\
172195104Srwatson		audit_arg_egid((egid));					\
173195104Srwatson} while (0)
174195104Srwatson
175195104Srwatson#define	AUDIT_ARG_ENVV(envv, envc, length) do {				\
176195104Srwatson	if (AUDITING_TD(curthread))					\
177195104Srwatson		audit_arg_envv((envv), (envc), (length));		\
178195104Srwatson} while (0)
179195104Srwatson
180195104Srwatson#define	AUDIT_ARG_EXIT(status, retval) do {				\
181195104Srwatson	if (AUDITING_TD(curthread))					\
182195104Srwatson		audit_arg_exit((status), (retval));			\
183195104Srwatson} while (0)
184195104Srwatson
185195104Srwatson#define	AUDIT_ARG_EUID(euid) do {					\
186195104Srwatson	if (AUDITING_TD(curthread))					\
187195104Srwatson		audit_arg_euid((euid));					\
188195104Srwatson} while (0)
189195104Srwatson
190195104Srwatson#define	AUDIT_ARG_FD(fd) do {						\
191195104Srwatson	if (AUDITING_TD(curthread))					\
192195104Srwatson		audit_arg_fd((fd));					\
193195104Srwatson} while (0)
194195104Srwatson
195195104Srwatson#define	AUDIT_ARG_FILE(p, fp) do {					\
196195104Srwatson	if (AUDITING_TD(curthread))					\
197195104Srwatson		audit_arg_file((p), (fp));				\
198195104Srwatson} while (0)
199195104Srwatson
200195104Srwatson#define	AUDIT_ARG_FFLAGS(fflags) do {					\
201195104Srwatson	if (AUDITING_TD(curthread))					\
202195104Srwatson		audit_arg_fflags((fflags));				\
203195104Srwatson} while (0)
204195104Srwatson
205195104Srwatson#define	AUDIT_ARG_GID(gid) do {						\
206195104Srwatson	if (AUDITING_TD(curthread))					\
207195104Srwatson		audit_arg_gid((gid));					\
208195104Srwatson} while (0)
209195104Srwatson
210195104Srwatson#define	AUDIT_ARG_GROUPSET(gidset, gidset_size) do {			\
211195104Srwatson	if (AUDITING_TD(curthread))					\
212195104Srwatson		audit_arg_groupset((gidset), (gidset_size));		\
213195104Srwatson} while (0)
214195104Srwatson
215195104Srwatson#define	AUDIT_ARG_MODE(mode) do {					\
216195104Srwatson	if (AUDITING_TD(curthread))					\
217195104Srwatson		audit_arg_mode((mode));					\
218195104Srwatson} while (0)
219195104Srwatson
220195104Srwatson#define	AUDIT_ARG_OWNER(uid, gid) do {					\
221195104Srwatson	if (AUDITING_TD(curthread))					\
222195104Srwatson		audit_arg_owner((uid), (gid));				\
223195104Srwatson} while (0)
224195104Srwatson
225195104Srwatson#define	AUDIT_ARG_PID(pid) do {						\
226195104Srwatson	if (AUDITING_TD(curthread))					\
227195104Srwatson		audit_arg_pid((pid));					\
228195104Srwatson} while (0)
229195104Srwatson
230195104Srwatson#define	AUDIT_ARG_PROCESS(p) do {					\
231195104Srwatson	if (AUDITING_TD(curthread))					\
232195104Srwatson		audit_arg_process((p));					\
233195104Srwatson} while (0)
234195104Srwatson
235195104Srwatson#define	AUDIT_ARG_RGID(rgid) do {					\
236195104Srwatson	if (AUDITING_TD(curthread))					\
237196122Srwatson		audit_arg_rgid((rgid));					\
238195104Srwatson} while (0)
239195104Srwatson
240224181Sjonathan#define	AUDIT_ARG_RIGHTS(rights) do {					\
241224181Sjonathan	if (AUDITING_TD(curthread))					\
242224181Sjonathan		audit_arg_rights((rights));				\
243224181Sjonathan} while (0)
244224181Sjonathan
245247602Spjd#define	AUDIT_ARG_FCNTL_RIGHTS(fcntlrights) do {			\
246247602Spjd	if (AUDITING_TD(curthread))					\
247247602Spjd		audit_arg_fcntl_rights((fcntlrights));			\
248247602Spjd} while (0)
249247602Spjd
250195104Srwatson#define	AUDIT_ARG_RUID(ruid) do {					\
251195104Srwatson	if (AUDITING_TD(curthread))					\
252195104Srwatson		audit_arg_ruid((ruid));					\
253195104Srwatson} while (0)
254195104Srwatson
255195104Srwatson#define	AUDIT_ARG_SIGNUM(signum) do {					\
256195104Srwatson	if (AUDITING_TD(curthread))					\
257195104Srwatson		audit_arg_signum((signum));				\
258195104Srwatson} while (0)
259195104Srwatson
260195104Srwatson#define	AUDIT_ARG_SGID(sgid) do {					\
261195104Srwatson	if (AUDITING_TD(curthread))					\
262195104Srwatson		audit_arg_sgid((sgid));					\
263195104Srwatson} while (0)
264195104Srwatson
265195252Srwatson#define	AUDIT_ARG_SOCKET(sodomain, sotype, soprotocol) do {		\
266195252Srwatson	if (AUDITING_TD(curthread))					\
267195252Srwatson		audit_arg_socket((sodomain), (sotype), (soprotocol));	\
268195252Srwatson} while (0)
269195252Srwatson
270247667Spjd#define	AUDIT_ARG_SOCKADDR(td, dirfd, sa) do {				\
271246446Spjd	if (AUDITING_TD(curthread))					\
272247667Spjd		audit_arg_sockaddr((td), (dirfd), (sa));		\
273246446Spjd} while (0)
274246446Spjd
275195104Srwatson#define	AUDIT_ARG_SUID(suid) do {					\
276195104Srwatson	if (AUDITING_TD(curthread))					\
277195104Srwatson		audit_arg_suid((suid));					\
278195104Srwatson} while (0)
279195104Srwatson
280195104Srwatson#define	AUDIT_ARG_TEXT(text) do {					\
281195104Srwatson	if (AUDITING_TD(curthread))					\
282195104Srwatson		audit_arg_text((text));					\
283195104Srwatson} while (0)
284195104Srwatson
285195104Srwatson#define	AUDIT_ARG_UID(uid) do {						\
286195104Srwatson	if (AUDITING_TD(curthread))					\
287195104Srwatson		audit_arg_uid((uid));					\
288195104Srwatson} while (0)
289195104Srwatson
290243726Spjd#define	AUDIT_ARG_UPATH1(td, dirfd, upath) do {				\
291195104Srwatson	if (AUDITING_TD(curthread))					\
292243726Spjd		audit_arg_upath1((td), (dirfd), (upath));		\
293195104Srwatson} while (0)
294195104Srwatson
295243726Spjd#define	AUDIT_ARG_UPATH2(td, dirfd, upath) do {				\
296195939Srwatson	if (AUDITING_TD(curthread))					\
297243726Spjd		audit_arg_upath2((td), (dirfd), (upath));		\
298195939Srwatson} while (0)
299195939Srwatson
300195104Srwatson#define	AUDIT_ARG_VALUE(value) do {					\
301195104Srwatson	if (AUDITING_TD(curthread))					\
302195104Srwatson		audit_arg_value((value));				\
303195104Srwatson} while (0)
304195104Srwatson
305195926Srwatson#define	AUDIT_ARG_VNODE1(vp) do {					\
306195104Srwatson	if (AUDITING_TD(curthread))					\
307195926Srwatson		audit_arg_vnode1((vp));					\
308195104Srwatson} while (0)
309195104Srwatson
310195926Srwatson#define	AUDIT_ARG_VNODE2(vp) do {					\
311195926Srwatson	if (AUDITING_TD(curthread))					\
312195926Srwatson		audit_arg_vnode2((vp));					\
313195926Srwatson} while (0)
314195926Srwatson
315161970Srwatson#define	AUDIT_SYSCALL_ENTER(code, td)	do {				\
316155192Srwatson	if (audit_enabled) {						\
317155192Srwatson		audit_syscall_enter(code, td);				\
318155192Srwatson	}								\
319156889Srwatson} while (0)
320155192Srwatson
321155192Srwatson/*
322155192Srwatson * Wrap the audit_syscall_exit() function so that it is called only when
323186822Srwatson * we have a audit record on the thread.  Audit records can persist after
324186822Srwatson * auditing is disabled, so we don't just check audit_enabled here.
325155192Srwatson */
326161970Srwatson#define	AUDIT_SYSCALL_EXIT(error, td)	do {				\
327189570Srwatson	if (td->td_pflags & TDP_AUDITREC)				\
328155192Srwatson		audit_syscall_exit(error, td);				\
329156889Srwatson} while (0)
330155192Srwatson
331155192Srwatson/*
332155192Srwatson * A Macro to wrap the audit_sysclose() function.
333155192Srwatson */
334155192Srwatson#define	AUDIT_SYSCLOSE(td, fd)	do {					\
335189570Srwatson	if (td->td_pflags & TDP_AUDITREC)				\
336155192Srwatson		audit_sysclose(td, fd);					\
337156889Srwatson} while (0)
338155192Srwatson
339155192Srwatson#else /* !AUDIT */
340155192Srwatson
341195104Srwatson#define	AUDIT_ARG_ADDR(addr)
342195104Srwatson#define	AUDIT_ARG_ARGV(argv, argc, length)
343195925Srwatson#define	AUDIT_ARG_ATFD1(atfd)
344195925Srwatson#define	AUDIT_ARG_ATFD2(atfd)
345195104Srwatson#define	AUDIT_ARG_AUDITON(udata)
346195104Srwatson#define	AUDIT_ARG_CMD(cmd)
347195104Srwatson#define	AUDIT_ARG_DEV(dev)
348195104Srwatson#define	AUDIT_ARG_EGID(egid)
349195104Srwatson#define	AUDIT_ARG_ENVV(envv, envc, length)
350195104Srwatson#define	AUDIT_ARG_EXIT(status, retval)
351195104Srwatson#define	AUDIT_ARG_EUID(euid)
352195104Srwatson#define	AUDIT_ARG_FD(fd)
353195104Srwatson#define	AUDIT_ARG_FILE(p, fp)
354195104Srwatson#define	AUDIT_ARG_FFLAGS(fflags)
355195104Srwatson#define	AUDIT_ARG_GID(gid)
356195104Srwatson#define	AUDIT_ARG_GROUPSET(gidset, gidset_size)
357195104Srwatson#define	AUDIT_ARG_MODE(mode)
358195104Srwatson#define	AUDIT_ARG_OWNER(uid, gid)
359195104Srwatson#define	AUDIT_ARG_PID(pid)
360195104Srwatson#define	AUDIT_ARG_PROCESS(p)
361195104Srwatson#define	AUDIT_ARG_RGID(rgid)
362224181Sjonathan#define	AUDIT_ARG_RIGHTS(rights)
363247602Spjd#define	AUDIT_ARG_FCNTL_RIGHTS(fcntlrights)
364195104Srwatson#define	AUDIT_ARG_RUID(ruid)
365195104Srwatson#define	AUDIT_ARG_SIGNUM(signum)
366195104Srwatson#define	AUDIT_ARG_SGID(sgid)
367195252Srwatson#define	AUDIT_ARG_SOCKET(sodomain, sotype, soprotocol)
368247667Spjd#define	AUDIT_ARG_SOCKADDR(td, dirfd, sa)
369195104Srwatson#define	AUDIT_ARG_SUID(suid)
370195104Srwatson#define	AUDIT_ARG_TEXT(text)
371195104Srwatson#define	AUDIT_ARG_UID(uid)
372243726Spjd#define	AUDIT_ARG_UPATH1(td, dirfd, upath)
373243726Spjd#define	AUDIT_ARG_UPATH2(td, dirfd, upath)
374195104Srwatson#define	AUDIT_ARG_VALUE(value)
375195926Srwatson#define	AUDIT_ARG_VNODE1(vp)
376195926Srwatson#define	AUDIT_ARG_VNODE2(vp)
377155192Srwatson
378195104Srwatson#define	AUDIT_SYSCALL_ENTER(code, td)
379195104Srwatson#define	AUDIT_SYSCALL_EXIT(error, td)
380155192Srwatson
381195104Srwatson#define	AUDIT_SYSCLOSE(p, fd)
382155192Srwatson
383155192Srwatson#endif /* AUDIT */
384155192Srwatson
385156882Srwatson#endif /* !_SECURITY_AUDIT_KERNEL_H_ */
386