xref: /freebsd/tests/sys/audit/open.c (revision c5fda9bac0325eb8c5b447717862d279006f318f)
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 /*
29  * Note: open(2) and openat(2) have 12 events each for various values of 'flag'
30  * Please see: contrib/openbsm/etc/audit_event#L261
31  *
32  * 270:AUE_OPENAT_R:openat(2) - read:fr
33  * 271:AUE_OPENAT_RC:openat(2) - read,creat:fc,fr,fa,fm
34  * 272:AUE_OPENAT_RT:openat(2) - read,trunc:fd,fr,fa,fm
35  * 273:AUE_OPENAT_RTC:openat(2) - read,creat,trunc:fc,fd,fr,fa,fm
36  * 274:AUE_OPENAT_W:openat(2) - write:fw
37  * 275:AUE_OPENAT_WC:openat(2) - write,creat:fc,fw,fa,fm
38  * 276:AUE_OPENAT_WT:openat(2) - write,trunc:fd,fw,fa,fm
39  * 277:AUE_OPENAT_WTC:openat(2) - write,creat,trunc:fc,fd,fw,fa,fm
40  * 278:AUE_OPENAT_RW:openat(2) - read,write:fr,fw
41  * 279:AUE_OPENAT_RWC:openat(2) - read,write,create:fc,fw,fr,fa,fm
42  * 280:AUE_OPENAT_RWT:openat(2) - read,write,trunc:fd,fw,fr,fa,fm
43  * 281:AUE_OPENAT_RWTC:openat(2) - read,write,creat,trunc:fc,fd,fw,fr,fa,fm
44  */
45 
46 #include <sys/syscall.h>
47 
48 #include <atf-c.h>
49 #include <fcntl.h>
50 
51 #include "utils.h"
52 
53 static struct pollfd fds[1];
54 static mode_t o_mode = 0777;
55 static char extregex[80];
56 static const char *path = "fileforaudit";
57 static const char *errpath = "adirhasnoname/fileforaudit";
58 
59 /*
60  * Define test-cases for success and failure modes of both open(2) and openat(2)
61  */
62 #define OPEN_AT_TC_DEFINE(mode, regex, flag, class) \
63 ATF_TC_WITH_CLEANUP(open_ ## mode ## _success); \
64 ATF_TC_HEAD(open_ ## mode ## _success, tc) \
65 { \
66 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " \
67 				"open(2) call with flags = %s", #flag); \
68 } \
69 ATF_TC_BODY(open_ ## mode ## _success, tc) \
70 { \
71 	snprintf(extregex, sizeof(extregex), \
72 		"open.*%s.*fileforaudit.*return,success", regex); \
73 	/* File needs to exist for successful open(2) invocation */ \
74 	ATF_REQUIRE(open(path, O_CREAT, o_mode) != -1); \
75 	FILE *pipefd = setup(fds, class); \
76 	ATF_REQUIRE(syscall(SYS_open, path, flag) != -1); \
77 	check_audit(fds, extregex, pipefd); \
78 } \
79 ATF_TC_CLEANUP(open_ ## mode ## _success, tc) \
80 { \
81 	cleanup(); \
82 } \
83 ATF_TC_WITH_CLEANUP(open_ ## mode ## _failure); \
84 ATF_TC_HEAD(open_ ## mode ## _failure, tc) \
85 { \
86 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " \
87 				"open(2) call with flags = %s", #flag); \
88 } \
89 ATF_TC_BODY(open_ ## mode ## _failure, tc) \
90 { \
91 	snprintf(extregex, sizeof(extregex), \
92 		"open.*%s.*fileforaudit.*return,failure", regex); \
93 	FILE *pipefd = setup(fds, class); \
94 	ATF_REQUIRE_EQ(-1, syscall(SYS_open, errpath, flag)); \
95 	check_audit(fds, extregex, pipefd); \
96 } \
97 ATF_TC_CLEANUP(open_ ## mode ## _failure, tc) \
98 { \
99 	cleanup(); \
100 } \
101 ATF_TC_WITH_CLEANUP(openat_ ## mode ## _success); \
102 ATF_TC_HEAD(openat_ ## mode ## _success, tc) \
103 { \
104 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful " \
105 				"openat(2) call with flags = %s", #flag); \
106 } \
107 ATF_TC_BODY(openat_ ## mode ## _success, tc) \
108 { \
109 	snprintf(extregex, sizeof(extregex), \
110 		"openat.*%s.*fileforaudit.*return,success", regex); \
111 	/* File needs to exist for successful openat(2) invocation */ \
112 	ATF_REQUIRE(open(path, O_CREAT, o_mode) != -1); \
113 	FILE *pipefd = setup(fds, class); \
114 	ATF_REQUIRE(openat(AT_FDCWD, path, flag) != -1); \
115 	check_audit(fds, extregex, pipefd); \
116 } \
117 ATF_TC_CLEANUP(openat_ ## mode ## _success, tc) \
118 { \
119 	cleanup(); \
120 } \
121 ATF_TC_WITH_CLEANUP(openat_ ## mode ## _failure); \
122 ATF_TC_HEAD(openat_ ## mode ## _failure, tc) \
123 { \
124 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful " \
125 				"openat(2) call with flags = %s", #flag); \
126 } \
127 ATF_TC_BODY(openat_ ## mode ## _failure, tc) \
128 { \
129 	snprintf(extregex, sizeof(extregex), \
130 		"openat.*%s.*fileforaudit.*return,failure", regex); \
131 	FILE *pipefd = setup(fds, class); \
132 	ATF_REQUIRE_EQ(-1, openat(AT_FDCWD, errpath, flag)); \
133 	check_audit(fds, extregex, pipefd); \
134 } \
135 ATF_TC_CLEANUP(openat_ ## mode ## _failure, tc) \
136 { \
137 	cleanup(); \
138 }
139 
140 /*
141  * Add both success and failure modes of open(2) and openat(2)
142  */
143 #define OPEN_AT_TC_ADD(tp, mode) \
144 do { \
145 	ATF_TP_ADD_TC(tp, open_ ## mode ## _success); \
146 	ATF_TP_ADD_TC(tp, open_ ## mode ## _failure); \
147 	ATF_TP_ADD_TC(tp, openat_ ## mode ## _success); \
148 	ATF_TP_ADD_TC(tp, openat_ ## mode ## _failure); \
149 } while (0)
150 
151 
152 /*
153  * Each of the 12 OPEN_AT_TC_DEFINE statement is a group of 4 test-cases
154  * corresponding to separate audit events for open(2) and openat(2)
155  */
156 OPEN_AT_TC_DEFINE(read, "read", O_RDONLY, "fr")
157 OPEN_AT_TC_DEFINE(read_creat, "read,creat", O_RDONLY | O_CREAT, "fr")
158 OPEN_AT_TC_DEFINE(read_trunc, "read,trunc", O_RDONLY | O_TRUNC, "fr")
159 OPEN_AT_TC_DEFINE(read_creat_trunc, "read,creat,trunc", O_RDONLY | O_CREAT
160 	| O_TRUNC, "fr")
161 OPEN_AT_TC_DEFINE(write, "write", O_WRONLY, "fw")
162 OPEN_AT_TC_DEFINE(write_creat, "write,creat", O_WRONLY | O_CREAT, "fw")
163 OPEN_AT_TC_DEFINE(write_trunc, "write,trunc", O_WRONLY | O_TRUNC, "fw")
164 OPEN_AT_TC_DEFINE(write_creat_trunc, "write,creat,trunc", O_WRONLY | O_CREAT
165 	| O_TRUNC, "fw")
166 OPEN_AT_TC_DEFINE(read_write, "read,write", O_RDWR, "fr")
167 OPEN_AT_TC_DEFINE(read_write_creat, "read,write,creat", O_RDWR | O_CREAT, "fw")
168 OPEN_AT_TC_DEFINE(read_write_trunc, "read,write,trunc", O_RDWR | O_TRUNC, "fr")
169 OPEN_AT_TC_DEFINE(read_write_creat_trunc, "read,write,creat,trunc", O_RDWR |
170 	O_CREAT | O_TRUNC, "fw")
171 
172 
173 ATF_TP_ADD_TCS(tp)
174 {
175 	OPEN_AT_TC_ADD(tp, read);
176 	OPEN_AT_TC_ADD(tp, read_creat);
177 	OPEN_AT_TC_ADD(tp, read_trunc);
178 	OPEN_AT_TC_ADD(tp, read_creat_trunc);
179 
180 	OPEN_AT_TC_ADD(tp, write);
181 	OPEN_AT_TC_ADD(tp, write_creat);
182 	OPEN_AT_TC_ADD(tp, write_trunc);
183 	OPEN_AT_TC_ADD(tp, write_creat_trunc);
184 
185 	OPEN_AT_TC_ADD(tp, read_write);
186 	OPEN_AT_TC_ADD(tp, read_write_creat);
187 	OPEN_AT_TC_ADD(tp, read_write_trunc);
188 	OPEN_AT_TC_ADD(tp, read_write_creat_trunc);
189 
190 	return (atf_no_error());
191 }
192