1 /*-
2  * Copyright (c) 2006 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * This software was developed by Robert Watson for the TrustedBSD Project.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 /*
32  * Simple audit pipe regression test to confirm that the ioctls for queue
33  * limit information basically work.  No attempt is made to validate the
34  * queue length returned, however.
35  */
36 
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 
40 #include <security/audit/audit_ioctl.h>
41 
42 #include <err.h>
43 #include <fcntl.h>
44 
45 int
46 main(int argc, char *argv[])
47 {
48 	u_int len, minlen, maxlen;
49 	u_int64_t astat;
50 	int fd;
51 
52 	fd = open("/dev/auditpipe", O_RDONLY);
53 	if (fd < 0)
54 		err(-1, "/dev/auditpipe");
55 
56 	/*
57 	 * First, test that we can read the queue length, queue limit, and
58 	 * bounds on queue length limits.
59 	 */
60 	len = (u_int)(-1);
61 	if (ioctl(fd, AUDITPIPE_GET_QLEN, &len) < 0)
62 		err(-1, "AUDITPIPE_GET_QLEN");
63 	if (len == (u_int)(-1))
64 		errx(-1, "AUDITPIPE_GET_QLEN: unchanged");
65 
66 	minlen = (u_int)(-1);
67 	if (ioctl(fd, AUDITPIPE_GET_QLIMIT_MIN, &minlen) < 0)
68 		err(-1, "AUDITPIPE_GET_QLIMIT_MIN");
69 	if (minlen == (u_int)(-1))
70 		errx(-1, "AUDITPIPE_GET_QLIMIT_MIN: unchanged");
71 
72 	maxlen = (u_int)(-1);
73 	if (ioctl(fd, AUDITPIPE_GET_QLIMIT_MAX, &maxlen) < 0)
74 		err(-1, "AUDITPIPE_GET_QLIMIT_MAX");
75 	if (maxlen == (u_int)(-1))
76 		errx(-1, "AUDITPIPE_GET_QLIMIT_MAX: unchanged");
77 
78 	len = (u_int)(-1);
79 	if (ioctl(fd, AUDITPIPE_GET_QLIMIT, &len) < 0)
80 		err(-1, "AUDITPIPE_GET_QLIMIT");
81 	if (len == (u_int)(-1))
82 		errx(-1, "AUDITPIPE_GET_QLIMIT: unchanged");
83 
84 	if (!(len >= minlen))
85 		errx(-1, "queue length < minlen");
86 
87 	if (!(len <= maxlen))
88 		errx(-1, "queue length > maxlen");
89 
90 	/*
91 	 * Try setting the queue length to first minimum, then maximum
92 	 * lengths.  Query after each to make sure it changed.
93 	 */
94 	len = minlen;
95 	if (ioctl(fd, AUDITPIPE_SET_QLIMIT, &len) < 0)
96 		err(-1, "AUDITPIPE_SET_QLIMIT(min)");
97 
98 	if (ioctl(fd, AUDITPIPE_GET_QLIMIT, &len) < 0)
99 		err(-1, "AUDITPIPE_GET_QLIMIT");
100 
101 	if (len != minlen)
102 		errx(-1, "set to minlen didn't work");
103 
104 	len = maxlen;
105 	if (ioctl(fd, AUDITPIPE_SET_QLIMIT, &len) < 0)
106 		err(-1, "AUDITPIPE_SET_QLIMIT(max)");
107 
108 	if (ioctl(fd, AUDITPIPE_GET_QLIMIT, &len) < 0)
109 		err(-1, "AUDITPIPE_GETQLIMIT");
110 
111 	if (len != maxlen)
112 		errx(-1, "set to maxlen didn't work");
113 
114 	/*
115 	 * Check that we can query the defined stats.  No attempt to
116 	 * validate.
117 	 */
118 	astat = (u_int64_t)(int64_t)(-1);
119 	if (ioctl(fd, AUDITPIPE_GET_INSERTS, &astat) < 0)
120 		err(-1, "AUDITPIPE_GET_INSERTS");
121 	if (astat == (u_int64_t)(int64_t)(-1))
122 		errx(-1, "AUDITPIPE_GET_INSERTS: unchanged");
123 
124 	astat = (u_int64_t)(int64_t)(-1);
125 	if (ioctl(fd, AUDITPIPE_GET_READS, &astat) < 0)
126 		err(-1, "AUDITPIPE_GET_READS");
127 	if (astat == (u_int64_t)(int64_t)(-1))
128 		errx(-1, "AUDITPIPE_GET_READS: unchanged");
129 
130 	astat = (u_int64_t)(int64_t)(-1);
131 	if (ioctl(fd, AUDITPIPE_GET_DROPS, &astat) < 0)
132 		err(-1, "AUDITPIPE_GET_DROPS");
133 	if (astat == (u_int64_t)(int64_t)(-1))
134 		errx(-1, "AUDITPIPE_GET_DROPS: unchanged");
135 
136 	astat = (u_int64_t)(int64_t)(-1);
137 	if (ioctl(fd, AUDITPIPE_GET_TRUNCATES, &astat) < 0)
138 		err(-1, "AUDITPIPE_GET_TRUNCATES");
139 	if (astat == (u_int64_t)(int64_t)(-1))
140 		errx(-1, "AUDITPIPE_GET_TRUNCATES: unchanged");
141 
142 	return (0);
143 }
144