1*c41bbc0aSAlan Somers /*- 2*c41bbc0aSAlan Somers * Copyright (c) 2018 Aniket Pandey 3*c41bbc0aSAlan Somers * 4*c41bbc0aSAlan Somers * Redistribution and use in source and binary forms, with or without 5*c41bbc0aSAlan Somers * modification, are permitted provided that the following conditions 6*c41bbc0aSAlan Somers * are met: 7*c41bbc0aSAlan Somers * 1. Redistributions of source code must retain the above copyright 8*c41bbc0aSAlan Somers * notice, this list of conditions and the following disclaimer. 9*c41bbc0aSAlan Somers * 2. Redistributions in binary form must reproduce the above copyright 10*c41bbc0aSAlan Somers * notice, this list of conditions and the following disclaimer in the 11*c41bbc0aSAlan Somers * documentation and/or other materials provided with the distribution. 12*c41bbc0aSAlan Somers * 13*c41bbc0aSAlan Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14*c41bbc0aSAlan Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15*c41bbc0aSAlan Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16*c41bbc0aSAlan Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17*c41bbc0aSAlan Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18*c41bbc0aSAlan Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19*c41bbc0aSAlan Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 20*c41bbc0aSAlan Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21*c41bbc0aSAlan Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22*c41bbc0aSAlan Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23*c41bbc0aSAlan Somers * SUCH DAMAGE. 24*c41bbc0aSAlan Somers * 25*c41bbc0aSAlan Somers * $FreeBSD$ 26*c41bbc0aSAlan Somers */ 27*c41bbc0aSAlan Somers 28*c41bbc0aSAlan Somers /* 29*c41bbc0aSAlan Somers * Note: open(2) and openat(2) have 12 events each for various values of 'flag' 30*c41bbc0aSAlan Somers * Please see: contrib/openbsm/etc/audit_event#L261 31*c41bbc0aSAlan Somers * 32*c41bbc0aSAlan Somers * 270:AUE_OPENAT_R:openat(2) - read:fr 33*c41bbc0aSAlan Somers * 271:AUE_OPENAT_RC:openat(2) - read,creat:fc,fr,fa,fm 34*c41bbc0aSAlan Somers * 272:AUE_OPENAT_RT:openat(2) - read,trunc:fd,fr,fa,fm 35*c41bbc0aSAlan Somers * 273:AUE_OPENAT_RTC:openat(2) - read,creat,trunc:fc,fd,fr,fa,fm 36*c41bbc0aSAlan Somers * 274:AUE_OPENAT_W:openat(2) - write:fw 37*c41bbc0aSAlan Somers * 275:AUE_OPENAT_WC:openat(2) - write,creat:fc,fw,fa,fm 38*c41bbc0aSAlan Somers * 276:AUE_OPENAT_WT:openat(2) - write,trunc:fd,fw,fa,fm 39*c41bbc0aSAlan Somers * 277:AUE_OPENAT_WTC:openat(2) - write,creat,trunc:fc,fd,fw,fa,fm 40*c41bbc0aSAlan Somers * 278:AUE_OPENAT_RW:openat(2) - read,write:fr,fw 41*c41bbc0aSAlan Somers * 279:AUE_OPENAT_RWC:openat(2) - read,write,create:fc,fw,fr,fa,fm 42*c41bbc0aSAlan Somers * 280:AUE_OPENAT_RWT:openat(2) - read,write,trunc:fd,fw,fr,fa,fm 43*c41bbc0aSAlan Somers * 281:AUE_OPENAT_RWTC:openat(2) - read,write,creat,trunc:fc,fd,fw,fr,fa,fm 44*c41bbc0aSAlan Somers */ 45*c41bbc0aSAlan Somers 46*c41bbc0aSAlan Somers #include <sys/syscall.h> 47*c41bbc0aSAlan Somers 48*c41bbc0aSAlan Somers #include <atf-c.h> 49*c41bbc0aSAlan Somers #include <fcntl.h> 50*c41bbc0aSAlan Somers 51*c41bbc0aSAlan Somers #include "utils.h" 52*c41bbc0aSAlan Somers 53*c41bbc0aSAlan Somers static struct pollfd fds[1]; 54*c41bbc0aSAlan Somers static mode_t o_mode = 0777; 55*c41bbc0aSAlan Somers static char extregex[80]; 56*c41bbc0aSAlan Somers static const char *path = "fileforaudit"; 57*c41bbc0aSAlan Somers static const char *errpath = "adirhasnoname/fileforaudit"; 58*c41bbc0aSAlan Somers 59*c41bbc0aSAlan Somers /* 60*c41bbc0aSAlan Somers * Define test-cases for success and failure modes of both open(2) and openat(2) 61*c41bbc0aSAlan Somers */ 62*c41bbc0aSAlan Somers #define OPEN_AT_TC_DEFINE(mode, regex, flag, class) \ 63*c41bbc0aSAlan Somers ATF_TC_WITH_CLEANUP(open_ ## mode ## _success); \ 64*c41bbc0aSAlan Somers ATF_TC_HEAD(open_ ## mode ## _success, tc) \ 65*c41bbc0aSAlan Somers { \ 66*c41bbc0aSAlan Somers atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " \ 67*c41bbc0aSAlan Somers "open(2) call with flags = %s", #flag); \ 68*c41bbc0aSAlan Somers } \ 69*c41bbc0aSAlan Somers ATF_TC_BODY(open_ ## mode ## _success, tc) \ 70*c41bbc0aSAlan Somers { \ 71*c41bbc0aSAlan Somers snprintf(extregex, sizeof(extregex), \ 72*c41bbc0aSAlan Somers "open.*%s.*fileforaudit.*return,success", regex); \ 73*c41bbc0aSAlan Somers /* File needs to exist for successful open(2) invocation */ \ 74*c41bbc0aSAlan Somers ATF_REQUIRE(open(path, O_CREAT, o_mode) != -1); \ 75*c41bbc0aSAlan Somers FILE *pipefd = setup(fds, class); \ 76*c41bbc0aSAlan Somers ATF_REQUIRE(syscall(SYS_open, path, flag) != -1); \ 77*c41bbc0aSAlan Somers check_audit(fds, extregex, pipefd); \ 78*c41bbc0aSAlan Somers } \ 79*c41bbc0aSAlan Somers ATF_TC_CLEANUP(open_ ## mode ## _success, tc) \ 80*c41bbc0aSAlan Somers { \ 81*c41bbc0aSAlan Somers cleanup(); \ 82*c41bbc0aSAlan Somers } \ 83*c41bbc0aSAlan Somers ATF_TC_WITH_CLEANUP(open_ ## mode ## _failure); \ 84*c41bbc0aSAlan Somers ATF_TC_HEAD(open_ ## mode ## _failure, tc) \ 85*c41bbc0aSAlan Somers { \ 86*c41bbc0aSAlan Somers atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " \ 87*c41bbc0aSAlan Somers "open(2) call with flags = %s", #flag); \ 88*c41bbc0aSAlan Somers } \ 89*c41bbc0aSAlan Somers ATF_TC_BODY(open_ ## mode ## _failure, tc) \ 90*c41bbc0aSAlan Somers { \ 91*c41bbc0aSAlan Somers snprintf(extregex, sizeof(extregex), \ 92*c41bbc0aSAlan Somers "open.*%s.*fileforaudit.*return,failure", regex); \ 93*c41bbc0aSAlan Somers FILE *pipefd = setup(fds, class); \ 94*c41bbc0aSAlan Somers ATF_REQUIRE_EQ(-1, syscall(SYS_open, errpath, flag)); \ 95*c41bbc0aSAlan Somers check_audit(fds, extregex, pipefd); \ 96*c41bbc0aSAlan Somers } \ 97*c41bbc0aSAlan Somers ATF_TC_CLEANUP(open_ ## mode ## _failure, tc) \ 98*c41bbc0aSAlan Somers { \ 99*c41bbc0aSAlan Somers cleanup(); \ 100*c41bbc0aSAlan Somers } \ 101*c41bbc0aSAlan Somers ATF_TC_WITH_CLEANUP(openat_ ## mode ## _success); \ 102*c41bbc0aSAlan Somers ATF_TC_HEAD(openat_ ## mode ## _success, tc) \ 103*c41bbc0aSAlan Somers { \ 104*c41bbc0aSAlan Somers atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " \ 105*c41bbc0aSAlan Somers "openat(2) call with flags = %s", #flag); \ 106*c41bbc0aSAlan Somers } \ 107*c41bbc0aSAlan Somers ATF_TC_BODY(openat_ ## mode ## _success, tc) \ 108*c41bbc0aSAlan Somers { \ 109*c41bbc0aSAlan Somers snprintf(extregex, sizeof(extregex), \ 110*c41bbc0aSAlan Somers "openat.*%s.*fileforaudit.*return,success", regex); \ 111*c41bbc0aSAlan Somers /* File needs to exist for successful openat(2) invocation */ \ 112*c41bbc0aSAlan Somers ATF_REQUIRE(open(path, O_CREAT, o_mode) != -1); \ 113*c41bbc0aSAlan Somers FILE *pipefd = setup(fds, class); \ 114*c41bbc0aSAlan Somers ATF_REQUIRE(openat(AT_FDCWD, path, flag) != -1); \ 115*c41bbc0aSAlan Somers check_audit(fds, extregex, pipefd); \ 116*c41bbc0aSAlan Somers } \ 117*c41bbc0aSAlan Somers ATF_TC_CLEANUP(openat_ ## mode ## _success, tc) \ 118*c41bbc0aSAlan Somers { \ 119*c41bbc0aSAlan Somers cleanup(); \ 120*c41bbc0aSAlan Somers } \ 121*c41bbc0aSAlan Somers ATF_TC_WITH_CLEANUP(openat_ ## mode ## _failure); \ 122*c41bbc0aSAlan Somers ATF_TC_HEAD(openat_ ## mode ## _failure, tc) \ 123*c41bbc0aSAlan Somers { \ 124*c41bbc0aSAlan Somers atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " \ 125*c41bbc0aSAlan Somers "openat(2) call with flags = %s", #flag); \ 126*c41bbc0aSAlan Somers } \ 127*c41bbc0aSAlan Somers ATF_TC_BODY(openat_ ## mode ## _failure, tc) \ 128*c41bbc0aSAlan Somers { \ 129*c41bbc0aSAlan Somers snprintf(extregex, sizeof(extregex), \ 130*c41bbc0aSAlan Somers "openat.*%s.*fileforaudit.*return,failure", regex); \ 131*c41bbc0aSAlan Somers FILE *pipefd = setup(fds, class); \ 132*c41bbc0aSAlan Somers ATF_REQUIRE_EQ(-1, openat(AT_FDCWD, errpath, flag)); \ 133*c41bbc0aSAlan Somers check_audit(fds, extregex, pipefd); \ 134*c41bbc0aSAlan Somers } \ 135*c41bbc0aSAlan Somers ATF_TC_CLEANUP(openat_ ## mode ## _failure, tc) \ 136*c41bbc0aSAlan Somers { \ 137*c41bbc0aSAlan Somers cleanup(); \ 138*c41bbc0aSAlan Somers } 139*c41bbc0aSAlan Somers 140*c41bbc0aSAlan Somers /* 141*c41bbc0aSAlan Somers * Add both success and failure modes of open(2) and openat(2) 142*c41bbc0aSAlan Somers */ 143*c41bbc0aSAlan Somers #define OPEN_AT_TC_ADD(tp, mode) \ 144*c41bbc0aSAlan Somers do { \ 145*c41bbc0aSAlan Somers ATF_TP_ADD_TC(tp, open_ ## mode ## _success); \ 146*c41bbc0aSAlan Somers ATF_TP_ADD_TC(tp, open_ ## mode ## _failure); \ 147*c41bbc0aSAlan Somers ATF_TP_ADD_TC(tp, openat_ ## mode ## _success); \ 148*c41bbc0aSAlan Somers ATF_TP_ADD_TC(tp, openat_ ## mode ## _failure); \ 149*c41bbc0aSAlan Somers } while (0) 150*c41bbc0aSAlan Somers 151*c41bbc0aSAlan Somers 152*c41bbc0aSAlan Somers /* 153*c41bbc0aSAlan Somers * Each of the 12 OPEN_AT_TC_DEFINE statement is a group of 4 test-cases 154*c41bbc0aSAlan Somers * corresponding to separate audit events for open(2) and openat(2) 155*c41bbc0aSAlan Somers */ 156*c41bbc0aSAlan Somers OPEN_AT_TC_DEFINE(read, "read", O_RDONLY, "fr") 157*c41bbc0aSAlan Somers OPEN_AT_TC_DEFINE(read_creat, "read,creat", O_RDONLY | O_CREAT, "fr") 158*c41bbc0aSAlan Somers OPEN_AT_TC_DEFINE(read_trunc, "read,trunc", O_RDONLY | O_TRUNC, "fr") 159*c41bbc0aSAlan Somers OPEN_AT_TC_DEFINE(read_creat_trunc, "read,creat,trunc", O_RDONLY | O_CREAT 160*c41bbc0aSAlan Somers | O_TRUNC, "fr") 161*c41bbc0aSAlan Somers OPEN_AT_TC_DEFINE(write, "write", O_WRONLY, "fw") 162*c41bbc0aSAlan Somers OPEN_AT_TC_DEFINE(write_creat, "write,creat", O_WRONLY | O_CREAT, "fw") 163*c41bbc0aSAlan Somers OPEN_AT_TC_DEFINE(write_trunc, "write,trunc", O_WRONLY | O_TRUNC, "fw") 164*c41bbc0aSAlan Somers OPEN_AT_TC_DEFINE(write_creat_trunc, "write,creat,trunc", O_WRONLY | O_CREAT 165*c41bbc0aSAlan Somers | O_TRUNC, "fw") 166*c41bbc0aSAlan Somers OPEN_AT_TC_DEFINE(read_write, "read,write", O_RDWR, "fr") 167*c41bbc0aSAlan Somers OPEN_AT_TC_DEFINE(read_write_creat, "read,write,creat", O_RDWR | O_CREAT, "fw") 168*c41bbc0aSAlan Somers OPEN_AT_TC_DEFINE(read_write_trunc, "read,write,trunc", O_RDWR | O_TRUNC, "fr") 169*c41bbc0aSAlan Somers OPEN_AT_TC_DEFINE(read_write_creat_trunc, "read,write,creat,trunc", O_RDWR | 170*c41bbc0aSAlan Somers O_CREAT | O_TRUNC, "fw") 171*c41bbc0aSAlan Somers 172*c41bbc0aSAlan Somers 173*c41bbc0aSAlan Somers ATF_TP_ADD_TCS(tp) 174*c41bbc0aSAlan Somers { 175*c41bbc0aSAlan Somers OPEN_AT_TC_ADD(tp, read); 176*c41bbc0aSAlan Somers OPEN_AT_TC_ADD(tp, read_creat); 177*c41bbc0aSAlan Somers OPEN_AT_TC_ADD(tp, read_trunc); 178*c41bbc0aSAlan Somers OPEN_AT_TC_ADD(tp, read_creat_trunc); 179*c41bbc0aSAlan Somers 180*c41bbc0aSAlan Somers OPEN_AT_TC_ADD(tp, write); 181*c41bbc0aSAlan Somers OPEN_AT_TC_ADD(tp, write_creat); 182*c41bbc0aSAlan Somers OPEN_AT_TC_ADD(tp, write_trunc); 183*c41bbc0aSAlan Somers OPEN_AT_TC_ADD(tp, write_creat_trunc); 184*c41bbc0aSAlan Somers 185*c41bbc0aSAlan Somers OPEN_AT_TC_ADD(tp, read_write); 186*c41bbc0aSAlan Somers OPEN_AT_TC_ADD(tp, read_write_creat); 187*c41bbc0aSAlan Somers OPEN_AT_TC_ADD(tp, read_write_trunc); 188*c41bbc0aSAlan Somers OPEN_AT_TC_ADD(tp, read_write_creat_trunc); 189*c41bbc0aSAlan Somers 190*c41bbc0aSAlan Somers return (atf_no_error()); 191*c41bbc0aSAlan Somers } 192