10ed3b28aSJohn Johansen /* 20ed3b28aSJohn Johansen * AppArmor security module 30ed3b28aSJohn Johansen * 40ed3b28aSJohn Johansen * This file contains AppArmor capability mediation functions 50ed3b28aSJohn Johansen * 60ed3b28aSJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 70ed3b28aSJohn Johansen * Copyright 2009-2010 Canonical Ltd. 80ed3b28aSJohn Johansen * 90ed3b28aSJohn Johansen * This program is free software; you can redistribute it and/or 100ed3b28aSJohn Johansen * modify it under the terms of the GNU General Public License as 110ed3b28aSJohn Johansen * published by the Free Software Foundation, version 2 of the 120ed3b28aSJohn Johansen * License. 130ed3b28aSJohn Johansen */ 140ed3b28aSJohn Johansen 150ed3b28aSJohn Johansen #include <linux/capability.h> 160ed3b28aSJohn Johansen #include <linux/errno.h> 170ed3b28aSJohn Johansen #include <linux/gfp.h> 1812eb87d5SJohn Johansen #include <linux/security.h> 190ed3b28aSJohn Johansen 200ed3b28aSJohn Johansen #include "include/apparmor.h" 210ed3b28aSJohn Johansen #include "include/capability.h" 220ed3b28aSJohn Johansen #include "include/context.h" 230ed3b28aSJohn Johansen #include "include/policy.h" 240ed3b28aSJohn Johansen #include "include/audit.h" 250ed3b28aSJohn Johansen 260ed3b28aSJohn Johansen /* 270ed3b28aSJohn Johansen * Table of capability names: we generate it from capabilities.h. 280ed3b28aSJohn Johansen */ 290ed3b28aSJohn Johansen #include "capability_names.h" 300ed3b28aSJohn Johansen 31*c97204baSJohn Johansen struct aa_sfs_entry aa_sfs_entry_caps[] = { 32*c97204baSJohn Johansen AA_SFS_FILE_STRING("mask", AA_SFS_CAPS_MASK), 3384f1f787SJohn Johansen { } 3484f1f787SJohn Johansen }; 3584f1f787SJohn Johansen 360ed3b28aSJohn Johansen struct audit_cache { 370ed3b28aSJohn Johansen struct aa_profile *profile; 380ed3b28aSJohn Johansen kernel_cap_t caps; 390ed3b28aSJohn Johansen }; 400ed3b28aSJohn Johansen 410ed3b28aSJohn Johansen static DEFINE_PER_CPU(struct audit_cache, audit_cache); 420ed3b28aSJohn Johansen 430ed3b28aSJohn Johansen /** 440ed3b28aSJohn Johansen * audit_cb - call back for capability components of audit struct 450ed3b28aSJohn Johansen * @ab - audit buffer (NOT NULL) 460ed3b28aSJohn Johansen * @va - audit struct to audit data from (NOT NULL) 470ed3b28aSJohn Johansen */ 480ed3b28aSJohn Johansen static void audit_cb(struct audit_buffer *ab, void *va) 490ed3b28aSJohn Johansen { 500ed3b28aSJohn Johansen struct common_audit_data *sa = va; 510ed3b28aSJohn Johansen audit_log_format(ab, " capname="); 520ed3b28aSJohn Johansen audit_log_untrustedstring(ab, capability_names[sa->u.cap]); 530ed3b28aSJohn Johansen } 540ed3b28aSJohn Johansen 550ed3b28aSJohn Johansen /** 560ed3b28aSJohn Johansen * audit_caps - audit a capability 57dd0c6e86SJohn Johansen * @profile: profile being tested for confinement (NOT NULL) 580ed3b28aSJohn Johansen * @cap: capability tested 5912eb87d5SJohn Johansen @audit: whether an audit record should be generated 600ed3b28aSJohn Johansen * @error: error code returned by test 610ed3b28aSJohn Johansen * 620ed3b28aSJohn Johansen * Do auditing of capability and handle, audit/complain/kill modes switching 630ed3b28aSJohn Johansen * and duplicate message elimination. 640ed3b28aSJohn Johansen * 650ed3b28aSJohn Johansen * Returns: 0 or sa->error on success, error code on failure 660ed3b28aSJohn Johansen */ 6712eb87d5SJohn Johansen static int audit_caps(struct aa_profile *profile, int cap, int audit, 6812eb87d5SJohn Johansen int error) 690ed3b28aSJohn Johansen { 700ed3b28aSJohn Johansen struct audit_cache *ent; 710ed3b28aSJohn Johansen int type = AUDIT_APPARMOR_AUTO; 72ef88a7acSJohn Johansen DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_CAP, OP_CAPABLE); 730ed3b28aSJohn Johansen sa.u.cap = cap; 74ef88a7acSJohn Johansen aad(&sa)->error = error; 7512eb87d5SJohn Johansen if (audit == SECURITY_CAP_NOAUDIT) 7612eb87d5SJohn Johansen aad(&sa)->info = "optional: no audit"; 770ed3b28aSJohn Johansen 780ed3b28aSJohn Johansen if (likely(!error)) { 790ed3b28aSJohn Johansen /* test if auditing is being forced */ 800ed3b28aSJohn Johansen if (likely((AUDIT_MODE(profile) != AUDIT_ALL) && 810ed3b28aSJohn Johansen !cap_raised(profile->caps.audit, cap))) 820ed3b28aSJohn Johansen return 0; 830ed3b28aSJohn Johansen type = AUDIT_APPARMOR_AUDIT; 840ed3b28aSJohn Johansen } else if (KILL_MODE(profile) || 850ed3b28aSJohn Johansen cap_raised(profile->caps.kill, cap)) { 860ed3b28aSJohn Johansen type = AUDIT_APPARMOR_KILL; 870ed3b28aSJohn Johansen } else if (cap_raised(profile->caps.quiet, cap) && 880ed3b28aSJohn Johansen AUDIT_MODE(profile) != AUDIT_NOQUIET && 890ed3b28aSJohn Johansen AUDIT_MODE(profile) != AUDIT_ALL) { 900ed3b28aSJohn Johansen /* quiet auditing */ 910ed3b28aSJohn Johansen return error; 920ed3b28aSJohn Johansen } 930ed3b28aSJohn Johansen 940ed3b28aSJohn Johansen /* Do simple duplicate message elimination */ 950ed3b28aSJohn Johansen ent = &get_cpu_var(audit_cache); 960ed3b28aSJohn Johansen if (profile == ent->profile && cap_raised(ent->caps, cap)) { 970ed3b28aSJohn Johansen put_cpu_var(audit_cache); 980ed3b28aSJohn Johansen if (COMPLAIN_MODE(profile)) 990ed3b28aSJohn Johansen return complain_error(error); 1000ed3b28aSJohn Johansen return error; 1010ed3b28aSJohn Johansen } else { 1020ed3b28aSJohn Johansen aa_put_profile(ent->profile); 1030ed3b28aSJohn Johansen ent->profile = aa_get_profile(profile); 1040ed3b28aSJohn Johansen cap_raise(ent->caps, cap); 1050ed3b28aSJohn Johansen } 1060ed3b28aSJohn Johansen put_cpu_var(audit_cache); 1070ed3b28aSJohn Johansen 108ef88a7acSJohn Johansen return aa_audit(type, profile, &sa, audit_cb); 1090ed3b28aSJohn Johansen } 1100ed3b28aSJohn Johansen 1110ed3b28aSJohn Johansen /** 1120ed3b28aSJohn Johansen * profile_capable - test if profile allows use of capability @cap 1130ed3b28aSJohn Johansen * @profile: profile being enforced (NOT NULL, NOT unconfined) 1140ed3b28aSJohn Johansen * @cap: capability to test if allowed 1150ed3b28aSJohn Johansen * 1160ed3b28aSJohn Johansen * Returns: 0 if allowed else -EPERM 1170ed3b28aSJohn Johansen */ 1180ed3b28aSJohn Johansen static int profile_capable(struct aa_profile *profile, int cap) 1190ed3b28aSJohn Johansen { 1200ed3b28aSJohn Johansen return cap_raised(profile->caps.allow, cap) ? 0 : -EPERM; 1210ed3b28aSJohn Johansen } 1220ed3b28aSJohn Johansen 1230ed3b28aSJohn Johansen /** 1240ed3b28aSJohn Johansen * aa_capable - test permission to use capability 125dd0c6e86SJohn Johansen * @profile: profile being tested against (NOT NULL) 1260ed3b28aSJohn Johansen * @cap: capability to be tested 1270ed3b28aSJohn Johansen * @audit: whether an audit record should be generated 1280ed3b28aSJohn Johansen * 1290ed3b28aSJohn Johansen * Look up capability in profile capability set. 1300ed3b28aSJohn Johansen * 1310ed3b28aSJohn Johansen * Returns: 0 on success, or else an error code. 1320ed3b28aSJohn Johansen */ 133dd0c6e86SJohn Johansen int aa_capable(struct aa_profile *profile, int cap, int audit) 1340ed3b28aSJohn Johansen { 1350ed3b28aSJohn Johansen int error = profile_capable(profile, cap); 1360ed3b28aSJohn Johansen 13712eb87d5SJohn Johansen if (audit == SECURITY_CAP_NOAUDIT) { 13812eb87d5SJohn Johansen if (!COMPLAIN_MODE(profile)) 1390ed3b28aSJohn Johansen return error; 1400ed3b28aSJohn Johansen } 1410ed3b28aSJohn Johansen 14212eb87d5SJohn Johansen return audit_caps(profile, cap, audit, error); 1430ed3b28aSJohn Johansen } 144