aboutsummaryrefslogtreecommitdiff
path: root/tests/sys/audit/miscellaneous.c
blob: 727d8300722f705126abc1901de141885ceef626 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*-
 * Copyright (c) 2018 Aniket Pandey
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/types.h>
#include <sys/sysctl.h>

#include <bsm/audit.h>
#include <machine/sysarch.h>

#include <atf-c.h>
#include <unistd.h>

#include "utils.h"

static pid_t pid;
static char miscreg[80];
static struct pollfd fds[1];
static const char *auclass = "ot";


/*
 * Success case of audit(2) is skipped for now as the behaviour is quite
 * undeterministic. It will be added when the intermittency is resolved.
 */


ATF_TC_WITH_CLEANUP(audit_failure);
ATF_TC_HEAD(audit_failure, tc)
{
	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
					"audit(2) call");
}

ATF_TC_BODY(audit_failure, tc)
{
	pid = getpid();
	snprintf(miscreg, sizeof(miscreg), "audit.*%d.*return,failure", pid);

	FILE *pipefd = setup(fds, auclass);
	/* Failure reason: Invalid argument */
	ATF_REQUIRE_EQ(-1, audit(NULL, -1));
	check_audit(fds, miscreg, pipefd);
}

ATF_TC_CLEANUP(audit_failure, tc)
{
	cleanup();
}


ATF_TC_WITH_CLEANUP(sysarch_success);
ATF_TC_HEAD(sysarch_success, tc)
{
	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
					"sysarch(2) call");
}

ATF_TC_BODY(sysarch_success, tc)
{
	pid = getpid();
	snprintf(miscreg, sizeof(miscreg), "sysarch.*%d.*return,success", pid);

	/* Set sysnum to the syscall corresponding to the system architecture */
#if defined(I386_GET_IOPERM)		/* i386 */
	struct i386_ioperm_args i3sysarg;
	bzero(&i3sysarg, sizeof(i3sysarg));

#elif defined(AMD64_GET_FSBASE)		/* amd64 */
	register_t amd64arg;

#elif defined(MIPS_GET_TLS)		/* MIPS */
	char *mipsarg;

#elif defined(ARM_SYNC_ICACHE)		/* ARM */
	struct arm_sync_icache_args armsysarg;
	bzero(&armsysarg, sizeof(armsysarg));

#elif defined(SPARC_UTRAP_INSTALL)	/* Sparc64 */
	struct sparc_utrap_args handler = {
		.type		= UT_DIVISION_BY_ZERO,
		/* We don't want to change the previous handlers */
		.new_precise	= (void *)UTH_NOCHANGE,
		.new_deferred	= (void *)UTH_NOCHANGE,
		.old_precise	= NULL,
		.old_deferred	= NULL
	};

	struct sparc_utrap_install_args sparc64arg = {
		.num 		= ST_DIVISION_BY_ZERO,
		.handlers	= &handler
	};
#else
	/* For PowerPC, ARM64, RISCV archs, sysarch(2) is not supported */
	atf_tc_skip("sysarch(2) is not supported for the system architecture");
#endif

	FILE *pipefd = setup(fds, auclass);
#if defined(I386_GET_IOPERM)
	ATF_REQUIRE_EQ(0, sysarch(I386_GET_IOPERM, &i3sysarg));
#elif defined(AMD64_GET_FSBASE)
	ATF_REQUIRE_EQ(0, sysarch(AMD64_GET_FSBASE, &amd64arg));
#elif defined(MIPS_GET_TLS)
	ATF_REQUIRE_EQ(0, sysarch(MIPS_GET_TLS, &mipsarg));
#elif defined(ARM_SYNC_ICACHE)
	ATF_REQUIRE_EQ(0, sysarch(ARM_SYNC_ICACHE, &armsysarg));
#elif defined(SPARC_UTRAP_INSTALL)
	ATF_REQUIRE_EQ(0, sysarch(SPARC_UTRAP_INSTALL, &sparc64arg));
#endif
	check_audit(fds, miscreg, pipefd);
}

ATF_TC_CLEANUP(sysarch_success, tc)
{
	cleanup();
}


ATF_TC_WITH_CLEANUP(sysarch_failure);
ATF_TC_HEAD(sysarch_failure, tc)
{
	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
				       "sysarch(2) call for any architecture");
}

ATF_TC_BODY(sysarch_failure, tc)
{
	pid = getpid();
	snprintf(miscreg, sizeof(miscreg), "sysarch.*%d.*return,failure", pid);

	FILE *pipefd = setup(fds, auclass);
	/* Failure reason: Invalid argument and Bad address */
	ATF_REQUIRE_EQ(-1, sysarch(-1, NULL));
	check_audit(fds, miscreg, pipefd);
}

ATF_TC_CLEANUP(sysarch_failure, tc)
{
	cleanup();
}


ATF_TC_WITH_CLEANUP(sysctl_success);
ATF_TC_HEAD(sysctl_success, tc)
{
	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
					"sysctl(3) call");
}

ATF_TC_BODY(sysctl_success, tc)
{
	int mib[2], maxproc;
	size_t proclen;

	/* Set mib to retrieve the maximum number of allowed processes */
	mib[0] = CTL_KERN;
	mib[1] = KERN_MAXPROC;
	proclen = sizeof(maxproc);

	pid = getpid();
	snprintf(miscreg, sizeof(miscreg), "sysctl.*%d.*return,success", pid);

	FILE *pipefd = setup(fds, auclass);
	ATF_REQUIRE_EQ(0, sysctl(mib, 2, &maxproc, &proclen, NULL, 0));
	check_audit(fds, miscreg, pipefd);
}

ATF_TC_CLEANUP(sysctl_success, tc)
{
	cleanup();
}


ATF_TC_WITH_CLEANUP(sysctl_failure);
ATF_TC_HEAD(sysctl_failure, tc)
{
	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
					"sysctl(3) call");
}

ATF_TC_BODY(sysctl_failure, tc)
{
	pid = getpid();
	snprintf(miscreg, sizeof(miscreg), "sysctl.*%d.*return,failure", pid);

	FILE *pipefd = setup(fds, auclass);
	/* Failure reason: Invalid arguments */
	ATF_REQUIRE_EQ(-1, sysctl(NULL, 0, NULL, NULL, NULL, 0));
	check_audit(fds, miscreg, pipefd);
}

ATF_TC_CLEANUP(sysctl_failure, tc)
{
	cleanup();
}


ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, audit_failure);

	ATF_TP_ADD_TC(tp, sysarch_success);
	ATF_TP_ADD_TC(tp, sysarch_failure);

	ATF_TP_ADD_TC(tp, sysctl_success);
	ATF_TP_ADD_TC(tp, sysctl_failure);

	return (atf_no_error());
}