xref: /freebsd/tests/sys/audit/file-read.c (revision 2ba1d4970a06a1660b46f6fd99351d154b295683)
1 /*-
2  * Copyright 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 
26 #include <atf-c.h>
27 #include <fcntl.h>
28 
29 #include "utils.h"
30 
31 static struct pollfd fds[1];
32 static char buff[1024];
33 static const char *path = "fileforaudit";
34 static const char *successreg = "fileforaudit.*return,success";
35 static const char *failurereg = "fileforaudit.*return,failure";
36 
37 
38 ATF_TC_WITH_CLEANUP(readlink_success);
39 ATF_TC_HEAD(readlink_success, tc)
40 {
41 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
42 					"readlink(2) call");
43 }
44 
45 ATF_TC_BODY(readlink_success, tc)
46 {
47 	memset(buff, 0, sizeof(buff));
48 	ATF_REQUIRE_EQ(0, symlink("symlink", path));
49 	FILE *pipefd = setup(fds, "fr");
50 	ATF_REQUIRE(readlink(path, buff, sizeof(buff)-1) != -1);
51 	check_audit(fds, successreg, pipefd);
52 }
53 
54 ATF_TC_CLEANUP(readlink_success, tc)
55 {
56 	cleanup();
57 }
58 
59 
60 ATF_TC_WITH_CLEANUP(readlink_failure);
61 ATF_TC_HEAD(readlink_failure, tc)
62 {
63 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
64 					"readlink(2) call");
65 }
66 
67 ATF_TC_BODY(readlink_failure, tc)
68 {
69 	memset(buff, 0, sizeof(buff));
70 	FILE *pipefd = setup(fds, "fr");
71 	/* Failure reason: symbolic link does not exist */
72 	ATF_REQUIRE_EQ(-1, readlink(path, buff, sizeof(buff)-1));
73 	check_audit(fds, failurereg, pipefd);
74 }
75 
76 ATF_TC_CLEANUP(readlink_failure, tc)
77 {
78 	cleanup();
79 }
80 
81 
82 ATF_TC_WITH_CLEANUP(readlinkat_success);
83 ATF_TC_HEAD(readlinkat_success, tc)
84 {
85 	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
86 					"readlinkat(2) call");
87 }
88 
89 ATF_TC_BODY(readlinkat_success, tc)
90 {
91 	memset(buff, 0, sizeof(buff));
92 	ATF_REQUIRE_EQ(0, symlink("symlink", path));
93 	FILE *pipefd = setup(fds, "fr");
94 	ATF_REQUIRE(readlinkat(AT_FDCWD, path, buff, sizeof(buff)-1) != -1);
95 	check_audit(fds, successreg, pipefd);
96 }
97 
98 ATF_TC_CLEANUP(readlinkat_success, tc)
99 {
100 	cleanup();
101 }
102 
103 
104 ATF_TC_WITH_CLEANUP(readlinkat_failure);
105 ATF_TC_HEAD(readlinkat_failure, tc)
106 {
107 	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
108 					"readlinkat(2) call");
109 }
110 
111 ATF_TC_BODY(readlinkat_failure, tc)
112 {
113 	memset(buff, 0, sizeof(buff));
114 	FILE *pipefd = setup(fds, "fr");
115 	/* Failure reason: symbolic link does not exist */
116 	ATF_REQUIRE_EQ(-1, readlinkat(AT_FDCWD, path, buff, sizeof(buff)-1));
117 	check_audit(fds, failurereg, pipefd);
118 }
119 
120 ATF_TC_CLEANUP(readlinkat_failure, tc)
121 {
122 	cleanup();
123 }
124 
125 
126 ATF_TP_ADD_TCS(tp)
127 {
128 	ATF_TP_ADD_TC(tp, readlink_success);
129 	ATF_TP_ADD_TC(tp, readlink_failure);
130 	ATF_TP_ADD_TC(tp, readlinkat_success);
131 	ATF_TP_ADD_TC(tp, readlinkat_failure);
132 
133 	return (atf_no_error());
134 }
135