xref: /freebsd/sys/security/mac/mac_pipe.c (revision 30d239bc4c510432e65a84fa1c14ed67a3ab1c92)
17bc82500SRobert Watson /*-
226ae2b86SRobert Watson  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
330d239bcSRobert Watson  * Copyright (c) 2006 SPARTA, Inc.
47bc82500SRobert Watson  * All rights reserved.
57bc82500SRobert Watson  *
66201265bSRobert Watson  * This software was developed for the FreeBSD Project in part by Network
76201265bSRobert Watson  * Associates Laboratories, the Security Research Division of Network
86201265bSRobert Watson  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
96201265bSRobert Watson  * as part of the DARPA CHATS research program.
107bc82500SRobert Watson  *
1130d239bcSRobert Watson  * This software was enhanced by SPARTA ISSO under SPAWAR contract
1230d239bcSRobert Watson  * N66001-04-C-6019 ("SEFOS").
1330d239bcSRobert Watson  *
147bc82500SRobert Watson  * Redistribution and use in source and binary forms, with or without
157bc82500SRobert Watson  * modification, are permitted provided that the following conditions
167bc82500SRobert Watson  * are met:
177bc82500SRobert Watson  * 1. Redistributions of source code must retain the above copyright
187bc82500SRobert Watson  *    notice, this list of conditions and the following disclaimer.
197bc82500SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
207bc82500SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
217bc82500SRobert Watson  *    documentation and/or other materials provided with the distribution.
227bc82500SRobert Watson  *
237bc82500SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
247bc82500SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
257bc82500SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
267bc82500SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
277bc82500SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
287bc82500SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
297bc82500SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
307bc82500SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
317bc82500SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
327bc82500SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
337bc82500SRobert Watson  * SUCH DAMAGE.
347bc82500SRobert Watson  */
35677b542eSDavid E. O'Brien 
36677b542eSDavid E. O'Brien #include <sys/cdefs.h>
37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
38677b542eSDavid E. O'Brien 
397bc82500SRobert Watson #include "opt_mac.h"
40f9d0d524SRobert Watson 
417bc82500SRobert Watson #include <sys/param.h>
4295fab37eSRobert Watson #include <sys/kernel.h>
4395fab37eSRobert Watson #include <sys/lock.h>
44b656366bSBruce Evans #include <sys/malloc.h>
455dba30f1SPoul-Henning Kamp #include <sys/module.h>
4695fab37eSRobert Watson #include <sys/mutex.h>
47f51e5803SRobert Watson #include <sys/sbuf.h>
4895fab37eSRobert Watson #include <sys/systm.h>
4995fab37eSRobert Watson #include <sys/vnode.h>
5095fab37eSRobert Watson #include <sys/pipe.h>
5195fab37eSRobert Watson #include <sys/sysctl.h>
5295fab37eSRobert Watson 
53aed55708SRobert Watson #include <security/mac/mac_framework.h>
5473275908SRobert Watson #include <security/mac/mac_internal.h>
550efd6615SRobert Watson #include <security/mac/mac_policy.h>
5695fab37eSRobert Watson 
57eca8a663SRobert Watson struct label *
58eca8a663SRobert Watson mac_pipe_label_alloc(void)
59f7b951a8SRobert Watson {
60eca8a663SRobert Watson 	struct label *label;
61f7b951a8SRobert Watson 
62eca8a663SRobert Watson 	label = mac_labelzone_alloc(M_WAITOK);
6330d239bcSRobert Watson 	MAC_PERFORM(pipe_init_label, label);
64eca8a663SRobert Watson 	return (label);
65f7b951a8SRobert Watson }
66f7b951a8SRobert Watson 
6708bcdc58SRobert Watson void
6830d239bcSRobert Watson mac_pipe_init(struct pipepair *pp)
6908bcdc58SRobert Watson {
7008bcdc58SRobert Watson 
714795b82cSRobert Watson 	pp->pp_label = mac_pipe_label_alloc();
7208bcdc58SRobert Watson }
7308bcdc58SRobert Watson 
742555374cSRobert Watson void
75eca8a663SRobert Watson mac_pipe_label_free(struct label *label)
76f7b951a8SRobert Watson {
77f7b951a8SRobert Watson 
7830d239bcSRobert Watson 	MAC_PERFORM(pipe_destroy_label, label);
79eca8a663SRobert Watson 	mac_labelzone_free(label);
80f7b951a8SRobert Watson }
81f7b951a8SRobert Watson 
8287807196SRobert Watson void
8330d239bcSRobert Watson mac_pipe_destroy(struct pipepair *pp)
8408bcdc58SRobert Watson {
8508bcdc58SRobert Watson 
864795b82cSRobert Watson 	mac_pipe_label_free(pp->pp_label);
874795b82cSRobert Watson 	pp->pp_label = NULL;
8887807196SRobert Watson }
8987807196SRobert Watson 
902555374cSRobert Watson void
9130d239bcSRobert Watson mac_pipe_copy_label(struct label *src, struct label *dest)
92f7b951a8SRobert Watson {
93f7b951a8SRobert Watson 
9430d239bcSRobert Watson 	MAC_PERFORM(pipe_copy_label, src, dest);
95f7b951a8SRobert Watson }
96f7b951a8SRobert Watson 
9773275908SRobert Watson int
9830d239bcSRobert Watson mac_pipe_externalize_label(struct label *label, char *elements,
9983b7b0edSRobert Watson     char *outbuf, size_t outbuflen)
100f7b951a8SRobert Watson {
101f7b951a8SRobert Watson 	int error;
102f7b951a8SRobert Watson 
103da77b2faSRobert Watson 	MAC_EXTERNALIZE(pipe, label, elements, outbuf, outbuflen);
104f7b951a8SRobert Watson 
105f7b951a8SRobert Watson 	return (error);
106f7b951a8SRobert Watson }
107f7b951a8SRobert Watson 
10873275908SRobert Watson int
10930d239bcSRobert Watson mac_pipe_internalize_label(struct label *label, char *string)
110f7b951a8SRobert Watson {
111f7b951a8SRobert Watson 	int error;
112f7b951a8SRobert Watson 
113da77b2faSRobert Watson 	MAC_INTERNALIZE(pipe, label, string);
114f7b951a8SRobert Watson 
115f7b951a8SRobert Watson 	return (error);
116f7b951a8SRobert Watson }
117f7b951a8SRobert Watson 
11895fab37eSRobert Watson void
11930d239bcSRobert Watson mac_pipe_create(struct ucred *cred, struct pipepair *pp)
12095fab37eSRobert Watson {
12195fab37eSRobert Watson 
12230d239bcSRobert Watson 	MAC_PERFORM(pipe_create, cred, pp, pp->pp_label);
12395fab37eSRobert Watson }
12495fab37eSRobert Watson 
12595fab37eSRobert Watson static void
12630d239bcSRobert Watson mac_pipe_relabel(struct ucred *cred, struct pipepair *pp,
1274795b82cSRobert Watson     struct label *newlabel)
12895fab37eSRobert Watson {
12995fab37eSRobert Watson 
13030d239bcSRobert Watson 	MAC_PERFORM(pipe_relabel, cred, pp, pp->pp_label, newlabel);
13195fab37eSRobert Watson }
13295fab37eSRobert Watson 
13395fab37eSRobert Watson int
13430d239bcSRobert Watson mac_pipe_check_ioctl(struct ucred *cred, struct pipepair *pp,
1354795b82cSRobert Watson     unsigned long cmd, void *data)
13695fab37eSRobert Watson {
13795fab37eSRobert Watson 	int error;
13895fab37eSRobert Watson 
1394795b82cSRobert Watson 	mtx_assert(&pp->pp_mtx, MA_OWNED);
1401aa37f53SRobert Watson 
14130d239bcSRobert Watson 	MAC_CHECK(pipe_check_ioctl, cred, pp, pp->pp_label, cmd, data);
14295fab37eSRobert Watson 
14395fab37eSRobert Watson 	return (error);
14495fab37eSRobert Watson }
14595fab37eSRobert Watson 
14695fab37eSRobert Watson int
14730d239bcSRobert Watson mac_pipe_check_poll(struct ucred *cred, struct pipepair *pp)
14895fab37eSRobert Watson {
14995fab37eSRobert Watson 	int error;
15095fab37eSRobert Watson 
1514795b82cSRobert Watson 	mtx_assert(&pp->pp_mtx, MA_OWNED);
1521aa37f53SRobert Watson 
15330d239bcSRobert Watson 	MAC_CHECK(pipe_check_poll, cred, pp, pp->pp_label);
154c024c3eeSRobert Watson 
155c024c3eeSRobert Watson 	return (error);
156c024c3eeSRobert Watson }
157c024c3eeSRobert Watson 
158c024c3eeSRobert Watson int
15930d239bcSRobert Watson mac_pipe_check_read(struct ucred *cred, struct pipepair *pp)
160c024c3eeSRobert Watson {
161c024c3eeSRobert Watson 	int error;
162c024c3eeSRobert Watson 
1634795b82cSRobert Watson 	mtx_assert(&pp->pp_mtx, MA_OWNED);
1641aa37f53SRobert Watson 
16530d239bcSRobert Watson 	MAC_CHECK(pipe_check_read, cred, pp, pp->pp_label);
16695fab37eSRobert Watson 
16795fab37eSRobert Watson 	return (error);
16895fab37eSRobert Watson }
16995fab37eSRobert Watson 
17095fab37eSRobert Watson static int
17130d239bcSRobert Watson mac_pipe_check_relabel(struct ucred *cred, struct pipepair *pp,
17295fab37eSRobert Watson     struct label *newlabel)
17395fab37eSRobert Watson {
17495fab37eSRobert Watson 	int error;
17595fab37eSRobert Watson 
1764795b82cSRobert Watson 	mtx_assert(&pp->pp_mtx, MA_OWNED);
1771aa37f53SRobert Watson 
17830d239bcSRobert Watson 	MAC_CHECK(pipe_check_relabel, cred, pp, pp->pp_label, newlabel);
17995fab37eSRobert Watson 
18095fab37eSRobert Watson 	return (error);
18195fab37eSRobert Watson }
18295fab37eSRobert Watson 
18395fab37eSRobert Watson int
18430d239bcSRobert Watson mac_pipe_check_stat(struct ucred *cred, struct pipepair *pp)
185c024c3eeSRobert Watson {
186c024c3eeSRobert Watson 	int error;
187c024c3eeSRobert Watson 
1884795b82cSRobert Watson 	mtx_assert(&pp->pp_mtx, MA_OWNED);
1891aa37f53SRobert Watson 
19030d239bcSRobert Watson 	MAC_CHECK(pipe_check_stat, cred, pp, pp->pp_label);
191c024c3eeSRobert Watson 
192c024c3eeSRobert Watson 	return (error);
193c024c3eeSRobert Watson }
194c024c3eeSRobert Watson 
195c024c3eeSRobert Watson int
19630d239bcSRobert Watson mac_pipe_check_write(struct ucred *cred, struct pipepair *pp)
197c024c3eeSRobert Watson {
198c024c3eeSRobert Watson 	int error;
199c024c3eeSRobert Watson 
2004795b82cSRobert Watson 	mtx_assert(&pp->pp_mtx, MA_OWNED);
2011aa37f53SRobert Watson 
20230d239bcSRobert Watson 	MAC_CHECK(pipe_check_write, cred, pp, pp->pp_label);
203c024c3eeSRobert Watson 
204c024c3eeSRobert Watson 	return (error);
205c024c3eeSRobert Watson }
206c024c3eeSRobert Watson 
207c024c3eeSRobert Watson int
2084795b82cSRobert Watson mac_pipe_label_set(struct ucred *cred, struct pipepair *pp,
2094795b82cSRobert Watson     struct label *label)
21095fab37eSRobert Watson {
21195fab37eSRobert Watson 	int error;
21295fab37eSRobert Watson 
2134795b82cSRobert Watson 	mtx_assert(&pp->pp_mtx, MA_OWNED);
2141aa37f53SRobert Watson 
21530d239bcSRobert Watson 	error = mac_pipe_check_relabel(cred, pp, label);
21695fab37eSRobert Watson 	if (error)
21795fab37eSRobert Watson 		return (error);
21895fab37eSRobert Watson 
21930d239bcSRobert Watson 	mac_pipe_relabel(cred, pp, label);
22095fab37eSRobert Watson 
22195fab37eSRobert Watson 	return (0);
22295fab37eSRobert Watson }
223