xref: /freebsd/sys/security/mac/mac_priv.c (revision db33c6f3ae9d1231087710068ee4ea5398aacca7)
1 /*-
2  * Copyright (c) 2006 nCircle Network Security, Inc.
3  * Copyright (c) 2009 Robert N. M. Watson
4  * All rights reserved.
5  *
6  * This software was developed by Robert N. M. Watson for the TrustedBSD
7  * Project under contract to nCircle Network Security, Inc.
8  *
9  * This software was developed at the University of Cambridge Computer
10  * Laboratory with support from a grant from Google, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
25  * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * MAC checks for system privileges.
36  */
37 
38 #include "sys/cdefs.h"
39 #include "opt_mac.h"
40 
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/priv.h>
44 #include <sys/sdt.h>
45 #include <sys/module.h>
46 
47 #include <security/mac/mac_framework.h>
48 #include <security/mac/mac_internal.h>
49 #include <security/mac/mac_policy.h>
50 
51 /*
52  * The MAC Framework interacts with kernel privilege checks in two ways: it
53  * may restrict the granting of privilege to a subject, and it may grant
54  * additional privileges to the subject.  Policies may implement none, one,
55  * or both of these entry points.  Restriction of privilege by any policy
56  * always overrides granting of privilege by any policy or other privilege
57  * mechanism.  See kern_priv.c:priv_check_cred() for details of the
58  * composition.
59  */
60 
61 MAC_CHECK_PROBE_DEFINE2(priv_check, "struct ucred *", "int");
62 
63 /*
64  * Restrict access to a privilege for a credential.  Return failure if any
65  * policy denies access.
66  */
67 int
68 mac_priv_check_impl(struct ucred *cred, int priv)
69 {
70 	int error;
71 
72 	MAC_POLICY_CHECK_NOSLEEP(priv_check, cred, priv);
73 	MAC_CHECK_PROBE2(priv_check, error, cred, priv);
74 
75 	return (error);
76 }
77 
78 MAC_GRANT_PROBE_DEFINE2(priv_grant, "struct ucred *", "int");
79 
80 /*
81  * Grant access to a privilege for a credential.  Return success if any
82  * policy grants access.
83  */
84 int
85 mac_priv_grant_impl(struct ucred *cred, int priv)
86 {
87 	int error;
88 
89 	MAC_POLICY_GRANT_NOSLEEP(priv_grant, cred, priv);
90 	MAC_GRANT_PROBE2(priv_grant, error, cred, priv);
91 
92 	return (error);
93 }
94