LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH 1/2] CRED: Fix BUG() upon security_cred_alloc_blank() failure
@ 2011-02-07 13:36 David Howells
2011-02-07 13:36 ` [PATCH 2/2] CRED: Fix memory and refcount leaks upon security_prepare_creds() failure David Howells
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: David Howells @ 2011-02-07 13:36 UTC (permalink / raw)
To: torvalds, akpm
Cc: linux-security-module, linux-kernel, Tetsuo Handa, David Howells
From: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
In cred_alloc_blank() since 2.6.32, abort_creds(new) is called with
new->security == NULL and new->magic == 0 when security_cred_alloc_blank()
returns an error. As a result, BUG() will be triggered if SELinux is enabled
or CONFIG_DEBUG_CREDENTIALS=y.
If CONFIG_DEBUG_CREDENTIALS=y, BUG() is called from __invalid_creds() because
cred->magic == 0. Failing that, BUG() is called from selinux_cred_free()
because selinux_cred_free() is not expecting cred->security == NULL. This does
not affect smack_cred_free(), tomoyo_cred_free() or apparmor_cred_free().
Fix these bugs by
(1) Set new->magic before calling security_cred_alloc_blank().
(2) Handle null cred->security in creds_are_invalid() and selinux_cred_free().
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Signed-off-by: David Howells <dhowells@redhat.com>
---
kernel/cred.c | 12 ++++++++----
security/selinux/hooks.c | 6 +++++-
2 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/kernel/cred.c b/kernel/cred.c
index 6a1aa00..fcf104b 100644
--- a/kernel/cred.c
+++ b/kernel/cred.c
@@ -252,13 +252,13 @@ struct cred *cred_alloc_blank(void)
#endif
atomic_set(&new->usage, 1);
+#ifdef CONFIG_DEBUG_CREDENTIALS
+ new->magic = CRED_MAGIC;
+#endif
if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)
goto error;
-#ifdef CONFIG_DEBUG_CREDENTIALS
- new->magic = CRED_MAGIC;
-#endif
return new;
error:
@@ -748,7 +748,11 @@ bool creds_are_invalid(const struct cred *cred)
if (cred->magic != CRED_MAGIC)
return true;
#ifdef CONFIG_SECURITY_SELINUX
- if (selinux_is_enabled()) {
+ /*
+ * cred->security == NULL if security_cred_alloc_blank() or
+ * security_prepare_creds() returned an error.
+ */
+ if (selinux_is_enabled() && cred->security) {
if ((unsigned long) cred->security < PAGE_SIZE)
return true;
if ((*(u32 *)cred->security & 0xffffff00) ==
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index e276eb4..c8d6992 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -3198,7 +3198,11 @@ static void selinux_cred_free(struct cred *cred)
{
struct task_security_struct *tsec = cred->security;
- BUG_ON((unsigned long) cred->security < PAGE_SIZE);
+ /*
+ * cred->security == NULL if security_cred_alloc_blank() or
+ * security_prepare_creds() returned an error.
+ */
+ BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
cred->security = (void *) 0x7UL;
kfree(tsec);
}
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] CRED: Fix memory and refcount leaks upon security_prepare_creds() failure
2011-02-07 13:36 [PATCH 1/2] CRED: Fix BUG() upon security_cred_alloc_blank() failure David Howells
@ 2011-02-07 13:36 ` David Howells
2011-02-07 23:23 ` [PATCH 1/2] CRED: Fix BUG() upon security_cred_alloc_blank() failure Andrew Morton
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: David Howells @ 2011-02-07 13:36 UTC (permalink / raw)
To: torvalds, akpm
Cc: linux-security-module, linux-kernel, Tetsuo Handa, David Howells
From: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
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@I-love.SAKURA.ne.jp>
Signed-off-by: David Howells <dhowells@redhat.com>
---
kernel/cred.c | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/kernel/cred.c b/kernel/cred.c
index fcf104b..3a9d6dd 100644
--- a/kernel/cred.c
+++ b/kernel/cred.c
@@ -657,6 +657,8 @@ struct cred *prepare_kernel_cred(struct task_struct *daemon)
validate_creds(old);
*new = *old;
+ atomic_set(&new->usage, 1);
+ set_cred_subscribers(new, 0);
get_uid(new->user);
get_group_info(new->group_info);
@@ -674,8 +676,6 @@ struct cred *prepare_kernel_cred(struct task_struct *daemon)
if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
goto error;
- atomic_set(&new->usage, 1);
- set_cred_subscribers(new, 0);
put_cred(old);
validate_creds(new);
return new;
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] CRED: Fix BUG() upon security_cred_alloc_blank() failure
2011-02-07 13:36 [PATCH 1/2] CRED: Fix BUG() upon security_cred_alloc_blank() failure David Howells
2011-02-07 13:36 ` [PATCH 2/2] CRED: Fix memory and refcount leaks upon security_prepare_creds() failure David Howells
@ 2011-02-07 23:23 ` Andrew Morton
2011-02-07 23:56 ` David Howells
2011-02-07 23:57 ` [PATCH 2/2] CRED: Fix memory and refcount leaks upon security_prepare_creds() failure David Howells
3 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2011-02-07 23:23 UTC (permalink / raw)
To: David Howells; +Cc: torvalds, linux-security-module, linux-kernel, Tetsuo Handa
On Mon, 07 Feb 2011 13:36:10 +0000
David Howells <dhowells@redhat.com> wrote:
> In cred_alloc_blank() since 2.6.32, abort_creds(new) is called with
> new->security == NULL and new->magic == 0 when security_cred_alloc_blank()
> returns an error. As a result, BUG() will be triggered if SELinux is enabled
> or CONFIG_DEBUG_CREDENTIALS=y.
>
> If CONFIG_DEBUG_CREDENTIALS=y, BUG() is called from __invalid_creds() because
> cred->magic == 0. Failing that, BUG() is called from selinux_cred_free()
> because selinux_cred_free() is not expecting cred->security == NULL. This does
> not affect smack_cred_free(), tomoyo_cred_free() or apparmor_cred_free().
>
> Fix these bugs by
>
> (1) Set new->magic before calling security_cred_alloc_blank().
>
> (2) Handle null cred->security in creds_are_invalid() and selinux_cred_free().
You don't feel that these two patches should be backported into -stable?
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] CRED: Fix BUG() upon security_cred_alloc_blank() failure
2011-02-07 13:36 [PATCH 1/2] CRED: Fix BUG() upon security_cred_alloc_blank() failure David Howells
2011-02-07 13:36 ` [PATCH 2/2] CRED: Fix memory and refcount leaks upon security_prepare_creds() failure David Howells
2011-02-07 23:23 ` [PATCH 1/2] CRED: Fix BUG() upon security_cred_alloc_blank() failure Andrew Morton
@ 2011-02-07 23:56 ` David Howells
2011-02-07 23:57 ` [PATCH 2/2] CRED: Fix memory and refcount leaks upon security_prepare_creds() failure David Howells
3 siblings, 0 replies; 5+ messages in thread
From: David Howells @ 2011-02-07 23:56 UTC (permalink / raw)
To: stable; +Cc: dhowells, akpm, linux-security-module, linux-kernel, Tetsuo Handa
From: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
In cred_alloc_blank() since 2.6.32, abort_creds(new) is called with
new->security == NULL and new->magic == 0 when security_cred_alloc_blank()
returns an error. As a result, BUG() will be triggered if SELinux is enabled
or CONFIG_DEBUG_CREDENTIALS=y.
If CONFIG_DEBUG_CREDENTIALS=y, BUG() is called from __invalid_creds() because
cred->magic == 0. Failing that, BUG() is called from selinux_cred_free()
because selinux_cred_free() is not expecting cred->security == NULL. This does
not affect smack_cred_free(), tomoyo_cred_free() or apparmor_cred_free().
Fix these bugs by
(1) Set new->magic before calling security_cred_alloc_blank().
(2) Handle null cred->security in creds_are_invalid() and selinux_cred_free().
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Signed-off-by: David Howells <dhowells@redhat.com>
---
kernel/cred.c | 12 ++++++++----
security/selinux/hooks.c | 6 +++++-
2 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/kernel/cred.c b/kernel/cred.c
index 6a1aa00..fcf104b 100644
--- a/kernel/cred.c
+++ b/kernel/cred.c
@@ -252,13 +252,13 @@ struct cred *cred_alloc_blank(void)
#endif
atomic_set(&new->usage, 1);
+#ifdef CONFIG_DEBUG_CREDENTIALS
+ new->magic = CRED_MAGIC;
+#endif
if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)
goto error;
-#ifdef CONFIG_DEBUG_CREDENTIALS
- new->magic = CRED_MAGIC;
-#endif
return new;
error:
@@ -748,7 +748,11 @@ bool creds_are_invalid(const struct cred *cred)
if (cred->magic != CRED_MAGIC)
return true;
#ifdef CONFIG_SECURITY_SELINUX
- if (selinux_is_enabled()) {
+ /*
+ * cred->security == NULL if security_cred_alloc_blank() or
+ * security_prepare_creds() returned an error.
+ */
+ if (selinux_is_enabled() && cred->security) {
if ((unsigned long) cred->security < PAGE_SIZE)
return true;
if ((*(u32 *)cred->security & 0xffffff00) ==
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index e276eb4..c8d6992 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -3198,7 +3198,11 @@ static void selinux_cred_free(struct cred *cred)
{
struct task_security_struct *tsec = cred->security;
- BUG_ON((unsigned long) cred->security < PAGE_SIZE);
+ /*
+ * cred->security == NULL if security_cred_alloc_blank() or
+ * security_prepare_creds() returned an error.
+ */
+ BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
cred->security = (void *) 0x7UL;
kfree(tsec);
}
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] CRED: Fix memory and refcount leaks upon security_prepare_creds() failure
2011-02-07 13:36 [PATCH 1/2] CRED: Fix BUG() upon security_cred_alloc_blank() failure David Howells
` (2 preceding siblings ...)
2011-02-07 23:56 ` David Howells
@ 2011-02-07 23:57 ` David Howells
3 siblings, 0 replies; 5+ messages in thread
From: David Howells @ 2011-02-07 23:57 UTC (permalink / raw)
To: stable; +Cc: dhowells, akpm, linux-security-module, linux-kernel, Tetsuo Handa
From: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
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@I-love.SAKURA.ne.jp>
Signed-off-by: David Howells <dhowells@redhat.com>
---
kernel/cred.c | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/kernel/cred.c b/kernel/cred.c
index fcf104b..3a9d6dd 100644
--- a/kernel/cred.c
+++ b/kernel/cred.c
@@ -657,6 +657,8 @@ struct cred *prepare_kernel_cred(struct task_struct *daemon)
validate_creds(old);
*new = *old;
+ atomic_set(&new->usage, 1);
+ set_cred_subscribers(new, 0);
get_uid(new->user);
get_group_info(new->group_info);
@@ -674,8 +676,6 @@ struct cred *prepare_kernel_cred(struct task_struct *daemon)
if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
goto error;
- atomic_set(&new->usage, 1);
- set_cred_subscribers(new, 0);
put_cred(old);
validate_creds(new);
return new;
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2011-02-07 23:57 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-07 13:36 [PATCH 1/2] CRED: Fix BUG() upon security_cred_alloc_blank() failure David Howells
2011-02-07 13:36 ` [PATCH 2/2] CRED: Fix memory and refcount leaks upon security_prepare_creds() failure David Howells
2011-02-07 23:23 ` [PATCH 1/2] CRED: Fix BUG() upon security_cred_alloc_blank() failure Andrew Morton
2011-02-07 23:56 ` David Howells
2011-02-07 23:57 ` [PATCH 2/2] CRED: Fix memory and refcount leaks upon security_prepare_creds() failure David Howells
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).