ugidfw_system.c revision 173138
1101099Srwatson/*- 2166905Srwatson * Copyright (c) 1999-2002, 2007 Robert N. M. Watson 3145412Strhodes * Copyright (c) 2001-2005 Networks Associates Technology, Inc. 4171253Srwatson * Copyright (c) 2005 Tom Rhodes 5172930Srwatson * Copyright (c) 2006 SPARTA, Inc. 6101099Srwatson * All rights reserved. 7101099Srwatson * 8101099Srwatson * This software was developed by Robert Watson for the TrustedBSD Project. 9145412Strhodes * It was later enhanced by Tom Rhodes for the TrustedBSD Project. 10101099Srwatson * 11106393Srwatson * This software was developed for the FreeBSD Project in part by Network 12106393Srwatson * Associates Laboratories, the Security Research Division of Network 13106393Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 14106393Srwatson * as part of the DARPA CHATS research program. 15101099Srwatson * 16172930Srwatson * This software was enhanced by SPARTA ISSO under SPAWAR contract 17172930Srwatson * N66001-04-C-6019 ("SEFOS"). 18172930Srwatson * 19101099Srwatson * Redistribution and use in source and binary forms, with or without 20101099Srwatson * modification, are permitted provided that the following conditions 21101099Srwatson * are met: 22101099Srwatson * 1. Redistributions of source code must retain the above copyright 23101099Srwatson * notice, this list of conditions and the following disclaimer. 24101099Srwatson * 2. Redistributions in binary form must reproduce the above copyright 25101099Srwatson * notice, this list of conditions and the following disclaimer in the 26101099Srwatson * documentation and/or other materials provided with the distribution. 27101099Srwatson * 28101099Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 29101099Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30101099Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31101099Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 32101099Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33101099Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34101099Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35101099Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36101099Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37101099Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38101099Srwatson * SUCH DAMAGE. 39101099Srwatson * 40101099Srwatson * $FreeBSD: head/sys/security/mac_bsdextended/mac_bsdextended.c 173138 2007-10-29 13:33:06Z rwatson $ 41101099Srwatson */ 42136774Srwatson 43101099Srwatson/* 44101099Srwatson * Developed by the TrustedBSD Project. 45171253Srwatson * 46171253Srwatson * "BSD Extended" MAC policy, allowing the administrator to impose mandatory 47171253Srwatson * firewall-like rules regarding users and file system objects. 48101099Srwatson */ 49101099Srwatson 50101099Srwatson#include <sys/param.h> 51101099Srwatson#include <sys/acl.h> 52101099Srwatson#include <sys/kernel.h> 53157986Sdwmalone#include <sys/jail.h> 54145412Strhodes#include <sys/lock.h> 55101099Srwatson#include <sys/malloc.h> 56166905Srwatson#include <sys/module.h> 57101099Srwatson#include <sys/mount.h> 58145412Strhodes#include <sys/mutex.h> 59170689Srwatson#include <sys/priv.h> 60101099Srwatson#include <sys/systm.h> 61101099Srwatson#include <sys/vnode.h> 62101099Srwatson#include <sys/sysctl.h> 63134132Strhodes#include <sys/syslog.h> 64101099Srwatson 65165469Srwatson#include <security/mac/mac_policy.h> 66101099Srwatson#include <security/mac_bsdextended/mac_bsdextended.h> 67101099Srwatson 68172955Srwatsonstatic struct mtx ugidfw_mtx; 69145412Strhodes 70101099SrwatsonSYSCTL_DECL(_security_mac); 71101099Srwatson 72101099SrwatsonSYSCTL_NODE(_security_mac, OID_AUTO, bsdextended, CTLFLAG_RW, 0, 73101099Srwatson "TrustedBSD extended BSD MAC policy controls"); 74101099Srwatson 75172955Srwatsonstatic int ugidfw_enabled = 1; 76101099SrwatsonSYSCTL_INT(_security_mac_bsdextended, OID_AUTO, enabled, CTLFLAG_RW, 77172955Srwatson &ugidfw_enabled, 0, "Enforce extended BSD policy"); 78172955SrwatsonTUNABLE_INT("security.mac.bsdextended.enabled", &ugidfw_enabled); 79101099Srwatson 80101099SrwatsonMALLOC_DEFINE(M_MACBSDEXTENDED, "mac_bsdextended", "BSD Extended MAC rule"); 81101099Srwatson 82101099Srwatson#define MAC_BSDEXTENDED_MAXRULES 250 83101099Srwatsonstatic struct mac_bsdextended_rule *rules[MAC_BSDEXTENDED_MAXRULES]; 84101099Srwatsonstatic int rule_count = 0; 85101099Srwatsonstatic int rule_slots = 0; 86157986Sdwmalonestatic int rule_version = MB_VERSION; 87101099Srwatson 88101099SrwatsonSYSCTL_INT(_security_mac_bsdextended, OID_AUTO, rule_count, CTLFLAG_RD, 89101099Srwatson &rule_count, 0, "Number of defined rules\n"); 90101099SrwatsonSYSCTL_INT(_security_mac_bsdextended, OID_AUTO, rule_slots, CTLFLAG_RD, 91101099Srwatson &rule_slots, 0, "Number of used rule slots\n"); 92157986SdwmaloneSYSCTL_INT(_security_mac_bsdextended, OID_AUTO, rule_version, CTLFLAG_RD, 93157986Sdwmalone &rule_version, 0, "Version number for API\n"); 94101099Srwatson 95134132Strhodes/* 96171253Srwatson * This is just used for logging purposes, eventually we would like to log 97171253Srwatson * much more then failed requests. 98134132Strhodes */ 99172955Srwatsonstatic int ugidfw_logging; 100134132StrhodesSYSCTL_INT(_security_mac_bsdextended, OID_AUTO, logging, CTLFLAG_RW, 101172955Srwatson &ugidfw_logging, 0, "Log failed authorization requests"); 102134132Strhodes 103134132Strhodes/* 104171253Srwatson * This tunable is here for compatibility. It will allow the user to switch 105171253Srwatson * between the new mode (first rule matches) and the old functionality (all 106171253Srwatson * rules match). 107134131Strhodes */ 108172955Srwatsonstatic int ugidfw_firstmatch_enabled; 109134131StrhodesSYSCTL_INT(_security_mac_bsdextended, OID_AUTO, firstmatch_enabled, 110172955Srwatson CTLFLAG_RW, &ugidfw_firstmatch_enabled, 1, 111171253Srwatson "Disable/enable match first rule functionality"); 112134131Strhodes 113134131Strhodesstatic int 114172955Srwatsonugidfw_rule_valid(struct mac_bsdextended_rule *rule) 115101099Srwatson{ 116101099Srwatson 117157986Sdwmalone if ((rule->mbr_subject.mbs_flags | MBS_ALL_FLAGS) != MBS_ALL_FLAGS) 118101099Srwatson return (EINVAL); 119157986Sdwmalone if ((rule->mbr_subject.mbs_neg | MBS_ALL_FLAGS) != MBS_ALL_FLAGS) 120101099Srwatson return (EINVAL); 121157986Sdwmalone if ((rule->mbr_object.mbo_flags | MBO_ALL_FLAGS) != MBO_ALL_FLAGS) 122157986Sdwmalone return (EINVAL); 123157986Sdwmalone if ((rule->mbr_object.mbo_neg | MBO_ALL_FLAGS) != MBO_ALL_FLAGS) 124157986Sdwmalone return (EINVAL); 125157986Sdwmalone if ((rule->mbr_object.mbo_neg | MBO_TYPE_DEFINED) && 126157986Sdwmalone (rule->mbr_object.mbo_type | MBO_ALL_TYPE) != MBO_ALL_TYPE) 127157986Sdwmalone return (EINVAL); 128136739Srwatson if ((rule->mbr_mode | MBI_ALLPERM) != MBI_ALLPERM) 129101099Srwatson return (EINVAL); 130101099Srwatson return (0); 131101099Srwatson} 132101099Srwatson 133101099Srwatsonstatic int 134101099Srwatsonsysctl_rule(SYSCTL_HANDLER_ARGS) 135101099Srwatson{ 136101099Srwatson struct mac_bsdextended_rule temprule, *ruleptr; 137101099Srwatson u_int namelen; 138101099Srwatson int error, index, *name; 139101099Srwatson 140145412Strhodes error = 0; 141101099Srwatson name = (int *)arg1; 142101099Srwatson namelen = arg2; 143101099Srwatson if (namelen != 1) 144101099Srwatson return (EINVAL); 145101099Srwatson index = name[0]; 146154386Scsjp if (index >= MAC_BSDEXTENDED_MAXRULES) 147101099Srwatson return (ENOENT); 148101099Srwatson 149145412Strhodes ruleptr = NULL; 150145412Strhodes if (req->newptr && req->newlen != 0) { 151145412Strhodes error = SYSCTL_IN(req, &temprule, sizeof(temprule)); 152101099Srwatson if (error) 153101099Srwatson return (error); 154145412Strhodes MALLOC(ruleptr, struct mac_bsdextended_rule *, 155145412Strhodes sizeof(*ruleptr), M_MACBSDEXTENDED, M_WAITOK | M_ZERO); 156101099Srwatson } 157101099Srwatson 158172955Srwatson mtx_lock(&ugidfw_mtx); 159145412Strhodes if (req->oldptr) { 160145412Strhodes if (index < 0 || index > rule_slots + 1) { 161145412Strhodes error = ENOENT; 162145412Strhodes goto out; 163101099Srwatson } 164145412Strhodes if (rules[index] == NULL) { 165145412Strhodes error = ENOENT; 166145412Strhodes goto out; 167145412Strhodes } 168145412Strhodes temprule = *rules[index]; 169145412Strhodes } 170145412Strhodes if (req->newptr && req->newlen == 0) { 171145412Strhodes KASSERT(ruleptr == NULL, ("sysctl_rule: ruleptr != NULL")); 172145412Strhodes ruleptr = rules[index]; 173145412Strhodes if (ruleptr == NULL) { 174145412Strhodes error = ENOENT; 175145412Strhodes goto out; 176145412Strhodes } 177145412Strhodes rule_count--; 178145412Strhodes rules[index] = NULL; 179145412Strhodes } else if (req->newptr) { 180172955Srwatson error = ugidfw_rule_valid(&temprule); 181101099Srwatson if (error) 182145412Strhodes goto out; 183101099Srwatson if (rules[index] == NULL) { 184101099Srwatson *ruleptr = temprule; 185101099Srwatson rules[index] = ruleptr; 186145412Strhodes ruleptr = NULL; 187145412Strhodes if (index + 1 > rule_slots) 188145412Strhodes rule_slots = index + 1; 189101099Srwatson rule_count++; 190171253Srwatson } else 191101099Srwatson *rules[index] = temprule; 192101099Srwatson } 193145412Strhodesout: 194172955Srwatson mtx_unlock(&ugidfw_mtx); 195145412Strhodes if (ruleptr != NULL) 196145412Strhodes FREE(ruleptr, M_MACBSDEXTENDED); 197148482Strhodes if (req->oldptr && error == 0) 198145412Strhodes error = SYSCTL_OUT(req, &temprule, sizeof(temprule)); 199148482Strhodes return (error); 200101099Srwatson} 201101099Srwatson 202171253SrwatsonSYSCTL_NODE(_security_mac_bsdextended, OID_AUTO, rules, CTLFLAG_RW, 203171253Srwatson sysctl_rule, "BSD extended MAC rules"); 204101099Srwatson 205101099Srwatsonstatic void 206172955Srwatsonugidfw_init(struct mac_policy_conf *mpc) 207101099Srwatson{ 208101099Srwatson 209172955Srwatson mtx_init(&ugidfw_mtx, "mac_bsdextended lock", NULL, MTX_DEF); 210101099Srwatson} 211101099Srwatson 212101099Srwatsonstatic void 213172955Srwatsonugidfw_destroy(struct mac_policy_conf *mpc) 214101099Srwatson{ 215101099Srwatson 216172955Srwatson mtx_destroy(&ugidfw_mtx); 217101099Srwatson} 218101099Srwatson 219101099Srwatsonstatic int 220172955Srwatsonugidfw_rulecheck(struct mac_bsdextended_rule *rule, 221157986Sdwmalone struct ucred *cred, struct vnode *vp, struct vattr *vap, int acc_mode) 222101099Srwatson{ 223101099Srwatson int match; 224157986Sdwmalone int i; 225101099Srwatson 226101099Srwatson /* 227101099Srwatson * Is there a subject match? 228101099Srwatson */ 229172955Srwatson mtx_assert(&ugidfw_mtx, MA_OWNED); 230157986Sdwmalone if (rule->mbr_subject.mbs_flags & MBS_UID_DEFINED) { 231157986Sdwmalone match = ((cred->cr_uid <= rule->mbr_subject.mbs_uid_max && 232157986Sdwmalone cred->cr_uid >= rule->mbr_subject.mbs_uid_min) || 233157986Sdwmalone (cred->cr_ruid <= rule->mbr_subject.mbs_uid_max && 234157986Sdwmalone cred->cr_ruid >= rule->mbr_subject.mbs_uid_min) || 235157986Sdwmalone (cred->cr_svuid <= rule->mbr_subject.mbs_uid_max && 236157986Sdwmalone cred->cr_svuid >= rule->mbr_subject.mbs_uid_min)); 237157986Sdwmalone if (rule->mbr_subject.mbs_neg & MBS_UID_DEFINED) 238101099Srwatson match = !match; 239101099Srwatson if (!match) 240101099Srwatson return (0); 241101099Srwatson } 242101099Srwatson 243157986Sdwmalone if (rule->mbr_subject.mbs_flags & MBS_GID_DEFINED) { 244157986Sdwmalone match = ((cred->cr_rgid <= rule->mbr_subject.mbs_gid_max && 245157986Sdwmalone cred->cr_rgid >= rule->mbr_subject.mbs_gid_min) || 246157986Sdwmalone (cred->cr_svgid <= rule->mbr_subject.mbs_gid_max && 247157986Sdwmalone cred->cr_svgid >= rule->mbr_subject.mbs_gid_min)); 248157986Sdwmalone if (!match) { 249171253Srwatson for (i = 0; i < cred->cr_ngroups; i++) { 250157986Sdwmalone if (cred->cr_groups[i] 251157986Sdwmalone <= rule->mbr_subject.mbs_gid_max && 252157986Sdwmalone cred->cr_groups[i] 253157986Sdwmalone >= rule->mbr_subject.mbs_gid_min) { 254157986Sdwmalone match = 1; 255157986Sdwmalone break; 256157986Sdwmalone } 257171253Srwatson } 258157986Sdwmalone } 259157986Sdwmalone if (rule->mbr_subject.mbs_neg & MBS_GID_DEFINED) 260101099Srwatson match = !match; 261101099Srwatson if (!match) 262101099Srwatson return (0); 263101099Srwatson } 264101099Srwatson 265157986Sdwmalone if (rule->mbr_subject.mbs_flags & MBS_PRISON_DEFINED) { 266157986Sdwmalone match = (cred->cr_prison != NULL && 267157986Sdwmalone cred->cr_prison->pr_id == rule->mbr_subject.mbs_prison); 268157986Sdwmalone if (rule->mbr_subject.mbs_neg & MBS_PRISON_DEFINED) 269157986Sdwmalone match = !match; 270157986Sdwmalone if (!match) 271157986Sdwmalone return (0); 272157986Sdwmalone } 273157986Sdwmalone 274101099Srwatson /* 275101099Srwatson * Is there an object match? 276101099Srwatson */ 277157986Sdwmalone if (rule->mbr_object.mbo_flags & MBO_UID_DEFINED) { 278157986Sdwmalone match = (vap->va_uid <= rule->mbr_object.mbo_uid_max && 279157986Sdwmalone vap->va_uid >= rule->mbr_object.mbo_uid_min); 280157986Sdwmalone if (rule->mbr_object.mbo_neg & MBO_UID_DEFINED) 281101099Srwatson match = !match; 282101099Srwatson if (!match) 283101099Srwatson return (0); 284101099Srwatson } 285101099Srwatson 286157986Sdwmalone if (rule->mbr_object.mbo_flags & MBO_GID_DEFINED) { 287157986Sdwmalone match = (vap->va_gid <= rule->mbr_object.mbo_gid_max && 288157986Sdwmalone vap->va_gid >= rule->mbr_object.mbo_gid_min); 289157986Sdwmalone if (rule->mbr_object.mbo_neg & MBO_GID_DEFINED) 290101099Srwatson match = !match; 291101099Srwatson if (!match) 292101099Srwatson return (0); 293101099Srwatson } 294101099Srwatson 295157986Sdwmalone if (rule->mbr_object.mbo_flags & MBO_FSID_DEFINED) { 296157986Sdwmalone match = (bcmp(&(vp->v_mount->mnt_stat.f_fsid), 297157986Sdwmalone &(rule->mbr_object.mbo_fsid), 298157986Sdwmalone sizeof(rule->mbr_object.mbo_fsid)) == 0); 299157986Sdwmalone if (rule->mbr_object.mbo_neg & MBO_FSID_DEFINED) 300157986Sdwmalone match = !match; 301157986Sdwmalone if (!match) 302171253Srwatson return (0); 303157986Sdwmalone } 304157986Sdwmalone 305157986Sdwmalone if (rule->mbr_object.mbo_flags & MBO_SUID) { 306157986Sdwmalone match = (vap->va_mode & VSUID); 307157986Sdwmalone if (rule->mbr_object.mbo_neg & MBO_SUID) 308157986Sdwmalone match = !match; 309157986Sdwmalone if (!match) 310171253Srwatson return (0); 311157986Sdwmalone } 312157986Sdwmalone 313157986Sdwmalone if (rule->mbr_object.mbo_flags & MBO_SGID) { 314157986Sdwmalone match = (vap->va_mode & VSGID); 315157986Sdwmalone if (rule->mbr_object.mbo_neg & MBO_SGID) 316157986Sdwmalone match = !match; 317157986Sdwmalone if (!match) 318171253Srwatson return (0); 319157986Sdwmalone } 320157986Sdwmalone 321157986Sdwmalone if (rule->mbr_object.mbo_flags & MBO_UID_SUBJECT) { 322157986Sdwmalone match = (vap->va_uid == cred->cr_uid || 323157986Sdwmalone vap->va_uid == cred->cr_ruid || 324157986Sdwmalone vap->va_uid == cred->cr_svuid); 325157986Sdwmalone if (rule->mbr_object.mbo_neg & MBO_UID_SUBJECT) 326157986Sdwmalone match = !match; 327157986Sdwmalone if (!match) 328171253Srwatson return (0); 329157986Sdwmalone } 330157986Sdwmalone 331157986Sdwmalone if (rule->mbr_object.mbo_flags & MBO_GID_SUBJECT) { 332157986Sdwmalone match = (groupmember(vap->va_gid, cred) || 333157986Sdwmalone vap->va_gid == cred->cr_rgid || 334157986Sdwmalone vap->va_gid == cred->cr_svgid); 335157986Sdwmalone if (rule->mbr_object.mbo_neg & MBO_GID_SUBJECT) 336157986Sdwmalone match = !match; 337157986Sdwmalone if (!match) 338171253Srwatson return (0); 339157986Sdwmalone } 340157986Sdwmalone 341157986Sdwmalone if (rule->mbr_object.mbo_flags & MBO_TYPE_DEFINED) { 342157986Sdwmalone switch (vap->va_type) { 343157986Sdwmalone case VREG: 344157986Sdwmalone match = (rule->mbr_object.mbo_type & MBO_TYPE_REG); 345157986Sdwmalone break; 346157986Sdwmalone case VDIR: 347157986Sdwmalone match = (rule->mbr_object.mbo_type & MBO_TYPE_DIR); 348157986Sdwmalone break; 349157986Sdwmalone case VBLK: 350157986Sdwmalone match = (rule->mbr_object.mbo_type & MBO_TYPE_BLK); 351157986Sdwmalone break; 352157986Sdwmalone case VCHR: 353157986Sdwmalone match = (rule->mbr_object.mbo_type & MBO_TYPE_CHR); 354157986Sdwmalone break; 355157986Sdwmalone case VLNK: 356157986Sdwmalone match = (rule->mbr_object.mbo_type & MBO_TYPE_LNK); 357157986Sdwmalone break; 358157986Sdwmalone case VSOCK: 359157986Sdwmalone match = (rule->mbr_object.mbo_type & MBO_TYPE_SOCK); 360157986Sdwmalone break; 361157986Sdwmalone case VFIFO: 362157986Sdwmalone match = (rule->mbr_object.mbo_type & MBO_TYPE_FIFO); 363157986Sdwmalone break; 364157986Sdwmalone default: 365157986Sdwmalone match = 0; 366157986Sdwmalone } 367157986Sdwmalone if (rule->mbr_object.mbo_neg & MBO_TYPE_DEFINED) 368157986Sdwmalone match = !match; 369157986Sdwmalone if (!match) 370171253Srwatson return (0); 371157986Sdwmalone } 372157986Sdwmalone 373101099Srwatson /* 374101099Srwatson * Is the access permitted? 375101099Srwatson */ 376101099Srwatson if ((rule->mbr_mode & acc_mode) != acc_mode) { 377172955Srwatson if (ugidfw_logging) 378134132Strhodes log(LOG_AUTHPRIV, "mac_bsdextended: %d:%d request %d" 379134132Strhodes " on %d:%d failed. \n", cred->cr_ruid, 380171253Srwatson cred->cr_rgid, acc_mode, vap->va_uid, 381171253Srwatson vap->va_gid); 382171253Srwatson return (EACCES); 383101099Srwatson } 384145412Strhodes 385134131Strhodes /* 386145412Strhodes * If the rule matched, permits access, and first match is enabled, 387145412Strhodes * return success. 388134131Strhodes */ 389172955Srwatson if (ugidfw_firstmatch_enabled) 390134131Strhodes return (EJUSTRETURN); 391134131Strhodes else 392171253Srwatson return (0); 393101099Srwatson} 394101099Srwatson 395101099Srwatsonstatic int 396172955Srwatsonugidfw_check(struct ucred *cred, struct vnode *vp, struct vattr *vap, 397106212Srwatson int acc_mode) 398101099Srwatson{ 399101099Srwatson int error, i; 400101099Srwatson 401164033Srwatson /* 402171253Srwatson * XXXRW: More specific privilege selection needed. 403164033Srwatson */ 404132563Srwatson if (suser_cred(cred, 0) == 0) 405132563Srwatson return (0); 406132563Srwatson 407166843Srwatson /* 408166843Srwatson * Since we do not separately handle append, map append to write. 409166843Srwatson */ 410166843Srwatson if (acc_mode & MBI_APPEND) { 411166843Srwatson acc_mode &= ~MBI_APPEND; 412166843Srwatson acc_mode |= MBI_WRITE; 413166843Srwatson } 414172955Srwatson mtx_lock(&ugidfw_mtx); 415101099Srwatson for (i = 0; i < rule_slots; i++) { 416101099Srwatson if (rules[i] == NULL) 417101099Srwatson continue; 418172955Srwatson error = ugidfw_rulecheck(rules[i], cred, 419157986Sdwmalone vp, vap, acc_mode); 420134131Strhodes if (error == EJUSTRETURN) 421134131Strhodes break; 422145412Strhodes if (error) { 423172955Srwatson mtx_unlock(&ugidfw_mtx); 424101099Srwatson return (error); 425145412Strhodes } 426101099Srwatson } 427172955Srwatson mtx_unlock(&ugidfw_mtx); 428101099Srwatson return (0); 429101099Srwatson} 430101099Srwatson 431101099Srwatsonstatic int 432172955Srwatsonugidfw_check_vp(struct ucred *cred, struct vnode *vp, int acc_mode) 433112575Srwatson{ 434156300Sdwmalone int error; 435112575Srwatson struct vattr vap; 436112575Srwatson 437172955Srwatson if (!ugidfw_enabled) 438112575Srwatson return (0); 439112575Srwatson error = VOP_GETATTR(vp, &vap, cred, curthread); 440112575Srwatson if (error) 441112575Srwatson return (error); 442172955Srwatson return (ugidfw_check(cred, vp, &vap, acc_mode)); 443112575Srwatson} 444112575Srwatson 445173138Srwatson/* 446173138Srwatson * Object-specific entry point implementations are sorted alphabetically by 447173138Srwatson * object type and then by operation. 448173138Srwatson */ 449112575Srwatsonstatic int 450172955Srwatsonugidfw_system_check_acct(struct ucred *cred, struct vnode *vp, 451168976Srwatson struct label *vplabel) 452168933Srwatson{ 453168933Srwatson 454172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_WRITE)); 455168933Srwatson} 456168933Srwatson 457168933Srwatsonstatic int 458172955Srwatsonugidfw_system_check_auditctl(struct ucred *cred, struct vnode *vp, 459168976Srwatson struct label *vplabel) 460168933Srwatson{ 461168933Srwatson 462172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_WRITE)); 463168933Srwatson} 464168933Srwatson 465168933Srwatsonstatic int 466172955Srwatsonugidfw_system_check_swapoff(struct ucred *cred, struct vnode *vp, 467168976Srwatson struct label *vplabel) 468168933Srwatson{ 469168933Srwatson 470172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_WRITE)); 471168933Srwatson} 472168933Srwatson 473168933Srwatsonstatic int 474172955Srwatsonugidfw_system_check_swapon(struct ucred *cred, struct vnode *vp, 475168976Srwatson struct label *vplabel) 476156300Sdwmalone{ 477156300Sdwmalone 478172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_WRITE)); 479156300Sdwmalone} 480156300Sdwmalone 481156300Sdwmalonestatic int 482172955Srwatsonugidfw_vnode_check_access(struct ucred *cred, struct vnode *vp, 483168976Srwatson struct label *vplabel, int acc_mode) 484101099Srwatson{ 485101099Srwatson 486172955Srwatson return (ugidfw_check_vp(cred, vp, acc_mode)); 487101099Srwatson} 488101099Srwatson 489101099Srwatsonstatic int 490172955Srwatsonugidfw_vnode_check_chdir(struct ucred *cred, struct vnode *dvp, 491168976Srwatson struct label *dvplabel) 492101099Srwatson{ 493101099Srwatson 494172955Srwatson return (ugidfw_check_vp(cred, dvp, MBI_EXEC)); 495101099Srwatson} 496101099Srwatson 497101099Srwatsonstatic int 498172955Srwatsonugidfw_vnode_check_chroot(struct ucred *cred, struct vnode *dvp, 499168976Srwatson struct label *dvplabel) 500101099Srwatson{ 501101099Srwatson 502172955Srwatson return (ugidfw_check_vp(cred, dvp, MBI_EXEC)); 503101099Srwatson} 504101099Srwatson 505101099Srwatsonstatic int 506172955Srwatsonugidfw_check_create_vnode(struct ucred *cred, struct vnode *dvp, 507168976Srwatson struct label *dvplabel, struct componentname *cnp, struct vattr *vap) 508101099Srwatson{ 509101099Srwatson 510172955Srwatson return (ugidfw_check_vp(cred, dvp, MBI_WRITE)); 511101099Srwatson} 512101099Srwatson 513101099Srwatsonstatic int 514172955Srwatsonugidfw_vnode_check_deleteacl(struct ucred *cred, struct vnode *vp, 515168976Srwatson struct label *vplabel, acl_type_t type) 516101099Srwatson{ 517101099Srwatson 518172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_ADMIN)); 519101099Srwatson} 520101099Srwatson 521101099Srwatsonstatic int 522172955Srwatsonugidfw_vnode_check_deleteextattr(struct ucred *cred, struct vnode *vp, 523172955Srwatson struct label *vplabel, int attrnamespace, const char *name) 524119202Srwatson{ 525119202Srwatson 526172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_WRITE)); 527119202Srwatson} 528119202Srwatson 529119202Srwatsonstatic int 530172955Srwatsonugidfw_vnode_check_exec(struct ucred *cred, struct vnode *vp, 531168976Srwatson struct label *vplabel, struct image_params *imgp, 532106648Srwatson struct label *execlabel) 533101099Srwatson{ 534101099Srwatson 535172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_READ|MBI_EXEC)); 536101099Srwatson} 537101099Srwatson 538101099Srwatsonstatic int 539172955Srwatsonugidfw_vnode_check_getacl(struct ucred *cred, struct vnode *vp, 540168976Srwatson struct label *vplabel, acl_type_t type) 541101099Srwatson{ 542101099Srwatson 543172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_STAT)); 544101099Srwatson} 545101099Srwatson 546101099Srwatsonstatic int 547172955Srwatsonugidfw_vnode_check_getextattr(struct ucred *cred, struct vnode *vp, 548168976Srwatson struct label *vplabel, int attrnamespace, const char *name, 549168976Srwatson struct uio *uio) 550101099Srwatson{ 551101099Srwatson 552172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_READ)); 553101099Srwatson} 554101099Srwatson 555101099Srwatsonstatic int 556172955Srwatsonugidfw_vnode_check_link(struct ucred *cred, struct vnode *dvp, 557168976Srwatson struct label *dvplabel, struct vnode *vp, struct label *label, 558104530Srwatson struct componentname *cnp) 559104530Srwatson{ 560104530Srwatson int error; 561104530Srwatson 562172955Srwatson error = ugidfw_check_vp(cred, dvp, MBI_WRITE); 563104530Srwatson if (error) 564104530Srwatson return (error); 565172955Srwatson error = ugidfw_check_vp(cred, vp, MBI_WRITE); 566104530Srwatson if (error) 567104530Srwatson return (error); 568104530Srwatson return (0); 569104530Srwatson} 570104530Srwatson 571104530Srwatsonstatic int 572172955Srwatsonugidfw_vnode_check_listextattr(struct ucred *cred, struct vnode *vp, 573168976Srwatson struct label *vplabel, int attrnamespace) 574119202Srwatson{ 575119202Srwatson 576172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_READ)); 577119202Srwatson} 578119202Srwatson 579119202Srwatsonstatic int 580172955Srwatsonugidfw_vnode_check_lookup(struct ucred *cred, struct vnode *dvp, 581168976Srwatson struct label *dvplabel, struct componentname *cnp) 582101099Srwatson{ 583117247Srwatson 584172955Srwatson return (ugidfw_check_vp(cred, dvp, MBI_EXEC)); 585101099Srwatson} 586101099Srwatson 587101099Srwatsonstatic int 588172955Srwatsonugidfw_vnode_check_open(struct ucred *cred, struct vnode *vp, 589168976Srwatson struct label *vplabel, int acc_mode) 590101099Srwatson{ 591101099Srwatson 592172955Srwatson return (ugidfw_check_vp(cred, vp, acc_mode)); 593101099Srwatson} 594101099Srwatson 595101099Srwatsonstatic int 596172955Srwatsonugidfw_vnode_check_readdir(struct ucred *cred, struct vnode *dvp, 597168976Srwatson struct label *dvplabel) 598101099Srwatson{ 599101099Srwatson 600172955Srwatson return (ugidfw_check_vp(cred, dvp, MBI_READ)); 601101099Srwatson} 602101099Srwatson 603101099Srwatsonstatic int 604172955Srwatsonugidfw_vnode_check_readdlink(struct ucred *cred, struct vnode *vp, 605168976Srwatson struct label *vplabel) 606101099Srwatson{ 607101099Srwatson 608172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_READ)); 609101099Srwatson} 610101099Srwatson 611101099Srwatsonstatic int 612172955Srwatsonugidfw_vnode_check_rename_from(struct ucred *cred, struct vnode *dvp, 613168976Srwatson struct label *dvplabel, struct vnode *vp, struct label *vplabel, 614101099Srwatson struct componentname *cnp) 615101099Srwatson{ 616101099Srwatson int error; 617101099Srwatson 618172955Srwatson error = ugidfw_check_vp(cred, dvp, MBI_WRITE); 619101099Srwatson if (error) 620101099Srwatson return (error); 621172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_WRITE)); 622101099Srwatson} 623101099Srwatson 624101099Srwatsonstatic int 625172955Srwatsonugidfw_vnode_check_rename_to(struct ucred *cred, struct vnode *dvp, 626168976Srwatson struct label *dvplabel, struct vnode *vp, struct label *vplabel, 627168976Srwatson int samedir, struct componentname *cnp) 628101099Srwatson{ 629101099Srwatson int error; 630101099Srwatson 631172955Srwatson error = ugidfw_check_vp(cred, dvp, MBI_WRITE); 632101099Srwatson if (error) 633101099Srwatson return (error); 634156300Sdwmalone if (vp != NULL) 635172955Srwatson error = ugidfw_check_vp(cred, vp, MBI_WRITE); 636101099Srwatson return (error); 637101099Srwatson} 638101099Srwatson 639101099Srwatsonstatic int 640172955Srwatsonugidfw_vnode_check_revoke(struct ucred *cred, struct vnode *vp, 641168976Srwatson struct label *vplabel) 642101099Srwatson{ 643101099Srwatson 644172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_ADMIN)); 645101099Srwatson} 646101099Srwatson 647101099Srwatsonstatic int 648172955Srwatsonugidfw_check_setacl_vnode(struct ucred *cred, struct vnode *vp, 649168976Srwatson struct label *vplabel, acl_type_t type, struct acl *acl) 650101099Srwatson{ 651101099Srwatson 652172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_ADMIN)); 653101099Srwatson} 654101099Srwatson 655101099Srwatsonstatic int 656172955Srwatsonugidfw_vnode_check_setextattr(struct ucred *cred, struct vnode *vp, 657168976Srwatson struct label *vplabel, int attrnamespace, const char *name, 658168976Srwatson struct uio *uio) 659101099Srwatson{ 660101099Srwatson 661172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_WRITE)); 662101099Srwatson} 663101099Srwatson 664101099Srwatsonstatic int 665172955Srwatsonugidfw_vnode_check_setflags(struct ucred *cred, struct vnode *vp, 666168976Srwatson struct label *vplabel, u_long flags) 667101099Srwatson{ 668101099Srwatson 669172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_ADMIN)); 670101099Srwatson} 671101099Srwatson 672101099Srwatsonstatic int 673172955Srwatsonugidfw_vnode_check_setmode(struct ucred *cred, struct vnode *vp, 674168976Srwatson struct label *vplabel, mode_t mode) 675101099Srwatson{ 676101099Srwatson 677172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_ADMIN)); 678101099Srwatson} 679101099Srwatson 680101099Srwatsonstatic int 681172955Srwatsonugidfw_vnode_check_setowner(struct ucred *cred, struct vnode *vp, 682168976Srwatson struct label *vplabel, uid_t uid, gid_t gid) 683101099Srwatson{ 684101099Srwatson 685172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_ADMIN)); 686101099Srwatson} 687101099Srwatson 688101099Srwatsonstatic int 689172955Srwatsonugidfw_vnode_check_setutimes(struct ucred *cred, struct vnode *vp, 690168976Srwatson struct label *vplabel, struct timespec atime, struct timespec utime) 691101099Srwatson{ 692101099Srwatson 693172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_ADMIN)); 694101099Srwatson} 695101099Srwatson 696101099Srwatsonstatic int 697172955Srwatsonugidfw_vnode_check_stat(struct ucred *active_cred, 698168976Srwatson struct ucred *file_cred, struct vnode *vp, struct label *vplabel) 699101099Srwatson{ 700101099Srwatson 701172955Srwatson return (ugidfw_check_vp(active_cred, vp, MBI_STAT)); 702101099Srwatson} 703101099Srwatson 704172107Srwatsonstatic int 705172955Srwatsonugidfw_vnode_check_unlink(struct ucred *cred, struct vnode *dvp, 706172107Srwatson struct label *dvplabel, struct vnode *vp, struct label *vplabel, 707172107Srwatson struct componentname *cnp) 708172107Srwatson{ 709172107Srwatson int error; 710172107Srwatson 711172955Srwatson error = ugidfw_check_vp(cred, dvp, MBI_WRITE); 712172107Srwatson if (error) 713172107Srwatson return (error); 714172955Srwatson return (ugidfw_check_vp(cred, vp, MBI_WRITE)); 715172107Srwatson} 716172107Srwatson 717172955Srwatsonstatic struct mac_policy_ops ugidfw_ops = 718101099Srwatson{ 719172955Srwatson .mpo_destroy = ugidfw_destroy, 720172955Srwatson .mpo_init = ugidfw_init, 721172955Srwatson .mpo_system_check_acct = ugidfw_system_check_acct, 722172955Srwatson .mpo_system_check_auditctl = ugidfw_system_check_auditctl, 723172955Srwatson .mpo_system_check_swapoff = ugidfw_system_check_swapoff, 724172955Srwatson .mpo_system_check_swapon = ugidfw_system_check_swapon, 725172955Srwatson .mpo_vnode_check_access = ugidfw_vnode_check_access, 726172955Srwatson .mpo_vnode_check_chdir = ugidfw_vnode_check_chdir, 727172955Srwatson .mpo_vnode_check_chroot = ugidfw_vnode_check_chroot, 728172955Srwatson .mpo_vnode_check_create = ugidfw_check_create_vnode, 729172955Srwatson .mpo_vnode_check_deleteacl = ugidfw_vnode_check_deleteacl, 730172955Srwatson .mpo_vnode_check_deleteextattr = ugidfw_vnode_check_deleteextattr, 731172955Srwatson .mpo_vnode_check_exec = ugidfw_vnode_check_exec, 732172955Srwatson .mpo_vnode_check_getacl = ugidfw_vnode_check_getacl, 733172955Srwatson .mpo_vnode_check_getextattr = ugidfw_vnode_check_getextattr, 734172955Srwatson .mpo_vnode_check_link = ugidfw_vnode_check_link, 735172955Srwatson .mpo_vnode_check_listextattr = ugidfw_vnode_check_listextattr, 736172955Srwatson .mpo_vnode_check_lookup = ugidfw_vnode_check_lookup, 737172955Srwatson .mpo_vnode_check_open = ugidfw_vnode_check_open, 738172955Srwatson .mpo_vnode_check_readdir = ugidfw_vnode_check_readdir, 739172955Srwatson .mpo_vnode_check_readlink = ugidfw_vnode_check_readdlink, 740172955Srwatson .mpo_vnode_check_rename_from = ugidfw_vnode_check_rename_from, 741172955Srwatson .mpo_vnode_check_rename_to = ugidfw_vnode_check_rename_to, 742172955Srwatson .mpo_vnode_check_revoke = ugidfw_vnode_check_revoke, 743172955Srwatson .mpo_vnode_check_setacl = ugidfw_check_setacl_vnode, 744172955Srwatson .mpo_vnode_check_setextattr = ugidfw_vnode_check_setextattr, 745172955Srwatson .mpo_vnode_check_setflags = ugidfw_vnode_check_setflags, 746172955Srwatson .mpo_vnode_check_setmode = ugidfw_vnode_check_setmode, 747172955Srwatson .mpo_vnode_check_setowner = ugidfw_vnode_check_setowner, 748172955Srwatson .mpo_vnode_check_setutimes = ugidfw_vnode_check_setutimes, 749172955Srwatson .mpo_vnode_check_stat = ugidfw_vnode_check_stat, 750172955Srwatson .mpo_vnode_check_unlink = ugidfw_vnode_check_unlink, 751101099Srwatson}; 752101099Srwatson 753172955SrwatsonMAC_POLICY_SET(&ugidfw_ops, mac_bsdextended, "TrustedBSD MAC/BSD Extended", 754172955Srwatson MPC_LOADTIME_FLAG_UNLOADOK, NULL); 755