mac_label.c revision 165422
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 165422 2006-12-20 23:13:04Z 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/* 48165422Srwatson * zone_label is the UMA zone from which all 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 67132987Sgreenstatic int 68132987Sgreenmac_labelzone_ctor(void *mem, int size, void *arg, int flags) 69122524Srwatson{ 70122524Srwatson struct label *label; 71122524Srwatson 72122524Srwatson KASSERT(size == sizeof(*label), ("mac_labelzone_ctor: wrong size\n")); 73122524Srwatson label = mem; 74122524Srwatson bzero(label, sizeof(*label)); 75122524Srwatson label->l_flags = MAC_FLAG_INITIALIZED; 76132987Sgreen return (0); 77122524Srwatson} 78122524Srwatson 79122524Srwatsonstatic void 80122524Srwatsonmac_labelzone_dtor(void *mem, int size, void *arg) 81122524Srwatson{ 82122524Srwatson struct label *label; 83122524Srwatson 84122524Srwatson KASSERT(size == sizeof(*label), ("mac_labelzone_dtor: wrong size\n")); 85122524Srwatson label = mem; 86136773Srwatson KASSERT(label->l_flags & MAC_FLAG_INITIALIZED, 87136773Srwatson ("mac_labelzone_dtor: label not initialized")); 88122524Srwatson#ifdef DIAGNOSTIC 89122524Srwatson bzero(label, sizeof(*label)); 90122524Srwatson#else 91122524Srwatson label->l_flags &= ~MAC_FLAG_INITIALIZED; 92122524Srwatson#endif 93122524Srwatson} 94122524Srwatson 95122524Srwatsonstruct label * 96122524Srwatsonmac_labelzone_alloc(int flags) 97122524Srwatson{ 98122524Srwatson 99122524Srwatson return (uma_zalloc(zone_label, flags)); 100122524Srwatson} 101122524Srwatson 102122524Srwatsonvoid 103122524Srwatsonmac_labelzone_free(struct label *label) 104122524Srwatson{ 105122524Srwatson 106122524Srwatson uma_zfree(zone_label, label); 107122524Srwatson} 108