1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 20ed3b28aSJohn Johansen /* 30ed3b28aSJohn Johansen * AppArmor security module 40ed3b28aSJohn Johansen * 50ed3b28aSJohn Johansen * This file contains AppArmor capability mediation functions 60ed3b28aSJohn Johansen * 70ed3b28aSJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 80ed3b28aSJohn Johansen * Copyright 2009-2010 Canonical Ltd. 90ed3b28aSJohn Johansen */ 100ed3b28aSJohn Johansen 110ed3b28aSJohn Johansen #include <linux/capability.h> 120ed3b28aSJohn Johansen #include <linux/errno.h> 130ed3b28aSJohn Johansen #include <linux/gfp.h> 1412eb87d5SJohn Johansen #include <linux/security.h> 150ed3b28aSJohn Johansen 160ed3b28aSJohn Johansen #include "include/apparmor.h" 170ed3b28aSJohn Johansen #include "include/capability.h" 18d8889d49SJohn Johansen #include "include/cred.h" 190ed3b28aSJohn Johansen #include "include/policy.h" 200ed3b28aSJohn Johansen #include "include/audit.h" 210ed3b28aSJohn Johansen 220ed3b28aSJohn Johansen /* 230ed3b28aSJohn Johansen * Table of capability names: we generate it from capabilities.h. 240ed3b28aSJohn Johansen */ 250ed3b28aSJohn Johansen #include "capability_names.h" 260ed3b28aSJohn Johansen 27c97204baSJohn Johansen struct aa_sfs_entry aa_sfs_entry_caps[] = { 28c97204baSJohn Johansen AA_SFS_FILE_STRING("mask", AA_SFS_CAPS_MASK), 2984f1f787SJohn Johansen { } 3084f1f787SJohn Johansen }; 3184f1f787SJohn Johansen 320ed3b28aSJohn Johansen struct audit_cache { 330ed3b28aSJohn Johansen struct aa_profile *profile; 340ed3b28aSJohn Johansen kernel_cap_t caps; 350ed3b28aSJohn Johansen }; 360ed3b28aSJohn Johansen 370ed3b28aSJohn Johansen static DEFINE_PER_CPU(struct audit_cache, audit_cache); 380ed3b28aSJohn Johansen 390ed3b28aSJohn Johansen /** 400ed3b28aSJohn Johansen * audit_cb - call back for capability components of audit struct 41c98c8972SGaosheng Cui * @ab: audit buffer (NOT NULL) 42c98c8972SGaosheng Cui * @va: audit struct to audit data from (NOT NULL) 430ed3b28aSJohn Johansen */ 440ed3b28aSJohn Johansen static void audit_cb(struct audit_buffer *ab, void *va) 450ed3b28aSJohn Johansen { 460ed3b28aSJohn Johansen struct common_audit_data *sa = va; 47c70c86c4SJohn Johansen 480ed3b28aSJohn Johansen audit_log_format(ab, " capname="); 490ed3b28aSJohn Johansen audit_log_untrustedstring(ab, capability_names[sa->u.cap]); 500ed3b28aSJohn Johansen } 510ed3b28aSJohn Johansen 520ed3b28aSJohn Johansen /** 530ed3b28aSJohn Johansen * audit_caps - audit a capability 546a810513SYang Li * @ad: audit data 55dd0c6e86SJohn Johansen * @profile: profile being tested for confinement (NOT NULL) 560ed3b28aSJohn Johansen * @cap: capability tested 570ed3b28aSJohn Johansen * @error: error code returned by test 580ed3b28aSJohn Johansen * 590ed3b28aSJohn Johansen * Do auditing of capability and handle, audit/complain/kill modes switching 600ed3b28aSJohn Johansen * and duplicate message elimination. 610ed3b28aSJohn Johansen * 62bd7bd201SJohn Johansen * Returns: 0 or ad->error on success, error code on failure 630ed3b28aSJohn Johansen */ 64bd7bd201SJohn Johansen static int audit_caps(struct apparmor_audit_data *ad, struct aa_profile *profile, 65c70c86c4SJohn Johansen int cap, int error) 660ed3b28aSJohn Johansen { 671ad22fccSJohn Johansen struct aa_ruleset *rules = list_first_entry(&profile->rules, 681ad22fccSJohn Johansen typeof(*rules), list); 690ed3b28aSJohn Johansen struct audit_cache *ent; 700ed3b28aSJohn Johansen int type = AUDIT_APPARMOR_AUTO; 71c70c86c4SJohn Johansen 72bd7bd201SJohn Johansen ad->error = error; 730ed3b28aSJohn Johansen 740ed3b28aSJohn Johansen if (likely(!error)) { 750ed3b28aSJohn Johansen /* test if auditing is being forced */ 760ed3b28aSJohn Johansen if (likely((AUDIT_MODE(profile) != AUDIT_ALL) && 77217af7e2SJohn Johansen !cap_raised(rules->caps.audit, cap))) 780ed3b28aSJohn Johansen return 0; 790ed3b28aSJohn Johansen type = AUDIT_APPARMOR_AUDIT; 800ed3b28aSJohn Johansen } else if (KILL_MODE(profile) || 81217af7e2SJohn Johansen cap_raised(rules->caps.kill, cap)) { 820ed3b28aSJohn Johansen type = AUDIT_APPARMOR_KILL; 83217af7e2SJohn Johansen } else if (cap_raised(rules->caps.quiet, cap) && 840ed3b28aSJohn Johansen AUDIT_MODE(profile) != AUDIT_NOQUIET && 850ed3b28aSJohn Johansen AUDIT_MODE(profile) != AUDIT_ALL) { 860ed3b28aSJohn Johansen /* quiet auditing */ 870ed3b28aSJohn Johansen return error; 880ed3b28aSJohn Johansen } 890ed3b28aSJohn Johansen 900ed3b28aSJohn Johansen /* Do simple duplicate message elimination */ 910ed3b28aSJohn Johansen ent = &get_cpu_var(audit_cache); 920ed3b28aSJohn Johansen if (profile == ent->profile && cap_raised(ent->caps, cap)) { 930ed3b28aSJohn Johansen put_cpu_var(audit_cache); 940ed3b28aSJohn Johansen if (COMPLAIN_MODE(profile)) 950ed3b28aSJohn Johansen return complain_error(error); 960ed3b28aSJohn Johansen return error; 970ed3b28aSJohn Johansen } else { 980ed3b28aSJohn Johansen aa_put_profile(ent->profile); 999b897132Schao liu if (profile != ent->profile) 1009b897132Schao liu cap_clear(ent->caps); 1010ed3b28aSJohn Johansen ent->profile = aa_get_profile(profile); 1020ed3b28aSJohn Johansen cap_raise(ent->caps, cap); 1030ed3b28aSJohn Johansen } 1040ed3b28aSJohn Johansen put_cpu_var(audit_cache); 1050ed3b28aSJohn Johansen 106bd7bd201SJohn Johansen return aa_audit(type, profile, ad, audit_cb); 1070ed3b28aSJohn Johansen } 1080ed3b28aSJohn Johansen 1090ed3b28aSJohn Johansen /** 1100ed3b28aSJohn Johansen * profile_capable - test if profile allows use of capability @cap 1110ed3b28aSJohn Johansen * @profile: profile being enforced (NOT NULL, NOT unconfined) 1120ed3b28aSJohn Johansen * @cap: capability to test if allowed 113c1a85a00SMicah Morton * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated 114*8532503eSRyan Lee * @ad: audit data (NOT NULL) 1150ed3b28aSJohn Johansen * 1160ed3b28aSJohn Johansen * Returns: 0 if allowed else -EPERM 1170ed3b28aSJohn Johansen */ 118c1a85a00SMicah Morton static int profile_capable(struct aa_profile *profile, int cap, 119bd7bd201SJohn Johansen unsigned int opts, struct apparmor_audit_data *ad) 1200ed3b28aSJohn Johansen { 1211ad22fccSJohn Johansen struct aa_ruleset *rules = list_first_entry(&profile->rules, 1221ad22fccSJohn Johansen typeof(*rules), list); 123c70c86c4SJohn Johansen int error; 124c70c86c4SJohn Johansen 125217af7e2SJohn Johansen if (cap_raised(rules->caps.allow, cap) && 126217af7e2SJohn Johansen !cap_raised(rules->caps.denied, cap)) 127c70c86c4SJohn Johansen error = 0; 128c70c86c4SJohn Johansen else 129c70c86c4SJohn Johansen error = -EPERM; 130c70c86c4SJohn Johansen 131c1a85a00SMicah Morton if (opts & CAP_OPT_NOAUDIT) { 132c70c86c4SJohn Johansen if (!COMPLAIN_MODE(profile)) 133c70c86c4SJohn Johansen return error; 134c70c86c4SJohn Johansen /* audit the cap request in complain mode but note that it 135c70c86c4SJohn Johansen * should be optional. 136c70c86c4SJohn Johansen */ 137bd7bd201SJohn Johansen ad->info = "optional: no audit"; 138c70c86c4SJohn Johansen } 139c70c86c4SJohn Johansen 140bd7bd201SJohn Johansen return audit_caps(ad, profile, cap, error); 1410ed3b28aSJohn Johansen } 1420ed3b28aSJohn Johansen 1430ed3b28aSJohn Johansen /** 1440ed3b28aSJohn Johansen * aa_capable - test permission to use capability 1456a810513SYang Li * @subj_cred: cred we are testing capability against 146c70c86c4SJohn Johansen * @label: label being tested for capability (NOT NULL) 1470ed3b28aSJohn Johansen * @cap: capability to be tested 148c1a85a00SMicah Morton * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated 1490ed3b28aSJohn Johansen * 1500ed3b28aSJohn Johansen * Look up capability in profile capability set. 1510ed3b28aSJohn Johansen * 1520ed3b28aSJohn Johansen * Returns: 0 on success, or else an error code. 1530ed3b28aSJohn Johansen */ 15490c436a6SJohn Johansen int aa_capable(const struct cred *subj_cred, struct aa_label *label, 15590c436a6SJohn Johansen int cap, unsigned int opts) 1560ed3b28aSJohn Johansen { 157c70c86c4SJohn Johansen struct aa_profile *profile; 158c70c86c4SJohn Johansen int error = 0; 159bd7bd201SJohn Johansen DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_CAP, AA_CLASS_CAP, OP_CAPABLE); 1600ed3b28aSJohn Johansen 16190c436a6SJohn Johansen ad.subj_cred = subj_cred; 162bd7bd201SJohn Johansen ad.common.u.cap = cap; 163c70c86c4SJohn Johansen error = fn_for_each_confined(label, profile, 164bd7bd201SJohn Johansen profile_capable(profile, cap, opts, &ad)); 165c70c86c4SJohn Johansen 1660ed3b28aSJohn Johansen return error; 1670ed3b28aSJohn Johansen } 168