Skip to content

Commit 7b94c99

Browse files
pcmooregregkh
authored andcommitted
selinux: ensure we cleanup the internal AVC counters on error in avc_insert()
[ Upstream commit d8db60c ] Fix avc_insert() to call avc_node_kill() if we've already allocated an AVC node and the code fails to insert the node in the cache. Fixes: fa1aa14 ("selinux: extended permissions for ioctls") Reported-by: rsiddoji@codeaurora.org Suggested-by: Stephen Smalley <sds@tycho.nsa.gov> Acked-by: Stephen Smalley <sds@tycho.nsa.gov> Signed-off-by: Paul Moore <paul@paul-moore.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 06389f6 commit 7b94c99

1 file changed

Lines changed: 24 additions & 27 deletions

File tree

‎security/selinux/avc.c‎

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -617,40 +617,37 @@ static struct avc_node *avc_insert(struct selinux_avc *avc,
617617
struct avc_node *pos, *node = NULL;
618618
int hvalue;
619619
unsigned long flag;
620+
spinlock_t *lock;
621+
struct hlist_head *head;
620622

621623
if (avc_latest_notif_update(avc, avd->seqno, 1))
622-
goto out;
624+
return NULL;
623625

624626
node = avc_alloc_node(avc);
625-
if (node) {
626-
struct hlist_head *head;
627-
spinlock_t *lock;
628-
int rc = 0;
629-
630-
hvalue = avc_hash(ssid, tsid, tclass);
631-
avc_node_populate(node, ssid, tsid, tclass, avd);
632-
rc = avc_xperms_populate(node, xp_node);
633-
if (rc) {
634-
kmem_cache_free(avc_node_cachep, node);
635-
return NULL;
636-
}
637-
head = &avc->avc_cache.slots[hvalue];
638-
lock = &avc->avc_cache.slots_lock[hvalue];
627+
if (!node)
628+
return NULL;
639629

640-
spin_lock_irqsave(lock, flag);
641-
hlist_for_each_entry(pos, head, list) {
642-
if (pos->ae.ssid == ssid &&
643-
pos->ae.tsid == tsid &&
644-
pos->ae.tclass == tclass) {
645-
avc_node_replace(avc, node, pos);
646-
goto found;
647-
}
630+
avc_node_populate(node, ssid, tsid, tclass, avd);
631+
if (avc_xperms_populate(node, xp_node)) {
632+
avc_node_kill(avc, node);
633+
return NULL;
634+
}
635+
636+
hvalue = avc_hash(ssid, tsid, tclass);
637+
head = &avc->avc_cache.slots[hvalue];
638+
lock = &avc->avc_cache.slots_lock[hvalue];
639+
spin_lock_irqsave(lock, flag);
640+
hlist_for_each_entry(pos, head, list) {
641+
if (pos->ae.ssid == ssid &&
642+
pos->ae.tsid == tsid &&
643+
pos->ae.tclass == tclass) {
644+
avc_node_replace(avc, node, pos);
645+
goto found;
648646
}
649-
hlist_add_head_rcu(&node->list, head);
650-
found:
651-
spin_unlock_irqrestore(lock, flag);
652647
}
653-
out:
648+
hlist_add_head_rcu(&node->list, head);
649+
found:
650+
spin_unlock_irqrestore(lock, flag);
654651
return node;
655652
}
656653

0 commit comments

Comments
 (0)