xref: /freebsd/sys/security/mac/mac_pipe.c (revision f856af0466c076beef4ea9b15d088e1119a945b8)
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/module.h>
42 #include <sys/mutex.h>
43 #include <sys/mac.h>
44 #include <sys/sbuf.h>
45 #include <sys/systm.h>
46 #include <sys/vnode.h>
47 #include <sys/pipe.h>
48 #include <sys/sysctl.h>
49 
50 #include <security/mac/mac_framework.h>
51 #include <security/mac/mac_internal.h>
52 #include <security/mac/mac_policy.h>
53 
54 struct label *
55 mac_pipe_label_alloc(void)
56 {
57 	struct label *label;
58 
59 	label = mac_labelzone_alloc(M_WAITOK);
60 	MAC_PERFORM(init_pipe_label, label);
61 	return (label);
62 }
63 
64 void
65 mac_init_pipe(struct pipepair *pp)
66 {
67 
68 	pp->pp_label = mac_pipe_label_alloc();
69 }
70 
71 void
72 mac_pipe_label_free(struct label *label)
73 {
74 
75 	MAC_PERFORM(destroy_pipe_label, label);
76 	mac_labelzone_free(label);
77 }
78 
79 void
80 mac_destroy_pipe(struct pipepair *pp)
81 {
82 
83 	mac_pipe_label_free(pp->pp_label);
84 	pp->pp_label = NULL;
85 }
86 
87 void
88 mac_copy_pipe_label(struct label *src, struct label *dest)
89 {
90 
91 	MAC_PERFORM(copy_pipe_label, src, dest);
92 }
93 
94 int
95 mac_externalize_pipe_label(struct label *label, char *elements,
96     char *outbuf, size_t outbuflen)
97 {
98 	int error;
99 
100 	MAC_EXTERNALIZE(pipe, label, elements, outbuf, outbuflen);
101 
102 	return (error);
103 }
104 
105 int
106 mac_internalize_pipe_label(struct label *label, char *string)
107 {
108 	int error;
109 
110 	MAC_INTERNALIZE(pipe, label, string);
111 
112 	return (error);
113 }
114 
115 void
116 mac_create_pipe(struct ucred *cred, struct pipepair *pp)
117 {
118 
119 	MAC_PERFORM(create_pipe, cred, pp, pp->pp_label);
120 }
121 
122 static void
123 mac_relabel_pipe(struct ucred *cred, struct pipepair *pp,
124     struct label *newlabel)
125 {
126 
127 	MAC_PERFORM(relabel_pipe, cred, pp, pp->pp_label, newlabel);
128 }
129 
130 int
131 mac_check_pipe_ioctl(struct ucred *cred, struct pipepair *pp,
132     unsigned long cmd, void *data)
133 {
134 	int error;
135 
136 	mtx_assert(&pp->pp_mtx, MA_OWNED);
137 
138 	MAC_CHECK(check_pipe_ioctl, cred, pp, pp->pp_label, cmd, data);
139 
140 	return (error);
141 }
142 
143 int
144 mac_check_pipe_poll(struct ucred *cred, struct pipepair *pp)
145 {
146 	int error;
147 
148 	mtx_assert(&pp->pp_mtx, MA_OWNED);
149 
150 	MAC_CHECK(check_pipe_poll, cred, pp, pp->pp_label);
151 
152 	return (error);
153 }
154 
155 int
156 mac_check_pipe_read(struct ucred *cred, struct pipepair *pp)
157 {
158 	int error;
159 
160 	mtx_assert(&pp->pp_mtx, MA_OWNED);
161 
162 	MAC_CHECK(check_pipe_read, cred, pp, pp->pp_label);
163 
164 	return (error);
165 }
166 
167 static int
168 mac_check_pipe_relabel(struct ucred *cred, struct pipepair *pp,
169     struct label *newlabel)
170 {
171 	int error;
172 
173 	mtx_assert(&pp->pp_mtx, MA_OWNED);
174 
175 	MAC_CHECK(check_pipe_relabel, cred, pp, pp->pp_label, newlabel);
176 
177 	return (error);
178 }
179 
180 int
181 mac_check_pipe_stat(struct ucred *cred, struct pipepair *pp)
182 {
183 	int error;
184 
185 	mtx_assert(&pp->pp_mtx, MA_OWNED);
186 
187 	MAC_CHECK(check_pipe_stat, cred, pp, pp->pp_label);
188 
189 	return (error);
190 }
191 
192 int
193 mac_check_pipe_write(struct ucred *cred, struct pipepair *pp)
194 {
195 	int error;
196 
197 	mtx_assert(&pp->pp_mtx, MA_OWNED);
198 
199 	MAC_CHECK(check_pipe_write, cred, pp, pp->pp_label);
200 
201 	return (error);
202 }
203 
204 int
205 mac_pipe_label_set(struct ucred *cred, struct pipepair *pp,
206     struct label *label)
207 {
208 	int error;
209 
210 	mtx_assert(&pp->pp_mtx, MA_OWNED);
211 
212 	error = mac_check_pipe_relabel(cred, pp, label);
213 	if (error)
214 		return (error);
215 
216 	mac_relabel_pipe(cred, pp, label);
217 
218 	return (0);
219 }
220