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