Skip to contentSkip to conversation

selinux: fix bad cleanup on error in hashtab_duplicate()

PATCH In review Ondrej Mosnacek 2 participants 2 messages May 17, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: fix bad cleanup on error in hashtab_duplicate()

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")
---
 security/selinux/ss/hashtab.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
security/selinux/ss/hashtab.c +2 -1
index 0ae4e4e57a401..3fb8f9026e9be 100644
@@ -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;
 }
 
Signature · 1 line
2.36.1
maintainer
#1 lore raw
Quoted 6 lines · Ondrej Mosnacek

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).

Quoted 6 lines
Fixes c7c556f1e81b ("selinux: refactor changing booleans")
---
 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.

Signature · 1 line
paul-moore.com