xref: /freebsd/sys/security/mac/mac_priv.c (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
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 __FBSDID("$FreeBSD$");
40 
41 #include "opt_mac.h"
42 
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/priv.h>
46 #include <sys/sdt.h>
47 #include <sys/module.h>
48 
49 #include <security/mac/mac_framework.h>
50 #include <security/mac/mac_internal.h>
51 #include <security/mac/mac_policy.h>
52 
53 /*
54  * The MAC Framework interacts with kernel privilege checks in two ways: it
55  * may restrict the granting of privilege to a subject, and it may grant
56  * additional privileges to the subject.  Policies may implement none, one,
57  * or both of these entry points.  Restriction of privilege by any policy
58  * always overrides granting of privilege by any policy or other privilege
59  * mechanism.  See kern_priv.c:priv_check_cred() for details of the
60  * composition.
61  */
62 
63 MAC_CHECK_PROBE_DEFINE2(priv_check, "struct ucred *", "int");
64 
65 /*
66  * Restrict access to a privilege for a credential.  Return failure if any
67  * policy denies access.
68  */
69 int
70 mac_priv_check(struct ucred *cred, int priv)
71 {
72 	int error;
73 
74 	MAC_POLICY_CHECK_NOSLEEP(priv_check, cred, priv);
75 	MAC_CHECK_PROBE2(priv_check, error, cred, priv);
76 
77 	return (error);
78 }
79 
80 MAC_GRANT_PROBE_DEFINE2(priv_grant, "struct ucred *", "int");
81 
82 /*
83  * Grant access to a privilege for a credential.  Return success if any
84  * policy grants access.
85  */
86 int
87 mac_priv_grant(struct ucred *cred, int priv)
88 {
89 	int error;
90 
91 	MAC_POLICY_GRANT_NOSLEEP(priv_grant, cred, priv);
92 	MAC_GRANT_PROBE2(priv_grant, error, cred, priv);
93 
94 	return (error);
95 }
96