1100894Srwatson/*- 2168955Srwatson * Copyright (c) 2002-2003 Networks Associates Technology, Inc. 3172930Srwatson * Copyright (c) 2006 SPARTA, Inc. 4189503Srwatson * Copyright (c) 2009 Robert N. M. Watson 5100894Srwatson * All rights reserved. 6100894Srwatson * 7106392Srwatson * This software was developed for the FreeBSD Project in part by Network 8106392Srwatson * Associates Laboratories, the Security Research Division of Network 9106392Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 10106392Srwatson * as part of the DARPA CHATS research program. 11100894Srwatson * 12172930Srwatson * This software was enhanced by SPARTA ISSO under SPAWAR contract 13172930Srwatson * N66001-04-C-6019 ("SEFOS"). 14172930Srwatson * 15189503Srwatson * This software was developed at the University of Cambridge Computer 16189503Srwatson * Laboratory with support from a grant from Google, Inc. 17189503Srwatson * 18100894Srwatson * Redistribution and use in source and binary forms, with or without 19100894Srwatson * modification, are permitted provided that the following conditions 20100894Srwatson * are met: 21100894Srwatson * 1. Redistributions of source code must retain the above copyright 22100894Srwatson * notice, this list of conditions and the following disclaimer. 23100894Srwatson * 2. Redistributions in binary form must reproduce the above copyright 24100894Srwatson * notice, this list of conditions and the following disclaimer in the 25100894Srwatson * documentation and/or other materials provided with the distribution. 26100894Srwatson * 27100894Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 28100894Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29100894Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30100894Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 31100894Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32100894Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33100894Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34100894Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35100894Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36100894Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37100894Srwatson * SUCH DAMAGE. 38100894Srwatson */ 39116182Sobrien 40116182Sobrien#include <sys/cdefs.h> 41116182Sobrien__FBSDID("$FreeBSD$"); 42116182Sobrien 43189503Srwatson#include "opt_kdtrace.h" 44100894Srwatson#include "opt_mac.h" 45101173Srwatson 46100894Srwatson#include <sys/param.h> 47100979Srwatson#include <sys/kernel.h> 48100979Srwatson#include <sys/lock.h> 49102949Sbde#include <sys/malloc.h> 50129880Sphk#include <sys/module.h> 51100979Srwatson#include <sys/mutex.h> 52116701Srwatson#include <sys/sbuf.h> 53189503Srwatson#include <sys/sdt.h> 54100979Srwatson#include <sys/systm.h> 55100979Srwatson#include <sys/vnode.h> 56100979Srwatson#include <sys/pipe.h> 57100979Srwatson#include <sys/sysctl.h> 58100894Srwatson 59163606Srwatson#include <security/mac/mac_framework.h> 60121359Srwatson#include <security/mac/mac_internal.h> 61165469Srwatson#include <security/mac/mac_policy.h> 62100979Srwatson 63122524Srwatsonstruct label * 64122524Srwatsonmac_pipe_label_alloc(void) 65105694Srwatson{ 66122524Srwatson struct label *label; 67105694Srwatson 68122524Srwatson label = mac_labelzone_alloc(M_WAITOK); 69191731Srwatson MAC_POLICY_PERFORM(pipe_init_label, label); 70122524Srwatson return (label); 71105694Srwatson} 72105694Srwatson 73104521Srwatsonvoid 74172930Srwatsonmac_pipe_init(struct pipepair *pp) 75104521Srwatson{ 76104521Srwatson 77182063Srwatson if (mac_labeled & MPC_OBJECT_PIPE) 78182063Srwatson pp->pp_label = mac_pipe_label_alloc(); 79182063Srwatson else 80182063Srwatson pp->pp_label = NULL; 81104521Srwatson} 82104521Srwatson 83107105Srwatsonvoid 84122524Srwatsonmac_pipe_label_free(struct label *label) 85104521Srwatson{ 86104521Srwatson 87191731Srwatson MAC_POLICY_PERFORM_NOSLEEP(pipe_destroy_label, label); 88122524Srwatson mac_labelzone_free(label); 89104521Srwatson} 90104521Srwatson 91105694Srwatsonvoid 92172930Srwatsonmac_pipe_destroy(struct pipepair *pp) 93105694Srwatson{ 94105694Srwatson 95182063Srwatson if (pp->pp_label != NULL) { 96182063Srwatson mac_pipe_label_free(pp->pp_label); 97182063Srwatson pp->pp_label = NULL; 98182063Srwatson } 99105694Srwatson} 100105694Srwatson 101107105Srwatsonvoid 102172930Srwatsonmac_pipe_copy_label(struct label *src, struct label *dest) 103105694Srwatson{ 104105694Srwatson 105191731Srwatson MAC_POLICY_PERFORM_NOSLEEP(pipe_copy_label, src, dest); 106105694Srwatson} 107105694Srwatson 108121359Srwatsonint 109172930Srwatsonmac_pipe_externalize_label(struct label *label, char *elements, 110122159Srwatson char *outbuf, size_t outbuflen) 111105694Srwatson{ 112105694Srwatson int error; 113105694Srwatson 114191731Srwatson MAC_POLICY_EXTERNALIZE(pipe, label, elements, outbuf, outbuflen); 115105694Srwatson 116105694Srwatson return (error); 117105694Srwatson} 118105694Srwatson 119121359Srwatsonint 120172930Srwatsonmac_pipe_internalize_label(struct label *label, char *string) 121105694Srwatson{ 122105694Srwatson int error; 123105694Srwatson 124191731Srwatson MAC_POLICY_INTERNALIZE(pipe, label, string); 125105694Srwatson 126105694Srwatson return (error); 127105694Srwatson} 128105694Srwatson 129104521Srwatsonvoid 130172930Srwatsonmac_pipe_create(struct ucred *cred, struct pipepair *pp) 131100979Srwatson{ 132100979Srwatson 133191731Srwatson MAC_POLICY_PERFORM_NOSLEEP(pipe_create, cred, pp, pp->pp_label); 134100979Srwatson} 135100979Srwatson 136100979Srwatsonstatic void 137172930Srwatsonmac_pipe_relabel(struct ucred *cred, struct pipepair *pp, 138125293Srwatson struct label *newlabel) 139100979Srwatson{ 140100979Srwatson 141191731Srwatson MAC_POLICY_PERFORM_NOSLEEP(pipe_relabel, cred, pp, pp->pp_label, 142191731Srwatson newlabel); 143100979Srwatson} 144100979Srwatson 145189503SrwatsonMAC_CHECK_PROBE_DEFINE4(pipe_check_ioctl, "struct ucred *", 146189503Srwatson "struct pipepair *", "unsigned long", "void *"); 147189503Srwatson 148100979Srwatsonint 149172930Srwatsonmac_pipe_check_ioctl(struct ucred *cred, struct pipepair *pp, 150125293Srwatson unsigned long cmd, void *data) 151100979Srwatson{ 152100979Srwatson int error; 153100979Srwatson 154125293Srwatson mtx_assert(&pp->pp_mtx, MA_OWNED); 155104269Srwatson 156191731Srwatson MAC_POLICY_CHECK_NOSLEEP(pipe_check_ioctl, cred, pp, pp->pp_label, 157191731Srwatson cmd, data); 158189503Srwatson MAC_CHECK_PROBE4(pipe_check_ioctl, error, cred, pp, cmd, data); 159100979Srwatson 160100979Srwatson return (error); 161100979Srwatson} 162100979Srwatson 163189503SrwatsonMAC_CHECK_PROBE_DEFINE2(pipe_check_poll, "struct ucred *", 164189503Srwatson "struct pipepair *"); 165189503Srwatson 166100979Srwatsonint 167172930Srwatsonmac_pipe_check_poll(struct ucred *cred, struct pipepair *pp) 168100979Srwatson{ 169100979Srwatson int error; 170100979Srwatson 171125293Srwatson mtx_assert(&pp->pp_mtx, MA_OWNED); 172104269Srwatson 173191731Srwatson MAC_POLICY_CHECK_NOSLEEP(pipe_check_poll, cred, pp, pp->pp_label); 174189503Srwatson MAC_CHECK_PROBE2(pipe_check_poll, error, cred, pp); 175100979Srwatson 176100979Srwatson return (error); 177100979Srwatson} 178100979Srwatson 179189503SrwatsonMAC_CHECK_PROBE_DEFINE2(pipe_check_read, "struct ucred *", 180189503Srwatson "struct pipepair *"); 181189503Srwatson 182102115Srwatsonint 183172930Srwatsonmac_pipe_check_read(struct ucred *cred, struct pipepair *pp) 184102115Srwatson{ 185102115Srwatson int error; 186102115Srwatson 187125293Srwatson mtx_assert(&pp->pp_mtx, MA_OWNED); 188104269Srwatson 189191731Srwatson MAC_POLICY_CHECK_NOSLEEP(pipe_check_read, cred, pp, pp->pp_label); 190189503Srwatson MAC_CHECK_PROBE2(pipe_check_read, error, cred, pp); 191102115Srwatson 192102115Srwatson return (error); 193102115Srwatson} 194102115Srwatson 195189503SrwatsonMAC_CHECK_PROBE_DEFINE3(pipe_check_relabel, "struct ucred *", 196189503Srwatson "struct pipepair *", "struct label *"); 197189503Srwatson 198100979Srwatsonstatic int 199172930Srwatsonmac_pipe_check_relabel(struct ucred *cred, struct pipepair *pp, 200100979Srwatson struct label *newlabel) 201100979Srwatson{ 202100979Srwatson int error; 203100979Srwatson 204125293Srwatson mtx_assert(&pp->pp_mtx, MA_OWNED); 205104269Srwatson 206191731Srwatson MAC_POLICY_CHECK_NOSLEEP(pipe_check_relabel, cred, pp, pp->pp_label, 207189797Srwatson newlabel); 208189503Srwatson MAC_CHECK_PROBE3(pipe_check_relabel, error, cred, pp, newlabel); 209100979Srwatson 210100979Srwatson return (error); 211100979Srwatson} 212100979Srwatson 213189503SrwatsonMAC_CHECK_PROBE_DEFINE2(pipe_check_stat, "struct ucred *", 214189503Srwatson "struct pipepair *"); 215189503Srwatson 216100979Srwatsonint 217172930Srwatsonmac_pipe_check_stat(struct ucred *cred, struct pipepair *pp) 218102115Srwatson{ 219102115Srwatson int error; 220102115Srwatson 221125293Srwatson mtx_assert(&pp->pp_mtx, MA_OWNED); 222104269Srwatson 223191731Srwatson MAC_POLICY_CHECK_NOSLEEP(pipe_check_stat, cred, pp, pp->pp_label); 224189503Srwatson MAC_CHECK_PROBE2(pipe_check_stat, error, cred, pp); 225102115Srwatson 226102115Srwatson return (error); 227102115Srwatson} 228102115Srwatson 229189503SrwatsonMAC_CHECK_PROBE_DEFINE2(pipe_check_write, "struct ucred *", 230189503Srwatson "struct pipepair *"); 231189503Srwatson 232102115Srwatsonint 233172930Srwatsonmac_pipe_check_write(struct ucred *cred, struct pipepair *pp) 234102115Srwatson{ 235102115Srwatson int error; 236102115Srwatson 237125293Srwatson mtx_assert(&pp->pp_mtx, MA_OWNED); 238104269Srwatson 239191731Srwatson MAC_POLICY_CHECK_NOSLEEP(pipe_check_write, cred, pp, pp->pp_label); 240189503Srwatson MAC_CHECK_PROBE2(pipe_check_write, error, cred, pp); 241102115Srwatson 242102115Srwatson return (error); 243102115Srwatson} 244102115Srwatson 245102115Srwatsonint 246125293Srwatsonmac_pipe_label_set(struct ucred *cred, struct pipepair *pp, 247125293Srwatson struct label *label) 248100979Srwatson{ 249100979Srwatson int error; 250100979Srwatson 251125293Srwatson mtx_assert(&pp->pp_mtx, MA_OWNED); 252104269Srwatson 253172930Srwatson error = mac_pipe_check_relabel(cred, pp, label); 254100979Srwatson if (error) 255100979Srwatson return (error); 256100979Srwatson 257172930Srwatson mac_pipe_relabel(cred, pp, label); 258100979Srwatson 259100979Srwatson return (0); 260100979Srwatson} 261