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$");
40187667Srwatson
41164032Srwatson#include "opt_mac.h"
42164032Srwatson
43164032Srwatson#include <sys/param.h>
44189503Srwatson#include <sys/kernel.h>
45164032Srwatson#include <sys/priv.h>
46189503Srwatson#include <sys/sdt.h>
47164032Srwatson#include <sys/module.h>
48164032Srwatson
49164032Srwatson#include <security/mac/mac_framework.h>
50164032Srwatson#include <security/mac/mac_internal.h>
51165469Srwatson#include <security/mac/mac_policy.h>
52164032Srwatson
53165424Srwatson/*
54165424Srwatson * The MAC Framework interacts with kernel privilege checks in two ways: it
55165424Srwatson * may restrict the granting of privilege to a subject, and it may grant
56165424Srwatson * additional privileges to the subject.  Policies may implement none, one,
57165424Srwatson * or both of these entry points.  Restriction of privilege by any policy
58165424Srwatson * always overrides granting of privilege by any policy or other privilege
59165424Srwatson * mechanism.  See kern_priv.c:priv_check_cred() for details of the
60165424Srwatson * composition.
61165424Srwatson */
62165424Srwatson
63189503SrwatsonMAC_CHECK_PROBE_DEFINE2(priv_check, "struct ucred *", "int");
64189503Srwatson
65165424Srwatson/*
66165424Srwatson * Restrict access to a privilege for a credential.  Return failure if any
67165424Srwatson * policy denies access.
68165424Srwatson */
69164032Srwatsonint
70164032Srwatsonmac_priv_check(struct ucred *cred, int priv)
71164032Srwatson{
72164032Srwatson	int error;
73164032Srwatson
74191731Srwatson	MAC_POLICY_CHECK_NOSLEEP(priv_check, cred, priv);
75189503Srwatson	MAC_CHECK_PROBE2(priv_check, error, cred, priv);
76164032Srwatson
77164032Srwatson	return (error);
78164032Srwatson}
79164032Srwatson
80189503SrwatsonMAC_GRANT_PROBE_DEFINE2(priv_grant, "struct ucred *", "int");
81189503Srwatson
82165424Srwatson/*
83165424Srwatson * Grant access to a privilege for a credential.  Return success if any
84165424Srwatson * policy grants access.
85165424Srwatson */
86164032Srwatsonint
87164032Srwatsonmac_priv_grant(struct ucred *cred, int priv)
88164032Srwatson{
89164032Srwatson	int error;
90164032Srwatson
91191731Srwatson	MAC_POLICY_GRANT_NOSLEEP(priv_grant, cred, priv);
92189503Srwatson	MAC_GRANT_PROBE2(priv_grant, error, cred, priv);
93164032Srwatson
94164032Srwatson	return (error);
95164032Srwatson}
96