xref: /freebsd/sys/security/mac/mac_pipe.c (revision eca8a663d442468f64e21ed869817b9048ab5a7b)
17bc82500SRobert Watson /*-
273275908SRobert Watson  * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
37bc82500SRobert Watson  * All rights reserved.
47bc82500SRobert Watson  *
56201265bSRobert Watson  * This software was developed for the FreeBSD Project in part by Network
66201265bSRobert Watson  * Associates Laboratories, the Security Research Division of Network
76201265bSRobert Watson  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
86201265bSRobert Watson  * as part of the DARPA CHATS research program.
97bc82500SRobert Watson  *
107bc82500SRobert Watson  * Redistribution and use in source and binary forms, with or without
117bc82500SRobert Watson  * modification, are permitted provided that the following conditions
127bc82500SRobert Watson  * are met:
137bc82500SRobert Watson  * 1. Redistributions of source code must retain the above copyright
147bc82500SRobert Watson  *    notice, this list of conditions and the following disclaimer.
157bc82500SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
167bc82500SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
177bc82500SRobert Watson  *    documentation and/or other materials provided with the distribution.
187bc82500SRobert Watson  *
197bc82500SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
207bc82500SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
217bc82500SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
227bc82500SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
237bc82500SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
247bc82500SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
257bc82500SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
267bc82500SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
277bc82500SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
287bc82500SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
297bc82500SRobert Watson  * SUCH DAMAGE.
307bc82500SRobert Watson  */
31677b542eSDavid E. O'Brien 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
357bc82500SRobert Watson #include "opt_mac.h"
36f9d0d524SRobert Watson 
377bc82500SRobert Watson #include <sys/param.h>
3895fab37eSRobert Watson #include <sys/kernel.h>
3995fab37eSRobert Watson #include <sys/lock.h>
40b656366bSBruce Evans #include <sys/malloc.h>
4195fab37eSRobert Watson #include <sys/mutex.h>
4295fab37eSRobert Watson #include <sys/mac.h>
43f51e5803SRobert Watson #include <sys/sbuf.h>
4495fab37eSRobert Watson #include <sys/systm.h>
4595fab37eSRobert Watson #include <sys/vnode.h>
4695fab37eSRobert Watson #include <sys/pipe.h>
4795fab37eSRobert Watson #include <sys/sysctl.h>
4895fab37eSRobert Watson 
4995fab37eSRobert Watson #include <sys/mac_policy.h>
5095fab37eSRobert Watson 
5173275908SRobert Watson #include <security/mac/mac_internal.h>
5295fab37eSRobert Watson 
53b88c98f6SRobert Watson static int	mac_enforce_pipe = 1;
54b88c98f6SRobert Watson SYSCTL_INT(_security_mac, OID_AUTO, enforce_pipe, CTLFLAG_RW,
55b88c98f6SRobert Watson     &mac_enforce_pipe, 0, "Enforce MAC policy on pipe operations");
56c031391bSRobert Watson TUNABLE_INT("security.mac.enforce_pipe", &mac_enforce_pipe);
57b88c98f6SRobert Watson 
58f050add5SRobert Watson #ifdef MAC_DEBUG
5973275908SRobert Watson static unsigned int nmacpipes;
60b2f0927aSRobert Watson SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, pipes, CTLFLAG_RD,
6195fab37eSRobert Watson     &nmacpipes, 0, "number of pipes in use");
62f050add5SRobert Watson #endif
6395fab37eSRobert Watson 
64eca8a663SRobert Watson struct label *
65eca8a663SRobert Watson mac_pipe_label_alloc(void)
66f7b951a8SRobert Watson {
67eca8a663SRobert Watson 	struct label *label;
68f7b951a8SRobert Watson 
69eca8a663SRobert Watson 	label = mac_labelzone_alloc(M_WAITOK);
70f7b951a8SRobert Watson 	MAC_PERFORM(init_pipe_label, label);
718d8d5ea8SRobert Watson 	MAC_DEBUG_COUNTER_INC(&nmacpipes);
72eca8a663SRobert Watson 	return (label);
73f7b951a8SRobert Watson }
74f7b951a8SRobert Watson 
7508bcdc58SRobert Watson void
7608bcdc58SRobert Watson mac_init_pipe(struct pipe *pipe)
7708bcdc58SRobert Watson {
7808bcdc58SRobert Watson 
79eca8a663SRobert Watson 	pipe->pipe_label = pipe->pipe_peer->pipe_label =
80eca8a663SRobert Watson 	    mac_pipe_label_alloc();
8108bcdc58SRobert Watson }
8208bcdc58SRobert Watson 
832555374cSRobert Watson void
84eca8a663SRobert Watson mac_pipe_label_free(struct label *label)
85f7b951a8SRobert Watson {
86f7b951a8SRobert Watson 
87f7b951a8SRobert Watson 	MAC_PERFORM(destroy_pipe_label, label);
88eca8a663SRobert Watson 	mac_labelzone_free(label);
898d8d5ea8SRobert Watson 	MAC_DEBUG_COUNTER_DEC(&nmacpipes);
90f7b951a8SRobert Watson }
91f7b951a8SRobert Watson 
9287807196SRobert Watson void
9387807196SRobert Watson mac_destroy_pipe(struct pipe *pipe)
9408bcdc58SRobert Watson {
9508bcdc58SRobert Watson 
96eca8a663SRobert Watson 	mac_pipe_label_free(pipe->pipe_label);
97eca8a663SRobert Watson 	pipe->pipe_label = NULL;
9887807196SRobert Watson }
9987807196SRobert Watson 
1002555374cSRobert Watson void
101f7b951a8SRobert Watson mac_copy_pipe_label(struct label *src, struct label *dest)
102f7b951a8SRobert Watson {
103f7b951a8SRobert Watson 
104f7b951a8SRobert Watson 	MAC_PERFORM(copy_pipe_label, src, dest);
105f7b951a8SRobert Watson }
106f7b951a8SRobert Watson 
10773275908SRobert Watson int
108f7b951a8SRobert Watson mac_externalize_pipe_label(struct label *label, char *elements,
10983b7b0edSRobert Watson     char *outbuf, size_t outbuflen)
110f7b951a8SRobert Watson {
111f7b951a8SRobert Watson 	int error;
112f7b951a8SRobert Watson 
113da77b2faSRobert Watson 	MAC_EXTERNALIZE(pipe, label, elements, outbuf, outbuflen);
114f7b951a8SRobert Watson 
115f7b951a8SRobert Watson 	return (error);
116f7b951a8SRobert Watson }
117f7b951a8SRobert Watson 
11873275908SRobert Watson int
119f7b951a8SRobert Watson mac_internalize_pipe_label(struct label *label, char *string)
120f7b951a8SRobert Watson {
121f7b951a8SRobert Watson 	int error;
122f7b951a8SRobert Watson 
123da77b2faSRobert Watson 	MAC_INTERNALIZE(pipe, label, string);
124f7b951a8SRobert Watson 
125f7b951a8SRobert Watson 	return (error);
126f7b951a8SRobert Watson }
127f7b951a8SRobert Watson 
12895fab37eSRobert Watson void
12995fab37eSRobert Watson mac_create_pipe(struct ucred *cred, struct pipe *pipe)
13095fab37eSRobert Watson {
13195fab37eSRobert Watson 
13295fab37eSRobert Watson 	MAC_PERFORM(create_pipe, cred, pipe, pipe->pipe_label);
13395fab37eSRobert Watson }
13495fab37eSRobert Watson 
13595fab37eSRobert Watson static void
13695fab37eSRobert Watson mac_relabel_pipe(struct ucred *cred, struct pipe *pipe, struct label *newlabel)
13795fab37eSRobert Watson {
13895fab37eSRobert Watson 
13995fab37eSRobert Watson 	MAC_PERFORM(relabel_pipe, cred, pipe, pipe->pipe_label, newlabel);
14095fab37eSRobert Watson }
14195fab37eSRobert Watson 
14295fab37eSRobert Watson int
14395fab37eSRobert Watson mac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe, unsigned long cmd,
14495fab37eSRobert Watson     void *data)
14595fab37eSRobert Watson {
14695fab37eSRobert Watson 	int error;
14795fab37eSRobert Watson 
1481aa37f53SRobert Watson 	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
1491aa37f53SRobert Watson 
1501aa37f53SRobert Watson 	if (!mac_enforce_pipe)
1511aa37f53SRobert Watson 		return (0);
1521aa37f53SRobert Watson 
15395fab37eSRobert Watson 	MAC_CHECK(check_pipe_ioctl, cred, pipe, pipe->pipe_label, cmd, data);
15495fab37eSRobert Watson 
15595fab37eSRobert Watson 	return (error);
15695fab37eSRobert Watson }
15795fab37eSRobert Watson 
15895fab37eSRobert Watson int
159c024c3eeSRobert Watson mac_check_pipe_poll(struct ucred *cred, struct pipe *pipe)
16095fab37eSRobert Watson {
16195fab37eSRobert Watson 	int error;
16295fab37eSRobert Watson 
1631aa37f53SRobert Watson 	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
1641aa37f53SRobert Watson 
1651aa37f53SRobert Watson 	if (!mac_enforce_pipe)
1661aa37f53SRobert Watson 		return (0);
1671aa37f53SRobert Watson 
168c024c3eeSRobert Watson 	MAC_CHECK(check_pipe_poll, cred, pipe, pipe->pipe_label);
169c024c3eeSRobert Watson 
170c024c3eeSRobert Watson 	return (error);
171c024c3eeSRobert Watson }
172c024c3eeSRobert Watson 
173c024c3eeSRobert Watson int
174c024c3eeSRobert Watson mac_check_pipe_read(struct ucred *cred, struct pipe *pipe)
175c024c3eeSRobert Watson {
176c024c3eeSRobert Watson 	int error;
177c024c3eeSRobert Watson 
1781aa37f53SRobert Watson 	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
1791aa37f53SRobert Watson 
1801aa37f53SRobert Watson 	if (!mac_enforce_pipe)
1811aa37f53SRobert Watson 		return (0);
1821aa37f53SRobert Watson 
183c024c3eeSRobert Watson 	MAC_CHECK(check_pipe_read, cred, pipe, pipe->pipe_label);
18495fab37eSRobert Watson 
18595fab37eSRobert Watson 	return (error);
18695fab37eSRobert Watson }
18795fab37eSRobert Watson 
18895fab37eSRobert Watson static int
18995fab37eSRobert Watson mac_check_pipe_relabel(struct ucred *cred, struct pipe *pipe,
19095fab37eSRobert Watson     struct label *newlabel)
19195fab37eSRobert Watson {
19295fab37eSRobert Watson 	int error;
19395fab37eSRobert Watson 
1941aa37f53SRobert Watson 	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
1951aa37f53SRobert Watson 
1961aa37f53SRobert Watson 	if (!mac_enforce_pipe)
1971aa37f53SRobert Watson 		return (0);
1981aa37f53SRobert Watson 
19995fab37eSRobert Watson 	MAC_CHECK(check_pipe_relabel, cred, pipe, pipe->pipe_label, newlabel);
20095fab37eSRobert Watson 
20195fab37eSRobert Watson 	return (error);
20295fab37eSRobert Watson }
20395fab37eSRobert Watson 
20495fab37eSRobert Watson int
205c024c3eeSRobert Watson mac_check_pipe_stat(struct ucred *cred, struct pipe *pipe)
206c024c3eeSRobert Watson {
207c024c3eeSRobert Watson 	int error;
208c024c3eeSRobert Watson 
2091aa37f53SRobert Watson 	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2101aa37f53SRobert Watson 
2111aa37f53SRobert Watson 	if (!mac_enforce_pipe)
2121aa37f53SRobert Watson 		return (0);
2131aa37f53SRobert Watson 
214c024c3eeSRobert Watson 	MAC_CHECK(check_pipe_stat, cred, pipe, pipe->pipe_label);
215c024c3eeSRobert Watson 
216c024c3eeSRobert Watson 	return (error);
217c024c3eeSRobert Watson }
218c024c3eeSRobert Watson 
219c024c3eeSRobert Watson int
220c024c3eeSRobert Watson mac_check_pipe_write(struct ucred *cred, struct pipe *pipe)
221c024c3eeSRobert Watson {
222c024c3eeSRobert Watson 	int error;
223c024c3eeSRobert Watson 
2241aa37f53SRobert Watson 	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2251aa37f53SRobert Watson 
2261aa37f53SRobert Watson 	if (!mac_enforce_pipe)
2271aa37f53SRobert Watson 		return (0);
2281aa37f53SRobert Watson 
229c024c3eeSRobert Watson 	MAC_CHECK(check_pipe_write, cred, pipe, pipe->pipe_label);
230c024c3eeSRobert Watson 
231c024c3eeSRobert Watson 	return (error);
232c024c3eeSRobert Watson }
233c024c3eeSRobert Watson 
234c024c3eeSRobert Watson int
23595fab37eSRobert Watson mac_pipe_label_set(struct ucred *cred, struct pipe *pipe, struct label *label)
23695fab37eSRobert Watson {
23795fab37eSRobert Watson 	int error;
23895fab37eSRobert Watson 
2391aa37f53SRobert Watson 	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2401aa37f53SRobert Watson 
24195fab37eSRobert Watson 	error = mac_check_pipe_relabel(cred, pipe, label);
24295fab37eSRobert Watson 	if (error)
24395fab37eSRobert Watson 		return (error);
24495fab37eSRobert Watson 
24595fab37eSRobert Watson 	mac_relabel_pipe(cred, pipe, label);
24695fab37eSRobert Watson 
24795fab37eSRobert Watson 	return (0);
24895fab37eSRobert Watson }
249