mac.c revision 122732
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: head/lib/libc/posix1e/mac.c 122732 2003-11-15 03:34:58Z rwatson $
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
50static 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 */
58LIST_HEAD(, label_default) label_default_head;
59struct label_default {
60	char				*ld_name;
61	char				*ld_labels;
62	LIST_ENTRY(label_default)	 ld_entries;
63};
64
65static void
66mac_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
78static void
79mac_destroy_internal(void)
80{
81
82	mac_destroy_labels();
83
84	internal_initialized = 0;
85}
86
87static int
88mac_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
152static char *
153next_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
164static int
165mac_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
267just_return:
268	if (error != 0)
269		mac_destroy_internal();
270	return (error);
271}
272
273static int
274mac_maybe_init_internal(void)
275{
276
277	if (!internal_initialized)
278		return (mac_init_internal(1));
279	else
280		return (0);
281}
282
283int
284mac_reload(void)
285{
286
287	if (internal_initialized)
288		mac_destroy_internal();
289	return (mac_init_internal(0));
290}
291
292int
293mac_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
303int
304mac_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
323int
324mac_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
333int
334mac_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
357int
358mac_prepare_type(struct mac **mac, const char *name)
359{
360	struct label_default *ld;
361	int error;
362
363	error = mac_maybe_init_internal();
364	if (error != 0)
365		return (error);
366
367	for (ld = LIST_FIRST(&label_default_head); ld != NULL;
368	    ld = LIST_NEXT(ld, ld_entries)) {
369		printf("%s\n", ld->ld_name);
370		if (strcmp(name, ld->ld_name) == 0)
371			return (mac_prepare(mac, ld->ld_labels));
372	}
373
374	errno = ENOENT;
375	return (-1);		/* XXXMAC: ENOLABEL */
376}
377
378int
379mac_prepare_ifnet_label(struct mac **mac)
380{
381
382	return (mac_prepare_type(mac, "ifnet"));
383}
384
385int
386mac_prepare_file_label(struct mac **mac)
387{
388
389	return (mac_prepare_type(mac, "file"));
390}
391
392int
393mac_prepare_packet_label(struct mac **mac)
394{
395
396	return (mac_prepare_type(mac, "packet"));
397}
398
399int
400mac_prepare_process_label(struct mac **mac)
401{
402
403	return (mac_prepare_type(mac, "process"));
404}
405
406/*
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
409 * a given policy.
410 */
411int
412mac_is_present(const char *policyname)
413{
414	int mib[5];
415	size_t siz;
416	char *mibname;
417	int error;
418
419	if (policyname != NULL) {
420		if (policyname[strcspn(policyname, ".=")] != '\0') {
421			errno = EINVAL;
422			return (-1);
423		}
424		mibname = malloc(sizeof("security.mac.") - 1 +
425		    strlen(policyname) + sizeof(".enabled"));
426		if (mibname == NULL)
427			return (-1);
428		strcpy(mibname, "security.mac.");
429		strcat(mibname, policyname);
430		strcat(mibname, ".enabled");
431		siz = 5;
432		error = sysctlnametomib(mibname, mib, &siz);
433		free(mibname);
434	} else {
435		siz = 3;
436		error = sysctlnametomib("security.mac", mib, &siz);
437	}
438	if (error == -1) {
439		switch (errno) {
440		case ENOTDIR:
441		case ENOENT:
442			return (0);
443		default:
444			return (error);
445		}
446	}
447	return (1);
448}
449