1 /*- 2 * Copyright (c) 2003-2006 SPARTA, Inc. 3 * Copyright (c) 2009-2011 Robert N. M. Watson 4 * All rights reserved. 5 * 6 * This software was developed for the FreeBSD Project in part by Network 7 * Associates Laboratories, the Security Research Division of Network 8 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 9 * as part of the DARPA CHATS research program. 10 * 11 * This software was enhanced by SPARTA ISSO under SPAWAR contract 12 * N66001-04-C-6019 ("SEFOS"). * 13 * 14 * This software was developed at the University of Cambridge Computer 15 * Laboratory with support from a grant from Google, Inc. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include "opt_mac.h" 43 44 #include <sys/param.h> 45 #include <sys/kernel.h> 46 #include <sys/mman.h> 47 #include <sys/malloc.h> 48 #include <sys/module.h> 49 #include <sys/sdt.h> 50 #include <sys/systm.h> 51 #include <sys/sysctl.h> 52 53 #include <security/mac/mac_framework.h> 54 #include <security/mac/mac_internal.h> 55 #include <security/mac/mac_policy.h> 56 57 static struct label * 58 mac_posixshm_label_alloc(void) 59 { 60 struct label *label; 61 62 label = mac_labelzone_alloc(M_WAITOK); 63 MAC_POLICY_PERFORM(posixshm_init_label, label); 64 return (label); 65 } 66 67 void 68 mac_posixshm_init(struct shmfd *shmfd) 69 { 70 71 if (mac_labeled & MPC_OBJECT_POSIXSHM) 72 shmfd->shm_label = mac_posixshm_label_alloc(); 73 else 74 shmfd->shm_label = NULL; 75 } 76 77 static void 78 mac_posixshm_label_free(struct label *label) 79 { 80 81 MAC_POLICY_PERFORM_NOSLEEP(posixshm_destroy_label, label); 82 mac_labelzone_free(label); 83 } 84 85 void 86 mac_posixshm_destroy(struct shmfd *shmfd) 87 { 88 89 if (shmfd->shm_label != NULL) { 90 mac_posixshm_label_free(shmfd->shm_label); 91 shmfd->shm_label = NULL; 92 } 93 } 94 95 void 96 mac_posixshm_create(struct ucred *cred, struct shmfd *shmfd) 97 { 98 99 MAC_POLICY_PERFORM_NOSLEEP(posixshm_create, cred, shmfd, 100 shmfd->shm_label); 101 } 102 103 MAC_CHECK_PROBE_DEFINE2(posixshm_check_create, "struct ucred *", 104 "const char *"); 105 106 int 107 mac_posixshm_check_create(struct ucred *cred, const char *path) 108 { 109 int error; 110 111 MAC_POLICY_CHECK_NOSLEEP(posixshm_check_create, cred, path); 112 MAC_CHECK_PROBE2(posixshm_check_create, error, cred, path); 113 114 return (error); 115 } 116 117 MAC_CHECK_PROBE_DEFINE4(posixshm_check_mmap, "struct ucred *", 118 "struct shmfd *", "int", "int"); 119 120 int 121 mac_posixshm_check_mmap(struct ucred *cred, struct shmfd *shmfd, int prot, 122 int flags) 123 { 124 int error; 125 126 MAC_POLICY_CHECK_NOSLEEP(posixshm_check_mmap, cred, shmfd, 127 shmfd->shm_label, prot, flags); 128 MAC_CHECK_PROBE4(posixshm_check_mmap, error, cred, shmfd, prot, 129 flags); 130 131 return (error); 132 } 133 134 MAC_CHECK_PROBE_DEFINE3(posixshm_check_open, "struct ucred *", 135 "struct shmfd *", "accmode_t"); 136 137 int 138 mac_posixshm_check_open(struct ucred *cred, struct shmfd *shmfd, 139 accmode_t accmode) 140 { 141 int error; 142 143 MAC_POLICY_CHECK_NOSLEEP(posixshm_check_open, cred, shmfd, 144 shmfd->shm_label, accmode); 145 MAC_CHECK_PROBE3(posixshm_check_open, error, cred, shmfd, accmode); 146 147 return (error); 148 } 149 150 MAC_CHECK_PROBE_DEFINE3(posixshm_check_stat, "struct ucred *", 151 "struct ucred *", "struct shmfd *"); 152 153 int 154 mac_posixshm_check_stat(struct ucred *active_cred, struct ucred *file_cred, 155 struct shmfd *shmfd) 156 { 157 int error; 158 159 MAC_POLICY_CHECK_NOSLEEP(posixshm_check_stat, active_cred, file_cred, 160 shmfd, shmfd->shm_label); 161 MAC_CHECK_PROBE3(posixshm_check_stat, error, active_cred, file_cred, 162 shmfd); 163 164 return (error); 165 } 166 167 MAC_CHECK_PROBE_DEFINE3(posixshm_check_truncate, "struct ucred *", 168 "struct ucred *", "struct shmfd *"); 169 170 int 171 mac_posixshm_check_truncate(struct ucred *active_cred, struct ucred *file_cred, 172 struct shmfd *shmfd) 173 { 174 int error; 175 176 MAC_POLICY_CHECK_NOSLEEP(posixshm_check_truncate, active_cred, 177 file_cred, shmfd, shmfd->shm_label); 178 MAC_CHECK_PROBE3(posixshm_check_truncate, error, active_cred, 179 file_cred, shmfd); 180 181 return (error); 182 } 183 184 MAC_CHECK_PROBE_DEFINE2(posixshm_check_unlink, "struct ucred *", 185 "struct shmfd *"); 186 187 int 188 mac_posixshm_check_unlink(struct ucred *cred, struct shmfd *shmfd) 189 { 190 int error; 191 192 MAC_POLICY_CHECK_NOSLEEP(posixshm_check_unlink, cred, shmfd, 193 shmfd->shm_label); 194 MAC_CHECK_PROBE2(posixshm_check_unlink, error, cred, shmfd); 195 196 return (error); 197 } 198 199 MAC_CHECK_PROBE_DEFINE3(posixshm_check_setmode, "struct ucred *", 200 "struct shmfd *", "mode_t"); 201 202 int 203 mac_posixshm_check_setmode(struct ucred *cred, struct shmfd *shmfd, mode_t mode) 204 { 205 int error; 206 207 MAC_POLICY_CHECK_NOSLEEP(posixshm_check_setmode, cred, shmfd, 208 shmfd->shm_label, mode); 209 MAC_CHECK_PROBE3(posixshm_check_setmode, error, cred, shmfd, mode); 210 211 return (error); 212 } 213 214 MAC_CHECK_PROBE_DEFINE4(posixshm_check_setowner, "struct ucred *", 215 "struct shmfd *", "uid_t", "gid_t"); 216 217 int 218 mac_posixshm_check_setowner(struct ucred *cred, struct shmfd *shmfd, uid_t uid, 219 gid_t gid) 220 { 221 int error; 222 223 MAC_POLICY_CHECK_NOSLEEP(posixshm_check_setowner, cred, shmfd, 224 shmfd->shm_label, uid, gid); 225 MAC_CHECK_PROBE4(posixshm_check_setowner, error, cred, shmfd, 226 uid, gid); 227 228 return (error); 229 } 230 231 MAC_CHECK_PROBE_DEFINE3(posixshm_check_read, "struct ucred *", 232 "struct ucred *", "struct shmfd *"); 233 234 int 235 mac_posixshm_check_read(struct ucred *active_cred, struct ucred *file_cred, 236 struct shmfd *shmfd) 237 { 238 int error; 239 240 MAC_POLICY_CHECK_NOSLEEP(posixshm_check_read, active_cred, 241 file_cred, shmfd, shmfd->shm_label); 242 MAC_CHECK_PROBE3(posixshm_check_read, error, active_cred, 243 file_cred, shmfd); 244 245 return (error); 246 } 247 248 MAC_CHECK_PROBE_DEFINE3(posixshm_check_write, "struct ucred *", 249 "struct ucred *", "struct shmfd *"); 250 251 int 252 mac_posixshm_check_write(struct ucred *active_cred, struct ucred *file_cred, 253 struct shmfd *shmfd) 254 { 255 int error; 256 257 MAC_POLICY_CHECK_NOSLEEP(posixshm_check_write, active_cred, 258 file_cred, shmfd, shmfd->shm_label); 259 MAC_CHECK_PROBE3(posixshm_check_write, error, active_cred, 260 file_cred, shmfd); 261 262 return (error); 263 } 264