/*-1* Copyright (c) 2018 Aniket Pandey2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND13* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE16* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* SUCH DAMAGE.23*/2425#include <sys/ioctl.h>2627#include <bsm/libbsm.h>28#include <security/audit/audit_ioctl.h>2930#include <atf-c.h>31#include <fcntl.h>32#include <unistd.h>3334#include "utils.h"3536static int filedesc;37static char ioregex[80];38static const char *auclass = "io";39static struct pollfd fds[1];40static unsigned long request = AUDITPIPE_FLUSH;414243ATF_TC_WITH_CLEANUP(ioctl_success);44ATF_TC_HEAD(ioctl_success, tc)45{46atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "47"ioctl(2) call");48}4950ATF_TC_BODY(ioctl_success, tc)51{52/* auditpipe(4) supports quite a few ioctls */53ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);54/* Prepare the regex to be checked in the audit record */55snprintf(ioregex, sizeof(ioregex),56"ioctl.*%#lx.*%#x.*return,success", request, filedesc);5758FILE *pipefd = setup(fds, auclass);59ATF_REQUIRE(ioctl(filedesc, request) != -1);60check_audit(fds, ioregex, pipefd);61close(filedesc);62}6364ATF_TC_CLEANUP(ioctl_success, tc)65{66cleanup();67}686970ATF_TC_WITH_CLEANUP(ioctl_failure);71ATF_TC_HEAD(ioctl_failure, tc)72{73atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "74"ioctl(2) call");75}7677ATF_TC_BODY(ioctl_failure, tc)78{79snprintf(ioregex, sizeof(ioregex),80"ioctl.*%#lx.*return,failure : Bad file descriptor", request);8182FILE *pipefd = setup(fds, auclass);83/* Failure reason: Invalid file descriptor */84ATF_REQUIRE_EQ(-1, ioctl(-1, request));85check_audit(fds, ioregex, pipefd);86}8788ATF_TC_CLEANUP(ioctl_failure, tc)89{90cleanup();91}929394ATF_TP_ADD_TCS(tp)95{96ATF_TP_ADD_TC(tp, ioctl_success);97ATF_TP_ADD_TC(tp, ioctl_failure);9899return (atf_no_error());100}101102103