xref: /freebsd/sys/security/mac_pimd/mac_pimd.c (revision db33c6f3ae9d1231087710068ee4ea5398aacca7)
1*14b77062SWojciech Macek /*-
2*14b77062SWojciech Macek  * SPDX-License-Identifier: BSD-2-Clause
3*14b77062SWojciech Macek  *
4*14b77062SWojciech Macek  * Copyright (c) 2022 Semihalf, Stormshield
5*14b77062SWojciech Macek  * Copyright (c) 2018 Ian Lepore <ian@FreeBSD.org>
6*14b77062SWojciech Macek  *
7*14b77062SWojciech Macek  * Redistribution and use in source and binary forms, with or without
8*14b77062SWojciech Macek  * modification, are permitted provided that the following conditions
9*14b77062SWojciech Macek  * are met:
10*14b77062SWojciech Macek  * 1. Redistributions of source code must retain the above copyright
11*14b77062SWojciech Macek  *    notice, this list of conditions and the following disclaimer.
12*14b77062SWojciech Macek  * 2. Redistributions in binary form must reproduce the above copyright
13*14b77062SWojciech Macek  *    notice, this list of conditions and the following disclaimer in the
14*14b77062SWojciech Macek  *    documentation and/or other materials provided with the distribution.
15*14b77062SWojciech Macek  *
16*14b77062SWojciech Macek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*14b77062SWojciech Macek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*14b77062SWojciech Macek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*14b77062SWojciech Macek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*14b77062SWojciech Macek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*14b77062SWojciech Macek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*14b77062SWojciech Macek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*14b77062SWojciech Macek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*14b77062SWojciech Macek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*14b77062SWojciech Macek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*14b77062SWojciech Macek  * SUCH DAMAGE.
27*14b77062SWojciech Macek  */
28*14b77062SWojciech Macek 
29*14b77062SWojciech Macek #include <sys/param.h>
30*14b77062SWojciech Macek #include <sys/kernel.h>
31*14b77062SWojciech Macek #include <sys/module.h>
32*14b77062SWojciech Macek #include <sys/priv.h>
33*14b77062SWojciech Macek #include <sys/sysctl.h>
34*14b77062SWojciech Macek #include <sys/ucred.h>
35*14b77062SWojciech Macek 
36*14b77062SWojciech Macek #include <security/mac/mac_policy.h>
37*14b77062SWojciech Macek 
38*14b77062SWojciech Macek static SYSCTL_NODE(_security_mac, OID_AUTO, pimd,
39*14b77062SWojciech Macek     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
40*14b77062SWojciech Macek     "mac_pimd policy controls");
41*14b77062SWojciech Macek 
42*14b77062SWojciech Macek static int pimd_enabled = 0;
43*14b77062SWojciech Macek SYSCTL_INT(_security_mac_pimd, OID_AUTO, enabled, CTLFLAG_RWTUN,
44*14b77062SWojciech Macek     &pimd_enabled, 0, "Enable mac_pimd policy");
45*14b77062SWojciech Macek 
46*14b77062SWojciech Macek static int pimd_uid = 0;
47*14b77062SWojciech Macek SYSCTL_INT(_security_mac_pimd, OID_AUTO, uid, CTLFLAG_RWTUN,
48*14b77062SWojciech Macek     &pimd_uid, 0, "User id for pimd user");
49*14b77062SWojciech Macek 
50*14b77062SWojciech Macek static int
pimd_priv_grant(struct ucred * cred,int priv)51*14b77062SWojciech Macek pimd_priv_grant(struct ucred *cred, int priv)
52*14b77062SWojciech Macek {
53*14b77062SWojciech Macek 
54*14b77062SWojciech Macek 	if (pimd_enabled && cred->cr_uid == pimd_uid) {
55*14b77062SWojciech Macek 		switch (priv) {
56*14b77062SWojciech Macek 		case PRIV_NETINET_MROUTE:
57*14b77062SWojciech Macek 			return (0);
58*14b77062SWojciech Macek 		default:
59*14b77062SWojciech Macek 			break;
60*14b77062SWojciech Macek 		}
61*14b77062SWojciech Macek 	}
62*14b77062SWojciech Macek 	return (EPERM);
63*14b77062SWojciech Macek }
64*14b77062SWojciech Macek 
65*14b77062SWojciech Macek static struct mac_policy_ops pimd_ops =
66*14b77062SWojciech Macek {
67*14b77062SWojciech Macek 	.mpo_priv_grant = pimd_priv_grant,
68*14b77062SWojciech Macek };
69*14b77062SWojciech Macek 
70*14b77062SWojciech Macek MAC_POLICY_SET(&pimd_ops, mac_pimd, "MAC/pimd",
71*14b77062SWojciech Macek     MPC_LOADTIME_FLAG_UNLOADOK, NULL);
72