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