Files
GUO Zihua 20f82769cd ima: Avoid blocking in RCU read-side critical section
BugLink: https://bugs.launchpad.net/bugs/2075154

commit 9a95c5bfbf02a0a7f5983280fe284a0ff0836c34 upstream.

A panic happens in ima_match_policy:

BUG: unable to handle kernel NULL pointer dereference at 0000000000000010
PGD 42f873067 P4D 0
Oops: 0000 [#1] SMP NOPTI
CPU: 5 PID: 1286325 Comm: kubeletmonit.sh
Kdump: loaded Tainted: P
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996),
               BIOS 0.0.0 02/06/2015
RIP: 0010:ima_match_policy+0x84/0x450
Code: 49 89 fc 41 89 cf 31 ed 89 44 24 14 eb 1c 44 39
      7b 18 74 26 41 83 ff 05 74 20 48 8b 1b 48 3b 1d
      f2 b9 f4 00 0f 84 9c 01 00 00 <44> 85 73 10 74 ea
      44 8b 6b 14 41 f6 c5 01 75 d4 41 f6 c5 02 74 0f
RSP: 0018:ff71570009e07a80 EFLAGS: 00010207
RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000200
RDX: ffffffffad8dc7c0 RSI: 0000000024924925 RDI: ff3e27850dea2000
RBP: 0000000000000000 R08: 0000000000000000 R09: ffffffffabfce739
R10: ff3e27810cc42400 R11: 0000000000000000 R12: ff3e2781825ef970
R13: 00000000ff3e2785 R14: 000000000000000c R15: 0000000000000001
FS:  00007f5195b51740(0000)
GS:ff3e278b12d40000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000000000000010 CR3: 0000000626d24002 CR4: 0000000000361ee0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Call Trace:
 ima_get_action+0x22/0x30
 process_measurement+0xb0/0x830
 ? page_add_file_rmap+0x15/0x170
 ? alloc_set_pte+0x269/0x4c0
 ? prep_new_page+0x81/0x140
 ? simple_xattr_get+0x75/0xa0
 ? selinux_file_open+0x9d/0xf0
 ima_file_check+0x64/0x90
 path_openat+0x571/0x1720
 do_filp_open+0x9b/0x110
 ? page_counter_try_charge+0x57/0xc0
 ? files_cgroup_alloc_fd+0x38/0x60
 ? __alloc_fd+0xd4/0x250
 ? do_sys_open+0x1bd/0x250
 do_sys_open+0x1bd/0x250
 do_syscall_64+0x5d/0x1d0
 entry_SYSCALL_64_after_hwframe+0x65/0xca

