Commit 2edeaa34a6e3f2c43b667f6c4f7b27944b811695

Authored by Tetsuo Handa
Committed by Linus Torvalds
1 parent 257a65d795

CRED: Fix BUG() upon security_cred_alloc_blank() failure

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>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 2 changed files with 13 additions and 5 deletions Side-by-side Diff

... ... @@ -252,13 +252,13 @@
252 252 #endif
253 253  
254 254 atomic_set(&new->usage, 1);
  255 +#ifdef CONFIG_DEBUG_CREDENTIALS
  256 + new->magic = CRED_MAGIC;
  257 +#endif
255 258  
256 259 if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)
257 260 goto error;
258 261  
259   -#ifdef CONFIG_DEBUG_CREDENTIALS
260   - new->magic = CRED_MAGIC;
261   -#endif
262 262 return new;
263 263  
264 264 error:
... ... @@ -748,7 +748,11 @@
748 748 if (cred->magic != CRED_MAGIC)
749 749 return true;
750 750 #ifdef CONFIG_SECURITY_SELINUX
751   - if (selinux_is_enabled()) {
  751 + /*
  752 + * cred->security == NULL if security_cred_alloc_blank() or
  753 + * security_prepare_creds() returned an error.
  754 + */
  755 + if (selinux_is_enabled() && cred->security) {
752 756 if ((unsigned long) cred->security < PAGE_SIZE)
753 757 return true;
754 758 if ((*(u32 *)cred->security & 0xffffff00) ==
security/selinux/hooks.c
... ... @@ -3198,7 +3198,11 @@
3198 3198 {
3199 3199 struct task_security_struct *tsec = cred->security;
3200 3200  
3201   - BUG_ON((unsigned long) cred->security < PAGE_SIZE);
  3201 + /*
  3202 + * cred->security == NULL if security_cred_alloc_blank() or
  3203 + * security_prepare_creds() returned an error.
  3204 + */
  3205 + BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3202 3206 cred->security = (void *) 0x7UL;
3203 3207 kfree(tsec);
3204 3208 }