17bc82500SRobert Watson /*- 226ae2b86SRobert Watson * Copyright (c) 2002-2003 Networks Associates Technology, Inc. 330d239bcSRobert Watson * Copyright (c) 2006 SPARTA, Inc. 42087a58cSRobert Watson * Copyright (c) 2007, 2009 Robert N. M. Watson 57bc82500SRobert Watson * All rights reserved. 67bc82500SRobert Watson * 76201265bSRobert Watson * This software was developed for the FreeBSD Project in part by Network 86201265bSRobert Watson * Associates Laboratories, the Security Research Division of Network 96201265bSRobert Watson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 106201265bSRobert Watson * as part of the DARPA CHATS research program. 117bc82500SRobert Watson * 12c14d15aeSRobert Watson * Portions of this software were developed by Robert Watson for the 13c14d15aeSRobert Watson * TrustedBSD Project. 14c14d15aeSRobert Watson * 1530d239bcSRobert Watson * This software was enhanced by SPARTA ISSO under SPAWAR contract 1630d239bcSRobert Watson * N66001-04-C-6019 ("SEFOS"). 1730d239bcSRobert Watson * 182087a58cSRobert Watson * This software was developed at the University of Cambridge Computer 192087a58cSRobert Watson * Laboratory with support from a grant from Google, Inc. 202087a58cSRobert Watson * 217bc82500SRobert Watson * Redistribution and use in source and binary forms, with or without 227bc82500SRobert Watson * modification, are permitted provided that the following conditions 237bc82500SRobert Watson * are met: 247bc82500SRobert Watson * 1. Redistributions of source code must retain the above copyright 257bc82500SRobert Watson * notice, this list of conditions and the following disclaimer. 267bc82500SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright 277bc82500SRobert Watson * notice, this list of conditions and the following disclaimer in the 287bc82500SRobert Watson * documentation and/or other materials provided with the distribution. 297bc82500SRobert Watson * 307bc82500SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 317bc82500SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 327bc82500SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 337bc82500SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 347bc82500SRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 357bc82500SRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 367bc82500SRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 377bc82500SRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 387bc82500SRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 397bc82500SRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 407bc82500SRobert Watson * SUCH DAMAGE. 417bc82500SRobert Watson */ 42677b542eSDavid E. O'Brien 43c14d15aeSRobert Watson /* 44c14d15aeSRobert Watson * MAC Framework entry points relating to overall operation of system, 45c14d15aeSRobert Watson * including global services such as the kernel environment and loadable 46c14d15aeSRobert Watson * modules. 47c14d15aeSRobert Watson * 48c14d15aeSRobert Watson * System checks often align with existing privilege checks, but provide 49c14d15aeSRobert Watson * additional security context that may be relevant to policies, such as the 50c14d15aeSRobert Watson * specific object being operated on. 51c14d15aeSRobert Watson */ 52c14d15aeSRobert Watson 53677b542eSDavid E. O'Brien #include <sys/cdefs.h> 54677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 55677b542eSDavid E. O'Brien 562087a58cSRobert Watson #include "opt_kdtrace.h" 577bc82500SRobert Watson #include "opt_mac.h" 58f9d0d524SRobert Watson 597bc82500SRobert Watson #include <sys/param.h> 6095fab37eSRobert Watson #include <sys/kernel.h> 6195fab37eSRobert Watson #include <sys/lock.h> 62b656366bSBruce Evans #include <sys/malloc.h> 635dba30f1SPoul-Henning Kamp #include <sys/module.h> 6495fab37eSRobert Watson #include <sys/mutex.h> 652087a58cSRobert Watson #include <sys/sdt.h> 6695fab37eSRobert Watson #include <sys/systm.h> 6795fab37eSRobert Watson #include <sys/vnode.h> 6895fab37eSRobert Watson #include <sys/sysctl.h> 6995fab37eSRobert Watson 70aed55708SRobert Watson #include <security/mac/mac_framework.h> 716bd11732SRobert Watson #include <security/mac/mac_internal.h> 720efd6615SRobert Watson #include <security/mac/mac_policy.h> 7395fab37eSRobert Watson 742087a58cSRobert Watson MAC_CHECK_PROBE_DEFINE1(kenv_check_dump, "struct ucred *"); 752087a58cSRobert Watson 7695fab37eSRobert Watson int 7730d239bcSRobert Watson mac_kenv_check_dump(struct ucred *cred) 78e686e5aeSRobert Watson { 79e686e5aeSRobert Watson int error; 80e686e5aeSRobert Watson 81fa765671SRobert Watson MAC_POLICY_CHECK_NOSLEEP(kenv_check_dump, cred); 822087a58cSRobert Watson MAC_CHECK_PROBE1(kenv_check_dump, error, cred); 83e686e5aeSRobert Watson 84e686e5aeSRobert Watson return (error); 85e686e5aeSRobert Watson } 86e686e5aeSRobert Watson 872087a58cSRobert Watson MAC_CHECK_PROBE_DEFINE2(kenv_check_get, "struct ucred *", "char *"); 882087a58cSRobert Watson 89e686e5aeSRobert Watson int 9030d239bcSRobert Watson mac_kenv_check_get(struct ucred *cred, char *name) 91e686e5aeSRobert Watson { 92e686e5aeSRobert Watson int error; 93e686e5aeSRobert Watson 94fa765671SRobert Watson MAC_POLICY_CHECK_NOSLEEP(kenv_check_get, cred, name); 952087a58cSRobert Watson MAC_CHECK_PROBE2(kenv_check_get, error, cred, name); 96e686e5aeSRobert Watson 97e686e5aeSRobert Watson return (error); 98e686e5aeSRobert Watson } 99e686e5aeSRobert Watson 1002087a58cSRobert Watson MAC_CHECK_PROBE_DEFINE3(kenv_check_set, "struct ucred *", "char *", 1012087a58cSRobert Watson "char *"); 1022087a58cSRobert Watson 103e686e5aeSRobert Watson int 10430d239bcSRobert Watson mac_kenv_check_set(struct ucred *cred, char *name, char *value) 105e686e5aeSRobert Watson { 106e686e5aeSRobert Watson int error; 107e686e5aeSRobert Watson 108fa765671SRobert Watson MAC_POLICY_CHECK_NOSLEEP(kenv_check_set, cred, name, value); 1092087a58cSRobert Watson MAC_CHECK_PROBE3(kenv_check_set, error, cred, name, value); 110e686e5aeSRobert Watson 111e686e5aeSRobert Watson return (error); 112e686e5aeSRobert Watson } 113e686e5aeSRobert Watson 1142087a58cSRobert Watson MAC_CHECK_PROBE_DEFINE2(kenv_check_unset, "struct ucred *", "char *"); 1152087a58cSRobert Watson 116e686e5aeSRobert Watson int 11730d239bcSRobert Watson mac_kenv_check_unset(struct ucred *cred, char *name) 118e686e5aeSRobert Watson { 119e686e5aeSRobert Watson int error; 120e686e5aeSRobert Watson 121fa765671SRobert Watson MAC_POLICY_CHECK_NOSLEEP(kenv_check_unset, cred, name); 1222087a58cSRobert Watson MAC_CHECK_PROBE2(kenv_check_unset, error, cred, name); 123e686e5aeSRobert Watson 124e686e5aeSRobert Watson return (error); 125e686e5aeSRobert Watson } 126e686e5aeSRobert Watson 1272087a58cSRobert Watson MAC_CHECK_PROBE_DEFINE2(kld_check_load, "struct ucred *", "struct vnode *"); 1282087a58cSRobert Watson 129e686e5aeSRobert Watson int 13030d239bcSRobert Watson mac_kld_check_load(struct ucred *cred, struct vnode *vp) 131a3df768bSRobert Watson { 132a3df768bSRobert Watson int error; 133a3df768bSRobert Watson 13430d239bcSRobert Watson ASSERT_VOP_LOCKED(vp, "mac_kld_check_load"); 135a3df768bSRobert Watson 136fa765671SRobert Watson MAC_POLICY_CHECK(kld_check_load, cred, vp, vp->v_label); 1372087a58cSRobert Watson MAC_CHECK_PROBE2(kld_check_load, error, cred, vp); 138a3df768bSRobert Watson 139a3df768bSRobert Watson return (error); 140a3df768bSRobert Watson } 141a3df768bSRobert Watson 1422087a58cSRobert Watson MAC_CHECK_PROBE_DEFINE1(kld_check_stat, "struct ucred *"); 1432087a58cSRobert Watson 144a3df768bSRobert Watson int 14530d239bcSRobert Watson mac_kld_check_stat(struct ucred *cred) 146a3df768bSRobert Watson { 147a3df768bSRobert Watson int error; 148a3df768bSRobert Watson 149fa765671SRobert Watson MAC_POLICY_CHECK_NOSLEEP(kld_check_stat, cred); 1502087a58cSRobert Watson MAC_CHECK_PROBE1(kld_check_stat, error, cred); 151a3df768bSRobert Watson 152a3df768bSRobert Watson return (error); 153a3df768bSRobert Watson } 154a3df768bSRobert Watson 1552087a58cSRobert Watson MAC_CHECK_PROBE_DEFINE2(system_check_acct, "struct ucred *", 1562087a58cSRobert Watson "struct vnode *"); 1572087a58cSRobert Watson 158a3df768bSRobert Watson int 15930d239bcSRobert Watson mac_system_check_acct(struct ucred *cred, struct vnode *vp) 160e5e820fdSRobert Watson { 161e5e820fdSRobert Watson int error; 162e5e820fdSRobert Watson 163e5e820fdSRobert Watson if (vp != NULL) { 16430d239bcSRobert Watson ASSERT_VOP_LOCKED(vp, "mac_system_check_acct"); 165e5e820fdSRobert Watson } 166e5e820fdSRobert Watson 167fa765671SRobert Watson MAC_POLICY_CHECK(system_check_acct, cred, vp, 168eca8a663SRobert Watson vp != NULL ? vp->v_label : NULL); 1692087a58cSRobert Watson MAC_CHECK_PROBE2(system_check_acct, error, cred, vp); 170e5e820fdSRobert Watson 171e5e820fdSRobert Watson return (error); 172e5e820fdSRobert Watson } 173e5e820fdSRobert Watson 1742087a58cSRobert Watson MAC_CHECK_PROBE_DEFINE2(system_check_reboot, "struct ucred *", "int"); 1752087a58cSRobert Watson 176e5e820fdSRobert Watson int 17730d239bcSRobert Watson mac_system_check_reboot(struct ucred *cred, int howto) 178a2ecb9b7SRobert Watson { 179a2ecb9b7SRobert Watson int error; 180a2ecb9b7SRobert Watson 181fa765671SRobert Watson MAC_POLICY_CHECK_NOSLEEP(system_check_reboot, cred, howto); 1822087a58cSRobert Watson MAC_CHECK_PROBE2(system_check_reboot, error, cred, howto); 1839e913ebdSRobert Watson 184a2ecb9b7SRobert Watson return (error); 185a2ecb9b7SRobert Watson } 186a2ecb9b7SRobert Watson 1872087a58cSRobert Watson MAC_CHECK_PROBE_DEFINE2(system_check_swapon, "struct ucred *", 1882087a58cSRobert Watson "struct vnode *"); 1892087a58cSRobert Watson 190a2ecb9b7SRobert Watson int 19130d239bcSRobert Watson mac_system_check_swapon(struct ucred *cred, struct vnode *vp) 19203ce2c0cSRobert Watson { 19303ce2c0cSRobert Watson int error; 19403ce2c0cSRobert Watson 19530d239bcSRobert Watson ASSERT_VOP_LOCKED(vp, "mac_system_check_swapon"); 19603ce2c0cSRobert Watson 197fa765671SRobert Watson MAC_POLICY_CHECK(system_check_swapon, cred, vp, vp->v_label); 1982087a58cSRobert Watson MAC_CHECK_PROBE2(system_check_swapon, error, cred, vp); 1992087a58cSRobert Watson 20003ce2c0cSRobert Watson return (error); 20103ce2c0cSRobert Watson } 20203ce2c0cSRobert Watson 2032087a58cSRobert Watson MAC_CHECK_PROBE_DEFINE2(system_check_swapoff, "struct ucred *", 2042087a58cSRobert Watson "struct vnode *"); 2052087a58cSRobert Watson 20603ce2c0cSRobert Watson int 20730d239bcSRobert Watson mac_system_check_swapoff(struct ucred *cred, struct vnode *vp) 2081b2c2ab2SRobert Watson { 2091b2c2ab2SRobert Watson int error; 2101b2c2ab2SRobert Watson 21130d239bcSRobert Watson ASSERT_VOP_LOCKED(vp, "mac_system_check_swapoff"); 2121b2c2ab2SRobert Watson 213fa765671SRobert Watson MAC_POLICY_CHECK(system_check_swapoff, cred, vp, vp->v_label); 2142087a58cSRobert Watson MAC_CHECK_PROBE2(system_check_swapoff, error, cred, vp); 2152087a58cSRobert Watson 2161b2c2ab2SRobert Watson return (error); 2171b2c2ab2SRobert Watson } 2181b2c2ab2SRobert Watson 2192087a58cSRobert Watson MAC_CHECK_PROBE_DEFINE3(system_check_sysctl, "struct ucred *", 2202087a58cSRobert Watson "struct sysctl_oid *", "struct sysctl_req *"); 2212087a58cSRobert Watson 2221b2c2ab2SRobert Watson int 22330d239bcSRobert Watson mac_system_check_sysctl(struct ucred *cred, struct sysctl_oid *oidp, 224c14d15aeSRobert Watson void *arg1, int arg2, struct sysctl_req *req) 225d3fc69eeSRobert Watson { 226d3fc69eeSRobert Watson int error; 227d3fc69eeSRobert Watson 228d3fc69eeSRobert Watson /* 229578994bbSChristian S.J. Peron * XXXMAC: We would very much like to assert the SYSCTL_LOCK here, 230d3fc69eeSRobert Watson * but since it's not exported from kern_sysctl.c, we can't. 231d3fc69eeSRobert Watson */ 232fa765671SRobert Watson MAC_POLICY_CHECK_NOSLEEP(system_check_sysctl, cred, oidp, arg1, arg2, 233fa765671SRobert Watson req); 2342087a58cSRobert Watson MAC_CHECK_PROBE3(system_check_sysctl, error, cred, oidp, req); 235d3fc69eeSRobert Watson 236d3fc69eeSRobert Watson return (error); 237d3fc69eeSRobert Watson } 238