2 * AppArmor security module
4 * This file contains AppArmor capability mediation functions
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
15 #include <linux/capability.h>
16 #include <linux/errno.h>
17 #include <linux/gfp.h>
19 #include "include/apparmor.h"
20 #include "include/capability.h"
21 #include "include/context.h"
22 #include "include/policy.h"
23 #include "include/audit.h"
26 * Table of capability names: we generate it from capabilities.h.
28 #include "capability_names.h"
30 struct aa_fs_entry aa_fs_entry_caps[] = {
31 AA_FS_FILE_STRING("mask", AA_FS_CAPS_MASK),
36 struct aa_profile *profile;
40 static DEFINE_PER_CPU(struct audit_cache, audit_cache);
43 * audit_cb - call back for capability components of audit struct
44 * @ab - audit buffer (NOT NULL)
45 * @va - audit struct to audit data from (NOT NULL)
47 static void audit_cb(struct audit_buffer *ab, void *va)
49 struct common_audit_data *sa = va;
50 audit_log_format(ab, " capname=");
51 audit_log_untrustedstring(ab, capability_names[sa->u.cap]);
55 * audit_caps - audit a capability
56 * @profile: profile confining task (NOT NULL)
57 * @task: task capability test was performed against (NOT NULL)
58 * @cap: capability tested
59 * @error: error code returned by test
61 * Do auditing of capability and handle, audit/complain/kill modes switching
62 * and duplicate message elimination.
64 * Returns: 0 or sa->error on success, error code on failure
66 static int audit_caps(struct aa_profile *profile, struct task_struct *task,
69 struct audit_cache *ent;
70 int type = AUDIT_APPARMOR_AUTO;
71 struct common_audit_data sa;
72 struct apparmor_audit_data aad = {0,};
73 sa.type = LSM_AUDIT_DATA_CAP;
77 sa.aad->op = OP_CAPABLE;
78 sa.aad->error = error;
81 /* test if auditing is being forced */
82 if (likely((AUDIT_MODE(profile) != AUDIT_ALL) &&
83 !cap_raised(profile->caps.audit, cap)))
85 type = AUDIT_APPARMOR_AUDIT;
86 } else if (KILL_MODE(profile) ||
87 cap_raised(profile->caps.kill, cap)) {
88 type = AUDIT_APPARMOR_KILL;
89 } else if (cap_raised(profile->caps.quiet, cap) &&
90 AUDIT_MODE(profile) != AUDIT_NOQUIET &&
91 AUDIT_MODE(profile) != AUDIT_ALL) {
96 /* Do simple duplicate message elimination */
97 ent = &get_cpu_var(audit_cache);
98 if (profile == ent->profile && cap_raised(ent->caps, cap)) {
99 put_cpu_var(audit_cache);
100 if (COMPLAIN_MODE(profile))
101 return complain_error(error);
104 aa_put_profile(ent->profile);
105 ent->profile = aa_get_profile(profile);
106 cap_raise(ent->caps, cap);
108 put_cpu_var(audit_cache);
110 return aa_audit(type, profile, GFP_ATOMIC, &sa, audit_cb);
114 * profile_capable - test if profile allows use of capability @cap
115 * @profile: profile being enforced (NOT NULL, NOT unconfined)
116 * @cap: capability to test if allowed
118 * Returns: 0 if allowed else -EPERM
120 static int profile_capable(struct aa_profile *profile, int cap)
122 return cap_raised(profile->caps.allow, cap) ? 0 : -EPERM;
126 * aa_capable - test permission to use capability
127 * @task: task doing capability test against (NOT NULL)
128 * @profile: profile confining @task (NOT NULL)
129 * @cap: capability to be tested
130 * @audit: whether an audit record should be generated
132 * Look up capability in profile capability set.
134 * Returns: 0 on success, or else an error code.
136 int aa_capable(struct task_struct *task, struct aa_profile *profile, int cap,
139 int error = profile_capable(profile, cap);
142 if (COMPLAIN_MODE(profile))
143 return complain_error(error);
147 return audit_caps(profile, task, cap, error);