Skip to contentSkip to conversation

CRED: Fix double free in prepare_usermodehelper_creds() error handling

PATCH In review David Howells 4 participants 4 messages Apr 20, 2010 – Apr 22, 2010 Credentials

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 CRED: Fix double free in prepare_usermodehelper_creds() error handling

Patch 570b8fb505896e007fd3bb07573ba6640e51851d:

Author: Mathieu Desnoyers <[email protected]>
Date: Tue Mar 30 00:04:00 2010 +0100
Subject: CRED: Fix memory leak in error handling

attempts to fix a memory leak in the error handling by making the offending return statement into a jump down to the bottom of the function where a kfree(tgcred) is inserted.

This is, however, incorrect, as it does a kfree() after doing put_cred() if security_prepare_creds() fails. That will result in a double free if 'error' is jumped to as put_cred() will also attempt to free the new tgcred record by virtue of it being pointed to by the new cred record.

---

 kernel/cred.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
kernel/cred.c +2 -0
index e1dbe9e..ce1a52b 100644
@@ -398,6 +398,8 @@ struct cred *prepare_usermodehelper_creds(void)
 
 error:
 	put_cred(new);
+	return NULL;
+
 free_tgcred:
 #ifdef CONFIG_KEYS
 	kfree(tgcred);
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · * David Howells ([email protected]) wrote:

Patch 570b8fb505896e007fd3bb07573ba6640e51851d:

Author: Mathieu Desnoyers <[email protected]>
Date: Tue Mar 30 00:04:00 2010 +0100
Subject: CRED: Fix memory leak in error handling

Quoted 8 lines

attempts to fix a memory leak in the error handling by making the offending return statement into a jump down to the bottom of the function where a kfree(tgcred) is inserted.

This is, however, incorrect, as it does a kfree() after doing put_cred() if security_prepare_creds() fails. That will result in a double free if 'error' is jumped to as put_cred() will also attempt to free the new tgcred record by virtue of it being pointed to by the new cred record.

OK, I missed the fact taht put_cred() performs the kfree. Thanks for the fix.

Quoted 21 lines

---

 kernel/cred.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
kernel/cred.c +2 -0
index e1dbe9e..ce1a52b 100644
@@ -398,6 +398,8 @@ struct cred *prepare_usermodehelper_creds(void)
 
 error:
 	put_cred(new);
+	return NULL;
+
 free_tgcred:
 #ifdef CONFIG_KEYS
 	kfree(tgcred);
Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#2 lore raw
In reply to David Howells
Quoted 26 lines · On Tue, 20 Apr 2010 22:41:18 +0100 David Howells <[email protected]> wrote:

Patch 570b8fb505896e007fd3bb07573ba6640e51851d:

Author: Mathieu Desnoyers <[email protected]>
Date: Tue Mar 30 00:04:00 2010 +0100
Subject: CRED: Fix memory leak in error handling

attempts to fix a memory leak in the error handling by making the offending return statement into a jump down to the bottom of the function where a kfree(tgcred) is inserted.

This is, however, incorrect, as it does a kfree() after doing put_cred() if security_prepare_creds() fails. That will result in a double free if 'error' is jumped to as put_cred() will also attempt to free the new tgcred record by virtue of it being pointed to by the new cred record.

---

 kernel/cred.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
kernel/cred.c +0 -0
index e1dbe9e..ce1a52b 100644

kernel/cred.c struct cred *prepare_usermodehelper_creds(void) · line 405

Quoted 8 lines
kernel/cred.c +2 -0
@@ -398,6 +398,8 @@ struct cred *prepare_usermodehelper_creds(void)
 
 error:
 	put_cred(new);
+	return NULL;
+
 free_tgcred:
 #ifdef CONFIG_KEYS
 	kfree(tgcred);

Oleg's umh-creds-kill-subprocess_info-cred-logic.patch removes prepare_usermodehelper_creds() altogether. An option would have been to promote that into 2.6.34?

#3 lore raw
Quoted 6 lines · Andrew Morton
Quoted 2 lines

#ifdef CONFIG_KEYS
kfree(tgcred);

Oleg's umh-creds-kill-subprocess_info-cred-logic.patch removes prepare_usermodehelper_creds() altogether. An option would have been to promote that into 2.6.34?

FYI, this fix is in Linus' tree, now.

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