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.
33 * Confirm that when security.bsd.unprivileged_read_msgbuf is set to 0,
34 * privilege is required to read the kernel message buffer.
37 #include <sys/types.h>
38 #include <sys/sysctl.h>
46 #define MSGBUF_CONTROL_NAME "security.bsd.unprivileged_read_msgbuf"
47 #define MSGBUF_NAME "kern.msgbuf"
50 * We must query and save the original value, then restore it when done.
52 static int unprivileged_read_msgbuf
;
53 static int unprivileged_read_msgbuf_initialized
;
56 priv_msgbuf_privonly_setup(int asroot
, int injail
, struct test
*test
)
62 * Separately query and set to make debugging easier.
64 len
= sizeof(unprivileged_read_msgbuf
);
65 if (sysctlbyname(MSGBUF_CONTROL_NAME
, &unprivileged_read_msgbuf
,
67 warn("priv_msgbuf_privonly_setup: sysctlbyname query");
71 if (sysctlbyname(MSGBUF_CONTROL_NAME
, NULL
, NULL
, &newval
,
72 sizeof(newval
)) < 0) {
73 warn("priv_msgbuf_privonly_setup: sysctlbyname set");
76 unprivileged_read_msgbuf_initialized
= 1;
81 priv_msgbuf_privonly(int asroot
, int injail
, struct test
*test
)
86 error
= sysctlbyname(MSGBUF_NAME
, NULL
, &len
, NULL
, 0);
88 expect("priv_msgbuf_privonly(asroot, injail)", error
, -1,
90 if (asroot
&& !injail
)
91 expect("priv_msgbuf_privonly(asroot, !injail)", error
, 0, 0);
92 if (!asroot
&& injail
)
93 expect("priv_msgbuf_privonly(!asroot, injail)", error
, -1,
95 if (!asroot
&& !injail
)
96 expect("priv_msgbuf_privonly(!asroot, !injail)", error
, -1,
101 priv_msgbuf_unprivok_setup(int asroot
, int injail
, struct test
*test
)
107 * Separately query and set to make debugging easier.
109 len
= sizeof(unprivileged_read_msgbuf
);
110 if (sysctlbyname(MSGBUF_CONTROL_NAME
, &unprivileged_read_msgbuf
, &len
,
112 warn("priv_msgbuf_unprivok_setup: sysctlbyname query");
116 if (sysctlbyname(MSGBUF_CONTROL_NAME
, NULL
, NULL
, &newval
,
117 sizeof(newval
)) < 0) {
118 warn("priv_msgbuf_unprivok_setup: sysctlbyname set");
121 unprivileged_read_msgbuf_initialized
= 1;
126 priv_msgbuf_unprivok(int asroot
, int injail
, struct test
*test
)
131 error
= sysctlbyname(MSGBUF_NAME
, NULL
, &len
, NULL
, 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);
138 if (!asroot
&& !injail
)
139 expect("priv_msgbuf_unprivok(!asroot, !injail)", error
, 0, 0);
143 priv_msgbuf_cleanup(int asroot
, int injail
, struct test
*test
)
146 if (unprivileged_read_msgbuf_initialized
) {
147 (void)sysctlbyname(MSGBUF_NAME
, NULL
, NULL
,
148 &unprivileged_read_msgbuf
,
149 sizeof(unprivileged_read_msgbuf
));
150 unprivileged_read_msgbuf_initialized
= 0;