xref: /freebsd/tests/sys/audit/utils.c (revision b13a70d5a4979997a0dbc0795d27bb7e22ec794b)
1f7f4e0f7SAlan Somers /*-
2f7f4e0f7SAlan Somers  * Copyright 2018 Aniket Pandey
3f7f4e0f7SAlan Somers  *
4f7f4e0f7SAlan Somers  * Redistribution and use in source and binary forms, with or without
5f7f4e0f7SAlan Somers  * modification, are permitted provided that the following conditions
6f7f4e0f7SAlan Somers  * are met:
7f7f4e0f7SAlan Somers  * 1. Redistributions of source code must retain the above copyright
8f7f4e0f7SAlan Somers  *    notice, this list of conditions and the following disclaimer.
9f7f4e0f7SAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
10f7f4e0f7SAlan Somers  *    notice, this list of conditions and the following disclaimer in the
11f7f4e0f7SAlan Somers  *    documentation and/or other materials provided with the distribution.
12f7f4e0f7SAlan Somers  *
13f7f4e0f7SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14f7f4e0f7SAlan Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15f7f4e0f7SAlan Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16f7f4e0f7SAlan Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17f7f4e0f7SAlan Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18f7f4e0f7SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19f7f4e0f7SAlan Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20f7f4e0f7SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21f7f4e0f7SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22f7f4e0f7SAlan Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23f7f4e0f7SAlan Somers  * SUCH DAMAGE.
24f7f4e0f7SAlan Somers  *
25f7f4e0f7SAlan Somers  * $FreeBSD$
26f7f4e0f7SAlan Somers  */
27f7f4e0f7SAlan Somers 
28f7f4e0f7SAlan Somers #include <sys/ioctl.h>
29f7f4e0f7SAlan Somers 
30f7f4e0f7SAlan Somers #include <bsm/libbsm.h>
31f7f4e0f7SAlan Somers #include <security/audit/audit_ioctl.h>
32f7f4e0f7SAlan Somers 
33*b13a70d5SAlan Somers #include <atf-c.h>
34*b13a70d5SAlan Somers #include <errno.h>
35*b13a70d5SAlan Somers #include <fcntl.h>
36*b13a70d5SAlan Somers #include <stdlib.h>
37*b13a70d5SAlan Somers #include <string.h>
38*b13a70d5SAlan Somers #include <time.h>
39*b13a70d5SAlan Somers #include <unistd.h>
40*b13a70d5SAlan Somers 
41f7f4e0f7SAlan Somers #include "utils.h"
42f7f4e0f7SAlan Somers 
43f7f4e0f7SAlan Somers /*
44f7f4e0f7SAlan Somers  * Checks the presence of "auditregex" in auditpipe(4) after the
45f7f4e0f7SAlan Somers  * corresponding system call has been triggered.
46f7f4e0f7SAlan Somers  */
47f7f4e0f7SAlan Somers static bool
48f7f4e0f7SAlan Somers get_records(const char *auditregex, FILE *pipestream)
49f7f4e0f7SAlan Somers {
50f7f4e0f7SAlan Somers 	uint8_t *buff;
51f7f4e0f7SAlan Somers 	tokenstr_t token;
52f7f4e0f7SAlan Somers 	ssize_t size = 1024;
53f7f4e0f7SAlan Somers 	char membuff[size];
54f7f4e0f7SAlan Somers 	char del[] = ",";
55f7f4e0f7SAlan Somers 	int reclen, bytes = 0;
56f7f4e0f7SAlan Somers 	FILE *memstream;
57f7f4e0f7SAlan Somers 
58f7f4e0f7SAlan Somers 	/*
59f7f4e0f7SAlan Somers 	 * Open a stream on 'membuff' (address to memory buffer) for storing
60f7f4e0f7SAlan Somers 	 * the audit records in the default mode.'reclen' is the length of the
61f7f4e0f7SAlan Somers 	 * available records from auditpipe which is passed to the functions
62f7f4e0f7SAlan Somers 	 * au_fetch_tok(3) and au_print_flags_tok(3) for further use.
63f7f4e0f7SAlan Somers 	 */
64f7f4e0f7SAlan Somers 	ATF_REQUIRE((memstream = fmemopen(membuff, size, "w")) != NULL);
65f7f4e0f7SAlan Somers 	ATF_REQUIRE((reclen = au_read_rec(pipestream, &buff)) != -1);
66f7f4e0f7SAlan Somers 
67f7f4e0f7SAlan Somers 	/*
68f7f4e0f7SAlan Somers 	 * Iterate through each BSM token, extracting the bits that are
69f7f4e0f7SAlan Somers 	 * required to start processing the token sequences.
70f7f4e0f7SAlan Somers 	 */
71f7f4e0f7SAlan Somers 	while (bytes < reclen) {
72f7f4e0f7SAlan Somers 		if (au_fetch_tok(&token, buff + bytes, reclen - bytes) == -1) {
73f7f4e0f7SAlan Somers 			perror("au_read_rec");
74f7f4e0f7SAlan Somers 			atf_tc_fail("Incomplete Audit Record");
75f7f4e0f7SAlan Somers 		}
76f7f4e0f7SAlan Somers 
77f7f4e0f7SAlan Somers 		/* Print the tokens as they are obtained, in the default form */
78f7f4e0f7SAlan Somers 		au_print_flags_tok(memstream, &token, del, AU_OFLAG_NONE);
79f7f4e0f7SAlan Somers 		bytes += token.len;
80f7f4e0f7SAlan Somers 	}
81f7f4e0f7SAlan Somers 
82f7f4e0f7SAlan Somers 	free(buff);
83*b13a70d5SAlan Somers 	ATF_REQUIRE_EQ(0, fclose(memstream));
84f7f4e0f7SAlan Somers 	return (atf_utils_grep_string("%s", membuff, auditregex));
85f7f4e0f7SAlan Somers }
86f7f4e0f7SAlan Somers 
87f7f4e0f7SAlan Somers /*
88f7f4e0f7SAlan Somers  * Override the system-wide audit mask settings in /etc/security/audit_control
89f7f4e0f7SAlan Somers  * and set the auditpipe's maximum allowed queue length limit
90f7f4e0f7SAlan Somers  */
91f7f4e0f7SAlan Somers static void
92f7f4e0f7SAlan Somers set_preselect_mode(int filedesc, au_mask_t *fmask)
93f7f4e0f7SAlan Somers {
94f7f4e0f7SAlan Somers 	int qlimit_max;
95f7f4e0f7SAlan Somers 	int fmode = AUDITPIPE_PRESELECT_MODE_LOCAL;
96f7f4e0f7SAlan Somers 
97f7f4e0f7SAlan Somers 	/* Set local preselection mode for auditing */
98f7f4e0f7SAlan Somers 	if (ioctl(filedesc, AUDITPIPE_SET_PRESELECT_MODE, &fmode) < 0)
99f7f4e0f7SAlan Somers 		atf_tc_fail("Preselection mode: %s", strerror(errno));
100f7f4e0f7SAlan Somers 
101f7f4e0f7SAlan Somers 	/* Set local preselection flag corresponding to the audit_event */
102f7f4e0f7SAlan Somers 	if (ioctl(filedesc, AUDITPIPE_SET_PRESELECT_FLAGS, fmask) < 0)
103f7f4e0f7SAlan Somers 		atf_tc_fail("Preselection flag: %s", strerror(errno));
104f7f4e0f7SAlan Somers 
105f7f4e0f7SAlan Somers 	/* Set local preselection flag for non-attributable audit_events */
106f7f4e0f7SAlan Somers 	if (ioctl(filedesc, AUDITPIPE_SET_PRESELECT_NAFLAGS, fmask) < 0)
107f7f4e0f7SAlan Somers 		atf_tc_fail("Preselection naflag: %s", strerror(errno));
108f7f4e0f7SAlan Somers 
109f7f4e0f7SAlan Somers 	/* Query the maximum possible queue length limit for auditpipe */
110f7f4e0f7SAlan Somers 	if (ioctl(filedesc, AUDITPIPE_GET_QLIMIT_MAX, &qlimit_max) < 0)
111f7f4e0f7SAlan Somers 		atf_tc_fail("Query max-limit: %s", strerror(errno));
112f7f4e0f7SAlan Somers 
113f7f4e0f7SAlan Somers 	/* Set the queue length limit as obtained from previous step */
114f7f4e0f7SAlan Somers 	if (ioctl(filedesc, AUDITPIPE_SET_QLIMIT, &qlimit_max) < 0)
115f7f4e0f7SAlan Somers 		atf_tc_fail("Set max-qlimit: %s", strerror(errno));
116f7f4e0f7SAlan Somers 
117f7f4e0f7SAlan Somers 	/* This removes any outstanding record on the auditpipe */
118f7f4e0f7SAlan Somers 	if (ioctl(filedesc, AUDITPIPE_FLUSH) < 0)
119f7f4e0f7SAlan Somers 		atf_tc_fail("Auditpipe flush: %s", strerror(errno));
120f7f4e0f7SAlan Somers }
121f7f4e0f7SAlan Somers 
122f7f4e0f7SAlan Somers /*
123f7f4e0f7SAlan Somers  * Get the corresponding audit_mask for class-name "name" then set the
124f7f4e0f7SAlan Somers  * success and failure bits for fmask to be used as the ioctl argument
125f7f4e0f7SAlan Somers  */
126f7f4e0f7SAlan Somers static au_mask_t
127f7f4e0f7SAlan Somers get_audit_mask(const char *name)
128f7f4e0f7SAlan Somers {
129f7f4e0f7SAlan Somers 	au_mask_t fmask;
130f7f4e0f7SAlan Somers 	au_class_ent_t *class;
131f7f4e0f7SAlan Somers 
132f7f4e0f7SAlan Somers 	ATF_REQUIRE((class = getauclassnam(name)) != NULL);
133f7f4e0f7SAlan Somers 	fmask.am_success = class->ac_class;
134f7f4e0f7SAlan Somers 	fmask.am_failure = class->ac_class;
135f7f4e0f7SAlan Somers 	return (fmask);
136f7f4e0f7SAlan Somers }
137f7f4e0f7SAlan Somers 
138f7f4e0f7SAlan Somers /*
139f7f4e0f7SAlan Somers  * Loop until the auditpipe returns something, check if it is what
140f7f4e0f7SAlan Somers  * we want, else repeat the procedure until ppoll(2) times out.
141f7f4e0f7SAlan Somers  */
142f7f4e0f7SAlan Somers static void
143f7f4e0f7SAlan Somers check_auditpipe(struct pollfd fd[], const char *auditregex, FILE *pipestream)
144f7f4e0f7SAlan Somers {
145f7f4e0f7SAlan Somers 	struct timespec currtime, endtime, timeout;
146f7f4e0f7SAlan Somers 
147f7f4e0f7SAlan Somers 	/* Set the expire time for poll(2) while waiting for syscall audit */
148f7f4e0f7SAlan Somers 	ATF_REQUIRE_EQ(0, clock_gettime(CLOCK_MONOTONIC, &endtime));
149f7f4e0f7SAlan Somers 	endtime.tv_sec += 10;
150f7f4e0f7SAlan Somers 	timeout.tv_nsec = endtime.tv_nsec;
151f7f4e0f7SAlan Somers 
152f7f4e0f7SAlan Somers 	for (;;) {
153f7f4e0f7SAlan Somers 		/* Update the time left for auditpipe to return any event */
154f7f4e0f7SAlan Somers 		ATF_REQUIRE_EQ(0, clock_gettime(CLOCK_MONOTONIC, &currtime));
155f7f4e0f7SAlan Somers 		timeout.tv_sec = endtime.tv_sec - currtime.tv_sec;
156f7f4e0f7SAlan Somers 
157f7f4e0f7SAlan Somers 		switch (ppoll(fd, 1, &timeout, NULL)) {
158f7f4e0f7SAlan Somers 		/* ppoll(2) returns, check if it's what we want */
159f7f4e0f7SAlan Somers 		case 1:
160f7f4e0f7SAlan Somers 			if (fd[0].revents & POLLIN) {
161f7f4e0f7SAlan Somers 				if (get_records(auditregex, pipestream))
162f7f4e0f7SAlan Somers 					return;
163f7f4e0f7SAlan Somers 			} else {
164f7f4e0f7SAlan Somers 				atf_tc_fail("Auditpipe returned an "
165f7f4e0f7SAlan Somers 				"unknown event %#x", fd[0].revents);
166f7f4e0f7SAlan Somers 			}
167f7f4e0f7SAlan Somers 			break;
168f7f4e0f7SAlan Somers 
169f7f4e0f7SAlan Somers 		/* poll(2) timed out */
170f7f4e0f7SAlan Somers 		case 0:
171f7f4e0f7SAlan Somers 			atf_tc_fail("%s not found in auditpipe within the "
172f7f4e0f7SAlan Somers 					"time limit", auditregex);
173f7f4e0f7SAlan Somers 			break;
174f7f4e0f7SAlan Somers 
175f7f4e0f7SAlan Somers 		/* poll(2) standard error */
176f7f4e0f7SAlan Somers 		case -1:
177f7f4e0f7SAlan Somers 			atf_tc_fail("Poll: %s", strerror(errno));
178f7f4e0f7SAlan Somers 			break;
179f7f4e0f7SAlan Somers 
180f7f4e0f7SAlan Somers 		default:
181f7f4e0f7SAlan Somers 			atf_tc_fail("Poll returned too many file descriptors");
182f7f4e0f7SAlan Somers 		}
183f7f4e0f7SAlan Somers 	}
184f7f4e0f7SAlan Somers }
185f7f4e0f7SAlan Somers 
186f7f4e0f7SAlan Somers /*
187f7f4e0f7SAlan Somers  * Wrapper functions around static "check_auditpipe"
188f7f4e0f7SAlan Somers  */
189f7f4e0f7SAlan Somers static void
190f7f4e0f7SAlan Somers check_audit_startup(struct pollfd fd[], const char *auditrgx, FILE *pipestream){
191f7f4e0f7SAlan Somers 	check_auditpipe(fd, auditrgx, pipestream);
192f7f4e0f7SAlan Somers }
193f7f4e0f7SAlan Somers 
194f7f4e0f7SAlan Somers void
195f7f4e0f7SAlan Somers check_audit(struct pollfd fd[], const char *auditrgx, FILE *pipestream) {
196f7f4e0f7SAlan Somers 	check_auditpipe(fd, auditrgx, pipestream);
197f7f4e0f7SAlan Somers 
198*b13a70d5SAlan Somers 	/* Teardown: /dev/auditpipe's instance opened for this test-suite */
199*b13a70d5SAlan Somers 	ATF_REQUIRE_EQ(0, fclose(pipestream));
200f7f4e0f7SAlan Somers }
201f7f4e0f7SAlan Somers 
202f7f4e0f7SAlan Somers FILE
203f7f4e0f7SAlan Somers *setup(struct pollfd fd[], const char *name)
204f7f4e0f7SAlan Somers {
205f7f4e0f7SAlan Somers 	au_mask_t fmask, nomask;
206f7f4e0f7SAlan Somers 	fmask = get_audit_mask(name);
207f7f4e0f7SAlan Somers 	nomask = get_audit_mask("no");
208f7f4e0f7SAlan Somers 	FILE *pipestream;
209f7f4e0f7SAlan Somers 
210*b13a70d5SAlan Somers 	ATF_REQUIRE((fd[0].fd = open("/dev/auditpipe", O_RDONLY)) != -1);
211*b13a70d5SAlan Somers 	ATF_REQUIRE((pipestream = fdopen(fd[0].fd, "r")) != NULL);
212f7f4e0f7SAlan Somers 	fd[0].events = POLLIN;
213f7f4e0f7SAlan Somers 
214f7f4e0f7SAlan Somers 	/* Set local preselection audit_class as "no" for audit startup */
215f7f4e0f7SAlan Somers 	set_preselect_mode(fd[0].fd, &nomask);
216f7f4e0f7SAlan Somers 	ATF_REQUIRE_EQ(0, system("service auditd onestatus || \
217f7f4e0f7SAlan Somers 	{ service auditd onestart && touch started_auditd ; }"));
218f7f4e0f7SAlan Somers 
219f7f4e0f7SAlan Somers 	/* If 'started_auditd' exists, that means we started auditd(8) */
220f7f4e0f7SAlan Somers 	if (atf_utils_file_exists("started_auditd"))
221f7f4e0f7SAlan Somers 		check_audit_startup(fd, "audit startup", pipestream);
222f7f4e0f7SAlan Somers 
223f7f4e0f7SAlan Somers 	/* Set local preselection parameters specific to "name" audit_class */
224f7f4e0f7SAlan Somers 	set_preselect_mode(fd[0].fd, &fmask);
225f7f4e0f7SAlan Somers 	return (pipestream);
226f7f4e0f7SAlan Somers }
227f7f4e0f7SAlan Somers 
228f7f4e0f7SAlan Somers void
229f7f4e0f7SAlan Somers cleanup(void)
230f7f4e0f7SAlan Somers {
231f7f4e0f7SAlan Somers 	if (atf_utils_file_exists("started_auditd"))
232f7f4e0f7SAlan Somers 		system("service auditd onestop > /dev/null 2>&1");
233f7f4e0f7SAlan Somers }
234