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> 33df093aa9SAlex 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); 82*c9730411SAlex Richardson fputc(',', memstream); 83f7f4e0f7SAlan Somers bytes += token.len; 84f7f4e0f7SAlan Somers } 85f7f4e0f7SAlan Somers 86f7f4e0f7SAlan Somers free(buff); 87b13a70d5SAlan Somers ATF_REQUIRE_EQ(0, fclose(memstream)); 88f7f4e0f7SAlan Somers return (atf_utils_grep_string("%s", membuff, auditregex)); 89f7f4e0f7SAlan Somers } 90f7f4e0f7SAlan Somers 91f7f4e0f7SAlan Somers /* 92f7f4e0f7SAlan Somers * Override the system-wide audit mask settings in /etc/security/audit_control 93f7f4e0f7SAlan Somers * and set the auditpipe's maximum allowed queue length limit 94f7f4e0f7SAlan Somers */ 95f7f4e0f7SAlan Somers static void 96f7f4e0f7SAlan Somers set_preselect_mode(int filedesc, au_mask_t *fmask) 97f7f4e0f7SAlan Somers { 98f7f4e0f7SAlan Somers int qlimit_max; 99f7f4e0f7SAlan Somers int fmode = AUDITPIPE_PRESELECT_MODE_LOCAL; 100f7f4e0f7SAlan Somers 101f7f4e0f7SAlan Somers /* Set local preselection mode for auditing */ 102f7f4e0f7SAlan Somers if (ioctl(filedesc, AUDITPIPE_SET_PRESELECT_MODE, &fmode) < 0) 103f7f4e0f7SAlan Somers atf_tc_fail("Preselection mode: %s", strerror(errno)); 104f7f4e0f7SAlan Somers 105f7f4e0f7SAlan Somers /* Set local preselection flag corresponding to the audit_event */ 106f7f4e0f7SAlan Somers if (ioctl(filedesc, AUDITPIPE_SET_PRESELECT_FLAGS, fmask) < 0) 107f7f4e0f7SAlan Somers atf_tc_fail("Preselection flag: %s", strerror(errno)); 108f7f4e0f7SAlan Somers 109f7f4e0f7SAlan Somers /* Set local preselection flag for non-attributable audit_events */ 110f7f4e0f7SAlan Somers if (ioctl(filedesc, AUDITPIPE_SET_PRESELECT_NAFLAGS, fmask) < 0) 111f7f4e0f7SAlan Somers atf_tc_fail("Preselection naflag: %s", strerror(errno)); 112f7f4e0f7SAlan Somers 113f7f4e0f7SAlan Somers /* Query the maximum possible queue length limit for auditpipe */ 114f7f4e0f7SAlan Somers if (ioctl(filedesc, AUDITPIPE_GET_QLIMIT_MAX, &qlimit_max) < 0) 115f7f4e0f7SAlan Somers atf_tc_fail("Query max-limit: %s", strerror(errno)); 116f7f4e0f7SAlan Somers 117f7f4e0f7SAlan Somers /* Set the queue length limit as obtained from previous step */ 118f7f4e0f7SAlan Somers if (ioctl(filedesc, AUDITPIPE_SET_QLIMIT, &qlimit_max) < 0) 119f7f4e0f7SAlan Somers atf_tc_fail("Set max-qlimit: %s", strerror(errno)); 120f7f4e0f7SAlan Somers 121f7f4e0f7SAlan Somers /* This removes any outstanding record on the auditpipe */ 122f7f4e0f7SAlan Somers if (ioctl(filedesc, AUDITPIPE_FLUSH) < 0) 123f7f4e0f7SAlan Somers atf_tc_fail("Auditpipe flush: %s", strerror(errno)); 124f7f4e0f7SAlan Somers } 125f7f4e0f7SAlan Somers 126f7f4e0f7SAlan Somers /* 127f7f4e0f7SAlan Somers * Get the corresponding audit_mask for class-name "name" then set the 128f7f4e0f7SAlan Somers * success and failure bits for fmask to be used as the ioctl argument 129f7f4e0f7SAlan Somers */ 130f7f4e0f7SAlan Somers static au_mask_t 131f7f4e0f7SAlan Somers get_audit_mask(const char *name) 132f7f4e0f7SAlan Somers { 133f7f4e0f7SAlan Somers au_mask_t fmask; 134f7f4e0f7SAlan Somers au_class_ent_t *class; 135f7f4e0f7SAlan Somers 136f7f4e0f7SAlan Somers ATF_REQUIRE((class = getauclassnam(name)) != NULL); 137f7f4e0f7SAlan Somers fmask.am_success = class->ac_class; 138f7f4e0f7SAlan Somers fmask.am_failure = class->ac_class; 139f7f4e0f7SAlan Somers return (fmask); 140f7f4e0f7SAlan Somers } 141f7f4e0f7SAlan Somers 142f7f4e0f7SAlan Somers /* 143f7f4e0f7SAlan Somers * Loop until the auditpipe returns something, check if it is what 144f7f4e0f7SAlan Somers * we want, else repeat the procedure until ppoll(2) times out. 145f7f4e0f7SAlan Somers */ 146f7f4e0f7SAlan Somers static void 147f7f4e0f7SAlan Somers check_auditpipe(struct pollfd fd[], const char *auditregex, FILE *pipestream) 148f7f4e0f7SAlan Somers { 149f7f4e0f7SAlan Somers struct timespec currtime, endtime, timeout; 150f7f4e0f7SAlan Somers 151f7f4e0f7SAlan Somers /* Set the expire time for poll(2) while waiting for syscall audit */ 152f7f4e0f7SAlan Somers ATF_REQUIRE_EQ(0, clock_gettime(CLOCK_MONOTONIC, &endtime)); 153869cc064SAlex Richardson /* Set limit to 30 seconds total and ~10s without an event. */ 154869cc064SAlex Richardson endtime.tv_sec += 30; 155f7f4e0f7SAlan Somers 156f7f4e0f7SAlan Somers for (;;) { 157f7f4e0f7SAlan Somers /* Update the time left for auditpipe to return any event */ 158f7f4e0f7SAlan Somers ATF_REQUIRE_EQ(0, clock_gettime(CLOCK_MONOTONIC, &currtime)); 159869cc064SAlex Richardson timespecsub(&endtime, &currtime, &timeout); 160869cc064SAlex Richardson timeout.tv_sec = MIN(timeout.tv_sec, 9); 161869cc064SAlex Richardson if (timeout.tv_sec < 0) { 162869cc064SAlex Richardson atf_tc_fail("%s not found in auditpipe within the " 163869cc064SAlex Richardson "time limit", auditregex); 164869cc064SAlex Richardson } 165f7f4e0f7SAlan Somers 166f7f4e0f7SAlan Somers switch (ppoll(fd, 1, &timeout, NULL)) { 167f7f4e0f7SAlan Somers /* ppoll(2) returns, check if it's what we want */ 168f7f4e0f7SAlan Somers case 1: 169f7f4e0f7SAlan Somers if (fd[0].revents & POLLIN) { 170f7f4e0f7SAlan Somers if (get_records(auditregex, pipestream)) 171f7f4e0f7SAlan Somers return; 172f7f4e0f7SAlan Somers } else { 173f7f4e0f7SAlan Somers atf_tc_fail("Auditpipe returned an " 174f7f4e0f7SAlan Somers "unknown event %#x", fd[0].revents); 175f7f4e0f7SAlan Somers } 176f7f4e0f7SAlan Somers break; 177f7f4e0f7SAlan Somers 178f7f4e0f7SAlan Somers /* poll(2) timed out */ 179f7f4e0f7SAlan Somers case 0: 180f7f4e0f7SAlan Somers atf_tc_fail("%s not found in auditpipe within the " 181f7f4e0f7SAlan Somers "time limit", auditregex); 182f7f4e0f7SAlan Somers break; 183f7f4e0f7SAlan Somers 184f7f4e0f7SAlan Somers /* poll(2) standard error */ 185f7f4e0f7SAlan Somers case -1: 186f7f4e0f7SAlan Somers atf_tc_fail("Poll: %s", strerror(errno)); 187f7f4e0f7SAlan Somers break; 188f7f4e0f7SAlan Somers 189f7f4e0f7SAlan Somers default: 190f7f4e0f7SAlan Somers atf_tc_fail("Poll returned too many file descriptors"); 191f7f4e0f7SAlan Somers } 192f7f4e0f7SAlan Somers } 193f7f4e0f7SAlan Somers } 194f7f4e0f7SAlan Somers 195f7f4e0f7SAlan Somers /* 196f7f4e0f7SAlan Somers * Wrapper functions around static "check_auditpipe" 197f7f4e0f7SAlan Somers */ 198f7f4e0f7SAlan Somers static void 199f7f4e0f7SAlan Somers check_audit_startup(struct pollfd fd[], const char *auditrgx, FILE *pipestream){ 200f7f4e0f7SAlan Somers check_auditpipe(fd, auditrgx, pipestream); 201f7f4e0f7SAlan Somers } 202f7f4e0f7SAlan Somers 203f7f4e0f7SAlan Somers void 204f7f4e0f7SAlan Somers check_audit(struct pollfd fd[], const char *auditrgx, FILE *pipestream) { 205f7f4e0f7SAlan Somers check_auditpipe(fd, auditrgx, pipestream); 206f7f4e0f7SAlan Somers 207b13a70d5SAlan Somers /* Teardown: /dev/auditpipe's instance opened for this test-suite */ 208b13a70d5SAlan Somers ATF_REQUIRE_EQ(0, fclose(pipestream)); 209f7f4e0f7SAlan Somers } 210f7f4e0f7SAlan Somers 21140407d39SAlex Richardson void 21240407d39SAlex Richardson skip_if_extattr_not_supported(const char *path) 21340407d39SAlex Richardson { 21440407d39SAlex Richardson ssize_t result; 21540407d39SAlex Richardson 21640407d39SAlex Richardson /* 21740407d39SAlex Richardson * Some file systems (e.g. tmpfs) do not support extattr, so we need 21840407d39SAlex Richardson * skip tests that use extattrs. To detect this we can check whether 21940407d39SAlex Richardson * the extattr_list_file returns EOPNOTSUPP. 22040407d39SAlex Richardson */ 22140407d39SAlex Richardson result = extattr_list_file(path, EXTATTR_NAMESPACE_USER, NULL, 0); 22240407d39SAlex Richardson if (result == -1 && errno == EOPNOTSUPP) { 22340407d39SAlex Richardson atf_tc_skip("File system does not support extattrs."); 22440407d39SAlex Richardson } 22540407d39SAlex Richardson } 22640407d39SAlex Richardson 227df093aa9SAlex Richardson static bool 228df093aa9SAlex Richardson is_auditd_running(void) 229df093aa9SAlex Richardson { 230df093aa9SAlex Richardson int trigger; 231df093aa9SAlex Richardson int err; 232df093aa9SAlex Richardson 233df093aa9SAlex Richardson /* 234df093aa9SAlex Richardson * AUDIT_TRIGGER_INITIALIZE is a no-op message on FreeBSD and can 235df093aa9SAlex Richardson * therefore be used to check whether auditd has already been started. 236df093aa9SAlex Richardson * This is significantly cheaper than running `service auditd onestatus` 237df093aa9SAlex Richardson * for each test case. It is also slightly less racy since it will only 238df093aa9SAlex Richardson * return true once auditd() has opened the trigger file rather than 239df093aa9SAlex Richardson * just when the pidfile has been created. 240df093aa9SAlex Richardson */ 241df093aa9SAlex Richardson trigger = AUDIT_TRIGGER_INITIALIZE; 242df093aa9SAlex Richardson err = auditon(A_SENDTRIGGER, &trigger, sizeof(trigger)); 243df093aa9SAlex Richardson if (err == 0) { 244df093aa9SAlex Richardson fprintf(stderr, "auditd(8) is running.\n"); 245df093aa9SAlex Richardson return (true); 246df093aa9SAlex Richardson } else { 247df093aa9SAlex Richardson /* 248df093aa9SAlex Richardson * A_SENDTRIGGER returns ENODEV if auditd isn't listening, 249df093aa9SAlex Richardson * all other error codes indicate a fatal error. 250df093aa9SAlex Richardson */ 251df093aa9SAlex Richardson ATF_REQUIRE_MSG(errno == ENODEV, 252df093aa9SAlex Richardson "Unexpected error from auditon(2): %s", strerror(errno)); 253df093aa9SAlex Richardson return (false); 254df093aa9SAlex Richardson } 255df093aa9SAlex Richardson 256df093aa9SAlex Richardson } 257df093aa9SAlex Richardson 258df093aa9SAlex Richardson FILE * 259df093aa9SAlex Richardson setup(struct pollfd fd[], const char *name) 260f7f4e0f7SAlan Somers { 261f7f4e0f7SAlan Somers au_mask_t fmask, nomask; 262df093aa9SAlex Richardson FILE *pipestream; 263f7f4e0f7SAlan Somers fmask = get_audit_mask(name); 264f7f4e0f7SAlan Somers nomask = get_audit_mask("no"); 265f7f4e0f7SAlan Somers 266b13a70d5SAlan Somers ATF_REQUIRE((fd[0].fd = open("/dev/auditpipe", O_RDONLY)) != -1); 267b13a70d5SAlan Somers ATF_REQUIRE((pipestream = fdopen(fd[0].fd, "r")) != NULL); 268f7f4e0f7SAlan Somers fd[0].events = POLLIN; 269f7f4e0f7SAlan Somers 270405f0931SAlan Somers /* 271405f0931SAlan Somers * Disable stream buffering for read operations from /dev/auditpipe. 272405f0931SAlan Somers * Otherwise it is possible that fread(3), called via au_read_rec(3), 273405f0931SAlan Somers * can store buffered data in user-space unbeknown to ppoll(2), which 274405f0931SAlan Somers * as a result, reports that /dev/auditpipe is empty. 275405f0931SAlan Somers */ 276405f0931SAlan Somers ATF_REQUIRE_EQ(0, setvbuf(pipestream, NULL, _IONBF, 0)); 277405f0931SAlan Somers 278f7f4e0f7SAlan Somers /* Set local preselection audit_class as "no" for audit startup */ 279f7f4e0f7SAlan Somers set_preselect_mode(fd[0].fd, &nomask); 280df093aa9SAlex Richardson if (!is_auditd_running()) { 281df093aa9SAlex Richardson fprintf(stderr, "Running audit_quick_start() for testing... "); 282df093aa9SAlex Richardson /* 283df093aa9SAlex Richardson * Previously, this test started auditd using 284df093aa9SAlex Richardson * `service auditd onestart`. However, there is a race condition 285df093aa9SAlex Richardson * there since service can return before auditd(8) has 286df093aa9SAlex Richardson * fully started (once the daemon parent process has forked) 287df093aa9SAlex Richardson * and this can cause check_audit_startup() to fail sometimes. 288df093aa9SAlex Richardson * 289df093aa9SAlex Richardson * In the CheriBSD CI this caused the first test executed by 290df093aa9SAlex Richardson * kyua (administrative:acct_failure) to fail every time, but 291df093aa9SAlex Richardson * subsequent ones would almost always succeed. 292df093aa9SAlex Richardson * 293df093aa9SAlex Richardson * To avoid this problem (and as a nice side-effect this speeds 294df093aa9SAlex Richardson * up the test quite a bit), we register this process as a 295df093aa9SAlex Richardson * "fake" auditd(8) using the audit_quick_start() function from 296df093aa9SAlex Richardson * libauditd. 297df093aa9SAlex Richardson */ 298df093aa9SAlex Richardson atf_utils_create_file("started_fake_auditd", "yes\n"); 299df093aa9SAlex Richardson ATF_REQUIRE(atf_utils_file_exists("started_fake_auditd")); 300df093aa9SAlex Richardson ATF_REQUIRE_EQ_MSG(0, audit_quick_start(), 301df093aa9SAlex Richardson "Failed to start fake auditd: %m"); 302df093aa9SAlex Richardson fprintf(stderr, "done.\n"); 303df093aa9SAlex Richardson /* audit_quick_start() should log an audit start event. */ 304f7f4e0f7SAlan Somers check_audit_startup(fd, "audit startup", pipestream); 305df093aa9SAlex Richardson /* 306df093aa9SAlex Richardson * If we exit cleanly shutdown audit_quick_start(), if not 307df093aa9SAlex Richardson * cleanup() will take care of it. 308df093aa9SAlex Richardson * This is not required, but makes it easier to run individual 309df093aa9SAlex Richardson * tests outside of kyua. 310df093aa9SAlex Richardson */ 311df093aa9SAlex Richardson atexit(cleanup); 312df093aa9SAlex Richardson } 313f7f4e0f7SAlan Somers 314f7f4e0f7SAlan Somers /* Set local preselection parameters specific to "name" audit_class */ 315f7f4e0f7SAlan Somers set_preselect_mode(fd[0].fd, &fmask); 316f7f4e0f7SAlan Somers return (pipestream); 317f7f4e0f7SAlan Somers } 318f7f4e0f7SAlan Somers 319f7f4e0f7SAlan Somers void 320f7f4e0f7SAlan Somers cleanup(void) 321f7f4e0f7SAlan Somers { 322df093aa9SAlex Richardson if (atf_utils_file_exists("started_fake_auditd")) { 323df093aa9SAlex Richardson fprintf(stderr, "Running audit_quick_stop()... "); 324df093aa9SAlex Richardson if (audit_quick_stop() != 0) { 325df093aa9SAlex Richardson fprintf(stderr, "Failed to stop fake auditd: %m\n"); 326df093aa9SAlex Richardson abort(); 327df093aa9SAlex Richardson } 328df093aa9SAlex Richardson fprintf(stderr, "done.\n"); 329df093aa9SAlex Richardson unlink("started_fake_auditd"); 330df093aa9SAlex Richardson } 331f7f4e0f7SAlan Somers } 332