xref: /freebsd/sys/security/mac/mac_pipe.c (revision 2357939bc239bd5334a169b62313806178dd8f30)
1 /*-
2  * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project in part by Network
6  * Associates Laboratories, the Security Research Division of Network
7  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
8  * as part of the DARPA CHATS research program.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_mac.h"
36 
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/mac.h>
43 #include <sys/sbuf.h>
44 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 #include <sys/pipe.h>
47 #include <sys/sysctl.h>
48 
49 #include <sys/mac_policy.h>
50 
51 #include <security/mac/mac_internal.h>
52 
53 static int	mac_enforce_pipe = 1;
54 SYSCTL_INT(_security_mac, OID_AUTO, enforce_pipe, CTLFLAG_RW,
55     &mac_enforce_pipe, 0, "Enforce MAC policy on pipe operations");
56 TUNABLE_INT("security.mac.enforce_pipe", &mac_enforce_pipe);
57 
58 #ifdef MAC_DEBUG
59 static unsigned int nmacpipes;
60 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, pipes, CTLFLAG_RD,
61     &nmacpipes, 0, "number of pipes in use");
62 #endif
63 
64 struct label *
65 mac_pipe_label_alloc(void)
66 {
67 	struct label *label;
68 
69 	label = mac_labelzone_alloc(M_WAITOK);
70 	MAC_PERFORM(init_pipe_label, label);
71 	MAC_DEBUG_COUNTER_INC(&nmacpipes);
72 	return (label);
73 }
74 
75 void
76 mac_init_pipe(struct pipepair *pp)
77 {
78 
79 	pp->pp_label = mac_pipe_label_alloc();
80 }
81 
82 void
83 mac_pipe_label_free(struct label *label)
84 {
85 
86 	MAC_PERFORM(destroy_pipe_label, label);
87 	mac_labelzone_free(label);
88 	MAC_DEBUG_COUNTER_DEC(&nmacpipes);
89 }
90 
91 void
92 mac_destroy_pipe(struct pipepair *pp)
93 {
94 
95 	mac_pipe_label_free(pp->pp_label);
96 	pp->pp_label = NULL;
97 }
98 
99 void
100 mac_copy_pipe_label(struct label *src, struct label *dest)
101 {
102 
103 	MAC_PERFORM(copy_pipe_label, src, dest);
104 }
105 
106 int
107 mac_externalize_pipe_label(struct label *label, char *elements,
108     char *outbuf, size_t outbuflen)
109 {
110 	int error;
111 
112 	MAC_EXTERNALIZE(pipe, label, elements, outbuf, outbuflen);
113 
114 	return (error);
115 }
116 
117 int
118 mac_internalize_pipe_label(struct label *label, char *string)
119 {
120 	int error;
121 
122 	MAC_INTERNALIZE(pipe, label, string);
123 
124 	return (error);
125 }
126 
127 void
128 mac_create_pipe(struct ucred *cred, struct pipepair *pp)
129 {
130 
131 	MAC_PERFORM(create_pipe, cred, pp, pp->pp_label);
132 }
133 
134 static void
135 mac_relabel_pipe(struct ucred *cred, struct pipepair *pp,
136     struct label *newlabel)
137 {
138 
139 	MAC_PERFORM(relabel_pipe, cred, pp, pp->pp_label, newlabel);
140 }
141 
142 int
143 mac_check_pipe_ioctl(struct ucred *cred, struct pipepair *pp,
144     unsigned long cmd, void *data)
145 {
146 	int error;
147 
148 	mtx_assert(&pp->pp_mtx, MA_OWNED);
149 
150 	if (!mac_enforce_pipe)
151 		return (0);
152 
153 	MAC_CHECK(check_pipe_ioctl, cred, pp, pp->pp_label, cmd, data);
154 
155 	return (error);
156 }
157 
158 int
159 mac_check_pipe_poll(struct ucred *cred, struct pipepair *pp)
160 {
161 	int error;
162 
163 	mtx_assert(&pp->pp_mtx, MA_OWNED);
164 
165 	if (!mac_enforce_pipe)
166 		return (0);
167 
168 	MAC_CHECK(check_pipe_poll, cred, pp, pp->pp_label);
169 
170 	return (error);
171 }
172 
173 int
174 mac_check_pipe_read(struct ucred *cred, struct pipepair *pp)
175 {
176 	int error;
177 
178 	mtx_assert(&pp->pp_mtx, MA_OWNED);
179 
180 	if (!mac_enforce_pipe)
181 		return (0);
182 
183 	MAC_CHECK(check_pipe_read, cred, pp, pp->pp_label);
184 
185 	return (error);
186 }
187 
188 static int
189 mac_check_pipe_relabel(struct ucred *cred, struct pipepair *pp,
190     struct label *newlabel)
191 {
192 	int error;
193 
194 	mtx_assert(&pp->pp_mtx, MA_OWNED);
195 
196 	if (!mac_enforce_pipe)
197 		return (0);
198 
199 	MAC_CHECK(check_pipe_relabel, cred, pp, pp->pp_label, newlabel);
200 
201 	return (error);
202 }
203 
204 int
205 mac_check_pipe_stat(struct ucred *cred, struct pipepair *pp)
206 {
207 	int error;
208 
209 	mtx_assert(&pp->pp_mtx, MA_OWNED);
210 
211 	if (!mac_enforce_pipe)
212 		return (0);
213 
214 	MAC_CHECK(check_pipe_stat, cred, pp, pp->pp_label);
215 
216 	return (error);
217 }
218 
219 int
220 mac_check_pipe_write(struct ucred *cred, struct pipepair *pp)
221 {
222 	int error;
223 
224 	mtx_assert(&pp->pp_mtx, MA_OWNED);
225 
226 	if (!mac_enforce_pipe)
227 		return (0);
228 
229 	MAC_CHECK(check_pipe_write, cred, pp, pp->pp_label);
230 
231 	return (error);
232 }
233 
234 int
235 mac_pipe_label_set(struct ucred *cred, struct pipepair *pp,
236     struct label *label)
237 {
238 	int error;
239 
240 	mtx_assert(&pp->pp_mtx, MA_OWNED);
241 
242 	error = mac_check_pipe_relabel(cred, pp, label);
243 	if (error)
244 		return (error);
245 
246 	mac_relabel_pipe(cred, pp, label);
247 
248 	return (0);
249 }
250