mac_label.c revision 136773
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 136773 2004-10-22 11:08:52Z 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
44122524Srwatson#include <security/mac/mac_internal.h>
45122524Srwatson
46122524Srwatsonuma_zone_t	zone_label;
47122524Srwatson
48132987Sgreenstatic int	mac_labelzone_ctor(void *mem, int size, void *arg, int flags);
49122524Srwatsonstatic void	mac_labelzone_dtor(void *mem, int size, void *arg);
50122524Srwatson
51122524Srwatsonvoid
52122524Srwatsonmac_labelzone_init(void)
53122524Srwatson{
54122524Srwatson
55122524Srwatson	zone_label = uma_zcreate("MAC labels", sizeof(struct label),
56122524Srwatson	    mac_labelzone_ctor, mac_labelzone_dtor, NULL, NULL,
57122524Srwatson	    UMA_ALIGN_PTR, 0);
58122524Srwatson}
59122524Srwatson
60132987Sgreenstatic int
61132987Sgreenmac_labelzone_ctor(void *mem, int size, void *arg, int flags)
62122524Srwatson{
63122524Srwatson	struct label *label;
64122524Srwatson
65122524Srwatson	KASSERT(size == sizeof(*label), ("mac_labelzone_ctor: wrong size\n"));
66122524Srwatson	label = mem;
67122524Srwatson	bzero(label, sizeof(*label));
68122524Srwatson	label->l_flags = MAC_FLAG_INITIALIZED;
69132987Sgreen	return (0);
70122524Srwatson}
71122524Srwatson
72122524Srwatsonstatic void
73122524Srwatsonmac_labelzone_dtor(void *mem, int size, void *arg)
74122524Srwatson{
75122524Srwatson	struct label *label;
76122524Srwatson
77122524Srwatson	KASSERT(size == sizeof(*label), ("mac_labelzone_dtor: wrong size\n"));
78122524Srwatson	label = mem;
79136773Srwatson	KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,
80136773Srwatson	    ("mac_labelzone_dtor: label not initialized"));
81122524Srwatson#ifdef DIAGNOSTIC
82122524Srwatson	bzero(label, sizeof(*label));
83122524Srwatson#else
84122524Srwatson	label->l_flags &= ~MAC_FLAG_INITIALIZED;
85122524Srwatson#endif
86122524Srwatson}
87122524Srwatson
88122524Srwatsonstruct label *
89122524Srwatsonmac_labelzone_alloc(int flags)
90122524Srwatson{
91122524Srwatson
92122524Srwatson	return (uma_zalloc(zone_label, flags));
93122524Srwatson}
94122524Srwatson
95122524Srwatsonvoid
96122524Srwatsonmac_labelzone_free(struct label *label)
97122524Srwatson{
98122524Srwatson
99122524Srwatson	uma_zfree(zone_label, label);
100122524Srwatson}
101