xref: /linux-6.15/security/apparmor/capability.c (revision d8889d49)
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"
22*d8889d49SJohn Johansen #include "include/cred.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 
31c97204baSJohn Johansen struct aa_sfs_entry aa_sfs_entry_caps[] = {
32c97204baSJohn 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;
51c70c86c4SJohn Johansen 
520ed3b28aSJohn Johansen 	audit_log_format(ab, " capname=");
530ed3b28aSJohn Johansen 	audit_log_untrustedstring(ab, capability_names[sa->u.cap]);
540ed3b28aSJohn Johansen }
550ed3b28aSJohn Johansen 
560ed3b28aSJohn Johansen /**
570ed3b28aSJohn Johansen  * audit_caps - audit a capability
58c70c86c4SJohn Johansen  * @sa: audit data
59dd0c6e86SJohn Johansen  * @profile: profile being tested for confinement (NOT NULL)
600ed3b28aSJohn Johansen  * @cap: capability tested
610ed3b28aSJohn Johansen  * @error: error code returned by test
620ed3b28aSJohn Johansen  *
630ed3b28aSJohn Johansen  * Do auditing of capability and handle, audit/complain/kill modes switching
640ed3b28aSJohn Johansen  * and duplicate message elimination.
650ed3b28aSJohn Johansen  *
660ed3b28aSJohn Johansen  * Returns: 0 or sa->error on success,  error code on failure
670ed3b28aSJohn Johansen  */
68c70c86c4SJohn Johansen static int audit_caps(struct common_audit_data *sa, struct aa_profile *profile,
69c70c86c4SJohn Johansen 		      int cap, int error)
700ed3b28aSJohn Johansen {
710ed3b28aSJohn Johansen 	struct audit_cache *ent;
720ed3b28aSJohn Johansen 	int type = AUDIT_APPARMOR_AUTO;
73c70c86c4SJohn Johansen 
74c70c86c4SJohn Johansen 	aad(sa)->error = error;
750ed3b28aSJohn Johansen 
760ed3b28aSJohn Johansen 	if (likely(!error)) {
770ed3b28aSJohn Johansen 		/* test if auditing is being forced */
780ed3b28aSJohn Johansen 		if (likely((AUDIT_MODE(profile) != AUDIT_ALL) &&
790ed3b28aSJohn Johansen 			   !cap_raised(profile->caps.audit, cap)))
800ed3b28aSJohn Johansen 			return 0;
810ed3b28aSJohn Johansen 		type = AUDIT_APPARMOR_AUDIT;
820ed3b28aSJohn Johansen 	} else if (KILL_MODE(profile) ||
830ed3b28aSJohn Johansen 		   cap_raised(profile->caps.kill, cap)) {
840ed3b28aSJohn Johansen 		type = AUDIT_APPARMOR_KILL;
850ed3b28aSJohn Johansen 	} else if (cap_raised(profile->caps.quiet, cap) &&
860ed3b28aSJohn Johansen 		   AUDIT_MODE(profile) != AUDIT_NOQUIET &&
870ed3b28aSJohn Johansen 		   AUDIT_MODE(profile) != AUDIT_ALL) {
880ed3b28aSJohn Johansen 		/* quiet auditing */
890ed3b28aSJohn Johansen 		return error;
900ed3b28aSJohn Johansen 	}
910ed3b28aSJohn Johansen 
920ed3b28aSJohn Johansen 	/* Do simple duplicate message elimination */
930ed3b28aSJohn Johansen 	ent = &get_cpu_var(audit_cache);
940ed3b28aSJohn Johansen 	if (profile == ent->profile && cap_raised(ent->caps, cap)) {
950ed3b28aSJohn Johansen 		put_cpu_var(audit_cache);
960ed3b28aSJohn Johansen 		if (COMPLAIN_MODE(profile))
970ed3b28aSJohn Johansen 			return complain_error(error);
980ed3b28aSJohn Johansen 		return error;
990ed3b28aSJohn Johansen 	} else {
1000ed3b28aSJohn Johansen 		aa_put_profile(ent->profile);
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 
106c70c86c4SJohn Johansen 	return aa_audit(type, profile, sa, 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
113c70c86c4SJohn Johansen  * @audit: whether an audit record should be generated
114c70c86c4SJohn Johansen  * @sa: audit data (MAY BE NULL indicating no auditing)
1150ed3b28aSJohn Johansen  *
1160ed3b28aSJohn Johansen  * Returns: 0 if allowed else -EPERM
1170ed3b28aSJohn Johansen  */
118c70c86c4SJohn Johansen static int profile_capable(struct aa_profile *profile, int cap, int audit,
119c70c86c4SJohn Johansen 			   struct common_audit_data *sa)
1200ed3b28aSJohn Johansen {
121c70c86c4SJohn Johansen 	int error;
122c70c86c4SJohn Johansen 
123c70c86c4SJohn Johansen 	if (cap_raised(profile->caps.allow, cap) &&
124c70c86c4SJohn Johansen 	    !cap_raised(profile->caps.denied, cap))
125c70c86c4SJohn Johansen 		error = 0;
126c70c86c4SJohn Johansen 	else
127c70c86c4SJohn Johansen 		error = -EPERM;
128c70c86c4SJohn Johansen 
129c70c86c4SJohn Johansen 	if (audit == SECURITY_CAP_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
1450ed3b28aSJohn Johansen  * @audit: whether an audit record should be 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  */
151c70c86c4SJohn Johansen int aa_capable(struct aa_label *label, int cap, int audit)
1520ed3b28aSJohn Johansen {
153c70c86c4SJohn Johansen 	struct aa_profile *profile;
154c70c86c4SJohn Johansen 	int error = 0;
155c70c86c4SJohn Johansen 	DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_CAP, OP_CAPABLE);
1560ed3b28aSJohn Johansen 
157c70c86c4SJohn Johansen 	sa.u.cap = cap;
158c70c86c4SJohn Johansen 	error = fn_for_each_confined(label, profile,
159c70c86c4SJohn Johansen 			profile_capable(profile, cap, audit, &sa));
160c70c86c4SJohn Johansen 
1610ed3b28aSJohn Johansen 	return error;
1620ed3b28aSJohn Johansen }
163