2 * Copyright (c) 2004-2008 Apple Inc.
3 * Copyright (c) 2016 Robert N. M. Watson
6 * Portions of this software were developed by BAE Systems, the University of
7 * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
8 * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
9 * Computing (TC) research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
20 * its contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * Tool used to merge and select audit records from audit trail files
41 * XXX Currently we do not support merging of records from multiple
42 * XXX audit trail files
43 * XXX We assume that records are sorted chronologically - both wrt to
44 * XXX the records present within the file and between the files themselves
47 #include <config/config.h>
49 #define _GNU_SOURCE /* Required for strptime() on glibc2. */
51 #ifdef HAVE_FULL_QUEUE_H
52 #include <sys/queue.h>
54 #include <compat/queue.h>
58 #include <sys/capsicum.h>
62 #include <bsm/libbsm.h>
78 #include <compat/strlcpy.h>
81 #include "auditreduce.h"
83 static TAILQ_HEAD(tailhead, re_entry) re_head =
84 TAILQ_HEAD_INITIALIZER(re_head);
87 extern int optind, optopt, opterr,optreset;
89 static au_mask_t maskp; /* Class. */
90 static time_t p_atime; /* Created after this time. */
91 static time_t p_btime; /* Created before this time. */
92 static int p_auid; /* Audit id. */
93 static int p_euid; /* Effective user id. */
94 static int p_egid; /* Effective group id. */
95 static int p_rgid; /* Real group id. */
96 static int p_ruid; /* Real user id. */
97 static int p_subid; /* Subject id. */
98 static const char *p_zone; /* Zone. */
101 * Maintain a dynamically sized array of events for -m
103 static uint16_t *p_evec; /* Event type list */
104 static int p_evec_used; /* Number of events used */
105 static int p_evec_alloc; /* Number of events allocated */
108 * Following are the objects (-o option) that we can select upon.
110 static char *p_fileobj = NULL;
111 static char *p_msgqobj = NULL;
112 static char *p_pidobj = NULL;
113 static char *p_semobj = NULL;
114 static char *p_shmobj = NULL;
115 static char *p_sockobj = NULL;
117 static uint32_t opttochk = 0;
119 static int select_zone(const char *zone, uint32_t *optchkd);
122 parse_regexp(char *re_string)
124 char *orig, *copy, re_error[64];
125 struct re_entry *rep;
126 int error, nstrs, i, len;
128 copy = strdup(re_string);
131 for (nstrs = 0, i = 0; i < len; i++) {
132 if (copy[i] == ',' && i > 0) {
133 if (copy[i - 1] == '\\')
134 strlcpy(©[i - 1], ©[i], len);
141 TAILQ_INIT(&re_head);
142 for (i = 0; i < nstrs + 1; i++) {
143 rep = calloc(1, sizeof(*rep));
145 (void) fprintf(stderr, "calloc: %s\n",
153 rep->re_pattern = strdup(copy);
154 error = regcomp(&rep->re_regexp, rep->re_pattern,
155 REG_EXTENDED | REG_NOSUB);
157 regerror(error, &rep->re_regexp, re_error, 64);
158 (void) fprintf(stderr, "regcomp: %s\n", re_error);
161 TAILQ_INSERT_TAIL(&re_head, rep, re_glue);
169 usage(const char *msg)
171 fprintf(stderr, "%s\n", msg);
172 fprintf(stderr, "Usage: auditreduce [options] [file ...]\n");
173 fprintf(stderr, "\tOptions are : \n");
174 fprintf(stderr, "\t-A : all records\n");
175 fprintf(stderr, "\t-a YYYYMMDD[HH[[MM[SS]]] : after date\n");
176 fprintf(stderr, "\t-b YYYYMMDD[HH[[MM[SS]]] : before date\n");
177 fprintf(stderr, "\t-c <flags> : matching class\n");
178 fprintf(stderr, "\t-d YYYYMMDD : on date\n");
179 fprintf(stderr, "\t-e <uid|name> : effective user\n");
180 fprintf(stderr, "\t-f <gid|group> : effective group\n");
181 fprintf(stderr, "\t-g <gid|group> : real group\n");
182 fprintf(stderr, "\t-j <pid> : subject id \n");
183 fprintf(stderr, "\t-m <evno|evname> : matching event\n");
184 fprintf(stderr, "\t-o objecttype=objectvalue\n");
185 fprintf(stderr, "\t\t file=<pathname>\n");
186 fprintf(stderr, "\t\t msgqid=<ID>\n");
187 fprintf(stderr, "\t\t pid=<ID>\n");
188 fprintf(stderr, "\t\t semid=<ID>\n");
189 fprintf(stderr, "\t\t shmid=<ID>\n");
190 fprintf(stderr, "\t-r <uid|name> : real user\n");
191 fprintf(stderr, "\t-u <uid|name> : audit user\n");
192 fprintf(stderr, "\t-v : select non-matching records\n");
193 fprintf(stderr, "\t-z <zone> : zone name\n");
198 * Check if the given auid matches the selection criteria.
204 /* Check if we want to select on auid. */
205 if (ISOPTSET(opttochk, OPT_u)) {
213 * Check if the given euid matches the selection criteria.
216 select_euid(int euser)
219 /* Check if we want to select on euid. */
220 if (ISOPTSET(opttochk, OPT_e)) {
228 * Check if the given egid matches the selection criteria.
231 select_egid(int egrp)
234 /* Check if we want to select on egid. */
235 if (ISOPTSET(opttochk, OPT_f)) {
243 * Check if the given rgid matches the selection criteria.
249 /* Check if we want to select on rgid. */
250 if (ISOPTSET(opttochk, OPT_g)) {
258 * Check if the given ruid matches the selection criteria.
261 select_ruid(int user)
264 /* Check if we want to select on rgid. */
265 if (ISOPTSET(opttochk, OPT_r)) {
273 * Check if the given subject id (pid) matches the selection criteria.
276 select_subid(int subid)
279 /* Check if we want to select on subject uid. */
280 if (ISOPTSET(opttochk, OPT_j)) {
281 if (subid != p_subid)
289 * Check if object's pid maches the given pid.
292 select_pidobj(uint32_t pid)
295 if (ISOPTSET(opttochk, OPT_op)) {
296 if (pid != (uint32_t)strtol(p_pidobj, (char **)NULL, 10))
303 * Check if the given ipc object with the given type matches the selection
307 select_ipcobj(u_char type, uint32_t id, uint32_t *optchkd)
310 if (type == AT_IPC_MSG) {
311 SETOPT((*optchkd), OPT_om);
312 if (ISOPTSET(opttochk, OPT_om)) {
313 if (id != (uint32_t)strtol(p_msgqobj, (char **)NULL,
318 } else if (type == AT_IPC_SEM) {
319 SETOPT((*optchkd), OPT_ose);
320 if (ISOPTSET(opttochk, OPT_ose)) {
321 if (id != (uint32_t)strtol(p_semobj, (char **)NULL, 10))
325 } else if (type == AT_IPC_SHM) {
326 SETOPT((*optchkd), OPT_osh);
327 if (ISOPTSET(opttochk, OPT_osh)) {
328 if (id != (uint32_t)strtol(p_shmobj, (char **)NULL, 10))
334 /* Unknown type -- filter if *any* ipc filtering is required. */
335 if (ISOPTSET(opttochk, OPT_om) || ISOPTSET(opttochk, OPT_ose)
336 || ISOPTSET(opttochk, OPT_osh))
344 * Check if the file name matches selection criteria.
347 select_filepath(char *path, uint32_t *optchkd)
349 struct re_entry *rep;
352 SETOPT((*optchkd), OPT_of);
354 if (ISOPTSET(opttochk, OPT_of)) {
356 TAILQ_FOREACH(rep, &re_head, re_glue) {
357 if (regexec(&rep->re_regexp, path, 0, NULL,
359 return (!rep->re_negate);
366 * Returns 1 if the following pass the selection rules:
375 select_hdr32(tokenstr_t tok, uint32_t *optchkd)
380 SETOPT((*optchkd), (OPT_A | OPT_a | OPT_b | OPT_c | OPT_m | OPT_v));
382 /* The A option overrides a, b and d. */
383 if (!ISOPTSET(opttochk, OPT_A)) {
384 if (ISOPTSET(opttochk, OPT_a)) {
385 if (difftime((time_t)tok.tt.hdr32.s, p_atime) < 0) {
386 /* Record was created before p_atime. */
391 if (ISOPTSET(opttochk, OPT_b)) {
392 if (difftime(p_btime, (time_t)tok.tt.hdr32.s) < 0) {
393 /* Record was created after p_btime. */
399 if (ISOPTSET(opttochk, OPT_c)) {
401 * Check if the classes represented by the event matches
404 if (au_preselect(tok.tt.hdr32.e_type, &maskp, AU_PRS_BOTH,
405 AU_PRS_USECACHE) != 1)
409 /* Check if event matches. */
410 if (ISOPTSET(opttochk, OPT_m)) {
412 for (ev = p_evec; ev < &p_evec[p_evec_used]; ev++)
413 if (tok.tt.hdr32.e_type == *ev)
423 select_return32(tokenstr_t tok_ret32, tokenstr_t tok_hdr32, uint32_t *optchkd)
427 SETOPT((*optchkd), (OPT_c));
428 if (tok_ret32.tt.ret32.status == 0)
429 sorf = AU_PRS_SUCCESS;
431 sorf = AU_PRS_FAILURE;
432 if (ISOPTSET(opttochk, OPT_c)) {
433 if (au_preselect(tok_hdr32.tt.hdr32.e_type, &maskp, sorf,
434 AU_PRS_USECACHE) != 1)
441 * Return 1 if checks for the the following succeed
450 select_proc32(tokenstr_t tok, uint32_t *optchkd)
453 SETOPT((*optchkd), (OPT_u | OPT_e | OPT_f | OPT_g | OPT_r | OPT_op));
455 if (!select_auid(tok.tt.proc32.auid))
457 if (!select_euid(tok.tt.proc32.euid))
459 if (!select_egid(tok.tt.proc32.egid))
461 if (!select_rgid(tok.tt.proc32.rgid))
463 if (!select_ruid(tok.tt.proc32.ruid))
465 if (!select_pidobj(tok.tt.proc32.pid))
471 * Return 1 if checks for the the following succeed
480 select_subj32(tokenstr_t tok, uint32_t *optchkd)
483 SETOPT((*optchkd), (OPT_u | OPT_e | OPT_f | OPT_g | OPT_r | OPT_j));
485 if (!select_auid(tok.tt.subj32.auid))
487 if (!select_euid(tok.tt.subj32.euid))
489 if (!select_egid(tok.tt.subj32.egid))
491 if (!select_rgid(tok.tt.subj32.rgid))
493 if (!select_ruid(tok.tt.subj32.ruid))
495 if (!select_subid(tok.tt.subj32.pid))
501 * Check if the given zone matches the selection criteria.
504 select_zone(const char *zone, uint32_t *optchkd)
507 SETOPT((*optchkd), OPT_z);
508 if (ISOPTSET(opttochk, OPT_z) && p_zone != NULL) {
509 if (fnmatch(p_zone, zone, FNM_PATHNAME) != 0)
516 * Read each record from the audit trail. Check if it is selected after
517 * passing through each of the options
520 select_records(FILE *fp)
522 tokenstr_t tok_hdr32_copy;
532 while ((reclen = au_read_rec(fp, &buf)) != -1) {
536 while ((selected == 1) && (bytesread < reclen)) {
537 if (-1 == au_fetch_tok(&tok, buf + bytesread,
538 reclen - bytesread)) {
539 /* Is this an incomplete record? */
545 * For each token type we have have different
546 * selection criteria.
550 selected = select_hdr32(tok,
552 bcopy(&tok, &tok_hdr32_copy,
557 selected = select_proc32(tok,
562 selected = select_subj32(tok,
567 selected = select_ipcobj(
568 tok.tt.ipc.type, tok.tt.ipc.id,
573 selected = select_filepath(
574 tok.tt.path.path, &optchkd);
578 selected = select_return32(tok,
579 tok_hdr32_copy, &optchkd);
583 selected = select_zone(tok.tt.zonename.zonename, &optchkd);
589 bytesread += tok.len;
591 /* Check if all the options were matched. */
592 print = ((selected == 1) && (!err) && (!(opttochk & ~optchkd)));
593 if (ISOPTSET(opttochk, OPT_v))
596 (void) fwrite(buf, 1, reclen, stdout);
603 * The -o option has the form object_type=object_value. Identify the object
607 parse_object_type(char *name, char *val)
612 if (!strcmp(name, FILEOBJ)) {
615 SETOPT(opttochk, OPT_of);
616 } else if (!strcmp(name, MSGQIDOBJ)) {
618 SETOPT(opttochk, OPT_om);
619 } else if (!strcmp(name, PIDOBJ)) {
621 SETOPT(opttochk, OPT_op);
622 } else if (!strcmp(name, SEMIDOBJ)) {
624 SETOPT(opttochk, OPT_ose);
625 } else if (!strcmp(name, SHMIDOBJ)) {
627 SETOPT(opttochk, OPT_osh);
628 } else if (!strcmp(name, SOCKOBJ)) {
630 SETOPT(opttochk, OPT_oso);
632 usage("unknown value for -o");
636 main(int argc, char **argv)
644 char *objval, *converr;
649 #ifdef HAVE_CAP_ENTER
656 while ((ch = getopt(argc, argv, "Aa:b:c:d:e:f:g:j:m:o:r:u:vz:")) != -1) {
659 SETOPT(opttochk, OPT_A);
663 if (ISOPTSET(opttochk, OPT_a)) {
664 usage("d is exclusive with a and b");
666 SETOPT(opttochk, OPT_a);
667 bzero(&tm, sizeof(tm));
668 strptime(optarg, "%Y%m%d%H%M%S", &tm);
669 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S",
671 /* fprintf(stderr, "Time converted = %s\n", timestr); */
672 p_atime = mktime(&tm);
676 if (ISOPTSET(opttochk, OPT_b)) {
677 usage("d is exclusive with a and b");
679 SETOPT(opttochk, OPT_b);
680 bzero(&tm, sizeof(tm));
681 strptime(optarg, "%Y%m%d%H%M%S", &tm);
682 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S",
684 /* fprintf(stderr, "Time converted = %s\n", timestr); */
685 p_btime = mktime(&tm);
689 if (0 != getauditflagsbin(optarg, &maskp)) {
690 /* Incorrect class */
691 usage("Incorrect class");
693 SETOPT(opttochk, OPT_c);
697 if (ISOPTSET(opttochk, OPT_b) || ISOPTSET(opttochk,
699 usage("'d' is exclusive with 'a' and 'b'");
700 SETOPT(opttochk, OPT_d);
701 bzero(&tm, sizeof(tm));
702 strptime(optarg, "%Y%m%d", &tm);
703 strftime(timestr, sizeof(timestr), "%Y%m%d", &tm);
704 /* fprintf(stderr, "Time converted = %s\n", timestr); */
705 p_atime = mktime(&tm);
709 strftime(timestr, sizeof(timestr), "%Y%m%d", &tm);
710 /* fprintf(stderr, "Time converted = %s\n", timestr); */
711 p_btime = mktime(&tm);
715 p_euid = strtol(optarg, &converr, 10);
716 if (*converr != '\0') {
717 /* Try the actual name */
718 if ((pw = getpwnam(optarg)) == NULL)
722 SETOPT(opttochk, OPT_e);
726 p_egid = strtol(optarg, &converr, 10);
727 if (*converr != '\0') {
728 /* Try actual group name. */
729 if ((grp = getgrnam(optarg)) == NULL)
731 p_egid = grp->gr_gid;
733 SETOPT(opttochk, OPT_f);
737 p_rgid = strtol(optarg, &converr, 10);
738 if (*converr != '\0') {
739 /* Try actual group name. */
740 if ((grp = getgrnam(optarg)) == NULL)
742 p_rgid = grp->gr_gid;
744 SETOPT(opttochk, OPT_g);
748 p_subid = strtol(optarg, (char **)NULL, 10);
749 SETOPT(opttochk, OPT_j);
753 if (p_evec == NULL) {
755 p_evec = malloc(sizeof(*etp) * p_evec_alloc);
758 } else if (p_evec_alloc == p_evec_used) {
760 p_evec = realloc(p_evec,
761 sizeof(*p_evec) * p_evec_alloc);
765 etp = &p_evec[p_evec_used++];
766 *etp = strtol(optarg, (char **)NULL, 10);
768 /* Could be the string representation. */
769 n = getauevnonam(optarg);
771 usage("Incorrect event name");
774 SETOPT(opttochk, OPT_m);
778 objval = strchr(optarg, '=');
779 if (objval != NULL) {
782 parse_object_type(optarg, objval);
787 p_ruid = strtol(optarg, &converr, 10);
788 if (*converr != '\0') {
789 if ((pw = getpwnam(optarg)) == NULL)
793 SETOPT(opttochk, OPT_r);
797 p_auid = strtol(optarg, &converr, 10);
798 if (*converr != '\0') {
799 if ((pw = getpwnam(optarg)) == NULL)
803 SETOPT(opttochk, OPT_u);
807 SETOPT(opttochk, OPT_v);
812 SETOPT(opttochk, OPT_z);
817 usage("Unknown option");
824 #ifdef HAVE_CAP_ENTER
825 retval = cap_enter();
826 if (retval != 0 && errno != ENOSYS)
827 err(EXIT_FAILURE, "cap_enter");
829 if (select_records(stdin) == -1)
831 "Couldn't select records from stdin");
836 * XXX: We should actually be merging records here.
838 for (i = 0; i < argc; i++) {
840 fp = fopen(fname, "r");
842 errx(EXIT_FAILURE, "Couldn't open %s", fname);
845 * If operating with sandboxing, create a sandbox process for
846 * each trail file we operate on. This avoids the need to do
847 * fancy things with file descriptors, etc, when iterating on
848 * a list of arguments.
850 * NB: Unlike praudit(1), auditreduce(1) terminates if it hits
851 * any errors. Propagate the error from the child to the
852 * parent if any problems arise.
854 #ifdef HAVE_CAP_ENTER
858 retval = cap_enter();
859 if (retval != 0 && errno != ENOSYS)
860 errx(EXIT_FAILURE, "cap_enter");
861 if (select_records(fp) == -1)
863 "Couldn't select records %s", fname);
867 /* Parent. Await child termination, check exit value. */
868 while ((pid = waitpid(childpid, &status, 0)) != childpid);
869 if (WEXITSTATUS(status) != 0)
872 if (select_records(fp) == -1)
873 errx(EXIT_FAILURE, "Couldn't select records %s",