diff mbox series

selinux: fix bad cleanup on error in hashtab_duplicate()

Message ID 20220517120816.769108-1-omosnace@redhat.com (mailing list archive)
State Accepted
Delegated to: Paul Moore
Headers show
Series selinux: fix bad cleanup on error in hashtab_duplicate() | expand

Commit Message

Ondrej Mosnacek May 17, 2022, 12:08 p.m. UTC
The code attempts to free the 'new' pointer using kmem_cache_free(),
which is wrong because this function isn't responsible of freeing it.
Instead, the function should free new->htable and clear the contents of
*new (to prevent double-free).

Fixes: c7c556f1e81b ("selinux: refactor changing booleans")
Reported-by: Wander Lairson Costa <wander@redhat.com>
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
---
 security/selinux/ss/hashtab.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Paul Moore May 17, 2022, 10:39 p.m. UTC | #1
On Tue, May 17, 2022 at 8:08 AM Ondrej Mosnacek <omosnace@redhat.com> wrote:
>
> The code attempts to free the 'new' pointer using kmem_cache_free(),
> which is wrong because this function isn't responsible of freeing it.
> Instead, the function should free new->htable and clear the contents of
> *new (to prevent double-free).
>
> Fixes: c7c556f1e81b ("selinux: refactor changing booleans")
> Reported-by: Wander Lairson Costa <wander@redhat.com>
> Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
> ---
>  security/selinux/ss/hashtab.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)

Good catch, merged into selinux/stable-5.18 and I'll plan on sending
this to Linus tomorrow once the tests finish running.
diff mbox series

Patch

diff --git a/security/selinux/ss/hashtab.c b/security/selinux/ss/hashtab.c
index 0ae4e4e57a401..3fb8f9026e9be 100644
--- a/security/selinux/ss/hashtab.c
+++ b/security/selinux/ss/hashtab.c
@@ -179,7 +179,8 @@  int hashtab_duplicate(struct hashtab *new, struct hashtab *orig,
 			kmem_cache_free(hashtab_node_cachep, cur);
 		}
 	}
-	kmem_cache_free(hashtab_node_cachep, new);
+	kfree(new->htable);
+	memset(new, 0, sizeof(*new));
 	return -ENOMEM;
 }