/*-1* Copyright (c) 2003-2004 Networks Associates Technology, Inc.2* Copyright (c) 2007 Robert N. M. Watson3* All rights reserved.4*5* This software was developed for the FreeBSD Project in part by Network6* Associates Laboratories, the Security Research Division of Network7* Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),8* as part of the DARPA CHATS research program.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18*19* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*/3132#include <sys/cdefs.h>33#include "opt_mac.h"3435#include <sys/param.h>36#include <sys/module.h>37#include <sys/sysctl.h>38#include <sys/systm.h>3940#include <vm/uma.h>4142#include <security/mac/mac_framework.h>43#include <security/mac/mac_internal.h>44#include <security/mac/mac_policy.h>4546/*47* zone_label is the UMA zone from which most labels are allocated. Label48* structures are initialized to zero bytes so that policies see a NULL/049* slot on first use, even if the policy is loaded after the label is50* allocated for an object.51*/52static uma_zone_t zone_label;5354static int mac_labelzone_ctor(void *mem, int size, void *arg, int flags);55static void mac_labelzone_dtor(void *mem, int size, void *arg);5657void58mac_labelzone_init(void)59{6061zone_label = uma_zcreate("MAC labels", sizeof(struct label),62mac_labelzone_ctor, mac_labelzone_dtor, NULL, NULL,63UMA_ALIGN_PTR, 0);64}6566/*67* mac_init_label() and mac_destroy_label() are exported so that they can be68* used in mbuf tag initialization, where labels are not slab allocated from69* the zone_label zone.70*/71void72mac_init_label(struct label *label)73{7475bzero(label, sizeof(*label));76label->l_flags = MAC_FLAG_INITIALIZED;77}7879void80mac_destroy_label(struct label *label)81{8283KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,84("destroying uninitialized label"));8586#ifdef DIAGNOSTIC87bzero(label, sizeof(*label));88#else89label->l_flags &= ~MAC_FLAG_INITIALIZED;90#endif91}9293static int94mac_labelzone_ctor(void *mem, int size, void *arg, int flags)95{96struct label *label;9798KASSERT(size == sizeof(*label), ("mac_labelzone_ctor: wrong size\n"));99label = mem;100mac_init_label(label);101return (0);102}103104static void105mac_labelzone_dtor(void *mem, int size, void *arg)106{107struct label *label;108109KASSERT(size == sizeof(*label), ("mac_labelzone_dtor: wrong size\n"));110label = mem;111mac_destroy_label(label);112}113114struct label *115mac_labelzone_alloc(int flags)116{117118return (uma_zalloc(zone_label, flags));119}120121void122mac_labelzone_free(struct label *label)123{124125uma_zfree(zone_label, label);126}127128/*129* Functions used by policy modules to get and set label values.130*/131intptr_t132mac_label_get(struct label *l, int slot)133{134135KASSERT(l != NULL, ("mac_label_get: NULL label"));136137return (l->l_perpolicy[slot]);138}139140void141mac_label_set(struct label *l, int slot, intptr_t v)142{143144KASSERT(l != NULL, ("mac_label_set: NULL label"));145146l->l_perpolicy[slot] = v;147}148149150