Skip to contentSkip to conversation

selinux: fix memory leak in sel_make_bools

PATCH Superseded Xiaotian Feng 4 participants 8 messages Aug 13, 2009 – Feb 8, 2010 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: fix memory leak in sel_make_bools

In sel_make_bools, kernel allocates memory for bool_pending_names[i] with security_get_bools. So if we just free bool_pending_names, those memories for bool_pending_names[i] will be leaked.

This patch resolves dozens of following kmemleak report after resuming from suspend:
unreferenced object 0xffff88022e4c7380 (size 32):
comm "init", pid 1, jiffies 4294677173
backtrace:

    [<ffffffff810f76b5>] create_object+0x1a2/0x2a9
    [<ffffffff810f78bb>] kmemleak_alloc+0x26/0x4b
    [<ffffffff810ef3eb>] __kmalloc+0x18f/0x1b8
    [<ffffffff811cd511>] security_get_bools+0xd7/0x16f
    [<ffffffff811c48c0>] sel_write_load+0x12e/0x62b
    [<ffffffff810f9a39>] vfs_write+0xae/0x10b
    [<ffffffff810f9b56>] sys_write+0x4a/0x6e
    [<ffffffff81011b82>] system_call_fastpath+0x16/0x1b
    [<ffffffffffffffff>] 0xffffffffffffffff
---
 security/selinux/selinuxfs.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
security/selinux/selinuxfs.c +5 -1
index b4fc506..ab93472 100644
@@ -979,7 +979,11 @@ static int sel_make_bools(void)
 	u32 sid;
 
 	/* remove any existing files */
-	kfree(bool_pending_names);
+	if (bool_pending_names) {
+		for (i = 0; i < bool_num; i++)
+			kfree(bool_pending_names[i]);
+		kfree(bool_pending_names);
+	}
 	kfree(bool_pending_values);
 	bool_pending_names = NULL;
 	bool_pending_values = NULL;
Signature · 1 line
1.6.2.5
#1 lore raw
Quoted 31 lines · Xiaotian Feng

In sel_make_bools, kernel allocates memory for bool_pending_names[i] with security_get_bools. So if we just free bool_pending_names, those memories for bool_pending_names[i] will be leaked.

This patch resolves dozens of following kmemleak report after resuming from suspend:
unreferenced object 0xffff88022e4c7380 (size 32):
comm "init", pid 1, jiffies 4294677173
backtrace:

    [<ffffffff810f76b5>] create_object+0x1a2/0x2a9
    [<ffffffff810f78bb>] kmemleak_alloc+0x26/0x4b
    [<ffffffff810ef3eb>] __kmalloc+0x18f/0x1b8
    [<ffffffff811cd511>] security_get_bools+0xd7/0x16f
    [<ffffffff811c48c0>] sel_write_load+0x12e/0x62b
    [<ffffffff810f9a39>] vfs_write+0xae/0x10b
    [<ffffffff810f9b56>] sys_write+0x4a/0x6e
    [<ffffffff81011b82>] system_call_fastpath+0x16/0x1b
    [<ffffffffffffffff>] 0xffffffffffffffff
---
 security/selinux/selinuxfs.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
security/selinux/selinuxfs.c +0 -0
index b4fc506..ab93472 100644
@@ -979,7 +979,11 @@ static int sel_make_bools(void)
 	u32 sid;
 

security/selinux/selinuxfs.c static int sel_make_bools(void) · line 989

Quoted 10 lines
security/selinux/selinuxfs.c +5 -1
 	/* remove any existing files */
-	kfree(bool_pending_names);
+	if (bool_pending_names) {
+		for (i = 0; i < bool_num; i++)
+			kfree(bool_pending_names[i]);
+		kfree(bool_pending_names);
+	}
 	kfree(bool_pending_values);
 	bool_pending_names = NULL;
 	bool_pending_values = NULL;

Since the code seems to rely on 'bool_num' in other places to ensure we don't walk off the end of the array it is probably safe to omit the 'if (bool_pending_names) ...' conditional and just rely on the for loop to do the right thing.

Signature · 2 lines
paul moore
linux @ hp
author
#2 lore raw
Quoted 32 lines · Paul Moore
Quoted 31 lines · Xiaotian Feng

In sel_make_bools, kernel allocates memory for bool_pending_names[i] with security_get_bools. So if we just free bool_pending_names, those memories for bool_pending_names[i] will be leaked.

This patch resolves dozens of following kmemleak report after resuming from suspend:
unreferenced object 0xffff88022e4c7380 (size 32):

   comm "init", pid 1, jiffies 4294677173
   backtrace:
     [<ffffffff810f76b5>] create_object+0x1a2/0x2a9
     [<ffffffff810f78bb>] kmemleak_alloc+0x26/0x4b
     [<ffffffff810ef3eb>] __kmalloc+0x18f/0x1b8
     [<ffffffff811cd511>] security_get_bools+0xd7/0x16f
     [<ffffffff811c48c0>] sel_write_load+0x12e/0x62b
     [<ffffffff810f9a39>] vfs_write+0xae/0x10b
     [<ffffffff810f9b56>] sys_write+0x4a/0x6e
     [<ffffffff81011b82>] system_call_fastpath+0x16/0x1b
     [<ffffffffffffffff>] 0xffffffffffffffff
---
  security/selinux/selinuxfs.c |    6 +++++-
  1 files changed, 5 insertions(+), 1 deletions(-)
security/selinux/selinuxfs.c +0 -0
index b4fc506..ab93472 100644
@@ -979,7 +979,11 @@ static int sel_make_bools(void)
  	u32 sid;
 
Quoted 15 lines

