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