1*4003d938SAlan Somers /*-
2*4003d938SAlan Somers * Copyright (c) 2018 Aniket Pandey
3*4003d938SAlan Somers *
4*4003d938SAlan Somers * Redistribution and use in source and binary forms, with or without
5*4003d938SAlan Somers * modification, are permitted provided that the following conditions
6*4003d938SAlan Somers * are met:
7*4003d938SAlan Somers * 1. Redistributions of source code must retain the above copyright
8*4003d938SAlan Somers * notice, this list of conditions and the following disclaimer.
9*4003d938SAlan Somers * 2. Redistributions in binary form must reproduce the above copyright
10*4003d938SAlan Somers * notice, this list of conditions and the following disclaimer in the
11*4003d938SAlan Somers * documentation and/or other materials provided with the distribution.
12*4003d938SAlan Somers *
13*4003d938SAlan Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*4003d938SAlan Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*4003d938SAlan Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*4003d938SAlan Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*4003d938SAlan Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*4003d938SAlan Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*4003d938SAlan Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20*4003d938SAlan Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*4003d938SAlan Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*4003d938SAlan Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*4003d938SAlan Somers * SUCH DAMAGE.
24*4003d938SAlan Somers */
25*4003d938SAlan Somers
26*4003d938SAlan Somers #include <sys/ioctl.h>
27*4003d938SAlan Somers
28*4003d938SAlan Somers #include <bsm/libbsm.h>
29*4003d938SAlan Somers #include <security/audit/audit_ioctl.h>
30*4003d938SAlan Somers
31*4003d938SAlan Somers #include <atf-c.h>
32*4003d938SAlan Somers #include <fcntl.h>
33*4003d938SAlan Somers #include <unistd.h>
34*4003d938SAlan Somers
35*4003d938SAlan Somers #include "utils.h"
36*4003d938SAlan Somers
37*4003d938SAlan Somers static int filedesc;
38*4003d938SAlan Somers static char ioregex[80];
39*4003d938SAlan Somers static const char *auclass = "io";
40*4003d938SAlan Somers static struct pollfd fds[1];
41*4003d938SAlan Somers static unsigned long request = AUDITPIPE_FLUSH;
42*4003d938SAlan Somers
43*4003d938SAlan Somers
44*4003d938SAlan Somers ATF_TC_WITH_CLEANUP(ioctl_success);
ATF_TC_HEAD(ioctl_success,tc)45*4003d938SAlan Somers ATF_TC_HEAD(ioctl_success, tc)
46*4003d938SAlan Somers {
47*4003d938SAlan Somers atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
48*4003d938SAlan Somers "ioctl(2) call");
49*4003d938SAlan Somers }
50*4003d938SAlan Somers
ATF_TC_BODY(ioctl_success,tc)51*4003d938SAlan Somers ATF_TC_BODY(ioctl_success, tc)
52*4003d938SAlan Somers {
53*4003d938SAlan Somers /* auditpipe(4) supports quite a few ioctls */
54*4003d938SAlan Somers ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
55*4003d938SAlan Somers /* Prepare the regex to be checked in the audit record */
56*4003d938SAlan Somers snprintf(ioregex, sizeof(ioregex),
57*4003d938SAlan Somers "ioctl.*%#lx.*%#x.*return,success", request, filedesc);
58*4003d938SAlan Somers
59*4003d938SAlan Somers FILE *pipefd = setup(fds, auclass);
60*4003d938SAlan Somers ATF_REQUIRE(ioctl(filedesc, request) != -1);
61*4003d938SAlan Somers check_audit(fds, ioregex, pipefd);
62*4003d938SAlan Somers close(filedesc);
63*4003d938SAlan Somers }
64*4003d938SAlan Somers
ATF_TC_CLEANUP(ioctl_success,tc)65*4003d938SAlan Somers ATF_TC_CLEANUP(ioctl_success, tc)
66*4003d938SAlan Somers {
67*4003d938SAlan Somers cleanup();
68*4003d938SAlan Somers }
69*4003d938SAlan Somers
70*4003d938SAlan Somers
71*4003d938SAlan Somers ATF_TC_WITH_CLEANUP(ioctl_failure);
ATF_TC_HEAD(ioctl_failure,tc)72*4003d938SAlan Somers ATF_TC_HEAD(ioctl_failure, tc)
73*4003d938SAlan Somers {
74*4003d938SAlan Somers atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
75*4003d938SAlan Somers "ioctl(2) call");
76*4003d938SAlan Somers }
77*4003d938SAlan Somers
ATF_TC_BODY(ioctl_failure,tc)78*4003d938SAlan Somers ATF_TC_BODY(ioctl_failure, tc)
79*4003d938SAlan Somers {
80*4003d938SAlan Somers snprintf(ioregex, sizeof(ioregex),
81*4003d938SAlan Somers "ioctl.*%#lx.*return,failure : Bad file descriptor", request);
82*4003d938SAlan Somers
83*4003d938SAlan Somers FILE *pipefd = setup(fds, auclass);
84*4003d938SAlan Somers /* Failure reason: Invalid file descriptor */
85*4003d938SAlan Somers ATF_REQUIRE_EQ(-1, ioctl(-1, request));
86*4003d938SAlan Somers check_audit(fds, ioregex, pipefd);
87*4003d938SAlan Somers }
88*4003d938SAlan Somers
ATF_TC_CLEANUP(ioctl_failure,tc)89*4003d938SAlan Somers ATF_TC_CLEANUP(ioctl_failure, tc)
90*4003d938SAlan Somers {
91*4003d938SAlan Somers cleanup();
92*4003d938SAlan Somers }
93*4003d938SAlan Somers
94*4003d938SAlan Somers
ATF_TP_ADD_TCS(tp)95*4003d938SAlan Somers ATF_TP_ADD_TCS(tp)
96*4003d938SAlan Somers {
97*4003d938SAlan Somers ATF_TP_ADD_TC(tp, ioctl_success);
98*4003d938SAlan Somers ATF_TP_ADD_TC(tp, ioctl_failure);
99*4003d938SAlan Somers
100*4003d938SAlan Somers return (atf_no_error());
101*4003d938SAlan Somers }
102