1*3468bf40SAlan Somers /*-
2*3468bf40SAlan Somers * Copyright (c) 2018 Aniket Pandey
3*3468bf40SAlan Somers *
4*3468bf40SAlan Somers * Redistribution and use in source and binary forms, with or without
5*3468bf40SAlan Somers * modification, are permitted provided that the following conditions
6*3468bf40SAlan Somers * are met:
7*3468bf40SAlan Somers * 1. Redistributions of source code must retain the above copyright
8*3468bf40SAlan Somers * notice, this list of conditions and the following disclaimer.
9*3468bf40SAlan Somers * 2. Redistributions in binary form must reproduce the above copyright
10*3468bf40SAlan Somers * notice, this list of conditions and the following disclaimer in the
11*3468bf40SAlan Somers * documentation and/or other materials provided with the distribution.
12*3468bf40SAlan Somers *
13*3468bf40SAlan Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*3468bf40SAlan Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*3468bf40SAlan Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*3468bf40SAlan Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*3468bf40SAlan Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*3468bf40SAlan Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*3468bf40SAlan Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20*3468bf40SAlan Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*3468bf40SAlan Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*3468bf40SAlan Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*3468bf40SAlan Somers * SUCH DAMAGE.
24*3468bf40SAlan Somers */
25*3468bf40SAlan Somers
26*3468bf40SAlan Somers #include <sys/ioctl.h>
27*3468bf40SAlan Somers
28*3468bf40SAlan Somers #include <bsm/audit.h>
29*3468bf40SAlan Somers #include <security/audit/audit_ioctl.h>
30*3468bf40SAlan Somers
31*3468bf40SAlan Somers #include <atf-c.h>
32*3468bf40SAlan Somers #include <fcntl.h>
33*3468bf40SAlan Somers #include <stdio.h>
34*3468bf40SAlan Somers #include <unistd.h>
35*3468bf40SAlan Somers
36*3468bf40SAlan Somers static int filedesc;
37*3468bf40SAlan Somers static FILE *fileptr;
38*3468bf40SAlan Somers
39*3468bf40SAlan Somers ATF_TC(auditpipe_get_qlen);
ATF_TC_HEAD(auditpipe_get_qlen,tc)40*3468bf40SAlan Somers ATF_TC_HEAD(auditpipe_get_qlen, tc)
41*3468bf40SAlan Somers {
42*3468bf40SAlan Somers atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
43*3468bf40SAlan Somers "AUDITPIPE_GET_QLEN works properly");
44*3468bf40SAlan Somers }
45*3468bf40SAlan Somers
ATF_TC_BODY(auditpipe_get_qlen,tc)46*3468bf40SAlan Somers ATF_TC_BODY(auditpipe_get_qlen, tc)
47*3468bf40SAlan Somers {
48*3468bf40SAlan Somers int qlen = -1;
49*3468bf40SAlan Somers ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
50*3468bf40SAlan Somers ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLEN, &qlen));
51*3468bf40SAlan Somers ATF_REQUIRE(qlen != -1);
52*3468bf40SAlan Somers close(filedesc);
53*3468bf40SAlan Somers }
54*3468bf40SAlan Somers
55*3468bf40SAlan Somers
56*3468bf40SAlan Somers ATF_TC(auditpipe_get_qlimit);
ATF_TC_HEAD(auditpipe_get_qlimit,tc)57*3468bf40SAlan Somers ATF_TC_HEAD(auditpipe_get_qlimit, tc)
58*3468bf40SAlan Somers {
59*3468bf40SAlan Somers atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
60*3468bf40SAlan Somers "AUDITPIPE_GET_QLIMIT works properly");
61*3468bf40SAlan Somers }
62*3468bf40SAlan Somers
ATF_TC_BODY(auditpipe_get_qlimit,tc)63*3468bf40SAlan Somers ATF_TC_BODY(auditpipe_get_qlimit, tc)
64*3468bf40SAlan Somers {
65*3468bf40SAlan Somers int qlimit = -1;
66*3468bf40SAlan Somers ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
67*3468bf40SAlan Somers ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT, &qlimit));
68*3468bf40SAlan Somers ATF_REQUIRE(qlimit != -1);
69*3468bf40SAlan Somers close(filedesc);
70*3468bf40SAlan Somers }
71*3468bf40SAlan Somers
72*3468bf40SAlan Somers
73*3468bf40SAlan Somers ATF_TC_WITH_CLEANUP(auditpipe_set_qlimit);
ATF_TC_HEAD(auditpipe_set_qlimit,tc)74*3468bf40SAlan Somers ATF_TC_HEAD(auditpipe_set_qlimit, tc)
75*3468bf40SAlan Somers {
76*3468bf40SAlan Somers atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
77*3468bf40SAlan Somers "AUDITPIPE_SET_QLIMIT works properly");
78*3468bf40SAlan Somers }
79*3468bf40SAlan Somers
ATF_TC_BODY(auditpipe_set_qlimit,tc)80*3468bf40SAlan Somers ATF_TC_BODY(auditpipe_set_qlimit, tc)
81*3468bf40SAlan Somers {
82*3468bf40SAlan Somers int test_qlimit, curr_qlimit, recv_qlimit;
83*3468bf40SAlan Somers
84*3468bf40SAlan Somers ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
85*3468bf40SAlan Somers /* Retreive the current QLIMIT value and store it in a file */
86*3468bf40SAlan Somers ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT, &curr_qlimit));
87*3468bf40SAlan Somers ATF_REQUIRE((fileptr = fopen("qlimit_store", "a")) != NULL);
88*3468bf40SAlan Somers ATF_REQUIRE_EQ(sizeof(curr_qlimit),
89*3468bf40SAlan Somers fprintf(fileptr, "%d\n", curr_qlimit));
90*3468bf40SAlan Somers
91*3468bf40SAlan Somers /*
92*3468bf40SAlan Somers * Set QLIMIT different from the current system value to confirm
93*3468bf40SAlan Somers * proper functioning of AUDITPIPE_SET_QLIMIT ioctl.
94*3468bf40SAlan Somers */
95*3468bf40SAlan Somers test_qlimit = curr_qlimit - 1;
96*3468bf40SAlan Somers ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_SET_QLIMIT, &test_qlimit));
97*3468bf40SAlan Somers /* Receive modified value and check whether QLIMIT was set correctly */
98*3468bf40SAlan Somers ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT, &recv_qlimit));
99*3468bf40SAlan Somers ATF_REQUIRE_EQ(test_qlimit, recv_qlimit);
100*3468bf40SAlan Somers
101*3468bf40SAlan Somers fclose(fileptr);
102*3468bf40SAlan Somers close(filedesc);
103*3468bf40SAlan Somers }
104*3468bf40SAlan Somers
ATF_TC_CLEANUP(auditpipe_set_qlimit,tc)105*3468bf40SAlan Somers ATF_TC_CLEANUP(auditpipe_set_qlimit, tc)
106*3468bf40SAlan Somers {
107*3468bf40SAlan Somers if (atf_utils_file_exists("qlimit_store")) {
108*3468bf40SAlan Somers int fd, curr_qlim;
109*3468bf40SAlan Somers ATF_REQUIRE((fileptr = fopen("qlimit_store", "r")) != NULL);
110*3468bf40SAlan Somers ATF_REQUIRE(fscanf(fileptr, "%d", &curr_qlim));
111*3468bf40SAlan Somers
112*3468bf40SAlan Somers ATF_REQUIRE((fd = open("/dev/auditpipe", O_RDONLY)) != -1);
113*3468bf40SAlan Somers /* Set QLIMIT's value as it was prior to test-case invocation */
114*3468bf40SAlan Somers ATF_REQUIRE_EQ(0, ioctl(fd, AUDITPIPE_SET_QLIMIT, &curr_qlim));
115*3468bf40SAlan Somers
116*3468bf40SAlan Somers close(fd);
117*3468bf40SAlan Somers fclose(fileptr);
118*3468bf40SAlan Somers }
119*3468bf40SAlan Somers }
120*3468bf40SAlan Somers
121*3468bf40SAlan Somers
122*3468bf40SAlan Somers ATF_TC(auditpipe_get_qlimit_min);
ATF_TC_HEAD(auditpipe_get_qlimit_min,tc)123*3468bf40SAlan Somers ATF_TC_HEAD(auditpipe_get_qlimit_min, tc)
124*3468bf40SAlan Somers {
125*3468bf40SAlan Somers atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
126*3468bf40SAlan Somers "AUDITPIPE_GET_QLIMIT_MIN works properly");
127*3468bf40SAlan Somers }
128*3468bf40SAlan Somers
ATF_TC_BODY(auditpipe_get_qlimit_min,tc)129*3468bf40SAlan Somers ATF_TC_BODY(auditpipe_get_qlimit_min, tc)
130*3468bf40SAlan Somers {
131*3468bf40SAlan Somers int qlim_min = -1;
132*3468bf40SAlan Somers ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
133*3468bf40SAlan Somers ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT_MIN, &qlim_min));
134*3468bf40SAlan Somers ATF_REQUIRE(qlim_min != -1);
135*3468bf40SAlan Somers close(filedesc);
136*3468bf40SAlan Somers }
137*3468bf40SAlan Somers
138*3468bf40SAlan Somers
139*3468bf40SAlan Somers ATF_TC(auditpipe_get_qlimit_max);
ATF_TC_HEAD(auditpipe_get_qlimit_max,tc)140*3468bf40SAlan Somers ATF_TC_HEAD(auditpipe_get_qlimit_max, tc)
141*3468bf40SAlan Somers {
142*3468bf40SAlan Somers atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
143*3468bf40SAlan Somers "AUDITPIPE_GET_QLIMIT_MAX works properly");
144*3468bf40SAlan Somers }
145*3468bf40SAlan Somers
ATF_TC_BODY(auditpipe_get_qlimit_max,tc)146*3468bf40SAlan Somers ATF_TC_BODY(auditpipe_get_qlimit_max, tc)
147*3468bf40SAlan Somers {
148*3468bf40SAlan Somers int qlim_max = -1;
149*3468bf40SAlan Somers ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
150*3468bf40SAlan Somers ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_QLIMIT_MAX, &qlim_max));
151*3468bf40SAlan Somers ATF_REQUIRE(qlim_max != -1);
152*3468bf40SAlan Somers close(filedesc);
153*3468bf40SAlan Somers }
154*3468bf40SAlan Somers
155*3468bf40SAlan Somers
156*3468bf40SAlan Somers ATF_TC(auditpipe_get_maxauditdata);
ATF_TC_HEAD(auditpipe_get_maxauditdata,tc)157*3468bf40SAlan Somers ATF_TC_HEAD(auditpipe_get_maxauditdata, tc)
158*3468bf40SAlan Somers {
159*3468bf40SAlan Somers atf_tc_set_md_var(tc, "descr", "Verifies whether the auditpipe ioctl, "
160*3468bf40SAlan Somers "AUDITPIPE_GET_MAXAUDITDATA works properly");
161*3468bf40SAlan Somers }
162*3468bf40SAlan Somers
ATF_TC_BODY(auditpipe_get_maxauditdata,tc)163*3468bf40SAlan Somers ATF_TC_BODY(auditpipe_get_maxauditdata, tc)
164*3468bf40SAlan Somers {
165*3468bf40SAlan Somers int audata = -1;
166*3468bf40SAlan Somers ATF_REQUIRE((filedesc = open("/dev/auditpipe", O_RDONLY)) != -1);
167*3468bf40SAlan Somers ATF_REQUIRE_EQ(0, ioctl(filedesc, AUDITPIPE_GET_MAXAUDITDATA, &audata));
168*3468bf40SAlan Somers ATF_REQUIRE(audata != -1);
169*3468bf40SAlan Somers close(filedesc);
170*3468bf40SAlan Somers }
171*3468bf40SAlan Somers
172*3468bf40SAlan Somers
ATF_TP_ADD_TCS(tp)173*3468bf40SAlan Somers ATF_TP_ADD_TCS(tp)
174*3468bf40SAlan Somers {
175*3468bf40SAlan Somers ATF_TP_ADD_TC(tp, auditpipe_get_qlen);
176*3468bf40SAlan Somers ATF_TP_ADD_TC(tp, auditpipe_get_qlimit);
177*3468bf40SAlan Somers ATF_TP_ADD_TC(tp, auditpipe_set_qlimit);
178*3468bf40SAlan Somers ATF_TP_ADD_TC(tp, auditpipe_get_qlimit_min);
179*3468bf40SAlan Somers ATF_TP_ADD_TC(tp, auditpipe_get_qlimit_max);
180*3468bf40SAlan Somers ATF_TP_ADD_TC(tp, auditpipe_get_maxauditdata);
181*3468bf40SAlan Somers
182*3468bf40SAlan Somers return (atf_no_error());
183*3468bf40SAlan Somers }
184