security/selinux/selinuxfs.c static int sel_make_bools(void) · line 989

Quoted 10 lines
security/selinux/selinuxfs.c +5 -1
  	/* remove any existing files */
-	kfree(bool_pending_names);
+	if (bool_pending_names) {
+		for (i = 0; i<  bool_num; i++)
+			kfree(bool_pending_names[i]);
+		kfree(bool_pending_names);
+	}
  	kfree(bool_pending_values);
  	bool_pending_names = NULL;
  	bool_pending_values = NULL;

Since the code seems to rely on 'bool_num' in other places to ensure we don't walk off the end of the array it is probably safe to omit the 'if (bool_pending_names) ...' conditional and just rely on the for loop to do the right thing.

Thanks for point out, I'll resend V2 patch :-)

#3 lore raw

PATCH v2 1/1 selinux: fix memory leak in sel_make_bools

In reply to Xiaotian Feng

In sel_make_bools, kernel allocates memory for bool_pending_names[i] with security_get_bools. So if we just free bool_pending_names, those memories for bool_pending_names[i] will be leaked.

This patch resolves dozens of following kmemleak report after resuming from suspend:
unreferenced object 0xffff88022e4c7380 (size 32):
comm "init", pid 1, jiffies 4294677173
backtrace:

    [<ffffffff810f76b5>] create_object+0x1a2/0x2a9
    [<ffffffff810f78bb>] kmemleak_alloc+0x26/0x4b
    [<ffffffff810ef3eb>] __kmalloc+0x18f/0x1b8
    [<ffffffff811cd511>] security_get_bools+0xd7/0x16f
    [<ffffffff811c48c0>] sel_write_load+0x12e/0x62b
    [<ffffffff810f9a39>] vfs_write+0xae/0x10b
    [<ffffffff810f9b56>] sys_write+0x4a/0x6e
    [<ffffffff81011b82>] system_call_fastpath+0x16/0x1b
    [<ffffffffffffffff>] 0xffffffffffffffff
---
 security/selinux/selinuxfs.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
security/selinux/selinuxfs.c +2 -0
index b4fc506..1a8a1c2 100644
@@ -979,6 +979,8 @@ static int sel_make_bools(void)
 	u32 sid;
 
 	/* remove any existing files */
+	for (i = 0; i < bool_num; i++)
+		kfree(bool_pending_names[i]);
 	kfree(bool_pending_names);
 	kfree(bool_pending_values);
 	bool_pending_names = NULL;
Signature · 1 line
1.6.2.5
Acked-by on v2 1/1
#4 lore raw
Quoted 19 lines · Xiaotian Feng

In sel_make_bools, kernel allocates memory for bool_pending_names[i] with security_get_bools. So if we just free bool_pending_names, those memories for bool_pending_names[i] will be leaked.

This patch resolves dozens of following kmemleak report after resuming from suspend:
unreferenced object 0xffff88022e4c7380 (size 32):
comm "init", pid 1, jiffies 4294677173
backtrace:

    [<ffffffff810f76b5>] create_object+0x1a2/0x2a9
    [<ffffffff810f78bb>] kmemleak_alloc+0x26/0x4b
    [<ffffffff810ef3eb>] __kmalloc+0x18f/0x1b8
    [<ffffffff811cd511>] security_get_bools+0xd7/0x16f
    [<ffffffff811c48c0>] sel_write_load+0x12e/0x62b
    [<ffffffff810f9a39>] vfs_write+0xae/0x10b
    [<ffffffff810f9b56>] sys_write+0x4a/0x6e
    [<ffffffff81011b82>] system_call_fastpath+0x16/0x1b
    [<ffffffffffffffff>] 0xffffffffffffffff
Quoted 1 line
Quoted 19 lines
---
 security/selinux/selinuxfs.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
security/selinux/selinuxfs.c +2 -0
index b4fc506..1a8a1c2 100644
@@ -979,6 +979,8 @@ static int sel_make_bools(void)
 	u32 sid;
 
 	/* remove any existing files */
+	for (i = 0; i < bool_num; i++)
+		kfree(bool_pending_names[i]);
 	kfree(bool_pending_names);
 	kfree(bool_pending_values);
 	bool_pending_names = NULL;
Signature · 1 line
1.6.2.5
Applied
#5 lore raw
In reply to Xiaotian Feng
Quoted 3 lines · Xiaotian Feng

In sel_make_bools, kernel allocates memory for bool_pending_names[i] with security_get_bools. So if we just free bool_pending_names, those memories for bool_pending_names[i] will be leaked.

Thanks, applied and pushed to Linus.

Signature · 2 lines
James Morris
<[email protected]>
#6 lore raw
Quoted 7 lines · James Morris
Quoted 3 lines · Xiaotian Feng

In sel_make_bools, kernel allocates memory for bool_pending_names[i] with security_get_bools. So if we just free bool_pending_names, those memories for bool_pending_names[i] will be leaked.

Thanks, applied and pushed to Linus.

This patch is missing or dropped? I'm not seeing this patch until 2.6.33-rc7, thanks.

#7 lore raw
Quoted 10 lines · Xiaotian Feng
Quoted 7 lines · James Morris
Quoted 3 lines · Xiaotian Feng

In sel_make_bools, kernel allocates memory for bool_pending_names[i] with security_get_bools. So if we just free bool_pending_names, those memories for bool_pending_names[i] will be leaked.

Thanks, applied and pushed to Linus.

This patch is missing or dropped? I'm not seeing this patch until 2.6.33-rc7, thanks.

Not sure what happened to that -- I pushed it to Linus, but it's not in the tree for some reason. Now applied to

git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/security-testing-2.6#next

Signature · 2 lines
James Morris
<[email protected]>