Skip to contentSkip to conversation

selinux: free str on error in str_read()

PATCH In review Ondrej Mosnacek 3 participants 5 messages Apr 14, 2020 – Apr 17, 2020 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: free str on error in str_read()

In [see "Fixes:"] I missed the fact that str_read() may give back an allocated pointer even if it returns an error, causing a potential memory leak in filename_trans_read_one(). Fix this by making the function free the allocated string whenever it returns a non-zero value, which also makes its behavior more obvious and prevents repeating the same mistake in the future.

Addresses-Coverity-ID: 1461665 ("Resource leaks")

Fixes c3a276111ea2 ("selinux: optimize storage of filename transitions")
---
 security/selinux/ss/policydb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
security/selinux/ss/policydb.c +4 -4
index 70ecdc78efbd..c21b922e5ebe 100644
@@ -1035,14 +1035,14 @@ static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
 	if (!str)
 		return -ENOMEM;
 
-	/* it's expected the caller should free the str */
-	*strp = str;
-
 	rc = next_entry(str, fp, len);
-	if (rc)
+	if (rc) {
+		kfree(str);
 		return rc;
+	}
 
 	str[len] = '\0';
+	*strp = str;
 	return 0;
 }
 
Signature · 1 line
2.25.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Ondrej Mosnacek

In [see "Fixes:"] I missed the fact that str_read() may give back an allocated pointer even if it returns an error, causing a potential memory leak in filename_trans_read_one(). Fix this by making the function free the allocated string whenever it returns a non-zero value, which also makes its behavior more obvious and prevents repeating the same mistake in the future.

Addresses-Coverity-ID: 1461665 ("Resource leaks")

Fixes c3a276111ea2 ("selinux: optimize storage of filename transitions")

-Kees

Quoted 30 lines
---
 security/selinux/ss/policydb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
security/selinux/ss/policydb.c +4 -4
index 70ecdc78efbd..c21b922e5ebe 100644
@@ -1035,14 +1035,14 @@ static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
 	if (!str)
 		return -ENOMEM;
 
-	/* it's expected the caller should free the str */
-	*strp = str;
-
 	rc = next_entry(str, fp, len);
-	if (rc)
+	if (rc) {
+		kfree(str);
 		return rc;
+	}
 
 	str[len] = '\0';
+	*strp = str;
 	return 0;
 }
 
Signature · 1 line
2.25.2
Signature · 1 line
Kees Cook
maintainer
#2 lore raw
In reply to Ondrej Mosnacek
Quoted 8 lines · Ondrej Mosnacek

In [see "Fixes:"] I missed the fact that str_read() may give back an allocated pointer even if it returns an error, causing a potential memory leak in filename_trans_read_one(). Fix this by making the function free the allocated string whenever it returns a non-zero value, which also makes its behavior more obvious and prevents repeating the same mistake in the future.

Quoted 7 lines

Addresses-Coverity-ID: 1461665 ("Resource leaks")

Fixes c3a276111ea2 ("selinux: optimize storage of filename transitions")
---
 security/selinux/ss/policydb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

I just merged this into selinux/stable-5.7 and assuming all goes well in testing I'll send this up to Linus later this week. Thanks Ondrej.

I also want to add my thanks to the "coverity bot", thanks Kees. Are you only running this only on Linus tree? If it's open to other trees it might be nice to get the selinux/next branch into the automated testing.

Signature · 2 lines
paul moore
www.paul-moore.com
#3 lore raw
Quoted 17 lines · Paul Moore
Quoted 8 lines · Ondrej Mosnacek

In [see "Fixes:"] I missed the fact that str_read() may give back an allocated pointer even if it returns an error, causing a potential memory leak in filename_trans_read_one(). Fix this by making the function free the allocated string whenever it returns a non-zero value, which also makes its behavior more obvious and prevents repeating the same mistake in the future.

Quoted 7 lines

Addresses-Coverity-ID: 1461665 ("Resource leaks")

Fixes c3a276111ea2 ("selinux: optimize storage of filename transitions")
---
 security/selinux/ss/policydb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 7 lines

I just merged this into selinux/stable-5.7 and assuming all goes well in testing I'll send this up to Linus later this week. Thanks Ondrej.

I also want to add my thanks to the "coverity bot", thanks Kees. Are you only running this only on Linus tree? If it's open to other trees it might be nice to get the selinux/next branch into the automated testing.

It's being run on linux-next. The free coverity scanner barely has the resources is keep up with one tree, so I just feed it -next. They were kind enough to let us upload daily now, so I've been trying to feed the emailed reports back. It's all just the tip of the iceberg, of course.

Signature · 1 line
Kees Cook
maintainer
#4 lore raw
Quoted 26 lines · Kees Cook
Quoted 17 lines · Paul Moore
Quoted 8 lines · Ondrej Mosnacek

In [see "Fixes:"] I missed the fact that str_read() may give back an allocated pointer even if it returns an error, causing a potential memory leak in filename_trans_read_one(). Fix this by making the function free the allocated string whenever it returns a non-zero value, which also makes its behavior more obvious and prevents repeating the same mistake in the future.

Quoted 7 lines

Addresses-Coverity-ID: 1461665 ("Resource leaks")

Fixes c3a276111ea2 ("selinux: optimize storage of filename transitions")
---
 security/selinux/ss/policydb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 7 lines

I just merged this into selinux/stable-5.7 and assuming all goes well in testing I'll send this up to Linus later this week. Thanks Ondrej.

I also want to add my thanks to the "coverity bot", thanks Kees. Are you only running this only on Linus tree? If it's open to other trees it might be nice to get the selinux/next branch into the automated testing.

Quoted 4 lines

It's being run on linux-next. The free coverity scanner barely has the resources is keep up with one tree, so I just feed it -next. They were kind enough to let us upload daily now, so I've been trying to feed the emailed reports back. It's all just the tip of the iceberg, of course.

Ah, okay, thanks. I had wondered about doing regular coverity runs for the SELinux/audit kernel code but was scared off by the limits; it looks like that wasn't an unwarranted fear.

Regardless, thanks for setting this up and running it on linux-next.

Signature · 2 lines
paul moore
www.paul-moore.com