2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
3 * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
6 * This software was developed by Robert Watson for the TrustedBSD Project.
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.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
37 #include <sys/types.h>
38 #include <sys/queue.h>
39 #include <sys/sysctl.h>
51 static int internal_initialized;
54 * Maintain a list of default label preparations for various object
55 * types. Each name will appear only once in the list.
57 * XXXMAC: Not thread-safe.
59 static LIST_HEAD(, label_default) label_default_head;
60 struct label_default {
63 LIST_ENTRY(label_default) ld_entries;
67 mac_destroy_labels(void)
69 struct label_default *ld;
71 while ((ld = LIST_FIRST(&label_default_head))) {
74 LIST_REMOVE(ld, ld_entries);
80 mac_destroy_internal(void)
85 internal_initialized = 0;
89 mac_add_type(const char *name, const char *labels)
91 struct label_default *ld, *ld_new;
92 char *name_dup, *labels_dup;
95 * Speculatively allocate all the memory now to avoid allocating
96 * later when we will someday hold a mutex.
98 name_dup = strdup(name);
99 if (name_dup == NULL) {
103 labels_dup = strdup(labels);
104 if (labels_dup == NULL) {
109 ld_new = malloc(sizeof(*ld));
110 if (ld_new == NULL) {
118 * If the type is already present, replace the current entry
119 * rather than add a new instance.
121 for (ld = LIST_FIRST(&label_default_head); ld != NULL;
122 ld = LIST_NEXT(ld, ld_entries)) {
123 if (strcmp(name, ld->ld_name) == 0)
129 ld->ld_labels = labels_dup;
133 ld->ld_name = name_dup;
134 ld->ld_labels = labels_dup;
140 LIST_INSERT_HEAD(&label_default_head, ld, ld_entries);
143 if (name_dup != NULL)
145 if (labels_dup != NULL)
154 next_token(char **string)
158 token = strsep(string, " \t");
159 while (token != NULL && *token == '\0')
160 token = strsep(string, " \t");
166 mac_init_internal(int ignore_errors)
168 const char *filename;
175 LIST_INIT(&label_default_head);
177 if (!issetugid() && getenv("MAC_CONFFILE") != NULL)
178 filename = getenv("MAC_CONFFILE");
180 filename = MAC_CONFFILE;
181 file = fopen(filename, "r");
185 while (fgets(line, LINE_MAX, file)) {
186 char *comment, *parse, *statement;
188 if (line[strlen(line)-1] == '\n')
189 line[strlen(line)-1] = '\0';
198 /* Remove any comment. */
200 parse = strsep(&comment, "#");
202 /* Blank lines OK. */
203 statement = next_token(&parse);
204 if (statement == NULL)
207 if (strcmp(statement, "default_labels") == 0) {
210 name = next_token(&parse);
211 labels = next_token(&parse);
212 if (name == NULL || labels == NULL ||
213 next_token(&parse) != NULL) {
221 if (mac_add_type(name, labels) == -1) {
227 } else if (strcmp(statement, "default_ifnet_labels") == 0 ||
228 strcmp(statement, "default_file_labels") == 0 ||
229 strcmp(statement, "default_process_labels") == 0) {
232 if (strcmp(statement, "default_ifnet_labels") == 0)
234 else if (strcmp(statement, "default_file_labels") == 0)
236 else if (strcmp(statement, "default_process_labels") ==
240 labels = next_token(&parse);
241 if (labels == NULL || next_token(&parse) != NULL) {
249 if (mac_add_type(type, labels) == -1) {
266 internal_initialized = 1;
270 mac_destroy_internal();
275 mac_maybe_init_internal(void)
278 if (!internal_initialized)
279 return (mac_init_internal(1));
288 if (internal_initialized)
289 mac_destroy_internal();
290 return (mac_init_internal(0));
294 mac_free(struct mac *mac)
297 if (mac->m_string != NULL)
305 mac_from_text(struct mac **mac, const char *text)
308 *mac = (struct mac *) malloc(sizeof(**mac));
312 (*mac)->m_string = strdup(text);
313 if ((*mac)->m_string == NULL) {
319 (*mac)->m_buflen = strlen((*mac)->m_string)+1;
325 mac_to_text(struct mac *mac, char **text)
328 *text = strdup(mac->m_string);
335 mac_prepare(struct mac **mac, const char *elements)
338 if (strlen(elements) >= MAC_MAX_LABEL_BUF_LEN)
341 *mac = (struct mac *) malloc(sizeof(**mac));
345 (*mac)->m_string = malloc(MAC_MAX_LABEL_BUF_LEN);
346 if ((*mac)->m_string == NULL) {
352 strcpy((*mac)->m_string, elements);
353 (*mac)->m_buflen = MAC_MAX_LABEL_BUF_LEN;
359 mac_prepare_type(struct mac **mac, const char *name)
361 struct label_default *ld;
364 error = mac_maybe_init_internal();
368 for (ld = LIST_FIRST(&label_default_head); ld != NULL;
369 ld = LIST_NEXT(ld, ld_entries)) {
370 if (strcmp(name, ld->ld_name) == 0)
371 return (mac_prepare(mac, ld->ld_labels));
375 return (-1); /* XXXMAC: ENOLABEL */
379 mac_prepare_ifnet_label(struct mac **mac)
382 return (mac_prepare_type(mac, "ifnet"));
386 mac_prepare_file_label(struct mac **mac)
389 return (mac_prepare_type(mac, "file"));
393 mac_prepare_packet_label(struct mac **mac)
396 return (mac_prepare_type(mac, "packet"));
400 mac_prepare_process_label(struct mac **mac)
403 return (mac_prepare_type(mac, "process"));
407 * Simply test whether the TrustedBSD/MAC MIB tree is present; if so,
408 * return 1 to indicate that the system has MAC enabled overall or for
412 mac_is_present(const char *policyname)
419 if (policyname != NULL) {
420 if (policyname[strcspn(policyname, ".=")] != '\0') {
424 mibname = malloc(sizeof("security.mac.") - 1 +
425 strlen(policyname) + sizeof(".enabled"));
428 strcpy(mibname, "security.mac.");
429 strcat(mibname, policyname);
430 strcat(mibname, ".enabled");
432 error = sysctlnametomib(mibname, mib, &siz);
436 error = sysctlnametomib("security.mac", mib, &siz);