Commit c7423dbdbc ("ima: Handle -ESTALE returned by
ima_filter_rule_match()") introduced call to ima_lsm_copy_rule within a
RCU read-side critical section which contains kmalloc with GFP_KERNEL.
This implies a possible sleep and violates limitations of RCU read-side
critical sections on non-PREEMPT systems.

Sleeping within RCU read-side critical section might cause
synchronize_rcu() returning early and break RCU protection, allowing a
UAF to happen.

The root cause of this issue could be described as follows:
|	Thread A	|	Thread B	|
|			|ima_match_policy	|
|			|  rcu_read_lock	|
|ima_lsm_update_rule	|			|
|  synchronize_rcu	|			|
|			|    kmalloc(GFP_KERNEL)|
|			|      sleep		|
==> synchronize_rcu returns early
|  kfree(entry)		|			|
|			|    entry = entry->next|
==> UAF happens and entry now becomes NULL (or could be anything).
|			|    entry->action	|
==> Accessing entry might cause panic.

To fix this issue, we are converting all kmalloc that is called within
RCU read-side critical section to use GFP_ATOMIC.

Fixes: c7423dbdbc ("ima: Handle -ESTALE returned by ima_filter_rule_match()")
Cc: stable@vger.kernel.org
Signed-off-by: GUO Zihua <guozihua@huawei.com>
Acked-by: John Johansen <john.johansen@canonical.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
[PM: fixed missing comment, long lines, !CONFIG_IMA_LSM_RULES case]
Signed-off-by: Paul Moore <paul@paul-moore.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Manuel Diewald <manuel.diewald@canonical.com>
Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
Signed-off-by: Roxana Nicolescu <roxana.nicolescu@canonical.com>
2024-08-13 12:13:03 +02:00

559 lines
12 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
/*
* AppArmor security module
*
* This file contains AppArmor auditing functions
*
* Copyright (C) 1998-2008 Novell/SUSE
* Copyright 2009-2010 Canonical Ltd.
*/
#include <linux/audit.h>
#include <linux/socket.h>
#include "include/apparmor.h"
#include "include/audit.h"
#include "include/policy.h"
#include "include/policy_ns.h"
#include "include/secid.h"
const char *const audit_mode_names[] = {
"normal",
"quiet_denied",
"quiet",
"noquiet",
"all"
};
static const char *const aa_audit_type[] = {
"AUDIT",
"ALLOWED",
"DENIED",
"HINT",
"STATUS",
"ERROR",
"KILLED",
"AUTO"
};
static const char *const aa_class_names[] = {
"none",
"unknown",
"file",
"cap",
"net",
"rlimits",
"domain",
"mount",
"unknown",
"ptrace",
"signal",
"xmatch",
"unknown",
"unknown",
"net",
"unknown",
"label",
"posix_mqueue",
"io_uring",
"module",
"lsm",
"namespace",
"io_uring",
"unknown",
"unknown",
"unknown",
"unknown",
"unknown",
"unknown",
"unknown",
"unknown",
"X",
"dbus",
};
/*
* Currently AppArmor auditing is fed straight into the audit framework.
*
* TODO:
* netlink interface for complain mode
* user auditing, - send user auditing to netlink interface
* system control of whether user audit messages go to system log
*/
/**
* audit_pre() - core AppArmor function.
* @ab: audit buffer to fill (NOT NULL)
* @va: audit structure containing data to audit (NOT NULL)
*
* Record common AppArmor audit data from @va
*/
static void audit_pre(struct audit_buffer *ab, void *va)
{
struct apparmor_audit_data *ad = aad_of_va(va);
if (aa_g_audit_header) {
audit_log_format(ab, "apparmor=\"%s\"",
aa_audit_type[ad->type]);
}
if (ad->op)
audit_log_format(ab, " operation=\"%s\"", ad->op);
if (ad->class)
audit_log_format(ab, " class=\"%s\"",
ad->class <= AA_CLASS_LAST ?
aa_class_names[ad->class] :
"unknown");
if (ad->info) {
audit_log_format(ab, " info=\"%s\"", ad->info);
if (ad->error)
audit_log_format(ab, " error=%d", ad->error);
}
if (ad->subj_label) {
struct aa_label *label = ad->subj_label;
if (label_isprofile(label)) {
struct aa_profile *profile = labels_profile(label);
if (profile->ns != root_ns) {
audit_log_format(ab, " namespace=");
audit_log_untrustedstring(ab,
profile->ns->base.hname);
}
audit_log_format(ab, " profile=");
audit_log_untrustedstring(ab, profile->base.hname);
} else {
audit_log_format(ab, " label=");
aa_label_xaudit(ab, root_ns, label, FLAG_VIEW_SUBNS,
GFP_ATOMIC);
}
}
if (ad->name) {
audit_log_format(ab, " name=");
audit_log_untrustedstring(ab, ad->name);
}
}
/**
* aa_audit_msg - Log a message to the audit subsystem
* @type: audit type for the message
* @ad: audit event structure (NOT NULL)
* @cb: optional callback fn for type specific fields (MAYBE NULL)
*/
void aa_audit_msg(int type, struct apparmor_audit_data *ad,
void (*cb) (struct audit_buffer *, void *))
{
ad->type = type;
common_lsm_audit(&ad->common, audit_pre, cb);
}
/**
* aa_audit - Log a profile based audit event to the audit subsystem
* @type: audit type for the message
* @profile: profile to check against (NOT NULL)
* @ad: audit event (NOT NULL)
* @cb: optional callback fn for type specific fields (MAYBE NULL)
*
* Handle default message switching based off of audit mode flags
*
* Returns: error on failure
*/
int aa_audit(int type, struct aa_profile *profile,
struct apparmor_audit_data *ad,
void (*cb) (struct audit_buffer *, void *))
{
AA_BUG(!profile);
if (type == AUDIT_APPARMOR_AUTO) {
if (likely(!ad->error)) {
if (AUDIT_MODE(profile) != AUDIT_ALL)
return 0;
type = AUDIT_APPARMOR_AUDIT;
} else if (COMPLAIN_MODE(profile))
type = AUDIT_APPARMOR_ALLOWED;
else
type = AUDIT_APPARMOR_DENIED;
}
if (AUDIT_MODE(profile) == AUDIT_QUIET ||
(type == AUDIT_APPARMOR_DENIED &&
AUDIT_MODE(profile) == AUDIT_QUIET_DENIED))
return ad->error;
if (KILL_MODE(profile) && type == AUDIT_APPARMOR_DENIED)
type = AUDIT_APPARMOR_KILL;
ad->subj_label = &profile->label;
aa_audit_msg(type, ad, cb);
if (ad->type == AUDIT_APPARMOR_KILL)
(void)send_sig_info(profile->signal, NULL,
ad->common.type == LSM_AUDIT_DATA_TASK &&
ad->common.u.tsk ? ad->common.u.tsk : current);
if (ad->type == AUDIT_APPARMOR_ALLOWED)
return complain_error(ad->error);
return ad->error;
}
struct aa_audit_rule {
struct aa_label *label;
};
/****************************** audit rules *******************************/
void aa_audit_rule_free(void *vrule, int lsmid)
{
struct aa_audit_rule *rule = vrule;
if (lsmid != LSM_ID_UNDEF || lsmid != LSM_ID_APPARMOR)
return;
if (rule) {
if (!IS_ERR(rule->label))
aa_put_label(rule->label);
kfree(rule);
}
}
int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule,
int lsmid, gfp_t gfp)
{
struct aa_audit_rule *rule;
if (lsmid != LSM_ID_UNDEF || lsmid != LSM_ID_APPARMOR)
return 0;
switch (field) {
case AUDIT_SUBJ_ROLE:
if (op != Audit_equal && op != Audit_not_equal)
return -EINVAL;
break;
default:
return -EINVAL;
}
rule = kzalloc(sizeof(struct aa_audit_rule), gfp);
if (!rule)
return -ENOMEM;
/* Currently rules are treated as coming from the root ns */
rule->label = aa_label_parse(&root_ns->unconfined->label, rulestr,
gfp, true, false);
if (IS_ERR(rule->label)) {
int err = PTR_ERR(rule->label);
aa_audit_rule_free(rule, LSM_ID_APPARMOR);
return err;
}
*vrule = rule;
return 0;
}
int aa_audit_rule_known(struct audit_krule *rule)
{
int i;
for (i = 0; i < rule->field_count; i++) {
struct audit_field *f = &rule->fields[i];
switch (f->type) {
case AUDIT_SUBJ_ROLE:
return 1;
}
}
return 0;
}
int aa_audit_rule_match(struct lsmblob *blob, u32 field, u32 op, void *vrule,
int lsmid)
{
struct aa_audit_rule *rule = vrule;
struct aa_label *label;
int found = 0;
if (lsmid != LSM_ID_UNDEF || lsmid != LSM_ID_APPARMOR)
return 0;
label = blob->apparmor.label;
if (!label)
return -ENOENT;
if (aa_label_is_subset(label, rule->label))
found = 1;
switch (field) {
case AUDIT_SUBJ_ROLE:
switch (op) {
case Audit_equal:
return found;
case Audit_not_equal:
return !found;
}
}
return 0;
}
/****************************** audit cache *******************************/
static int uid_cmp(kuid_t lhs, kuid_t rhs)
{
if (uid_lt(lhs, rhs))
return -1;
if (uid_gt(lhs, rhs))
return 1;
return 0;
}
/* std C cmp. negative is less than, 0 is equal, positive greater than */
long aa_audit_data_cmp(struct apparmor_audit_data *lhs,
struct apparmor_audit_data *rhs)
{
long res;
/* don't compare type */
res = lhs->class - rhs->class;
if (res)
return res;
/* don't compare op */
if (lhs->flags & AUDIT_TAILGLOB_NAME)
/* lhs glob matches strings longer than it */
res = strncmp(lhs->name, rhs->name, strlen(lhs->name));
else
res = strcmp(lhs->name, rhs->name);
if (res)
return res;
res = aa_label_cmp(lhs->subj_label, rhs->subj_label);
if (res)
return res;
switch (lhs->class) {
case AA_CLASS_FILE:
if (lhs->subj_cred) {
if (rhs->subj_cred) {
return uid_cmp(lhs->subj_cred->fsuid,
rhs->subj_cred->fsuid);
} else {
return 1;
}
} else if (rhs->subj_cred) {
return -1;
}
res = uid_cmp(lhs->fs.ouid, rhs->fs.ouid);
if (res)
return res;
res = lhs->fs.target - rhs->fs.target;
if (res)
return res;
}
return 0;
}
static void audit_node_free(struct aa_audit_node *node)
{
if (!node)
return;
AA_BUG(!list_empty(&node->list));
/* common data that needs freed */
kfree(node->data.name);
aa_put_label(node->data.subj_label);
if (node->data.subj_cred)
put_cred(node->data.subj_cred);
/* class specific data that needs freed */
switch (node->data.class) {
case AA_CLASS_FILE:
aa_put_label(node->data.peer);
kfree(node->data.fs.target);
}
kmem_cache_free(aa_audit_slab, node);
}
void aa_audit_node_free_kref(struct kref *kref)
{
struct aa_audit_node *node = container_of(kref, struct aa_audit_node,
count);
audit_node_free(node);
}
struct aa_audit_node *aa_dup_audit_data(struct apparmor_audit_data *orig,
gfp_t gfp)
{
struct aa_audit_node *copy;
copy = kmem_cache_zalloc(aa_audit_slab, gfp);
if (!copy)
return NULL;
kref_init(&copy->count);
copy->knotif.ad = &copy->data;
INIT_LIST_HEAD(&copy->list);
/* copy class early so aa_free_audit_node can use switch on failure */
copy->data.class = orig->class;
copy->data.flags = orig->flags;
/* handle anything with possible failure first */
if (orig->name) {
copy->data.name = kstrdup(orig->name, gfp);
if (!copy->data.name)
goto fail;
}
/* don't dup info */
switch (orig->class) {
case AA_CLASS_FILE:
if (orig->fs.target) {
copy->data.fs.target = kstrdup(orig->fs.target, gfp);
if (!copy->data.fs.target)
goto fail;
}
break;
case AA_CLASS_MOUNT:
if (orig->mnt.src_name) {
copy->data.mnt.src_name = kstrdup(orig->mnt.src_name, gfp);
if (!copy->data.mnt.src_name)
goto fail;
}
if (orig->mnt.type) {
copy->data.mnt.type = kstrdup(orig->mnt.type, gfp);
if (!copy->data.mnt.type)
goto fail;
}
// copy->mnt.trans; not used atm
if (orig->mnt.data) {
copy->data.mnt.data = kstrdup(orig->mnt.data, gfp);
if (!copy->data.mnt.data)
goto fail;
}
break;
}
/* now inc counts, and copy data that can't fail */
copy->data.error = orig->error;
copy->data.type = orig->type;
copy->data.request = orig->request;
copy->data.denied = orig->denied;
copy->data.subj_label = aa_get_label(orig->subj_label);
if (orig->subj_cred)
copy->data.subj_cred = get_cred(orig->subj_cred);
switch (orig->class) {
case AA_CLASS_NET:
/*
* peer_sk;
* addr;
*/
fallthrough;
case AA_CLASS_FILE:
copy->data.fs.ouid = orig->fs.ouid;
break;
case AA_CLASS_RLIMITS:
case AA_CLASS_SIGNAL:
case AA_CLASS_POSIX_MQUEUE:
copy->data.peer = aa_get_label(orig->peer);
break;
/*
* case AA_CLASS_IFACE:
* copy->data.iface.profile = aa_get_label(orig.iface.profile);
* break;
*/
};
return copy;
fail:
audit_node_free(copy);
return NULL;
}
#define __audit_cache_find(C, AD, COND...) \
({ \
struct aa_audit_node *__node; \
list_for_each_entry_rcu(__node, &(C)->head, list, COND) { \
if (aa_audit_data_cmp(&__node->data, AD) == 0) \
goto __out_skip; \
} \
__node = NULL; \
__out_skip: \
__node; \
})
// increments refcount on node
struct aa_audit_node *aa_audit_cache_find(struct aa_audit_cache *cache,
struct apparmor_audit_data *ad)
{
struct aa_audit_node *node;
rcu_read_lock();
node = __audit_cache_find(cache, ad);
aa_get_audit_node(node);
rcu_read_unlock();
return node;
}
/**
* aa_audit_cache_insert - insert an audit node into the cache
* @cache: the cache to insert into
* @node: the audit node to insert into the cache
*
* Returns: refcounted matching node in cache OR @node if @node was inserted.
*
* Increments refcount on node if successfully inserted. Assumes caller
* already has valid ref count.
* Increments refcount on existing node if returned
*/
struct aa_audit_node *aa_audit_cache_insert(struct aa_audit_cache *cache,
struct aa_audit_node *node)
{
struct aa_audit_node *tmp;
spin_lock(&cache->lock);
tmp = __audit_cache_find(cache, &node->data,
spin_is_lock(&cache->lock));
if (!tmp) {
list_add_rcu(&node->list, &cache->head);
tmp = node;
cache->size++;
}
aa_get_audit_node(tmp);
/* else raced another insert */
spin_unlock(&cache->lock);
return tmp;
}
void aa_audit_cache_update_ent(struct aa_audit_cache *cache,
struct aa_audit_node *node,
struct apparmor_audit_data *data)
{
spin_lock(&cache->lock);
node->data.denied |= data->denied;
node->data.request = (node->data.request | data->request) &
~node->data.denied;
spin_unlock(&cache->lock);
}
/* assumes rcu callback has already happened and list can not be walked */
void aa_audit_cache_destroy(struct aa_audit_cache *cache)
{
struct aa_audit_node *node, *tmp;
list_for_each_entry_safe(node, tmp, &cache->head, list) {
list_del_init(&node->list);
aa_put_audit_node(node);
}
cache->size = 0;
}