[17/68] CRED: Fix memory and refcount leaks upon security_prepare_creds() failure

From: Greg KH
Date: Mon Feb 28 2011 - 11:51:19 EST

2.6.32-longterm review patch. If anyone has any objections, please let us know.


From: Tetsuo Handa <penguin-kernel@xxxxxxxxxxxxxxxxxxx>

commit fb2b2a1d37f80cc818fd4487b510f4e11816e5e1 upstream.

In prepare_kernel_cred() since 2.6.29, put_cred(new) is called without
assigning new->usage when security_prepare_creds() returned an error. As a
result, memory for new and refcount for new->{user,group_info,tgcred} are
leaked because put_cred(new) won't call __put_cred() unless old->usage == 1.

Fix these leaks by assigning new->usage (and new->subscribers which was added
in 2.6.32) before calling security_prepare_creds().

Signed-off-by: Tetsuo Handa <penguin-kernel@xxxxxxxxxxxxxxxxxxx>
Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
Signed-off-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxx>

kernel/cred.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

--- a/kernel/cred.c
+++ b/kernel/cred.c
@@ -718,6 +718,8 @@ struct cred *prepare_kernel_cred(struct

*new = *old;
+ atomic_set(&new->usage, 1);
+ set_cred_subscribers(new, 0);

@@ -735,8 +737,6 @@ struct cred *prepare_kernel_cred(struct
if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
goto error;

- atomic_set(&new->usage, 1);
- set_cred_subscribers(new, 0);
return new;

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/