Skip to contentSkip to conversation

selinux: fix another double free

PATCH Superseded [email protected] 3 participants 3 messages Jun 11, 2020 SELinux security module

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: fix another double free

From: Tom Rix <[email protected]>

Clang static analysis reports this double free error

security/selinux/ss/conditional.c:139:2: warning: Attempt to free released memory [unix.Malloc]

        kfree(node->expr.nodes);
        ^~~~~~~~~~~~~~~~~~~~~~~

When cond_read_node fails, it calls cond_node_destroy which frees the node but does not poison the entry in the node list. So when it returns to its caller cond_read_list, cond_read_list deletes the partial list. The latest entry in the list will be deleted twice.

So instead of freeing the node in cond_read_node, let list freeing in code_read_list handle the freeing the problem node along with all of the the earlier nodes.

---
 security/selinux/ss/conditional.c | 1 -
 1 file changed, 1 deletion(-)
security/selinux/ss/conditional.c +0 -1
index da94a1b4bfda..ffb31af22f4f 100644
@@ -411,7 +411,6 @@ static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp)
 		goto err;
 	return 0;
 err:
-	cond_node_destroy(node);
 	return rc;
 }
 
Signature · 1 line
2.18.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 14 lines · On Thu, Jun 11, 2020 at 11:58 AM <[email protected]> wrote:

From: Tom Rix <[email protected]>

Clang static analysis reports this double free error

security/selinux/ss/conditional.c:139:2: warning: Attempt to free released memory [unix.Malloc]

        kfree(node->expr.nodes);
        ^~~~~~~~~~~~~~~~~~~~~~~

When cond_read_node fails, it calls cond_node_destroy which frees the node but does not poison the entry in the node list. So when it returns to its caller cond_read_list, cond_read_list deletes the partial list. The latest entry in the list will be deleted twice.

Quoted 5 lines

So instead of freeing the node in cond_read_node, let list freeing in code_read_list handle the freeing the problem node along with all of the the earlier nodes.

Looks like this was introduced by 60abd3181db29ea81742106cc0ac2e27fd05b418 ("selinux: convert cond_list to array").

#2 lore raw
In reply to [email protected]

Hi Tom,

Quoted 8 lines · On Thu, Jun 11, 2020 at 5:58 PM <[email protected]> wrote:

From: Tom Rix <[email protected]>

Clang static analysis reports this double free error

security/selinux/ss/conditional.c:139:2: warning: Attempt to free released memory [unix.Malloc]

        kfree(node->expr.nodes);
        ^~~~~~~~~~~~~~~~~~~~~~~
Quoted 7 lines

When cond_read_node fails, it calls cond_node_destroy which frees the node but does not poison the entry in the node list. So when it returns to its caller cond_read_list, cond_read_list deletes the partial list. The latest entry in the list will be deleted twice.

So instead of freeing the node in cond_read_node, let list freeing in code_read_list handle the freeing the problem node along with all of the the

There is an extra "the" before "freeing".

Quoted 12 lines

earlier nodes.

---
 security/selinux/ss/conditional.c | 1 -
 1 file changed, 1 deletion(-)
security/selinux/ss/conditional.c +0 -0
index da94a1b4bfda..ffb31af22f4f 100644

security/selinux/ss/conditional.c static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp) · line 414

Quoted 5 lines
security/selinux/ss/conditional.c +0 -1
@@ -411,7 +411,6 @@ static int cond_read_node(struct policydb *p, struct cond_node *node, void *fp)
                goto err;
        return 0;
 err:
-       cond_node_destroy(node);
        return rc;

Since there is now just "return rc" in the error path, can you please replace all the gotos with plain return statements? And please also add a Fixes: tag pointing to the commit that introduced the bug (see Stephen's reply).

Thanks,

Signature · 3 lines
Ondrej Mosnacek
Software Engineer, Platform Security - SELinux kernel
Red Hat, Inc.