xref: /linux-6.15/security/apparmor/capability.c (revision 1ad22fcc)
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
410ed3b28aSJohn Johansen  * @ab - audit buffer   (NOT NULL)
420ed3b28aSJohn Johansen  * @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
54c70c86c4SJohn Johansen  * @sa: 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  *
620ed3b28aSJohn Johansen  * Returns: 0 or sa->error on success,  error code on failure
630ed3b28aSJohn Johansen  */
64c70c86c4SJohn Johansen static int audit_caps(struct common_audit_data *sa, struct aa_profile *profile,
65c70c86c4SJohn Johansen 		      int cap, int error)
660ed3b28aSJohn Johansen {
67*1ad22fccSJohn Johansen 	struct aa_ruleset *rules = list_first_entry(&profile->rules,
68*1ad22fccSJohn Johansen 						    typeof(*rules), list);
690ed3b28aSJohn Johansen 	struct audit_cache *ent;
700ed3b28aSJohn Johansen 	int type = AUDIT_APPARMOR_AUTO;
71c70c86c4SJohn Johansen 
72c70c86c4SJohn Johansen 	aad(sa)->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);
990ed3b28aSJohn Johansen 		ent->profile = aa_get_profile(profile);
1000ed3b28aSJohn Johansen 		cap_raise(ent->caps, cap);
1010ed3b28aSJohn Johansen 	}
1020ed3b28aSJohn Johansen 	put_cpu_var(audit_cache);
1030ed3b28aSJohn Johansen 
104c70c86c4SJohn Johansen 	return aa_audit(type, profile, sa, audit_cb);
1050ed3b28aSJohn Johansen }
1060ed3b28aSJohn Johansen 
1070ed3b28aSJohn Johansen /**
1080ed3b28aSJohn Johansen  * profile_capable - test if profile allows use of capability @cap
1090ed3b28aSJohn Johansen  * @profile: profile being enforced    (NOT NULL, NOT unconfined)
1100ed3b28aSJohn Johansen  * @cap: capability to test if allowed
111c1a85a00SMicah Morton  * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
112c70c86c4SJohn Johansen  * @sa: audit data (MAY BE NULL indicating no auditing)
1130ed3b28aSJohn Johansen  *
1140ed3b28aSJohn Johansen  * Returns: 0 if allowed else -EPERM
1150ed3b28aSJohn Johansen  */
116c1a85a00SMicah Morton static int profile_capable(struct aa_profile *profile, int cap,
117c1a85a00SMicah Morton 			   unsigned int opts, struct common_audit_data *sa)
1180ed3b28aSJohn Johansen {
119*1ad22fccSJohn Johansen 	struct aa_ruleset *rules = list_first_entry(&profile->rules,
120*1ad22fccSJohn Johansen 						    typeof(*rules), list);
121c70c86c4SJohn Johansen 	int error;
122c70c86c4SJohn Johansen 
123217af7e2SJohn Johansen 	if (cap_raised(rules->caps.allow, cap) &&
124217af7e2SJohn Johansen 	    !cap_raised(rules->caps.denied, cap))
125c70c86c4SJohn Johansen 		error = 0;
126c70c86c4SJohn Johansen 	else
127c70c86c4SJohn Johansen 		error = -EPERM;
128c70c86c4SJohn Johansen 
129c1a85a00SMicah Morton 	if (opts & CAP_OPT_NOAUDIT) {
130c70c86c4SJohn Johansen 		if (!COMPLAIN_MODE(profile))
131c70c86c4SJohn Johansen 			return error;
132c70c86c4SJohn Johansen 		/* audit the cap request in complain mode but note that it
133c70c86c4SJohn Johansen 		 * should be optional.
134c70c86c4SJohn Johansen 		 */
135c70c86c4SJohn Johansen 		aad(sa)->info = "optional: no audit";
136c70c86c4SJohn Johansen 	}
137c70c86c4SJohn Johansen 
138c70c86c4SJohn Johansen 	return audit_caps(sa, profile, cap, error);
1390ed3b28aSJohn Johansen }
1400ed3b28aSJohn Johansen 
1410ed3b28aSJohn Johansen /**
1420ed3b28aSJohn Johansen  * aa_capable - test permission to use capability
143c70c86c4SJohn Johansen  * @label: label being tested for capability (NOT NULL)
1440ed3b28aSJohn Johansen  * @cap: capability to be tested
145c1a85a00SMicah Morton  * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
1460ed3b28aSJohn Johansen  *
1470ed3b28aSJohn Johansen  * Look up capability in profile capability set.
1480ed3b28aSJohn Johansen  *
1490ed3b28aSJohn Johansen  * Returns: 0 on success, or else an error code.
1500ed3b28aSJohn Johansen  */
151c1a85a00SMicah Morton int aa_capable(struct aa_label *label, int cap, unsigned int opts)
1520ed3b28aSJohn Johansen {
153c70c86c4SJohn Johansen 	struct aa_profile *profile;
154c70c86c4SJohn Johansen 	int error = 0;
1558c4b785aSJohn Johansen 	DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_CAP, AA_CLASS_CAP, OP_CAPABLE);
1560ed3b28aSJohn Johansen 
157c70c86c4SJohn Johansen 	sa.u.cap = cap;
158c70c86c4SJohn Johansen 	error = fn_for_each_confined(label, profile,
159c1a85a00SMicah Morton 			profile_capable(profile, cap, opts, &sa));
160c70c86c4SJohn Johansen 
1610ed3b28aSJohn Johansen 	return error;
1620ed3b28aSJohn Johansen }
163