1f7f4e0f7SAlan Somers /*- 2f7f4e0f7SAlan Somers * Copyright 2018 Aniket Pandey 3f7f4e0f7SAlan Somers * 4f7f4e0f7SAlan Somers * Redistribution and use in source and binary forms, with or without 5f7f4e0f7SAlan Somers * modification, are permitted provided that the following conditions 6f7f4e0f7SAlan Somers * are met: 7f7f4e0f7SAlan Somers * 1. Redistributions of source code must retain the above copyright 8f7f4e0f7SAlan Somers * notice, this list of conditions and the following disclaimer. 9f7f4e0f7SAlan Somers * 2. Redistributions in binary form must reproduce the above copyright 10f7f4e0f7SAlan Somers * notice, this list of conditions and the following disclaimer in the 11f7f4e0f7SAlan Somers * documentation and/or other materials provided with the distribution. 12f7f4e0f7SAlan Somers * 13f7f4e0f7SAlan Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14f7f4e0f7SAlan Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15f7f4e0f7SAlan Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16f7f4e0f7SAlan Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17f7f4e0f7SAlan Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18f7f4e0f7SAlan Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19f7f4e0f7SAlan Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 20f7f4e0f7SAlan Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21f7f4e0f7SAlan Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22f7f4e0f7SAlan Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23f7f4e0f7SAlan Somers * SUCH DAMAGE. 24f7f4e0f7SAlan Somers * 25f7f4e0f7SAlan Somers * $FreeBSD$ 26f7f4e0f7SAlan Somers */ 27f7f4e0f7SAlan Somers 2840407d39SAlex Richardson #include <sys/types.h> 2940407d39SAlex Richardson #include <sys/extattr.h> 30f7f4e0f7SAlan Somers #include <sys/ioctl.h> 31f7f4e0f7SAlan Somers 32f7f4e0f7SAlan Somers #include <bsm/libbsm.h> 33*df093aa9SAlex Richardson #include <bsm/auditd_lib.h> 34f7f4e0f7SAlan Somers #include <security/audit/audit_ioctl.h> 35f7f4e0f7SAlan Somers 36b13a70d5SAlan Somers #include <atf-c.h> 37b13a70d5SAlan Somers #include <errno.h> 38b13a70d5SAlan Somers #include <fcntl.h> 39b13a70d5SAlan Somers #include <stdlib.h> 40b13a70d5SAlan Somers #include <string.h> 41b13a70d5SAlan Somers #include <time.h> 42b13a70d5SAlan Somers #include <unistd.h> 43b13a70d5SAlan Somers 44f7f4e0f7SAlan Somers #include "utils.h" 45f7f4e0f7SAlan Somers 46f7f4e0f7SAlan Somers /* 47f7f4e0f7SAlan Somers * Checks the presence of "auditregex" in auditpipe(4) after the 48f7f4e0f7SAlan Somers * corresponding system call has been triggered. 49f7f4e0f7SAlan Somers */ 50f7f4e0f7SAlan Somers static bool 51f7f4e0f7SAlan Somers get_records(const char *auditregex, FILE *pipestream) 52f7f4e0f7SAlan Somers { 53f7f4e0f7SAlan Somers uint8_t *buff; 54f7f4e0f7SAlan Somers tokenstr_t token; 55f7f4e0f7SAlan Somers ssize_t size = 1024; 56f7f4e0f7SAlan Somers char membuff[size]; 57f7f4e0f7SAlan Somers char del[] = ","; 58f7f4e0f7SAlan Somers int reclen, bytes = 0; 59f7f4e0f7SAlan Somers FILE *memstream; 60f7f4e0f7SAlan Somers 61f7f4e0f7SAlan Somers /* 62f7f4e0f7SAlan Somers * Open a stream on 'membuff' (address to memory buffer) for storing 63f7f4e0f7SAlan Somers * the audit records in the default mode.'reclen' is the length of the 64f7f4e0f7SAlan Somers * available records from auditpipe which is passed to the functions 65f7f4e0f7SAlan Somers * au_fetch_tok(3) and au_print_flags_tok(3) for further use. 66f7f4e0f7SAlan Somers */ 67f7f4e0f7SAlan Somers ATF_REQUIRE((memstream = fmemopen(membuff, size, "w")) != NULL); 68f7f4e0f7SAlan Somers ATF_REQUIRE((reclen = au_read_rec(pipestream, &buff)) != -1); 69f7f4e0f7SAlan Somers 70f7f4e0f7SAlan Somers /* 71f7f4e0f7SAlan Somers * Iterate through each BSM token, extracting the bits that are 72f7f4e0f7SAlan Somers * required to start processing the token sequences. 73f7f4e0f7SAlan Somers */ 74f7f4e0f7SAlan Somers while (bytes < reclen) { 75f7f4e0f7SAlan Somers if (au_fetch_tok(&token, buff + bytes, reclen - bytes) == -1) { 76f7f4e0f7SAlan Somers perror("au_read_rec"); 77f7f4e0f7SAlan Somers atf_tc_fail("Incomplete Audit Record"); 78f7f4e0f7SAlan Somers } 79f7f4e0f7SAlan Somers 80f7f4e0f7SAlan Somers /* Print the tokens as they are obtained, in the default form */ 81f7f4e0f7SAlan Somers au_print_flags_tok(memstream, &token, del, AU_OFLAG_NONE); 82f7f4e0f7SAlan Somers bytes += token.len; 83f7f4e0f7SAlan Somers } 84f7f4e0f7SAlan Somers 85f7f4e0f7SAlan Somers free(buff); 86b13a70d5SAlan Somers ATF_REQUIRE_EQ(0, fclose(memstream)); 87f7f4e0f7SAlan Somers return (atf_utils_grep_string("%s", membuff, auditregex)); 88f7f4e0f7SAlan Somers } 89f7f4e0f7SAlan Somers 90f7f4e0f7SAlan Somers /* 91f7f4e0f7SAlan Somers * Override the system-wide audit mask settings in /etc/security/audit_control 92f7f4e0f7SAlan Somers * and set the auditpipe's maximum allowed queue length limit 93f7f4e0f7SAlan Somers */ 94f7f4e0f7SAlan Somers static void 95f7f4e0f7SAlan Somers set_preselect_mode(int filedesc, au_mask_t *fmask) 96f7f4e0f7SAlan Somers { 97f7f4e0f7SAlan Somers int qlimit_max; 98f7f4e0f7SAlan Somers int fmode = AUDITPIPE_PRESELECT_MODE_LOCAL; 99f7f4e0f7SAlan Somers 100f7f4e0f7SAlan Somers /* Set local preselection mode for auditing */ 101f7f4e0f7SAlan Somers if (ioctl(filedesc, AUDITPIPE_SET_PRESELECT_MODE, &fmode) < 0) 102f7f4e0f7SAlan Somers atf_tc_fail("Preselection mode: %s", strerror(errno)); 103f7f4e0f7SAlan Somers 104f7f4e0f7SAlan Somers /* Set local preselection flag corresponding to the audit_event */ 105f7f4e0f7SAlan Somers if (ioctl(filedesc, AUDITPIPE_SET_PRESELECT_FLAGS, fmask) < 0) 106f7f4e0f7SAlan Somers atf_tc_fail("Preselection flag: %s", strerror(errno)); 107f7f4e0f7SAlan Somers 108f7f4e0f7SAlan Somers /* Set local preselection flag for non-attributable audit_events */ 109f7f4e0f7SAlan Somers if (ioctl(filedesc, AUDITPIPE_SET_PRESELECT_NAFLAGS, fmask) < 0) 110f7f4e0f7SAlan Somers atf_tc_fail("Preselection naflag: %s", strerror(errno)); 111f7f4e0f7SAlan Somers 112f7f4e0f7SAlan Somers /* Query the maximum possible queue length limit for auditpipe */ 113f7f4e0f7SAlan Somers if (ioctl(filedesc, AUDITPIPE_GET_QLIMIT_MAX, &qlimit_max) < 0) 114f7f4e0f7SAlan Somers atf_tc_fail("Query max-limit: %s", strerror(errno)); 115f7f4e0f7SAlan Somers 116f7f4e0f7SAlan Somers /* Set the queue length limit as obtained from previous step */ 117f7f4e0f7SAlan Somers if (ioctl(filedesc, AUDITPIPE_SET_QLIMIT, &qlimit_max) < 0) 118f7f4e0f7SAlan Somers atf_tc_fail("Set max-qlimit: %s", strerror(errno)); 119f7f4e0f7SAlan Somers 120f7f4e0f7SAlan Somers /* This removes any outstanding record on the auditpipe */ 121f7f4e0f7SAlan Somers if (ioctl(filedesc, AUDITPIPE_FLUSH) < 0) 122f7f4e0f7SAlan Somers atf_tc_fail("Auditpipe flush: %s", strerror(errno)); 123f7f4e0f7SAlan Somers } 124f7f4e0f7SAlan Somers 125f7f4e0f7SAlan Somers /* 126f7f4e0f7SAlan Somers * Get the corresponding audit_mask for class-name "name" then set the 127f7f4e0f7SAlan Somers * success and failure bits for fmask to be used as the ioctl argument 128f7f4e0f7SAlan Somers */ 129f7f4e0f7SAlan Somers static au_mask_t 130f7f4e0f7SAlan Somers get_audit_mask(const char *name) 131f7f4e0f7SAlan Somers { 132f7f4e0f7SAlan Somers au_mask_t fmask; 133f7f4e0f7SAlan Somers au_class_ent_t *class; 134f7f4e0f7SAlan Somers 135f7f4e0f7SAlan Somers ATF_REQUIRE((class = getauclassnam(name)) != NULL); 136f7f4e0f7SAlan Somers fmask.am_success = class->ac_class; 137f7f4e0f7SAlan Somers fmask.am_failure = class->ac_class; 138f7f4e0f7SAlan Somers return (fmask); 139f7f4e0f7SAlan Somers } 140f7f4e0f7SAlan Somers 141f7f4e0f7SAlan Somers /* 142f7f4e0f7SAlan Somers * Loop until the auditpipe returns something, check if it is what 143f7f4e0f7SAlan Somers * we want, else repeat the procedure until ppoll(2) times out. 144f7f4e0f7SAlan Somers */ 145f7f4e0f7SAlan Somers static void 146f7f4e0f7SAlan Somers check_auditpipe(struct pollfd fd[], const char *auditregex, FILE *pipestream) 147f7f4e0f7SAlan Somers { 148f7f4e0f7SAlan Somers struct timespec currtime, endtime, timeout; 149f7f4e0f7SAlan Somers 150f7f4e0f7SAlan Somers /* Set the expire time for poll(2) while waiting for syscall audit */ 151f7f4e0f7SAlan Somers ATF_REQUIRE_EQ(0, clock_gettime(CLOCK_MONOTONIC, &endtime)); 152869cc064SAlex Richardson /* Set limit to 30 seconds total and ~10s without an event. */ 153869cc064SAlex Richardson endtime.tv_sec += 30; 154f7f4e0f7SAlan Somers 155f7f4e0f7SAlan Somers for (;;) { 156f7f4e0f7SAlan Somers /* Update the time left for auditpipe to return any event */ 157f7f4e0f7SAlan Somers ATF_REQUIRE_EQ(0, clock_gettime(CLOCK_MONOTONIC, &currtime)); 158869cc064SAlex Richardson timespecsub(&endtime, &currtime, &timeout); 159869cc064SAlex Richardson timeout.tv_sec = MIN(timeout.tv_sec, 9); 160869cc064SAlex Richardson if (timeout.tv_sec < 0) { 161869cc064SAlex Richardson atf_tc_fail("%s not found in auditpipe within the " 162869cc064SAlex Richardson "time limit", auditregex); 163869cc064SAlex Richardson } 164f7f4e0f7SAlan Somers 165f7f4e0f7SAlan Somers switch (ppoll(fd, 1, &timeout, NULL)) { 166f7f4e0f7SAlan Somers /* ppoll(2) returns, check if it's what we want */ 167f7f4e0f7SAlan Somers case 1: 168f7f4e0f7SAlan Somers if (fd[0].revents & POLLIN) { 169f7f4e0f7SAlan Somers if (get_records(auditregex, pipestream)) 170f7f4e0f7SAlan Somers return; 171f7f4e0f7SAlan Somers } else { 172f7f4e0f7SAlan Somers atf_tc_fail("Auditpipe returned an " 173f7f4e0f7SAlan Somers "unknown event %#x", fd[0].revents); 174f7f4e0f7SAlan Somers } 175f7f4e0f7SAlan Somers break; 176f7f4e0f7SAlan Somers 177f7f4e0f7SAlan Somers /* poll(2) timed out */ 178f7f4e0f7SAlan Somers case 0: 179f7f4e0f7SAlan Somers atf_tc_fail("%s not found in auditpipe within the " 180f7f4e0f7SAlan Somers "time limit", auditregex); 181f7f4e0f7SAlan Somers break; 182f7f4e0f7SAlan Somers 183f7f4e0f7SAlan Somers /* poll(2) standard error */ 184f7f4e0f7SAlan Somers case -1: 185f7f4e0f7SAlan Somers atf_tc_fail("Poll: %s", strerror(errno)); 186f7f4e0f7SAlan Somers break; 187f7f4e0f7SAlan Somers 188f7f4e0f7SAlan Somers default: 189f7f4e0f7SAlan Somers atf_tc_fail("Poll returned too many file descriptors"); 190f7f4e0f7SAlan Somers } 191f7f4e0f7SAlan Somers } 192f7f4e0f7SAlan Somers } 193f7f4e0f7SAlan Somers 194f7f4e0f7SAlan Somers /* 195f7f4e0f7SAlan Somers * Wrapper functions around static "check_auditpipe" 196f7f4e0f7SAlan Somers */ 197f7f4e0f7SAlan Somers static void 198f7f4e0f7SAlan Somers check_audit_startup(struct pollfd fd[], const char *auditrgx, FILE *pipestream){ 199f7f4e0f7SAlan Somers check_auditpipe(fd, auditrgx, pipestream); 200f7f4e0f7SAlan Somers } 201f7f4e0f7SAlan Somers 202f7f4e0f7SAlan Somers void 203f7f4e0f7SAlan Somers check_audit(struct pollfd fd[], const char *auditrgx, FILE *pipestream) { 204f7f4e0f7SAlan Somers check_auditpipe(fd, auditrgx, pipestream); 205f7f4e0f7SAlan Somers 206b13a70d5SAlan Somers /* Teardown: /dev/auditpipe's instance opened for this test-suite */ 207b13a70d5SAlan Somers ATF_REQUIRE_EQ(0, fclose(pipestream)); 208f7f4e0f7SAlan Somers } 209f7f4e0f7SAlan Somers 21040407d39SAlex Richardson void 21140407d39SAlex Richardson skip_if_extattr_not_supported(const char *path) 21240407d39SAlex Richardson { 21340407d39SAlex Richardson ssize_t result; 21440407d39SAlex Richardson 21540407d39SAlex Richardson /* 21640407d39SAlex Richardson * Some file systems (e.g. tmpfs) do not support extattr, so we need 21740407d39SAlex Richardson * skip tests that use extattrs. To detect this we can check whether 21840407d39SAlex Richardson * the extattr_list_file returns EOPNOTSUPP. 21940407d39SAlex Richardson */ 22040407d39SAlex Richardson result = extattr_list_file(path, EXTATTR_NAMESPACE_USER, NULL, 0); 22140407d39SAlex Richardson if (result == -1 && errno == EOPNOTSUPP) { 22240407d39SAlex Richardson atf_tc_skip("File system does not support extattrs."); 22340407d39SAlex Richardson } 22440407d39SAlex Richardson } 22540407d39SAlex Richardson 226*df093aa9SAlex Richardson static bool 227*df093aa9SAlex Richardson is_auditd_running(void) 228*df093aa9SAlex Richardson { 229*df093aa9SAlex Richardson int trigger; 230*df093aa9SAlex Richardson int err; 231*df093aa9SAlex Richardson 232*df093aa9SAlex Richardson /* 233*df093aa9SAlex Richardson * AUDIT_TRIGGER_INITIALIZE is a no-op message on FreeBSD and can 234*df093aa9SAlex Richardson * therefore be used to check whether auditd has already been started. 235*df093aa9SAlex Richardson * This is significantly cheaper than running `service auditd onestatus` 236*df093aa9SAlex Richardson * for each test case. It is also slightly less racy since it will only 237*df093aa9SAlex Richardson * return true once auditd() has opened the trigger file rather than 238*df093aa9SAlex Richardson * just when the pidfile has been created. 239*df093aa9SAlex Richardson */ 240*df093aa9SAlex Richardson trigger = AUDIT_TRIGGER_INITIALIZE; 241*df093aa9SAlex Richardson err = auditon(A_SENDTRIGGER, &trigger, sizeof(trigger)); 242*df093aa9SAlex Richardson if (err == 0) { 243*df093aa9SAlex Richardson fprintf(stderr, "auditd(8) is running.\n"); 244*df093aa9SAlex Richardson return (true); 245*df093aa9SAlex Richardson } else { 246*df093aa9SAlex Richardson /* 247*df093aa9SAlex Richardson * A_SENDTRIGGER returns ENODEV if auditd isn't listening, 248*df093aa9SAlex Richardson * all other error codes indicate a fatal error. 249*df093aa9SAlex Richardson */ 250*df093aa9SAlex Richardson ATF_REQUIRE_MSG(errno == ENODEV, 251*df093aa9SAlex Richardson "Unexpected error from auditon(2): %s", strerror(errno)); 252*df093aa9SAlex Richardson return (false); 253*df093aa9SAlex Richardson } 254*df093aa9SAlex Richardson 255*df093aa9SAlex Richardson } 256*df093aa9SAlex Richardson 257*df093aa9SAlex Richardson FILE * 258*df093aa9SAlex Richardson setup(struct pollfd fd[], const char *name) 259f7f4e0f7SAlan Somers { 260f7f4e0f7SAlan Somers au_mask_t fmask, nomask; 261*df093aa9SAlex Richardson FILE *pipestream; 262f7f4e0f7SAlan Somers fmask = get_audit_mask(name); 263f7f4e0f7SAlan Somers nomask = get_audit_mask("no"); 264f7f4e0f7SAlan Somers 265b13a70d5SAlan Somers ATF_REQUIRE((fd[0].fd = open("/dev/auditpipe", O_RDONLY)) != -1); 266b13a70d5SAlan Somers ATF_REQUIRE((pipestream = fdopen(fd[0].fd, "r")) != NULL); 267f7f4e0f7SAlan Somers fd[0].events = POLLIN; 268f7f4e0f7SAlan Somers 269405f0931SAlan Somers /* 270405f0931SAlan Somers * Disable stream buffering for read operations from /dev/auditpipe. 271405f0931SAlan Somers * Otherwise it is possible that fread(3), called via au_read_rec(3), 272405f0931SAlan Somers * can store buffered data in user-space unbeknown to ppoll(2), which 273405f0931SAlan Somers * as a result, reports that /dev/auditpipe is empty. 274405f0931SAlan Somers */ 275405f0931SAlan Somers ATF_REQUIRE_EQ(0, setvbuf(pipestream, NULL, _IONBF, 0)); 276405f0931SAlan Somers 277f7f4e0f7SAlan Somers /* Set local preselection audit_class as "no" for audit startup */ 278f7f4e0f7SAlan Somers set_preselect_mode(fd[0].fd, &nomask); 279*df093aa9SAlex Richardson if (!is_auditd_running()) { 280*df093aa9SAlex Richardson fprintf(stderr, "Running audit_quick_start() for testing... "); 281*df093aa9SAlex Richardson /* 282*df093aa9SAlex Richardson * Previously, this test started auditd using 283*df093aa9SAlex Richardson * `service auditd onestart`. However, there is a race condition 284*df093aa9SAlex Richardson * there since service can return before auditd(8) has 285*df093aa9SAlex Richardson * fully started (once the daemon parent process has forked) 286*df093aa9SAlex Richardson * and this can cause check_audit_startup() to fail sometimes. 287*df093aa9SAlex Richardson * 288*df093aa9SAlex Richardson * In the CheriBSD CI this caused the first test executed by 289*df093aa9SAlex Richardson * kyua (administrative:acct_failure) to fail every time, but 290*df093aa9SAlex Richardson * subsequent ones would almost always succeed. 291*df093aa9SAlex Richardson * 292*df093aa9SAlex Richardson * To avoid this problem (and as a nice side-effect this speeds 293*df093aa9SAlex Richardson * up the test quite a bit), we register this process as a 294*df093aa9SAlex Richardson * "fake" auditd(8) using the audit_quick_start() function from 295*df093aa9SAlex Richardson * libauditd. 296*df093aa9SAlex Richardson */ 297*df093aa9SAlex Richardson atf_utils_create_file("started_fake_auditd", "yes\n"); 298*df093aa9SAlex Richardson ATF_REQUIRE(atf_utils_file_exists("started_fake_auditd")); 299*df093aa9SAlex Richardson ATF_REQUIRE_EQ_MSG(0, audit_quick_start(), 300*df093aa9SAlex Richardson "Failed to start fake auditd: %m"); 301*df093aa9SAlex Richardson fprintf(stderr, "done.\n"); 302*df093aa9SAlex Richardson /* audit_quick_start() should log an audit start event. */ 303f7f4e0f7SAlan Somers check_audit_startup(fd, "audit startup", pipestream); 304*df093aa9SAlex Richardson /* 305*df093aa9SAlex Richardson * If we exit cleanly shutdown audit_quick_start(), if not 306*df093aa9SAlex Richardson * cleanup() will take care of it. 307*df093aa9SAlex Richardson * This is not required, but makes it easier to run individual 308*df093aa9SAlex Richardson * tests outside of kyua. 309*df093aa9SAlex Richardson */ 310*df093aa9SAlex Richardson atexit(cleanup); 311*df093aa9SAlex Richardson } 312f7f4e0f7SAlan Somers 313f7f4e0f7SAlan Somers /* Set local preselection parameters specific to "name" audit_class */ 314f7f4e0f7SAlan Somers set_preselect_mode(fd[0].fd, &fmask); 315f7f4e0f7SAlan Somers return (pipestream); 316f7f4e0f7SAlan Somers } 317f7f4e0f7SAlan Somers 318f7f4e0f7SAlan Somers void 319f7f4e0f7SAlan Somers cleanup(void) 320f7f4e0f7SAlan Somers { 321*df093aa9SAlex Richardson if (atf_utils_file_exists("started_fake_auditd")) { 322*df093aa9SAlex Richardson fprintf(stderr, "Running audit_quick_stop()... "); 323*df093aa9SAlex Richardson if (audit_quick_stop() != 0) { 324*df093aa9SAlex Richardson fprintf(stderr, "Failed to stop fake auditd: %m\n"); 325*df093aa9SAlex Richardson abort(); 326*df093aa9SAlex Richardson } 327*df093aa9SAlex Richardson fprintf(stderr, "done.\n"); 328*df093aa9SAlex Richardson unlink("started_fake_auditd"); 329*df093aa9SAlex Richardson } 330f7f4e0f7SAlan Somers } 331