]> git.saurik.com Git - apple/xnu.git/blame - security/mac_label.c
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / security / mac_label.c
CommitLineData
2d21ac55
A
1/*-
2 * Copyright (c) 2004 Networks Associates Technology, Inc.
3 * Copyright (c) 2005 SPARTA, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project in part by Network
7 * Associates Laboratories, the Security Research Division of Network
8 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
9 * as part of the DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <kern/zalloc.h>
34#include <security/_label.h>
35#include <sys/param.h>
36#include <sys/queue.h>
37#include <sys/systm.h>
38#include <security/mac_internal.h>
39
f427ee49 40static ZONE_DECLARE(zone_label, "MAC Labels", sizeof(struct label), ZC_ZFREE_CLEARMEM);
2d21ac55
A
41
42struct label *
43mac_labelzone_alloc(int flags)
44{
f427ee49 45 int zflags = Z_ZERO | (flags & MAC_NOWAIT);
2d21ac55
A
46 struct label *l;
47
f427ee49
A
48 static_assert(MAC_NOWAIT == Z_NOWAIT);
49 l = zalloc_flags(zone_label, zflags);
50 if (l) {
51 l->l_flags = MAC_FLAG_INITIALIZED;
0a7de745 52 }
0a7de745 53 return l;
2d21ac55
A
54}
55
56void
57mac_labelzone_free(struct label *l)
58{
0a7de745 59 if (l == NULL) {
2d21ac55 60 panic("Free of NULL MAC label\n");
0a7de745 61 }
2d21ac55 62
0a7de745 63 if ((l->l_flags & MAC_FLAG_INITIALIZED) == 0) {
2d21ac55 64 panic("Free of uninitialized label\n");
0a7de745 65 }
2d21ac55
A
66 bzero(l, sizeof(struct label));
67 zfree(zone_label, l);
68}
b0d623f7
A
69
70/*
71 * Functions used by policy modules to get and set label values.
72 */
73intptr_t
74mac_label_get(struct label *l, int slot)
75{
76 KASSERT(l != NULL, ("mac_label_get: NULL label"));
77
0a7de745 78 return (intptr_t) (l->l_perpolicy[slot].l_ptr);
b0d623f7
A
79}
80
81void
82mac_label_set(struct label *l, int slot, intptr_t v)
83{
84 KASSERT(l != NULL, ("mac_label_set: NULL label"));
85
86 l->l_perpolicy[slot].l_ptr = (void *) v;
87}