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