mac_priv.c revision 189503
1164032Srwatson/*- 2164032Srwatson * Copyright (c) 2006 nCircle Network Security, Inc. 3189503Srwatson * Copyright (c) 2009 Robert N. M. Watson 4164032Srwatson * All rights reserved. 5164032Srwatson * 6164032Srwatson * This software was developed by Robert N. M. Watson for the TrustedBSD 7164032Srwatson * Project under contract to nCircle Network Security, Inc. 8164032Srwatson * 9189503Srwatson * This software was developed at the University of Cambridge Computer 10189503Srwatson * Laboratory with support from a grant from Google, Inc. 11189503Srwatson * 12164032Srwatson * Redistribution and use in source and binary forms, with or without 13164032Srwatson * modification, are permitted provided that the following conditions 14164032Srwatson * are met: 15164032Srwatson * 1. Redistributions of source code must retain the above copyright 16164032Srwatson * notice, this list of conditions and the following disclaimer. 17164032Srwatson * 2. Redistributions in binary form must reproduce the above copyright 18164032Srwatson * notice, this list of conditions and the following disclaimer in the 19164032Srwatson * documentation and/or other materials provided with the distribution. 20164032Srwatson * 21164032Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22164032Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23164032Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24164032Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY, 25164032Srwatson * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26164032Srwatson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 27164032Srwatson * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 28164032Srwatson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29164032Srwatson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30164032Srwatson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31164032Srwatson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32164032Srwatson */ 33164032Srwatson 34164032Srwatson/* 35164032Srwatson * MAC checks for system privileges. 36164032Srwatson */ 37164032Srwatson 38187667Srwatson#include "sys/cdefs.h" 39187667Srwatson__FBSDID("$FreeBSD: head/sys/security/mac/mac_priv.c 189503 2009-03-08 00:50:37Z rwatson $"); 40187667Srwatson 41189503Srwatson#include "opt_kdtrace.h" 42164032Srwatson#include "opt_mac.h" 43164032Srwatson 44164032Srwatson#include <sys/param.h> 45189503Srwatson#include <sys/kernel.h> 46164032Srwatson#include <sys/priv.h> 47189503Srwatson#include <sys/sdt.h> 48164032Srwatson#include <sys/module.h> 49164032Srwatson 50164032Srwatson#include <security/mac/mac_framework.h> 51164032Srwatson#include <security/mac/mac_internal.h> 52165469Srwatson#include <security/mac/mac_policy.h> 53164032Srwatson 54165424Srwatson/* 55165424Srwatson * The MAC Framework interacts with kernel privilege checks in two ways: it 56165424Srwatson * may restrict the granting of privilege to a subject, and it may grant 57165424Srwatson * additional privileges to the subject. Policies may implement none, one, 58165424Srwatson * or both of these entry points. Restriction of privilege by any policy 59165424Srwatson * always overrides granting of privilege by any policy or other privilege 60165424Srwatson * mechanism. See kern_priv.c:priv_check_cred() for details of the 61165424Srwatson * composition. 62165424Srwatson */ 63165424Srwatson 64189503SrwatsonMAC_CHECK_PROBE_DEFINE2(priv_check, "struct ucred *", "int"); 65189503Srwatson 66165424Srwatson/* 67165424Srwatson * Restrict access to a privilege for a credential. Return failure if any 68165424Srwatson * policy denies access. 69165424Srwatson */ 70164032Srwatsonint 71164032Srwatsonmac_priv_check(struct ucred *cred, int priv) 72164032Srwatson{ 73164032Srwatson int error; 74164032Srwatson 75164032Srwatson MAC_CHECK(priv_check, cred, priv); 76189503Srwatson MAC_CHECK_PROBE2(priv_check, error, cred, priv); 77164032Srwatson 78164032Srwatson return (error); 79164032Srwatson} 80164032Srwatson 81189503SrwatsonMAC_GRANT_PROBE_DEFINE2(priv_grant, "struct ucred *", "int"); 82189503Srwatson 83165424Srwatson/* 84165424Srwatson * Grant access to a privilege for a credential. Return success if any 85165424Srwatson * policy grants access. 86165424Srwatson */ 87164032Srwatsonint 88164032Srwatsonmac_priv_grant(struct ucred *cred, int priv) 89164032Srwatson{ 90164032Srwatson int error; 91164032Srwatson 92164032Srwatson MAC_GRANT(priv_grant, cred, priv); 93189503Srwatson MAC_GRANT_PROBE2(priv_grant, error, cred, priv); 94164032Srwatson 95164032Srwatson return (error); 96164032Srwatson} 97