2019-06-01 08:08:55 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2010-07-29 21:48:05 +00:00
|
|
|
/*
|
|
|
|
* AppArmor security module
|
|
|
|
*
|
|
|
|
* This file contains AppArmor capability mediation functions
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998-2008 Novell/SUSE
|
|
|
|
* Copyright 2009-2010 Canonical Ltd.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/capability.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/gfp.h>
|
2017-01-16 08:43:08 +00:00
|
|
|
#include <linux/security.h>
|
2024-09-20 19:53:15 +00:00
|
|
|
#include <linux/timekeeping.h>
|
2010-07-29 21:48:05 +00:00
|
|
|
|
|
|
|
#include "include/apparmor.h"
|
|
|
|
#include "include/capability.h"
|
2017-10-11 08:04:48 +00:00
|
|
|
#include "include/cred.h"
|
2010-07-29 21:48:05 +00:00
|
|
|
#include "include/policy.h"
|
|
|
|
#include "include/audit.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Table of capability names: we generate it from capabilities.h.
|
|
|
|
*/
|
|
|
|
#include "capability_names.h"
|
|
|
|
|
2017-05-25 13:23:42 +00:00
|
|
|
struct aa_sfs_entry aa_sfs_entry_caps[] = {
|
|
|
|
AA_SFS_FILE_STRING("mask", AA_SFS_CAPS_MASK),
|
2013-08-14 18:27:32 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2010-07-29 21:48:05 +00:00
|
|
|
struct audit_cache {
|
2024-09-25 18:30:11 +00:00
|
|
|
const struct cred *ad_subj_cred;
|
2024-09-20 19:53:15 +00:00
|
|
|
/* Capabilities go from 0 to CAP_LAST_CAP */
|
|
|
|
u64 ktime_ns_expiration[CAP_LAST_CAP+1];
|
2010-07-29 21:48:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static DEFINE_PER_CPU(struct audit_cache, audit_cache);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* audit_cb - call back for capability components of audit struct
|
2023-06-25 01:13:40 +00:00
|
|
|
* @ab: audit buffer (NOT NULL)
|
|
|
|
* @va: audit struct to audit data from (NOT NULL)
|
2010-07-29 21:48:05 +00:00
|
|
|
*/
|
|
|
|
static void audit_cb(struct audit_buffer *ab, void *va)
|
|
|
|
{
|
|
|
|
struct common_audit_data *sa = va;
|
2017-06-09 21:07:02 +00:00
|
|
|
|
2010-07-29 21:48:05 +00:00
|
|
|
audit_log_format(ab, " capname=");
|
|
|
|
audit_log_untrustedstring(ab, capability_names[sa->u.cap]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* audit_caps - audit a capability
|
2023-10-23 00:55:17 +00:00
|
|
|
* @ad: audit data
|
2013-10-08 12:37:18 +00:00
|
|
|
* @profile: profile being tested for confinement (NOT NULL)
|
2010-07-29 21:48:05 +00:00
|
|
|
* @cap: capability tested
|
|
|
|
* @error: error code returned by test
|
|
|
|
*
|
|
|
|
* Do auditing of capability and handle, audit/complain/kill modes switching
|
|
|
|
* and duplicate message elimination.
|
|
|
|
*
|
2022-09-14 07:20:12 +00:00
|
|
|
* Returns: 0 or ad->error on success, error code on failure
|
2010-07-29 21:48:05 +00:00
|
|
|
*/
|
2022-09-14 07:20:12 +00:00
|
|
|
static int audit_caps(struct apparmor_audit_data *ad, struct aa_profile *profile,
|
2017-06-09 21:07:02 +00:00
|
|
|
int cap, int error)
|
2010-07-29 21:48:05 +00:00
|
|
|
{
|
2024-09-20 19:53:15 +00:00
|
|
|
const u64 AUDIT_CACHE_TIMEOUT_NS = 1000*1000*1000; /* 1 second */
|
|
|
|
|
2022-09-06 03:47:36 +00:00
|
|
|
struct aa_ruleset *rules = list_first_entry(&profile->rules,
|
|
|
|
typeof(*rules), list);
|
2010-07-29 21:48:05 +00:00
|
|
|
struct audit_cache *ent;
|
|
|
|
int type = AUDIT_APPARMOR_AUTO;
|
2017-06-09 21:07:02 +00:00
|
|
|
|
2022-09-14 07:20:12 +00:00
|
|
|
ad->error = error;
|
2010-07-29 21:48:05 +00:00
|
|
|
|
|
|
|
if (likely(!error)) {
|
|
|
|
/* test if auditing is being forced */
|
|
|
|
if (likely((AUDIT_MODE(profile) != AUDIT_ALL) &&
|
2022-07-30 00:17:31 +00:00
|
|
|
!cap_raised(rules->caps.audit, cap)))
|
2010-07-29 21:48:05 +00:00
|
|
|
return 0;
|
|
|
|
type = AUDIT_APPARMOR_AUDIT;
|
|
|
|
} else if (KILL_MODE(profile) ||
|
2022-07-30 00:17:31 +00:00
|
|
|
cap_raised(rules->caps.kill, cap)) {
|
2010-07-29 21:48:05 +00:00
|
|
|
type = AUDIT_APPARMOR_KILL;
|
2022-07-30 00:17:31 +00:00
|
|
|
} else if (cap_raised(rules->caps.quiet, cap) &&
|
2010-07-29 21:48:05 +00:00
|
|
|
AUDIT_MODE(profile) != AUDIT_NOQUIET &&
|
|
|
|
AUDIT_MODE(profile) != AUDIT_ALL) {
|
|
|
|
/* quiet auditing */
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do simple duplicate message elimination */
|
|
|
|
ent = &get_cpu_var(audit_cache);
|
2024-09-20 19:53:15 +00:00
|
|
|
/* If the capability was never raised the timestamp check would also catch that */
|
2024-09-25 18:30:11 +00:00
|
|
|
if (ad->subj_cred == ent->ad_subj_cred && ktime_get_ns() <= ent->ktime_ns_expiration[cap]) {
|
2010-07-29 21:48:05 +00:00
|
|
|
put_cpu_var(audit_cache);
|
|
|
|
if (COMPLAIN_MODE(profile))
|
|
|
|
return complain_error(error);
|
|
|
|
return error;
|
|
|
|
} else {
|
2024-09-25 18:30:11 +00:00
|
|
|
put_cred(ent->ad_subj_cred);
|
|
|
|
ent->ad_subj_cred = get_cred(ad->subj_cred);
|
2024-09-20 19:53:15 +00:00
|
|
|
ent->ktime_ns_expiration[cap] = ktime_get_ns() + AUDIT_CACHE_TIMEOUT_NS;
|
2010-07-29 21:48:05 +00:00
|
|
|
}
|
|
|
|
put_cpu_var(audit_cache);
|
|
|
|
|
2022-09-14 07:20:12 +00:00
|
|
|
return aa_audit(type, profile, ad, audit_cb);
|
2010-07-29 21:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* profile_capable - test if profile allows use of capability @cap
|
|
|
|
* @profile: profile being enforced (NOT NULL, NOT unconfined)
|
|
|
|
* @cap: capability to test if allowed
|
2019-01-08 00:10:53 +00:00
|
|
|
* @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
|
2024-09-25 00:56:05 +00:00
|
|
|
* @ad: audit data (NOT NULL)
|
2010-07-29 21:48:05 +00:00
|
|
|
*
|
|
|
|
* Returns: 0 if allowed else -EPERM
|
|
|
|
*/
|
2019-01-08 00:10:53 +00:00
|
|
|
static int profile_capable(struct aa_profile *profile, int cap,
|
2022-09-14 07:20:12 +00:00
|
|
|
unsigned int opts, struct apparmor_audit_data *ad)
|
2010-07-29 21:48:05 +00:00
|
|
|
{
|
2022-09-06 03:47:36 +00:00
|
|
|
struct aa_ruleset *rules = list_first_entry(&profile->rules,
|
|
|
|
typeof(*rules), list);
|
2017-06-09 21:07:02 +00:00
|
|
|
int error;
|
|
|
|
|
2022-07-30 00:17:31 +00:00
|
|
|
if (cap_raised(rules->caps.allow, cap) &&
|
|
|
|
!cap_raised(rules->caps.denied, cap))
|
2017-06-09 21:07:02 +00:00
|
|
|
error = 0;
|
|
|
|
else
|
|
|
|
error = -EPERM;
|
|
|
|
|
2019-01-08 00:10:53 +00:00
|
|
|
if (opts & CAP_OPT_NOAUDIT) {
|
2017-06-09 21:07:02 +00:00
|
|
|
if (!COMPLAIN_MODE(profile))
|
|
|
|
return error;
|
|
|
|
/* audit the cap request in complain mode but note that it
|
|
|
|
* should be optional.
|
|
|
|
*/
|
2022-09-14 07:20:12 +00:00
|
|
|
ad->info = "optional: no audit";
|
2017-06-09 21:07:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 07:20:12 +00:00
|
|
|
return audit_caps(ad, profile, cap, error);
|
2010-07-29 21:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* aa_capable - test permission to use capability
|
2023-10-23 00:55:17 +00:00
|
|
|
* @subj_cred: cred we are testing capability against
|
2017-06-09 21:07:02 +00:00
|
|
|
* @label: label being tested for capability (NOT NULL)
|
2010-07-29 21:48:05 +00:00
|
|
|
* @cap: capability to be tested
|
2019-01-08 00:10:53 +00:00
|
|
|
* @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
|
2010-07-29 21:48:05 +00:00
|
|
|
*
|
|
|
|
* Look up capability in profile capability set.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, or else an error code.
|
|
|
|
*/
|
2022-09-20 03:48:48 +00:00
|
|
|
int aa_capable(const struct cred *subj_cred, struct aa_label *label,
|
|
|
|
int cap, unsigned int opts)
|
2010-07-29 21:48:05 +00:00
|
|
|
{
|
2017-06-09 21:07:02 +00:00
|
|
|
struct aa_profile *profile;
|
|
|
|
int error = 0;
|
2022-09-14 07:20:12 +00:00
|
|
|
DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_CAP, AA_CLASS_CAP, OP_CAPABLE);
|
2010-07-29 21:48:05 +00:00
|
|
|
|
2022-09-20 03:48:48 +00:00
|
|
|
ad.subj_cred = subj_cred;
|
2022-09-14 07:20:12 +00:00
|
|
|
ad.common.u.cap = cap;
|
2017-06-09 21:07:02 +00:00
|
|
|
error = fn_for_each_confined(label, profile,
|
2022-09-14 07:20:12 +00:00
|
|
|
profile_capable(profile, cap, opts, &ad));
|
2010-07-29 21:48:05 +00:00
|
|
|
|
2017-06-09 21:07:02 +00:00
|
|
|
return error;
|
2010-07-29 21:48:05 +00:00
|
|
|
}
|