1 /*-
2 * Copyright (c) 2007 Robert M. M. Watson
3 * All rights reserved.
4 *
5 * This software was developed by Robert N. M. Watson for the TrustedBSD
6 * Project.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
16 *
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.
28 */
29
30 /*
31 * Confirm that when security.bsd.unprivileged_read_msgbuf is set to 0,
32 * privilege is required to read the kernel message buffer.
33 */
34
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
37
38 #include <err.h>
39 #include <errno.h>
40 #include <stdio.h>
41
42 #include "main.h"
43
44 #define MSGBUF_CONTROL_NAME "security.bsd.unprivileged_read_msgbuf"
45 #define MSGBUF_NAME "kern.msgbuf"
46
47 /*
48 * We must query and save the original value, then restore it when done.
49 */
50 static int unprivileged_read_msgbuf;
51 static int unprivileged_read_msgbuf_initialized;
52
53 int
priv_msgbuf_privonly_setup(int asroot,int injail,struct test * test)54 priv_msgbuf_privonly_setup(int asroot, int injail, struct test *test)
55 {
56 size_t len;
57 int newval;
58
59 /*
60 * Separately query and set to make debugging easier.
61 */
62 len = sizeof(unprivileged_read_msgbuf);
63 if (sysctlbyname(MSGBUF_CONTROL_NAME, &unprivileged_read_msgbuf,
64 &len, NULL, 0) < 0) {
65 warn("priv_msgbuf_privonly_setup: sysctlbyname query");
66 return (-1);
67 }
68 newval = 0;
69 if (sysctlbyname(MSGBUF_CONTROL_NAME, NULL, NULL, &newval,
70 sizeof(newval)) < 0) {
71 warn("priv_msgbuf_privonly_setup: sysctlbyname set");
72 return (-1);
73 }
74 unprivileged_read_msgbuf_initialized = 1;
75 return (0);
76 }
77
78 void
priv_msgbuf_privonly(int asroot,int injail,struct test * test)79 priv_msgbuf_privonly(int asroot, int injail, struct test *test)
80 {
81 size_t len;
82 int error;
83
84 error = sysctlbyname(MSGBUF_NAME, NULL, &len, NULL, 0);
85 if (asroot && injail)
86 expect("priv_msgbuf_privonly(asroot, injail)", error, -1,
87 EPERM);
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,
92 EPERM);
93 if (!asroot && !injail)
94 expect("priv_msgbuf_privonly(!asroot, !injail)", error, -1,
95 EPERM);
96 }
97
98 int
priv_msgbuf_unprivok_setup(int asroot,int injail,struct test * test)99 priv_msgbuf_unprivok_setup(int asroot, int injail, struct test *test)
100 {
101 size_t len;
102 int newval;
103
104 /*
105 * Separately query and set to make debugging easier.
106 */
107 len = sizeof(unprivileged_read_msgbuf);
108 if (sysctlbyname(MSGBUF_CONTROL_NAME, &unprivileged_read_msgbuf, &len,
109 NULL, 0) < 0) {
110 warn("priv_msgbuf_unprivok_setup: sysctlbyname query");
111 return (-1);
112 }
113 newval = 1;
114 if (sysctlbyname(MSGBUF_CONTROL_NAME, NULL, NULL, &newval,
115 sizeof(newval)) < 0) {
116 warn("priv_msgbuf_unprivok_setup: sysctlbyname set");
117 return (-1);
118 }
119 unprivileged_read_msgbuf_initialized = 1;
120 return (0);
121 }
122
123 void
priv_msgbuf_unprivok(int asroot,int injail,struct test * test)124 priv_msgbuf_unprivok(int asroot, int injail, struct test *test)
125 {
126 size_t len;
127 int error;
128
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);
138 }
139
140 void
priv_msgbuf_cleanup(int asroot,int injail,struct test * test)141 priv_msgbuf_cleanup(int asroot, int injail, struct test *test)
142 {
143
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;
149 }
150 }
151