2 * Copyright (c) 1999-2009 Apple Inc.
3 * Copyright (c) 2005 Robert N. M. Watson
6 * @APPLE_BSD_LICENSE_HEADER_START@
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 * @APPLE_BSD_LICENSE_HEADER_END@
35 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
36 * support for mandatory and extensible security protections. This notice
37 * is included in support of clause 2.2 (b) of the Apple Public License,
41 #include <sys/systm.h>
42 #include <sys/sysent.h>
43 #include <sys/types.h>
44 #include <sys/proc_internal.h>
45 #include <sys/vnode_internal.h>
46 #include <sys/fcntl.h>
47 #include <sys/filedesc.h>
50 #include <bsm/audit.h>
51 #include <bsm/audit_kevents.h>
52 #include <security/audit/audit.h>
53 #include <security/audit/audit_bsd.h>
54 #include <security/audit/audit_private.h>
58 * Hash table functions for the audit event number to event class mask
61 #define EVCLASSMAP_HASH_TABLE_SIZE 251
65 LIST_ENTRY(evclass_elem
) entry
;
68 LIST_HEAD(, evclass_elem
) head
;
71 static MALLOC_DEFINE(M_AUDITEVCLASS
, "audit_evclass", "Audit event class");
72 static struct rwlock evclass_lock
;
73 static struct evclass_list evclass_hash
[EVCLASSMAP_HASH_TABLE_SIZE
];
75 #define EVCLASS_LOCK_INIT() rw_init(&evclass_lock, "evclass_lock")
76 #define EVCLASS_RLOCK() rw_rlock(&evclass_lock)
77 #define EVCLASS_RUNLOCK() rw_runlock(&evclass_lock)
78 #define EVCLASS_WLOCK() rw_wlock(&evclass_lock)
79 #define EVCLASS_WUNLOCK() rw_wunlock(&evclass_lock)
82 * Look up the class for an audit event in the class mapping table.
85 au_event_class(au_event_t event
)
87 struct evclass_list
*evcl
;
88 struct evclass_elem
*evc
;
92 evcl
= &evclass_hash
[event
% EVCLASSMAP_HASH_TABLE_SIZE
];
94 LIST_FOREACH(evc
, &evcl
->head
, entry
) {
95 if (evc
->event
== event
) {
106 * Insert a event to class mapping. If the event already exists in the
107 * mapping, then replace the mapping with the new one.
109 * XXX There is currently no constraints placed on the number of mappings.
110 * May want to either limit to a number, or in terms of memory usage.
113 au_evclassmap_insert(au_event_t event
, au_class_t
class)
115 struct evclass_list
*evcl
;
116 struct evclass_elem
*evc
, *evc_new
;
119 * If this event requires auditing a system call then add it to our
120 * audit kernel event mask. We use audit_kevent_mask to check to see
121 * if the audit syscalls flag needs to be set when preselection masks
124 if (AUE_IS_A_KEVENT(event
))
125 audit_kevent_mask
|= class;
128 * Pessimistically, always allocate storage before acquiring mutex.
129 * Free if there is already a mapping for this event.
131 evc_new
= malloc(sizeof(*evc
), M_AUDITEVCLASS
, M_WAITOK
);
134 evcl
= &evclass_hash
[event
% EVCLASSMAP_HASH_TABLE_SIZE
];
135 LIST_FOREACH(evc
, &evcl
->head
, entry
) {
136 if (evc
->event
== event
) {
139 free(evc_new
, M_AUDITEVCLASS
);
146 LIST_INSERT_HEAD(&evcl
->head
, evc
, entry
);
151 au_evclassmap_init(void)
156 for (i
= 0; i
< EVCLASSMAP_HASH_TABLE_SIZE
; i
++)
157 LIST_INIT(&evclass_hash
[i
].head
);
160 * Set up the initial event to class mapping for system calls.
162 for (i
= 0; i
< NUM_SYSENT
; i
++) {
163 if (sys_au_event
[i
] != AUE_NULL
)
164 au_evclassmap_insert(sys_au_event
[i
], 0);
169 * Add the Mach system call events. These are not in sys_au_event[].
171 au_evclassmap_insert(AUE_TASKFORPID
, 0);
172 au_evclassmap_insert(AUE_PIDFORTASK
, 0);
173 au_evclassmap_insert(AUE_SWAPON
, 0);
174 au_evclassmap_insert(AUE_SWAPOFF
, 0);
175 au_evclassmap_insert(AUE_MAPFD
, 0);
176 au_evclassmap_insert(AUE_INITPROCESS
, 0);
180 * Check whether an event is aditable by comparing the mask of classes this
181 * event is part of against the given mask.
184 au_preselect(__unused au_event_t event
, au_class_t
class, au_mask_t
*mask_p
,
187 au_class_t effmask
= 0;
193 * Perform the actual check of the masks against the event.
195 if (sorf
& AU_PRS_SUCCESS
)
196 effmask
|= (mask_p
->am_success
& class);
198 if (sorf
& AU_PRS_FAILURE
)
199 effmask
|= (mask_p
->am_failure
& class);
208 * Convert sysctl names and present arguments to events.
211 audit_ctlname_to_sysctlevent(int name
[], uint64_t valid_arg
)
214 /* can't parse it - so return the worst case */
215 if ((valid_arg
& (ARG_CTLNAME
| ARG_LEN
)) != (ARG_CTLNAME
| ARG_LEN
))
219 /* non-admin "lookups" treat them special */
229 case KERN_JOB_CONTROL
:
234 case KERN_SHREG_PRIVATIZABLE
:
236 return (AUE_SYSCTL_NONADMIN
);
238 /* only treat the changeable controls as admin */
242 case KERN_MAXPROCPERUID
:
243 case KERN_MAXFILESPERPROC
:
246 case KERN_AIOPROCMAX
:
247 case KERN_AIOTHREADS
:
249 case KERN_SUGID_COREDUMP
:
250 case KERN_NX_PROTECTION
:
251 return ((valid_arg
& ARG_VALUE32
) ?
252 AUE_SYSCTL
: AUE_SYSCTL_NONADMIN
);
261 * Convert an open flags specifier into a specific type of open event for
265 audit_flags_and_error_to_openevent(int oflags
, int error
)
270 * Need to check only those flags we care about.
272 oflags
= oflags
& (O_RDONLY
| O_CREAT
| O_TRUNC
| O_RDWR
| O_WRONLY
);
275 * These checks determine what flags are on with the condition that
276 * ONLY that combination is on, and no other flags are on.
283 case (O_RDONLY
| O_CREAT
):
284 aevent
= AUE_OPEN_RC
;
287 case (O_RDONLY
| O_CREAT
| O_TRUNC
):
288 aevent
= AUE_OPEN_RTC
;
291 case (O_RDONLY
| O_TRUNC
):
292 aevent
= AUE_OPEN_RT
;
296 aevent
= AUE_OPEN_RW
;
299 case (O_RDWR
| O_CREAT
):
300 aevent
= AUE_OPEN_RWC
;
303 case (O_RDWR
| O_CREAT
| O_TRUNC
):
304 aevent
= AUE_OPEN_RWTC
;
307 case (O_RDWR
| O_TRUNC
):
308 aevent
= AUE_OPEN_RWT
;
315 case (O_WRONLY
| O_CREAT
):
316 aevent
= AUE_OPEN_WC
;
319 case (O_WRONLY
| O_CREAT
| O_TRUNC
):
320 aevent
= AUE_OPEN_WTC
;
323 case (O_WRONLY
| O_TRUNC
):
324 aevent
= AUE_OPEN_WT
;
333 * Convert chatty errors to better matching events. Failures to
334 * find a file are really just attribute events -- so recast them as
337 * XXXAUDIT: Solaris defines that AUE_OPEN will never be returned, it
338 * is just a placeholder. However, in Darwin we return that in
339 * preference to other events.
341 * XXXRW: This behavior differs from FreeBSD, so possibly revise this
342 * code or this comment.
358 * Convert an open flags specifier into a specific type of open_extended event
359 * for auditing purposes.
362 audit_flags_and_error_to_openextendedevent(int oflags
, int error
)
367 * Need to check only those flags we care about.
369 oflags
= oflags
& (O_RDONLY
| O_CREAT
| O_TRUNC
| O_RDWR
| O_WRONLY
);
372 * These checks determine what flags are on with the condition that
373 * ONLY that combination is on, and no other flags are on.
377 aevent
= AUE_OPEN_EXTENDED_R
;
380 case (O_RDONLY
| O_CREAT
):
381 aevent
= AUE_OPEN_EXTENDED_RC
;
384 case (O_RDONLY
| O_CREAT
| O_TRUNC
):
385 aevent
= AUE_OPEN_EXTENDED_RTC
;
388 case (O_RDONLY
| O_TRUNC
):
389 aevent
= AUE_OPEN_EXTENDED_RT
;
393 aevent
= AUE_OPEN_EXTENDED_RW
;
396 case (O_RDWR
| O_CREAT
):
397 aevent
= AUE_OPEN_EXTENDED_RWC
;
400 case (O_RDWR
| O_CREAT
| O_TRUNC
):
401 aevent
= AUE_OPEN_EXTENDED_RWTC
;
404 case (O_RDWR
| O_TRUNC
):
405 aevent
= AUE_OPEN_EXTENDED_RWT
;
409 aevent
= AUE_OPEN_EXTENDED_W
;
412 case (O_WRONLY
| O_CREAT
):
413 aevent
= AUE_OPEN_EXTENDED_WC
;
416 case (O_WRONLY
| O_CREAT
| O_TRUNC
):
417 aevent
= AUE_OPEN_EXTENDED_WTC
;
420 case (O_WRONLY
| O_TRUNC
):
421 aevent
= AUE_OPEN_EXTENDED_WT
;
425 aevent
= AUE_OPEN_EXTENDED
;
430 * Convert chatty errors to better matching events. Failures to
431 * find a file are really just attribute events -- so recast them as
434 * XXXAUDIT: Solaris defines that AUE_OPEN will never be returned, it
435 * is just a placeholder. However, in Darwin we return that in
436 * preference to other events.
438 * XXXRW: This behavior differs from FreeBSD, so possibly revise this
439 * code or this comment.
442 case AUE_OPEN_EXTENDED_R
:
443 case AUE_OPEN_EXTENDED_RT
:
444 case AUE_OPEN_EXTENDED_RW
:
445 case AUE_OPEN_EXTENDED_RWT
:
446 case AUE_OPEN_EXTENDED_W
:
447 case AUE_OPEN_EXTENDED_WT
:
449 aevent
= AUE_OPEN_EXTENDED
;
455 * Convert a MSGCTL command to a specific event.
458 audit_msgctl_to_event(int cmd
)
463 return (AUE_MSGCTL_RMID
);
466 return (AUE_MSGCTL_SET
);
469 return (AUE_MSGCTL_STAT
);
472 /* We will audit a bad command. */
478 * Convert a SEMCTL command to a specific event.
481 audit_semctl_to_event(int cmd
)
486 return (AUE_SEMCTL_GETALL
);
489 return (AUE_SEMCTL_GETNCNT
);
492 return (AUE_SEMCTL_GETPID
);
495 return (AUE_SEMCTL_GETVAL
);
498 return (AUE_SEMCTL_GETZCNT
);
501 return (AUE_SEMCTL_RMID
);
504 return (AUE_SEMCTL_SET
);
507 return (AUE_SEMCTL_SETALL
);
510 return (AUE_SEMCTL_SETVAL
);
513 return (AUE_SEMCTL_STAT
);
516 /* We will audit a bad command. */
522 * Convert a command for the auditon() system call to a audit event.
525 auditon_command_event(int cmd
)
530 return (AUE_AUDITON_GPOLICY
);
533 return (AUE_AUDITON_SPOLICY
);
536 return (AUE_AUDITON_GETKMASK
);
539 return (AUE_AUDITON_SETKMASK
);
542 return (AUE_AUDITON_GQCTRL
);
545 return (AUE_AUDITON_SQCTRL
);
548 return (AUE_AUDITON_GETCWD
);
551 return (AUE_AUDITON_GETCAR
);
554 return (AUE_AUDITON_GETSTAT
);
557 return (AUE_AUDITON_SETSTAT
);
560 return (AUE_AUDITON_SETUMASK
);
563 return (AUE_AUDITON_SETSMASK
);
566 return (AUE_AUDITON_GETCOND
);
569 return (AUE_AUDITON_SETCOND
);
572 return (AUE_AUDITON_GETCLASS
);
575 return (AUE_AUDITON_SETCLASS
);
581 case A_GETPINFO_ADDR
:
584 case A_GETSINFO_ADDR
:
586 return (AUE_AUDITON
); /* No special record */
591 * For darwin we rewrite events generated by fcntl(F_OPENFROM,...) and
592 * fcntl(F_UNLINKFROM,...) system calls to AUE_OPENAT_* and AUE_UNLINKAT audit
596 audit_fcntl_command_event(int cmd
, int oflags
, int error
)
603 * Need to check only those flags we care about.
605 oflags
= oflags
& (O_RDONLY
| O_CREAT
| O_TRUNC
| O_RDWR
|
609 * These checks determine what flags are on with the condition
610 * that ONLY that combination is on, and no other flags are on.
614 aevent
= AUE_OPENAT_R
;
617 case (O_RDONLY
| O_CREAT
):
618 aevent
= AUE_OPENAT_RC
;
621 case (O_RDONLY
| O_CREAT
| O_TRUNC
):
622 aevent
= AUE_OPENAT_RTC
;
625 case (O_RDONLY
| O_TRUNC
):
626 aevent
= AUE_OPENAT_RT
;
630 aevent
= AUE_OPENAT_RW
;
633 case (O_RDWR
| O_CREAT
):
634 aevent
= AUE_OPENAT_RWC
;
637 case (O_RDWR
| O_CREAT
| O_TRUNC
):
638 aevent
= AUE_OPENAT_RWTC
;
641 case (O_RDWR
| O_TRUNC
):
642 aevent
= AUE_OPENAT_RWT
;
646 aevent
= AUE_OPENAT_W
;
649 case (O_WRONLY
| O_CREAT
):
650 aevent
= AUE_OPENAT_WC
;
653 case (O_WRONLY
| O_CREAT
| O_TRUNC
):
654 aevent
= AUE_OPENAT_WTC
;
657 case (O_WRONLY
| O_TRUNC
):
658 aevent
= AUE_OPENAT_WT
;
667 * Convert chatty errors to better matching events. Failures to
668 * find a file are really just attribute events -- so recast
685 return (AUE_UNLINKAT
);
688 return (AUE_FCNTL
); /* Don't change from AUE_FCNTL. */
693 * Create a canonical path from given path by prefixing either the root
694 * directory, or the current working directory.
697 audit_canon_path(struct vnode
*cwd_vp
, char *path
, char *cpath
)
704 * Convert multiple leading '/' into a single '/' if the cwd_vp is
705 * NULL (i.e. an absolute path), and strip them entirely if the
706 * cwd_vp represents a chroot directory (i.e. the caller checked for
707 * an initial '/' character itself, saw one, and passed fdp->fd_rdir).
708 * Somewhat complicated, but it places the onus for locking structs
709 * involved on the caller, and makes proxy operations explicit rather
712 if (*(path
) == '/') {
713 while (*(bufp
) == '/')
714 bufp
++; /* skip leading '/'s */
716 bufp
--; /* restore one '/' */
718 if (cwd_vp
!= NULL
) {
720 ret
= vn_getpath(cwd_vp
, cpath
, &len
);
725 if (len
< MAXPATHLEN
)
727 strlcpy(cpath
+ len
, bufp
, MAXPATHLEN
- len
);
729 strlcpy(cpath
, bufp
, MAXPATHLEN
);
733 #endif /* CONFIG_AUDIT */