17bc82500SRobert Watson /*- 226ae2b86SRobert 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> 415dba30f1SPoul-Henning Kamp #include <sys/module.h> 4295fab37eSRobert Watson #include <sys/mutex.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 49aed55708SRobert Watson #include <security/mac/mac_framework.h> 5073275908SRobert Watson #include <security/mac/mac_internal.h> 510efd6615SRobert Watson #include <security/mac/mac_policy.h> 5295fab37eSRobert Watson 53eca8a663SRobert Watson struct label * 54eca8a663SRobert Watson mac_pipe_label_alloc(void) 55f7b951a8SRobert Watson { 56eca8a663SRobert Watson struct label *label; 57f7b951a8SRobert Watson 58eca8a663SRobert Watson label = mac_labelzone_alloc(M_WAITOK); 59f7b951a8SRobert Watson MAC_PERFORM(init_pipe_label, label); 60eca8a663SRobert Watson return (label); 61f7b951a8SRobert Watson } 62f7b951a8SRobert Watson 6308bcdc58SRobert Watson void 644795b82cSRobert Watson mac_init_pipe(struct pipepair *pp) 6508bcdc58SRobert Watson { 6608bcdc58SRobert Watson 674795b82cSRobert Watson pp->pp_label = mac_pipe_label_alloc(); 6808bcdc58SRobert Watson } 6908bcdc58SRobert Watson 702555374cSRobert Watson void 71eca8a663SRobert Watson mac_pipe_label_free(struct label *label) 72f7b951a8SRobert Watson { 73f7b951a8SRobert Watson 74f7b951a8SRobert Watson MAC_PERFORM(destroy_pipe_label, label); 75eca8a663SRobert Watson mac_labelzone_free(label); 76f7b951a8SRobert Watson } 77f7b951a8SRobert Watson 7887807196SRobert Watson void 794795b82cSRobert Watson mac_destroy_pipe(struct pipepair *pp) 8008bcdc58SRobert Watson { 8108bcdc58SRobert Watson 824795b82cSRobert Watson mac_pipe_label_free(pp->pp_label); 834795b82cSRobert Watson pp->pp_label = NULL; 8487807196SRobert Watson } 8587807196SRobert Watson 862555374cSRobert Watson void 87f7b951a8SRobert Watson mac_copy_pipe_label(struct label *src, struct label *dest) 88f7b951a8SRobert Watson { 89f7b951a8SRobert Watson 90f7b951a8SRobert Watson MAC_PERFORM(copy_pipe_label, src, dest); 91f7b951a8SRobert Watson } 92f7b951a8SRobert Watson 9373275908SRobert Watson int 94f7b951a8SRobert Watson mac_externalize_pipe_label(struct label *label, char *elements, 9583b7b0edSRobert Watson char *outbuf, size_t outbuflen) 96f7b951a8SRobert Watson { 97f7b951a8SRobert Watson int error; 98f7b951a8SRobert Watson 99da77b2faSRobert Watson MAC_EXTERNALIZE(pipe, label, elements, outbuf, outbuflen); 100f7b951a8SRobert Watson 101f7b951a8SRobert Watson return (error); 102f7b951a8SRobert Watson } 103f7b951a8SRobert Watson 10473275908SRobert Watson int 105f7b951a8SRobert Watson mac_internalize_pipe_label(struct label *label, char *string) 106f7b951a8SRobert Watson { 107f7b951a8SRobert Watson int error; 108f7b951a8SRobert Watson 109da77b2faSRobert Watson MAC_INTERNALIZE(pipe, label, string); 110f7b951a8SRobert Watson 111f7b951a8SRobert Watson return (error); 112f7b951a8SRobert Watson } 113f7b951a8SRobert Watson 11495fab37eSRobert Watson void 1154795b82cSRobert Watson mac_create_pipe(struct ucred *cred, struct pipepair *pp) 11695fab37eSRobert Watson { 11795fab37eSRobert Watson 1184795b82cSRobert Watson MAC_PERFORM(create_pipe, cred, pp, pp->pp_label); 11995fab37eSRobert Watson } 12095fab37eSRobert Watson 12195fab37eSRobert Watson static void 1224795b82cSRobert Watson mac_relabel_pipe(struct ucred *cred, struct pipepair *pp, 1234795b82cSRobert Watson struct label *newlabel) 12495fab37eSRobert Watson { 12595fab37eSRobert Watson 1264795b82cSRobert Watson MAC_PERFORM(relabel_pipe, cred, pp, pp->pp_label, newlabel); 12795fab37eSRobert Watson } 12895fab37eSRobert Watson 12995fab37eSRobert Watson int 1304795b82cSRobert Watson mac_check_pipe_ioctl(struct ucred *cred, struct pipepair *pp, 1314795b82cSRobert Watson unsigned long cmd, void *data) 13295fab37eSRobert Watson { 13395fab37eSRobert Watson int error; 13495fab37eSRobert Watson 1354795b82cSRobert Watson mtx_assert(&pp->pp_mtx, MA_OWNED); 1361aa37f53SRobert Watson 1374795b82cSRobert Watson MAC_CHECK(check_pipe_ioctl, cred, pp, pp->pp_label, cmd, data); 13895fab37eSRobert Watson 13995fab37eSRobert Watson return (error); 14095fab37eSRobert Watson } 14195fab37eSRobert Watson 14295fab37eSRobert Watson int 1434795b82cSRobert Watson mac_check_pipe_poll(struct ucred *cred, struct pipepair *pp) 14495fab37eSRobert Watson { 14595fab37eSRobert Watson int error; 14695fab37eSRobert Watson 1474795b82cSRobert Watson mtx_assert(&pp->pp_mtx, MA_OWNED); 1481aa37f53SRobert Watson 1494795b82cSRobert Watson MAC_CHECK(check_pipe_poll, cred, pp, pp->pp_label); 150c024c3eeSRobert Watson 151c024c3eeSRobert Watson return (error); 152c024c3eeSRobert Watson } 153c024c3eeSRobert Watson 154c024c3eeSRobert Watson int 1554795b82cSRobert Watson mac_check_pipe_read(struct ucred *cred, struct pipepair *pp) 156c024c3eeSRobert Watson { 157c024c3eeSRobert Watson int error; 158c024c3eeSRobert Watson 1594795b82cSRobert Watson mtx_assert(&pp->pp_mtx, MA_OWNED); 1601aa37f53SRobert Watson 1614795b82cSRobert Watson MAC_CHECK(check_pipe_read, cred, pp, pp->pp_label); 16295fab37eSRobert Watson 16395fab37eSRobert Watson return (error); 16495fab37eSRobert Watson } 16595fab37eSRobert Watson 16695fab37eSRobert Watson static int 1674795b82cSRobert Watson mac_check_pipe_relabel(struct ucred *cred, struct pipepair *pp, 16895fab37eSRobert Watson struct label *newlabel) 16995fab37eSRobert Watson { 17095fab37eSRobert Watson int error; 17195fab37eSRobert Watson 1724795b82cSRobert Watson mtx_assert(&pp->pp_mtx, MA_OWNED); 1731aa37f53SRobert Watson 1744795b82cSRobert Watson MAC_CHECK(check_pipe_relabel, cred, pp, pp->pp_label, newlabel); 17595fab37eSRobert Watson 17695fab37eSRobert Watson return (error); 17795fab37eSRobert Watson } 17895fab37eSRobert Watson 17995fab37eSRobert Watson int 1804795b82cSRobert Watson mac_check_pipe_stat(struct ucred *cred, struct pipepair *pp) 181c024c3eeSRobert Watson { 182c024c3eeSRobert Watson int error; 183c024c3eeSRobert Watson 1844795b82cSRobert Watson mtx_assert(&pp->pp_mtx, MA_OWNED); 1851aa37f53SRobert Watson 1864795b82cSRobert Watson MAC_CHECK(check_pipe_stat, cred, pp, pp->pp_label); 187c024c3eeSRobert Watson 188c024c3eeSRobert Watson return (error); 189c024c3eeSRobert Watson } 190c024c3eeSRobert Watson 191c024c3eeSRobert Watson int 1924795b82cSRobert Watson mac_check_pipe_write(struct ucred *cred, struct pipepair *pp) 193c024c3eeSRobert Watson { 194c024c3eeSRobert Watson int error; 195c024c3eeSRobert Watson 1964795b82cSRobert Watson mtx_assert(&pp->pp_mtx, MA_OWNED); 1971aa37f53SRobert Watson 1984795b82cSRobert Watson MAC_CHECK(check_pipe_write, cred, pp, pp->pp_label); 199c024c3eeSRobert Watson 200c024c3eeSRobert Watson return (error); 201c024c3eeSRobert Watson } 202c024c3eeSRobert Watson 203c024c3eeSRobert Watson int 2044795b82cSRobert Watson mac_pipe_label_set(struct ucred *cred, struct pipepair *pp, 2054795b82cSRobert Watson struct label *label) 20695fab37eSRobert Watson { 20795fab37eSRobert Watson int error; 20895fab37eSRobert Watson 2094795b82cSRobert Watson mtx_assert(&pp->pp_mtx, MA_OWNED); 2101aa37f53SRobert Watson 2114795b82cSRobert Watson error = mac_check_pipe_relabel(cred, pp, label); 21295fab37eSRobert Watson if (error) 21395fab37eSRobert Watson return (error); 21495fab37eSRobert Watson 2154795b82cSRobert Watson mac_relabel_pipe(cred, pp, label); 21695fab37eSRobert Watson 21795fab37eSRobert Watson return (0); 21895fab37eSRobert Watson } 219