1 /* 2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson 3 * Copyright (c) 2002, 2003 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed by Robert Watson for the TrustedBSD Project. 7 * 8 * This software was developed for the FreeBSD Project in part by Network 9 * Associates Laboratories, the Security Research Division of Network 10 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 11 * as part of the DARPA CHATS research program. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $FreeBSD$ 35 */ 36 37 #include <sys/types.h> 38 #include <sys/queue.h> 39 #include <sys/sysctl.h> 40 41 #include <dlfcn.h> 42 #include <errno.h> 43 #include <limits.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 48 #include <sys/mac.h> 49 50 static int internal_initialized; 51 52 /* 53 * Maintain a list of default label preparations for various object 54 * types. Each name will appear only once in the list. 55 * 56 * XXXMAC: Not thread-safe. 57 */ 58 LIST_HEAD(, label_default) label_default_head; 59 struct label_default { 60 char *ld_name; 61 char *ld_labels; 62 LIST_ENTRY(label_default) ld_entries; 63 }; 64 65 static void 66 mac_destroy_labels(void) 67 { 68 struct label_default *ld; 69 70 while ((ld = LIST_FIRST(&label_default_head))) { 71 free(ld->ld_name); 72 free(ld->ld_labels); 73 LIST_REMOVE(ld, ld_entries); 74 free(ld); 75 } 76 } 77 78 static void 79 mac_destroy_internal(void) 80 { 81 82 mac_destroy_labels(); 83 84 internal_initialized = 0; 85 } 86 87 static int 88 mac_add_type(const char *name, const char *labels) 89 { 90 struct label_default *ld, *ld_new; 91 char *name_dup, *labels_dup; 92 93 /* 94 * Speculatively allocate all the memory now to avoid allocating 95 * later when we will someday hold a mutex. 96 */ 97 name_dup = strdup(name); 98 if (name_dup == NULL) { 99 errno = ENOMEM; 100 return (-1); 101 } 102 labels_dup = strdup(labels); 103 if (labels_dup == NULL) { 104 free(name_dup); 105 errno = ENOMEM; 106 return (-1); 107 } 108 ld_new = malloc(sizeof(*ld)); 109 if (ld_new == NULL) { 110 free(name_dup); 111 free(labels_dup); 112 errno = ENOMEM; 113 return (-1); 114 } 115 116 /* 117 * If the type is already present, replace the current entry 118 * rather than add a new instance. 119 */ 120 for (ld = LIST_FIRST(&label_default_head); ld != NULL; 121 ld = LIST_NEXT(ld, ld_entries)) { 122 if (strcmp(name, ld->ld_name) == 0) 123 break; 124 } 125 126 if (ld != NULL) { 127 free(ld->ld_labels); 128 ld->ld_labels = labels_dup; 129 labels_dup = NULL; 130 } else { 131 ld = ld_new; 132 ld->ld_name = name_dup; 133 ld->ld_labels = labels_dup; 134 135 ld_new = NULL; 136 name_dup = NULL; 137 labels_dup = NULL; 138 139 LIST_INSERT_HEAD(&label_default_head, ld, ld_entries); 140 } 141 142 if (name_dup != NULL) 143 free(name_dup); 144 if (labels_dup != NULL) 145 free(labels_dup); 146 if (ld_new != NULL) 147 free(ld_new); 148 149 return (0); 150 } 151 152 static char * 153 next_token(char **string) 154 { 155 char *token; 156 157 token = strsep(string, " \t"); 158 while (token != NULL && *token == '\0') 159 token = strsep(string, " \t"); 160 161 return (token); 162 } 163 164 static int 165 mac_init_internal(int ignore_errors) 166 { 167 const char *filename; 168 char line[LINE_MAX]; 169 FILE *file; 170 int error; 171 172 error = 0; 173 174 LIST_INIT(&label_default_head); 175 176 if (!issetugid() && getenv("MAC_CONFFILE") != NULL) 177 filename = getenv("MAC_CONFFILE"); 178 else 179 filename = MAC_CONFFILE; 180 file = fopen(filename, "r"); 181 if (file == NULL) 182 return (0); 183 184 while (fgets(line, LINE_MAX, file)) { 185 char *arg, *comment, *parse, *statement; 186 187 if (line[strlen(line)-1] == '\n') 188 line[strlen(line)-1] = '\0'; 189 else { 190 if (ignore_errors) 191 continue; 192 fclose(file); 193 error = EINVAL; 194 goto just_return; 195 } 196 197 /* Remove any comment. */ 198 comment = line; 199 parse = strsep(&comment, "#"); 200 201 /* Blank lines OK. */ 202 statement = next_token(&parse); 203 if (statement == NULL) 204 continue; 205 206 if (strcmp(statement, "default_labels") == 0) { 207 char *name, *labels; 208 209 name = next_token(&parse); 210 labels = next_token(&parse); 211 if (name == NULL || labels == NULL || 212 next_token(&parse) != NULL) { 213 if (ignore_errors) 214 continue; 215 error = EINVAL; 216 fclose(file); 217 goto just_return; 218 } 219 220 if (mac_add_type(name, labels) == -1) { 221 if (ignore_errors) 222 continue; 223 fclose(file); 224 goto just_return; 225 } 226 } else if (strcmp(statement, "default_ifnet_labels") == 0 || 227 strcmp(statement, "default_file_labels") == 0 || 228 strcmp(statement, "default_process_labels") == 0) { 229 char *labels, *type; 230 231 if (strcmp(statement, "default_ifnet_labels") == 0) 232 type = "ifnet"; 233 else if (strcmp(statement, "default_file_labels") == 0) 234 type = "file"; 235 else if (strcmp(statement, "default_process_labels") == 236 0) 237 type = "process"; 238 239 labels = next_token(&parse); 240 if (labels == NULL || next_token(&parse) != NULL) { 241 if (ignore_errors) 242 continue; 243 error = EINVAL; 244 fclose(file); 245 goto just_return; 246 } 247 248 if (mac_add_type(type, labels) == -1) { 249 if (ignore_errors) 250 continue; 251 fclose(file); 252 goto just_return; 253 } 254 } else { 255 if (ignore_errors) 256 continue; 257 fclose(file); 258 error = EINVAL; 259 goto just_return; 260 } 261 } 262 263 fclose(file); 264 265 internal_initialized = 1; 266 267 just_return: 268 if (error != 0) 269 mac_destroy_internal(); 270 return (error); 271 } 272 273 static int 274 mac_maybe_init_internal(void) 275 { 276 277 if (!internal_initialized) 278 return (mac_init_internal(1)); 279 else 280 return (0); 281 } 282 283 int 284 mac_reload(void) 285 { 286 287 if (internal_initialized) 288 mac_destroy_internal(); 289 return (mac_init_internal(0)); 290 } 291 292 int 293 mac_free(struct mac *mac) 294 { 295 296 if (mac->m_string != NULL) 297 free(mac->m_string); 298 free(mac); 299 300 return (0); 301 } 302 303 int 304 mac_from_text(struct mac **mac, const char *text) 305 { 306 307 *mac = (struct mac *) malloc(sizeof(**mac)); 308 if (*mac == NULL) 309 return (ENOMEM); 310 311 (*mac)->m_string = strdup(text); 312 if ((*mac)->m_string == NULL) { 313 free(*mac); 314 *mac = NULL; 315 return (ENOMEM); 316 } 317 318 (*mac)->m_buflen = strlen((*mac)->m_string)+1; 319 320 return (0); 321 } 322 323 int 324 mac_to_text(struct mac *mac, char **text) 325 { 326 327 *text = strdup(mac->m_string); 328 if (*text == NULL) 329 return (ENOMEM); 330 return (0); 331 } 332 333 int 334 mac_prepare(struct mac **mac, const char *elements) 335 { 336 337 if (strlen(elements) >= MAC_MAX_LABEL_BUF_LEN) 338 return (EINVAL); 339 340 *mac = (struct mac *) malloc(sizeof(**mac)); 341 if (*mac == NULL) 342 return (ENOMEM); 343 344 (*mac)->m_string = malloc(MAC_MAX_LABEL_BUF_LEN); 345 if ((*mac)->m_string == NULL) { 346 free(*mac); 347 *mac = NULL; 348 return (ENOMEM); 349 } 350 351 strcpy((*mac)->m_string, elements); 352 (*mac)->m_buflen = MAC_MAX_LABEL_BUF_LEN; 353 354 return (0); 355 } 356 357 int 358 mac_prepare_type(struct mac **mac, const char *name) 359 { 360 struct label_default *ld; 361 362 for (ld = LIST_FIRST(&label_default_head); ld != NULL; 363 ld = LIST_NEXT(ld, ld_entries)) { 364 if (strcmp(name, ld->ld_name) == 0) 365 return (mac_prepare(mac, ld->ld_labels)); 366 } 367 368 return (ENOENT); /* XXXMAC: ENOLABEL */ 369 } 370 371 int 372 mac_prepare_ifnet_label(struct mac **mac) 373 { 374 int error; 375 376 error = mac_maybe_init_internal(); 377 if (error != 0) 378 return (error); 379 380 return (mac_prepare_type(mac, "ifnet")); 381 } 382 383 int 384 mac_prepare_file_label(struct mac **mac) 385 { 386 int error; 387 388 error = mac_maybe_init_internal(); 389 if (error != 0) 390 return (error); 391 392 return (mac_prepare_type(mac, "file")); 393 } 394 395 int 396 mac_prepare_packet_label(struct mac **mac) 397 { 398 int error; 399 400 error = mac_maybe_init_internal(); 401 if (error != 0) 402 return (error); 403 404 return (mac_prepare_type(mac, "packet")); 405 } 406 407 int 408 mac_prepare_process_label(struct mac **mac) 409 { 410 int error; 411 412 error = mac_maybe_init_internal(); 413 if (error != 0) 414 return (error); 415 416 return (mac_prepare_type(mac, "process")); 417 } 418 419 /* 420 * Simply test whether the TrustedBSD/MAC MIB tree is present; if so, 421 * return 1 to indicate that the system has MAC enabled overall or for 422 * a given policy. 423 */ 424 int 425 mac_is_present(const char *policyname) 426 { 427 int mib[5]; 428 size_t siz; 429 char *mibname; 430 int error; 431 432 if (policyname != NULL) { 433 if (policyname[strcspn(policyname, ".=")] != '\0') { 434 errno = EINVAL; 435 return (-1); 436 } 437 mibname = malloc(sizeof("security.mac.") - 1 + 438 strlen(policyname) + sizeof(".enabled")); 439 if (mibname == NULL) 440 return (-1); 441 strcpy(mibname, "security.mac."); 442 strcat(mibname, policyname); 443 strcat(mibname, ".enabled"); 444 siz = 5; 445 error = sysctlnametomib(mibname, mib, &siz); 446 free(mibname); 447 } else { 448 siz = 3; 449 error = sysctlnametomib("security.mac", mib, &siz); 450 } 451 if (error == -1) { 452 switch (errno) { 453 case ENOTDIR: 454 case ENOENT: 455 return (0); 456 default: 457 return (error); 458 } 459 } 460 return (1); 461 } 462