xref: /linux-6.15/security/apparmor/capability.c (revision 217af7e2)
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*217af7e2SJohn Johansen 	struct aa_ruleset *rules = &profile->rules;
680ed3b28aSJohn Johansen 	struct audit_cache *ent;
690ed3b28aSJohn Johansen 	int type = AUDIT_APPARMOR_AUTO;
70c70c86c4SJohn Johansen 
71c70c86c4SJohn Johansen 	aad(sa)->error = error;
720ed3b28aSJohn Johansen 
730ed3b28aSJohn Johansen 	if (likely(!error)) {
740ed3b28aSJohn Johansen 		/* test if auditing is being forced */
750ed3b28aSJohn Johansen 		if (likely((AUDIT_MODE(profile) != AUDIT_ALL) &&
76*217af7e2SJohn Johansen 			   !cap_raised(rules->caps.audit, cap)))
770ed3b28aSJohn Johansen 			return 0;
780ed3b28aSJohn Johansen 		type = AUDIT_APPARMOR_AUDIT;
790ed3b28aSJohn Johansen 	} else if (KILL_MODE(profile) ||
80*217af7e2SJohn Johansen 		   cap_raised(rules->caps.kill, cap)) {
810ed3b28aSJohn Johansen 		type = AUDIT_APPARMOR_KILL;
82*217af7e2SJohn Johansen 	} else if (cap_raised(rules->caps.quiet, cap) &&
830ed3b28aSJohn Johansen 		   AUDIT_MODE(profile) != AUDIT_NOQUIET &&
840ed3b28aSJohn Johansen 		   AUDIT_MODE(profile) != AUDIT_ALL) {
850ed3b28aSJohn Johansen 		/* quiet auditing */
860ed3b28aSJohn Johansen 		return error;
870ed3b28aSJohn Johansen 	}
880ed3b28aSJohn Johansen 
890ed3b28aSJohn Johansen 	/* Do simple duplicate message elimination */
900ed3b28aSJohn Johansen 	ent = &get_cpu_var(audit_cache);
910ed3b28aSJohn Johansen 	if (profile == ent->profile && cap_raised(ent->caps, cap)) {
920ed3b28aSJohn Johansen 		put_cpu_var(audit_cache);
930ed3b28aSJohn Johansen 		if (COMPLAIN_MODE(profile))
940ed3b28aSJohn Johansen 			return complain_error(error);
950ed3b28aSJohn Johansen 		return error;
960ed3b28aSJohn Johansen 	} else {
970ed3b28aSJohn Johansen 		aa_put_profile(ent->profile);
980ed3b28aSJohn Johansen 		ent->profile = aa_get_profile(profile);
990ed3b28aSJohn Johansen 		cap_raise(ent->caps, cap);
1000ed3b28aSJohn Johansen 	}
1010ed3b28aSJohn Johansen 	put_cpu_var(audit_cache);
1020ed3b28aSJohn Johansen 
103c70c86c4SJohn Johansen 	return aa_audit(type, profile, sa, audit_cb);
1040ed3b28aSJohn Johansen }
1050ed3b28aSJohn Johansen 
1060ed3b28aSJohn Johansen /**
1070ed3b28aSJohn Johansen  * profile_capable - test if profile allows use of capability @cap
1080ed3b28aSJohn Johansen  * @profile: profile being enforced    (NOT NULL, NOT unconfined)
1090ed3b28aSJohn Johansen  * @cap: capability to test if allowed
110c1a85a00SMicah Morton  * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
111c70c86c4SJohn Johansen  * @sa: audit data (MAY BE NULL indicating no auditing)
1120ed3b28aSJohn Johansen  *
1130ed3b28aSJohn Johansen  * Returns: 0 if allowed else -EPERM
1140ed3b28aSJohn Johansen  */
115c1a85a00SMicah Morton static int profile_capable(struct aa_profile *profile, int cap,
116c1a85a00SMicah Morton 			   unsigned int opts, struct common_audit_data *sa)
1170ed3b28aSJohn Johansen {
118*217af7e2SJohn Johansen 	struct aa_ruleset *rules = &profile->rules;
119c70c86c4SJohn Johansen 	int error;
120c70c86c4SJohn Johansen 
121*217af7e2SJohn Johansen 	if (cap_raised(rules->caps.allow, cap) &&
122*217af7e2SJohn Johansen 	    !cap_raised(rules->caps.denied, cap))
123c70c86c4SJohn Johansen 		error = 0;
124c70c86c4SJohn Johansen 	else
125c70c86c4SJohn Johansen 		error = -EPERM;
126c70c86c4SJohn Johansen 
127c1a85a00SMicah Morton 	if (opts & CAP_OPT_NOAUDIT) {
128c70c86c4SJohn Johansen 		if (!COMPLAIN_MODE(profile))
129c70c86c4SJohn Johansen 			return error;
130c70c86c4SJohn Johansen 		/* audit the cap request in complain mode but note that it
131c70c86c4SJohn Johansen 		 * should be optional.
132c70c86c4SJohn Johansen 		 */
133c70c86c4SJohn Johansen 		aad(sa)->info = "optional: no audit";
134c70c86c4SJohn Johansen 	}
135c70c86c4SJohn Johansen 
136c70c86c4SJohn Johansen 	return audit_caps(sa, profile, cap, error);
1370ed3b28aSJohn Johansen }
1380ed3b28aSJohn Johansen 
1390ed3b28aSJohn Johansen /**
1400ed3b28aSJohn Johansen  * aa_capable - test permission to use capability
141c70c86c4SJohn Johansen  * @label: label being tested for capability (NOT NULL)
1420ed3b28aSJohn Johansen  * @cap: capability to be tested
143c1a85a00SMicah Morton  * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
1440ed3b28aSJohn Johansen  *
1450ed3b28aSJohn Johansen  * Look up capability in profile capability set.
1460ed3b28aSJohn Johansen  *
1470ed3b28aSJohn Johansen  * Returns: 0 on success, or else an error code.
1480ed3b28aSJohn Johansen  */
149c1a85a00SMicah Morton int aa_capable(struct aa_label *label, int cap, unsigned int opts)
1500ed3b28aSJohn Johansen {
151c70c86c4SJohn Johansen 	struct aa_profile *profile;
152c70c86c4SJohn Johansen 	int error = 0;
1538c4b785aSJohn Johansen 	DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_CAP, AA_CLASS_CAP, OP_CAPABLE);
1540ed3b28aSJohn Johansen 
155c70c86c4SJohn Johansen 	sa.u.cap = cap;
156c70c86c4SJohn Johansen 	error = fn_for_each_confined(label, profile,
157c1a85a00SMicah Morton 			profile_capable(profile, cap, opts, &sa));
158c70c86c4SJohn Johansen 
1590ed3b28aSJohn Johansen 	return error;
1600ed3b28aSJohn Johansen }
161