2 * Copyright (c) 2007 Robert M. M. Watson
5 * This software was developed by Robert N. M. Watson for the TrustedBSD
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
21 * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Confirm that when security.bsd.unprivileged_read_msgbuf is set to 0,
32 * privilege is required to read the kernel message buffer.
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
44 #define MSGBUF_CONTROL_NAME "security.bsd.unprivileged_read_msgbuf"
45 #define MSGBUF_NAME "kern.msgbuf"
48 * We must query and save the original value, then restore it when done.
50 static int unprivileged_read_msgbuf;
51 static int unprivileged_read_msgbuf_initialized;
54 priv_msgbuf_privonly_setup(int asroot, int injail, struct test *test)
60 * Separately query and set to make debugging easier.
62 len = sizeof(unprivileged_read_msgbuf);
63 if (sysctlbyname(MSGBUF_CONTROL_NAME, &unprivileged_read_msgbuf,
65 warn("priv_msgbuf_privonly_setup: sysctlbyname query");
69 if (sysctlbyname(MSGBUF_CONTROL_NAME, NULL, NULL, &newval,
70 sizeof(newval)) < 0) {
71 warn("priv_msgbuf_privonly_setup: sysctlbyname set");
74 unprivileged_read_msgbuf_initialized = 1;
79 priv_msgbuf_privonly(int asroot, int injail, struct test *test)
84 error = sysctlbyname(MSGBUF_NAME, NULL, &len, NULL, 0);
86 expect("priv_msgbuf_privonly(asroot, injail)", error, -1,
88 if (asroot && !injail)
89 expect("priv_msgbuf_privonly(asroot, !injail)", error, 0, 0);
90 if (!asroot && injail)
91 expect("priv_msgbuf_privonly(!asroot, injail)", error, -1,
93 if (!asroot && !injail)
94 expect("priv_msgbuf_privonly(!asroot, !injail)", error, -1,
99 priv_msgbuf_unprivok_setup(int asroot, int injail, struct test *test)
105 * Separately query and set to make debugging easier.
107 len = sizeof(unprivileged_read_msgbuf);
108 if (sysctlbyname(MSGBUF_CONTROL_NAME, &unprivileged_read_msgbuf, &len,
110 warn("priv_msgbuf_unprivok_setup: sysctlbyname query");
114 if (sysctlbyname(MSGBUF_CONTROL_NAME, NULL, NULL, &newval,
115 sizeof(newval)) < 0) {
116 warn("priv_msgbuf_unprivok_setup: sysctlbyname set");
119 unprivileged_read_msgbuf_initialized = 1;
124 priv_msgbuf_unprivok(int asroot, int injail, struct test *test)
129 error = sysctlbyname(MSGBUF_NAME, NULL, &len, NULL, 0);
130 if (asroot && injail)
131 expect("priv_msgbuf_unprivok(asroot, injail)", error, 0, 0);
132 if (asroot && !injail)
133 expect("priv_msgbuf_unprivok(asroot, !injail)", error, 0, 0);
134 if (!asroot && injail)
135 expect("priv_msgbuf_unprivok(!asroot, injail)", error, 0, 0);
136 if (!asroot && !injail)
137 expect("priv_msgbuf_unprivok(!asroot, !injail)", error, 0, 0);
141 priv_msgbuf_cleanup(int asroot, int injail, struct test *test)
144 if (unprivileged_read_msgbuf_initialized) {
145 (void)sysctlbyname(MSGBUF_NAME, NULL, NULL,
146 &unprivileged_read_msgbuf,
147 sizeof(unprivileged_read_msgbuf));
148 unprivileged_read_msgbuf_initialized = 0;