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 an open flags specifier into a specific type of open_extended event
456 * for auditing purposes.
459 audit_flags_and_error_to_openatevent(int oflags
, int error
)
464 * Need to check only those flags we care about.
466 oflags
= oflags
& (O_RDONLY
| O_CREAT
| O_TRUNC
| O_RDWR
| O_WRONLY
);
469 * These checks determine what flags are on with the condition that
470 * ONLY that combination is on, and no other flags are on.
474 aevent
= AUE_OPENAT_R
;
477 case (O_RDONLY
| O_CREAT
):
478 aevent
= AUE_OPENAT_RC
;
481 case (O_RDONLY
| O_CREAT
| O_TRUNC
):
482 aevent
= AUE_OPENAT_RTC
;
485 case (O_RDONLY
| O_TRUNC
):
486 aevent
= AUE_OPENAT_RT
;
490 aevent
= AUE_OPENAT_RW
;
493 case (O_RDWR
| O_CREAT
):
494 aevent
= AUE_OPENAT_RWC
;
497 case (O_RDWR
| O_CREAT
| O_TRUNC
):
498 aevent
= AUE_OPENAT_RWTC
;
501 case (O_RDWR
| O_TRUNC
):
502 aevent
= AUE_OPENAT_RWT
;
506 aevent
= AUE_OPENAT_W
;
509 case (O_WRONLY
| O_CREAT
):
510 aevent
= AUE_OPENAT_WC
;
513 case (O_WRONLY
| O_CREAT
| O_TRUNC
):
514 aevent
= AUE_OPENAT_WTC
;
517 case (O_WRONLY
| O_TRUNC
):
518 aevent
= AUE_OPENAT_WT
;
527 * Convert chatty errors to better matching events. Failures to
528 * find a file are really just attribute events -- so recast them as
531 * XXXAUDIT: Solaris defines that AUE_OPENAT will never be returned, it
532 * is just a placeholder. However, in Darwin we return that in
533 * preference to other events.
535 * XXXRW: This behavior differs from FreeBSD, so possibly revise this
536 * code or this comment.
552 * Convert an open flags specifier into a specific type of openbyid event
553 * for auditing purposes.
556 audit_flags_and_error_to_openbyidevent(int oflags
, int error
)
561 * Need to check only those flags we care about.
563 oflags
= oflags
& (O_RDONLY
| O_TRUNC
| O_RDWR
| O_WRONLY
);
566 * These checks determine what flags are on with the condition that
567 * ONLY that combination is on, and no other flags are on.
571 aevent
= AUE_OPENBYID_R
;
574 case (O_RDONLY
| O_TRUNC
):
575 aevent
= AUE_OPENBYID_RT
;
579 aevent
= AUE_OPENBYID_RW
;
582 case (O_RDWR
| O_TRUNC
):
583 aevent
= AUE_OPENBYID_RWT
;
587 aevent
= AUE_OPENBYID_W
;
590 case (O_WRONLY
| O_TRUNC
):
591 aevent
= AUE_OPENBYID_WT
;
595 aevent
= AUE_OPENBYID
;
600 * Convert chatty errors to better matching events. Failures to
601 * find a file are really just attribute events -- so recast them as
606 case AUE_OPENBYID_RT
:
607 case AUE_OPENBYID_RW
:
608 case AUE_OPENBYID_RWT
:
610 case AUE_OPENBYID_WT
:
612 aevent
= AUE_OPENBYID
;
618 * Convert a MSGCTL command to a specific event.
621 audit_msgctl_to_event(int cmd
)
626 return (AUE_MSGCTL_RMID
);
629 return (AUE_MSGCTL_SET
);
632 return (AUE_MSGCTL_STAT
);
635 /* We will audit a bad command. */
641 * Convert a SEMCTL command to a specific event.
644 audit_semctl_to_event(int cmd
)
649 return (AUE_SEMCTL_GETALL
);
652 return (AUE_SEMCTL_GETNCNT
);
655 return (AUE_SEMCTL_GETPID
);
658 return (AUE_SEMCTL_GETVAL
);
661 return (AUE_SEMCTL_GETZCNT
);
664 return (AUE_SEMCTL_RMID
);
667 return (AUE_SEMCTL_SET
);
670 return (AUE_SEMCTL_SETALL
);
673 return (AUE_SEMCTL_SETVAL
);
676 return (AUE_SEMCTL_STAT
);
679 /* We will audit a bad command. */
685 * Convert a command for the auditon() system call to a audit event.
688 auditon_command_event(int cmd
)
693 return (AUE_AUDITON_GPOLICY
);
696 return (AUE_AUDITON_SPOLICY
);
699 return (AUE_AUDITON_GETKMASK
);
702 return (AUE_AUDITON_SETKMASK
);
705 return (AUE_AUDITON_GQCTRL
);
708 return (AUE_AUDITON_SQCTRL
);
711 return (AUE_AUDITON_GETCWD
);
714 return (AUE_AUDITON_GETCAR
);
717 return (AUE_AUDITON_GETSTAT
);
720 return (AUE_AUDITON_SETSTAT
);
723 return (AUE_AUDITON_SETUMASK
);
726 return (AUE_AUDITON_SETSMASK
);
729 return (AUE_AUDITON_GETCOND
);
732 return (AUE_AUDITON_SETCOND
);
735 return (AUE_AUDITON_GETCLASS
);
738 return (AUE_AUDITON_SETCLASS
);
744 case A_GETPINFO_ADDR
:
747 case A_GETSINFO_ADDR
:
749 return (AUE_AUDITON
); /* No special record */
754 * For darwin we rewrite events generated by fcntl(F_OPENFROM,...) and
755 * fcntl(F_UNLINKFROM,...) system calls to AUE_OPENAT_* and AUE_UNLINKAT audit
759 audit_fcntl_command_event(int cmd
, int oflags
, int error
)
763 return (audit_flags_and_error_to_openatevent(oflags
, error
));
766 return (AUE_UNLINKAT
);
769 return (AUE_FCNTL
); /* Don't change from AUE_FCNTL. */
774 * Create a canonical path from given path by prefixing either the root
775 * directory, or the current working directory.
778 audit_canon_path(struct vnode
*cwd_vp
, char *path
, char *cpath
)
785 * Convert multiple leading '/' into a single '/' if the cwd_vp is
786 * NULL (i.e. an absolute path), and strip them entirely if the
787 * cwd_vp represents a chroot directory (i.e. the caller checked for
788 * an initial '/' character itself, saw one, and passed fdp->fd_rdir).
789 * Somewhat complicated, but it places the onus for locking structs
790 * involved on the caller, and makes proxy operations explicit rather
793 if (*(path
) == '/') {
794 while (*(bufp
) == '/')
795 bufp
++; /* skip leading '/'s */
797 bufp
--; /* restore one '/' */
799 if (cwd_vp
!= NULL
) {
801 ret
= vn_getpath(cwd_vp
, cpath
, &len
);
806 if (len
< MAXPATHLEN
)
808 strlcpy(cpath
+ len
, bufp
, MAXPATHLEN
- len
);
810 strlcpy(cpath
, bufp
, MAXPATHLEN
);
814 #endif /* CONFIG_AUDIT */