Skip to contentSkip to conversation

checkpolicy: fix double free of fstype in define_genfs_context_helper

PATCH In review shafiuzzaman-md 3 participants 3 messages Jul 10 – Jul 15 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpolicy: fix double free of fstype in define_genfs_context_helper

From: Md Shafiuzzaman <[email protected]>

When parsing a new filesystem type, define_genfs_context_helper() transfers ownership of fstype into a genfs struct linked into the policydb's genfs list, but does not clear the local fstype. A later goto fail then frees it, leaving a dangling pointer that policydb_destroy() frees again.

Clear fstype after the ownership transfer, mirroring the else branch.

---
 checkpolicy/policy_define.c | 2 ++
 1 file changed, 2 insertions(+)
checkpolicy/policy_define.c +2 -0
index 982de3f..3015782 100644
@@ -6054,6 +6054,8 @@ static int define_genfs_context_helper(char *fstype, int has_type)
 			return -1;
 		}
 		newgenfs->fstype = fstype;
+		/* ownership transferred into the genfs list; do not free on error */
+		fstype = NULL;
 		newgenfs->next = genfs;
 		if (genfs_p)
 			genfs_p->next = newgenfs;
Signature · 1 line
2.54.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · shafiuzzaman-md

From: Md Shafiuzzaman <[email protected]>

When parsing a new filesystem type, define_genfs_context_helper() transfers ownership of fstype into a genfs struct linked into the policydb's genfs list, but does not clear the local fstype. A later goto fail then frees it, leaving a dangling pointer that policydb_destroy() frees again.

Quoted 4 lines

Clear fstype after the ownership transfer, mirroring the else branch.

Quoted 20 lines
---
 checkpolicy/policy_define.c | 2 ++
 1 file changed, 2 insertions(+)
checkpolicy/policy_define.c +2 -1
index 982de3f..3015782 100644
@@ -6054,6 +6054,8 @@ static int define_genfs_context_helper(char *fstype, int has_type)
                        return -1;
                }
                newgenfs->fstype = fstype;
+               /* ownership transferred into the genfs list; do not free on error */
+               fstype = NULL;
                newgenfs->next = genfs;
                if (genfs_p)
                        genfs_p->next = newgenfs;
--

2.54.0

#2 lore raw
Quoted 11 lines · Stephen Smalley
Quoted 8 lines · shafiuzzaman-md

From: Md Shafiuzzaman <[email protected]>

When parsing a new filesystem type, define_genfs_context_helper() transfers ownership of fstype into a genfs struct linked into the policydb's genfs list, but does not clear the local fstype. A later goto fail then frees it, leaving a dangling pointer that policydb_destroy() frees again.

Quoted 7 lines
Quoted 4 lines

Clear fstype after the ownership transfer, mirroring the else branch.

This has been merged.
Thanks,
Jim

Quoted 21 lines
Quoted 20 lines
---
 checkpolicy/policy_define.c | 2 ++
 1 file changed, 2 insertions(+)
checkpolicy/policy_define.c +2 -1
index 982de3f..3015782 100644
@@ -6054,6 +6054,8 @@ static int define_genfs_context_helper(char *fstype, int has_type)
                        return -1;
                }
                newgenfs->fstype = fstype;
+               /* ownership transferred into the genfs list; do not free on error */
+               fstype = NULL;
                newgenfs->next = genfs;
                if (genfs_p)
                        genfs_p->next = newgenfs;
--

2.54.0