mac_label.c revision 165593
1122524Srwatson/*-
2136773Srwatson * Copyright (c) 2003-2004 Networks Associates Technology, Inc.
3122524Srwatson * All rights reserved.
4122524Srwatson *
5122524Srwatson * This software was developed for the FreeBSD Project in part by Network
6122524Srwatson * Associates Laboratories, the Security Research Division of Network
7122524Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
8122524Srwatson * as part of the DARPA CHATS research program.
9122524Srwatson *
10122524Srwatson * Redistribution and use in source and binary forms, with or without
11122524Srwatson * modification, are permitted provided that the following conditions
12122524Srwatson * are met:
13122524Srwatson * 1. Redistributions of source code must retain the above copyright
14122524Srwatson *    notice, this list of conditions and the following disclaimer.
15122524Srwatson * 2. Redistributions in binary form must reproduce the above copyright
16122524Srwatson *    notice, this list of conditions and the following disclaimer in the
17122524Srwatson *    documentation and/or other materials provided with the distribution.
18122524Srwatson *
19122524Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20122524Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21122524Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22122524Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23122524Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24122524Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25122524Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26122524Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27122524Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28122524Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29122524Srwatson * SUCH DAMAGE.
30122524Srwatson */
31122524Srwatson
32122524Srwatson#include <sys/cdefs.h>
33122524Srwatson__FBSDID("$FreeBSD: head/sys/security/mac/mac_label.c 165593 2006-12-28 21:15:37Z rwatson $");
34122524Srwatson
35122524Srwatson#include "opt_mac.h"
36122524Srwatson
37122524Srwatson#include <sys/param.h>
38122524Srwatson#include <sys/mac.h>
39122524Srwatson#include <sys/sysctl.h>
40122524Srwatson#include <sys/systm.h>
41122524Srwatson
42122524Srwatson#include <vm/uma.h>
43122524Srwatson
44163606Srwatson#include <security/mac/mac_framework.h>
45122524Srwatson#include <security/mac/mac_internal.h>
46122524Srwatson
47165422Srwatson/*
48165593Srwatson * zone_label is the UMA zone from which most labels are allocated.  Label
49165422Srwatson * structures are initialized to zero bytes so that policies see a NULL/0
50165422Srwatson * slot on first use, even if the policy is loaded after the label is
51165422Srwatson * allocated for an object.
52165422Srwatson */
53165422Srwatsonstatic uma_zone_t	zone_label;
54122524Srwatson
55132987Sgreenstatic int	mac_labelzone_ctor(void *mem, int size, void *arg, int flags);
56122524Srwatsonstatic void	mac_labelzone_dtor(void *mem, int size, void *arg);
57122524Srwatson
58122524Srwatsonvoid
59122524Srwatsonmac_labelzone_init(void)
60122524Srwatson{
61122524Srwatson
62122524Srwatson	zone_label = uma_zcreate("MAC labels", sizeof(struct label),
63122524Srwatson	    mac_labelzone_ctor, mac_labelzone_dtor, NULL, NULL,
64122524Srwatson	    UMA_ALIGN_PTR, 0);
65122524Srwatson}
66122524Srwatson
67165593Srwatson/*
68165593Srwatson * mac_init_label() and mac_destroy_label() are exported so that they can be
69165593Srwatson * used in mbuf tag initialization, where labels are not slab allocated from
70165593Srwatson * the zone_label zone.
71165593Srwatson */
72165593Srwatsonvoid
73165593Srwatsonmac_init_label(struct label *label)
74165593Srwatson{
75165593Srwatson
76165593Srwatson	bzero(label, sizeof(*label));
77165593Srwatson	label->l_flags = MAC_FLAG_INITIALIZED;
78165593Srwatson}
79165593Srwatson
80165593Srwatsonvoid
81165593Srwatsonmac_destroy_label(struct label *label)
82165593Srwatson{
83165593Srwatson
84165593Srwatson	KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,
85165593Srwatson	    ("destroying uninitialized label"));
86165593Srwatson
87165593Srwatson#ifdef DIAGNOSTIC
88165593Srwatson	bzero(label, sizeof(*label));
89165593Srwatson#else
90165593Srwatson	label->l_flags &= ~MAC_FLAG_INITIALIZED;
91165593Srwatson#endif
92165593Srwatson}
93165593Srwatson
94165593Srwatson
95132987Sgreenstatic int
96132987Sgreenmac_labelzone_ctor(void *mem, int size, void *arg, int flags)
97122524Srwatson{
98122524Srwatson	struct label *label;
99122524Srwatson
100122524Srwatson	KASSERT(size == sizeof(*label), ("mac_labelzone_ctor: wrong size\n"));
101122524Srwatson	label = mem;
102165593Srwatson	mac_init_label(label);
103132987Sgreen	return (0);
104122524Srwatson}
105122524Srwatson
106122524Srwatsonstatic void
107122524Srwatsonmac_labelzone_dtor(void *mem, int size, void *arg)
108122524Srwatson{
109122524Srwatson	struct label *label;
110122524Srwatson
111122524Srwatson	KASSERT(size == sizeof(*label), ("mac_labelzone_dtor: wrong size\n"));
112122524Srwatson	label = mem;
113165593Srwatson	mac_destroy_label(label);
114122524Srwatson}
115122524Srwatson
116122524Srwatsonstruct label *
117122524Srwatsonmac_labelzone_alloc(int flags)
118122524Srwatson{
119122524Srwatson
120122524Srwatson	return (uma_zalloc(zone_label, flags));
121122524Srwatson}
122122524Srwatson
123122524Srwatsonvoid
124122524Srwatsonmac_labelzone_free(struct label *label)
125122524Srwatson{
126122524Srwatson
127122524Srwatson	uma_zfree(zone_label, label);
128122524Srwatson}
129