2 * Copyright (c) 1999-2005 Apple Computer, Inc.
3 * Copyright (c) 2005 Robert N. M. Watson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/param.h>
34 #include <sys/fcntl.h>
35 #include <sys/filedesc.h>
36 #include <sys/libkern.h>
37 #include <sys/malloc.h>
38 #include <sys/mount.h>
41 #include <sys/syscall.h>
42 #include <sys/sysctl.h>
43 #include <sys/sysent.h>
44 #include <sys/vnode.h>
46 #include <bsm/audit.h>
47 #include <bsm/audit_kevents.h>
48 #include <security/audit/audit.h>
49 #include <security/audit/audit_private.h>
52 * Hash table functions for the audit event number to event class mask
55 #define EVCLASSMAP_HASH_TABLE_SIZE 251
59 LIST_ENTRY(evclass_elem) entry;
62 LIST_HEAD(, evclass_elem) head;
65 static MALLOC_DEFINE(M_AUDITEVCLASS, "audit_evclass", "Audit event class");
66 static struct mtx evclass_mtx;
67 static struct evclass_list evclass_hash[EVCLASSMAP_HASH_TABLE_SIZE];
70 * Look up the class for an audit event in the class mapping table.
73 au_event_class(au_event_t event)
75 struct evclass_list *evcl;
76 struct evclass_elem *evc;
79 mtx_lock(&evclass_mtx);
80 evcl = &evclass_hash[event % EVCLASSMAP_HASH_TABLE_SIZE];
82 LIST_FOREACH(evc, &evcl->head, entry) {
83 if (evc->event == event) {
89 mtx_unlock(&evclass_mtx);
94 * Insert a event to class mapping. If the event already exists in the
95 * mapping, then replace the mapping with the new one.
97 * XXX There is currently no constraints placed on the number of mappings.
98 * May want to either limit to a number, or in terms of memory usage.
101 au_evclassmap_insert(au_event_t event, au_class_t class)
103 struct evclass_list *evcl;
104 struct evclass_elem *evc, *evc_new;
107 * Pessimistically, always allocate storage before acquiring mutex.
108 * Free if there is already a mapping for this event.
110 evc_new = malloc(sizeof(*evc), M_AUDITEVCLASS, M_WAITOK);
112 mtx_lock(&evclass_mtx);
113 evcl = &evclass_hash[event % EVCLASSMAP_HASH_TABLE_SIZE];
114 LIST_FOREACH(evc, &evcl->head, entry) {
115 if (evc->event == event) {
117 mtx_unlock(&evclass_mtx);
118 free(evc_new, M_AUDITEVCLASS);
125 LIST_INSERT_HEAD(&evcl->head, evc, entry);
126 mtx_unlock(&evclass_mtx);
130 au_evclassmap_init(void)
134 mtx_init(&evclass_mtx, "evclass_mtx", NULL, MTX_DEF);
135 for (i = 0; i < EVCLASSMAP_HASH_TABLE_SIZE; i++)
136 LIST_INIT(&evclass_hash[i].head);
139 * Set up the initial event to class mapping for system calls.
141 * XXXRW: Really, this should walk all possible audit events, not all
142 * native ABI system calls, as there may be audit events reachable
143 * only through non-native system calls. It also seems a shame to
144 * frob the mutex this early.
146 for (i = 0; i < SYS_MAXSYSCALL; i++) {
147 if (sysent[i].sy_auevent != AUE_NULL)
148 au_evclassmap_insert(sysent[i].sy_auevent, AU_NULL);
153 * Check whether an event is aditable by comparing the mask of classes this
154 * event is part of against the given mask.
157 au_preselect(au_event_t event, au_mask_t *mask_p, int sorf)
159 au_class_t effmask = 0;
165 ae_class = au_event_class(event);
168 * Perform the actual check of the masks against the event.
170 if (sorf & AU_PRS_SUCCESS)
171 effmask |= (mask_p->am_success & ae_class);
173 if (sorf & AU_PRS_FAILURE)
174 effmask |= (mask_p->am_failure & ae_class);
183 * Convert sysctl names and present arguments to events.
186 ctlname_to_sysctlevent(int name[], uint64_t valid_arg)
189 /* can't parse it - so return the worst case */
190 if ((valid_arg & (ARG_CTLNAME | ARG_LEN)) != (ARG_CTLNAME | ARG_LEN))
194 /* non-admin "lookups" treat them special */
204 case KERN_JOB_CONTROL:
208 return (AUE_SYSCTL_NONADMIN);
210 /* only treat the changeable controls as admin */
214 case KERN_MAXPROCPERUID:
215 case KERN_MAXFILESPERPROC:
223 case KERN_NISDOMAINNAME:
224 case KERN_UPDATEINTERVAL:
229 case KERN_PS_STRINGS:
231 case KERN_LOGSIGEXIT:
234 return ((valid_arg & ARG_VALUE) ?
235 AUE_SYSCTL : AUE_SYSCTL_NONADMIN);
244 * Convert an open flags specifier into a specific type of open event for
248 flags_and_error_to_openevent(int oflags, int error)
253 * Need to check only those flags we care about.
255 oflags = oflags & (O_RDONLY | O_CREAT | O_TRUNC | O_RDWR | O_WRONLY);
258 * These checks determine what flags are on with the condition that
259 * ONLY that combination is on, and no other flags are on.
266 case (O_RDONLY | O_CREAT):
267 aevent = AUE_OPEN_RC;
270 case (O_RDONLY | O_CREAT | O_TRUNC):
271 aevent = AUE_OPEN_RTC;
274 case (O_RDONLY | O_TRUNC):
275 aevent = AUE_OPEN_RT;
279 aevent = AUE_OPEN_RW;
282 case (O_RDWR | O_CREAT):
283 aevent = AUE_OPEN_RWC;
286 case (O_RDWR | O_CREAT | O_TRUNC):
287 aevent = AUE_OPEN_RWTC;
290 case (O_RDWR | O_TRUNC):
291 aevent = AUE_OPEN_RWT;
298 case (O_WRONLY | O_CREAT):
299 aevent = AUE_OPEN_WC;
302 case (O_WRONLY | O_CREAT | O_TRUNC):
303 aevent = AUE_OPEN_WTC;
306 case (O_WRONLY | O_TRUNC):
307 aevent = AUE_OPEN_WT;
317 * Convert chatty errors to better matching events.
318 * Failures to find a file are really just attribute
319 * events - so recast them as such.
321 * XXXAUDIT: Solaris defines that AUE_OPEN will never be returned, it
322 * is just a placeholder. However, in Darwin we return that in
323 * preference to other events. For now, comment this out as we don't
324 * have a BSM conversion routine for AUE_OPEN.
341 * Convert a MSGCTL command to a specific event.
344 msgctl_to_event(int cmd)
349 return (AUE_MSGCTL_RMID);
352 return (AUE_MSGCTL_SET);
355 return (AUE_MSGCTL_STAT);
358 /* We will audit a bad command */
364 * Convert a SEMCTL command to a specific event.
367 semctl_to_event(int cmd)
372 return (AUE_SEMCTL_GETALL);
375 return (AUE_SEMCTL_GETNCNT);
378 return (AUE_SEMCTL_GETPID);
381 return (AUE_SEMCTL_GETVAL);
384 return (AUE_SEMCTL_GETZCNT);
387 return (AUE_SEMCTL_RMID);
390 return (AUE_SEMCTL_SET);
393 return (AUE_SEMCTL_SETALL);
396 return (AUE_SEMCTL_SETVAL);
399 return (AUE_SEMCTL_STAT);
402 /* We will audit a bad command */
408 * Convert a command for the auditon() system call to a audit event.
411 auditon_command_event(int cmd)
416 return (AUE_AUDITON_GPOLICY);
419 return (AUE_AUDITON_SPOLICY);
422 return (AUE_AUDITON_GETKMASK);
425 return (AUE_AUDITON_SETKMASK);
428 return (AUE_AUDITON_GQCTRL);
431 return (AUE_AUDITON_SQCTRL);
434 return (AUE_AUDITON_GETCWD);
437 return (AUE_AUDITON_GETCAR);
440 return (AUE_AUDITON_GETSTAT);
443 return (AUE_AUDITON_SETSTAT);
446 return (AUE_AUDITON_SETUMASK);
449 return (AUE_AUDITON_SETSMASK);
452 return (AUE_AUDITON_GETCOND);
455 return (AUE_AUDITON_SETCOND);
458 return (AUE_AUDITON_GETCLASS);
461 return (AUE_AUDITON_SETCLASS);
467 case A_GETPINFO_ADDR:
471 return (AUE_AUDITON); /* No special record */
476 * Create a canonical path from given path by prefixing either the root
477 * directory, or the current working directory. If the process working
478 * directory is NULL, we could use 'rootvnode' to obtain the root directoty,
479 * but this results in a volfs name written to the audit log. So we will
480 * leave the filename starting with '/' in the audit log in this case.
482 * XXXRW: Since we combine two paths here, ideally a buffer of size
483 * MAXPATHLEN * 2 would be passed in.
486 canon_path(struct thread *td, char *path, char *cpath)
489 char *retbuf, *freebuf;
491 struct filedesc *fdp;
492 int error, vfslocked;
494 fdp = td->td_proc->p_fd;
497 if (*(path) == '/') {
498 while (*(bufp) == '/')
499 bufp++; /* Skip leading '/'s. */
501 * If no process root, or it is the same as the system root,
502 * audit the path as passed in with a single '/'.
504 if ((fdp->fd_rdir == NULL) ||
505 (fdp->fd_rdir == rootvnode)) {
507 bufp--; /* Restore one '/'. */
509 vnp = fdp->fd_rdir; /* Use process root. */
513 vnp = fdp->fd_cdir; /* Prepend the current dir. */
517 FILEDESC_UNLOCK(fdp);
520 * XXX: vn_fullpath() on FreeBSD is "less reliable" than
521 * vn_getpath() on Darwin, so this will need more attention
522 * in the future. Also, the question and string bounding
523 * here seems a bit questionable and will also require
526 vfslocked = VFS_LOCK_GIANT(vnp->v_mount);
527 vn_lock(vnp, LK_EXCLUSIVE | LK_RETRY, td);
528 error = vn_fullpath(td, vnp, &retbuf, &freebuf);
530 /* Copy and free buffer allocated by vn_fullpath(). */
531 snprintf(cpath, MAXPATHLEN, "%s/%s", retbuf, bufp);
532 free(freebuf, M_TEMP);
536 VFS_UNLOCK_GIANT(vfslocked);
538 strlcpy(cpath, bufp, MAXPATHLEN);