public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kref: oops on zero or negative refcount
@ 2014-02-20 17:44 Mateusz Guzik
  2014-02-20 18:14 ` Dave Jones
  0 siblings, 1 reply; 5+ messages in thread
From: Mateusz Guzik @ 2014-02-20 17:44 UTC (permalink / raw)
  To: linux-kernel; +Cc: Rik van Riel

In use after free situations, it is possible for one thread to write to
memory that has just been reallocated to a new user. This could open up
potential security issues.

Close off those potential security holes by terminating the current
thread when kref encounters such a race condition or underflow.

Signed-off-by: Mateusz Guzik <mguzik@redhat.com>
Cc: Rik van Riel <riel@redhat.com>
---
 include/linux/kref.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/include/linux/kref.h b/include/linux/kref.h
index 484604d..c3f8a0a 100644
--- a/include/linux/kref.h
+++ b/include/linux/kref.h
@@ -43,8 +43,10 @@ static inline void kref_get(struct kref *kref)
 	/* If refcount was 0 before incrementing then we have a race
 	 * condition when this kref is freeing by some other thread right now.
 	 * In this case one should use kref_get_unless_zero()
+	 *
+	 * Terminate the current thread to stop potential security exploits.
 	 */
-	WARN_ON_ONCE(atomic_inc_return(&kref->refcount) < 2);
+	BUG_ON(atomic_inc_return(&kref->refcount) < 2);
 }
 
 /**
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] kref: oops on zero or negative refcount
  2014-02-20 17:44 [PATCH] kref: oops on zero or negative refcount Mateusz Guzik
@ 2014-02-20 18:14 ` Dave Jones
  2014-02-20 18:17   ` Rik van Riel
  2014-02-21 12:05   ` Mateusz Guzik
  0 siblings, 2 replies; 5+ messages in thread
From: Dave Jones @ 2014-02-20 18:14 UTC (permalink / raw)
  To: Mateusz Guzik; +Cc: linux-kernel, Rik van Riel

On Thu, Feb 20, 2014 at 06:44:59PM +0100, Mateusz Guzik wrote:
 > In use after free situations, it is possible for one thread to write to
 > memory that has just been reallocated to a new user. This could open up
 > potential security issues.
 > 
 > diff --git a/include/linux/kref.h b/include/linux/kref.h
 > index 484604d..c3f8a0a 100644
 > --- a/include/linux/kref.h
 > +++ b/include/linux/kref.h
 > @@ -43,8 +43,10 @@ static inline void kref_get(struct kref *kref)
 >  	/* If refcount was 0 before incrementing then we have a race
 >  	 * condition when this kref is freeing by some other thread right now.
 >  	 * In this case one should use kref_get_unless_zero()
 > +	 *
 > +	 * Terminate the current thread to stop potential security exploits.
 >  	 */
 > -	WARN_ON_ONCE(atomic_inc_return(&kref->refcount) < 2);
 > +	BUG_ON(atomic_inc_return(&kref->refcount) < 2);

This isn't "terminating the thread", this is "lock up the box".

	Dave

 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] kref: oops on zero or negative refcount
  2014-02-20 18:14 ` Dave Jones
@ 2014-02-20 18:17   ` Rik van Riel
  2014-02-21 12:21     ` Rafael Aquini
  2014-02-21 12:05   ` Mateusz Guzik
  1 sibling, 1 reply; 5+ messages in thread
From: Rik van Riel @ 2014-02-20 18:17 UTC (permalink / raw)
  To: Dave Jones, Mateusz Guzik, linux-kernel

On 02/20/2014 01:14 PM, Dave Jones wrote:
> On Thu, Feb 20, 2014 at 06:44:59PM +0100, Mateusz Guzik wrote:
>  > In use after free situations, it is possible for one thread to write to
>  > memory that has just been reallocated to a new user. This could open up
>  > potential security issues.
>  > 
>  > diff --git a/include/linux/kref.h b/include/linux/kref.h
>  > index 484604d..c3f8a0a 100644
>  > --- a/include/linux/kref.h
>  > +++ b/include/linux/kref.h
>  > @@ -43,8 +43,10 @@ static inline void kref_get(struct kref *kref)
>  >  	/* If refcount was 0 before incrementing then we have a race
>  >  	 * condition when this kref is freeing by some other thread right now.
>  >  	 * In this case one should use kref_get_unless_zero()
>  > +	 *
>  > +	 * Terminate the current thread to stop potential security exploits.
>  >  	 */
>  > -	WARN_ON_ONCE(atomic_inc_return(&kref->refcount) < 2);
>  > +	BUG_ON(atomic_inc_return(&kref->refcount) < 2);
> 
> This isn't "terminating the thread", this is "lock up the box".

Only if kref_get holds a lock while encountering a refcount
underflow, right?

-- 
All rights reversed

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] kref: oops on zero or negative refcount
  2014-02-20 18:14 ` Dave Jones
  2014-02-20 18:17   ` Rik van Riel
@ 2014-02-21 12:05   ` Mateusz Guzik
  1 sibling, 0 replies; 5+ messages in thread
From: Mateusz Guzik @ 2014-02-21 12:05 UTC (permalink / raw)
  To: Dave Jones, linux-kernel, Rik van Riel

On Thu, Feb 20, 2014 at 01:14:40PM -0500, Dave Jones wrote:
> On Thu, Feb 20, 2014 at 06:44:59PM +0100, Mateusz Guzik wrote:
>  > In use after free situations, it is possible for one thread to write to
>  > memory that has just been reallocated to a new user. This could open up
>  > potential security issues.
>  > 
>  > diff --git a/include/linux/kref.h b/include/linux/kref.h
>  > index 484604d..c3f8a0a 100644
>  > --- a/include/linux/kref.h
>  > +++ b/include/linux/kref.h
>  > @@ -43,8 +43,10 @@ static inline void kref_get(struct kref *kref)
>  >  	/* If refcount was 0 before incrementing then we have a race
>  >  	 * condition when this kref is freeing by some other thread right now.
>  >  	 * In this case one should use kref_get_unless_zero()
>  > +	 *
>  > +	 * Terminate the current thread to stop potential security exploits.
>  >  	 */
>  > -	WARN_ON_ONCE(atomic_inc_return(&kref->refcount) < 2);
>  > +	BUG_ON(atomic_inc_return(&kref->refcount) < 2);
> 
> This isn't "terminating the thread", this is "lock up the box".
> 

Well, extent of damage caused by non-panicking BUG_ON (if any) depends
on the state when kref_get was executed.

However, since this condition is already a sign of big trouble (and a
potential exploitation attempt), I think a WARN_ON_ONCE is not
sufficient.

That said, can you elaborate on your concers? You just don't like that
comment, don't want that BUG_ON (want a panic instead) or maybe you
don't like the change at all (or something else)?

Thanks,
-- 
Mateusz Guzik

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] kref: oops on zero or negative refcount
  2014-02-20 18:17   ` Rik van Riel
@ 2014-02-21 12:21     ` Rafael Aquini
  0 siblings, 0 replies; 5+ messages in thread
From: Rafael Aquini @ 2014-02-21 12:21 UTC (permalink / raw)
  To: Rik van Riel; +Cc: Dave Jones, Mateusz Guzik, linux-kernel

On Thu, Feb 20, 2014 at 01:17:44PM -0500, Rik van Riel wrote:
> On 02/20/2014 01:14 PM, Dave Jones wrote:
> > On Thu, Feb 20, 2014 at 06:44:59PM +0100, Mateusz Guzik wrote:
> >  > In use after free situations, it is possible for one thread to write to
> >  > memory that has just been reallocated to a new user. This could open up
> >  > potential security issues.
> >  > 
> >  > diff --git a/include/linux/kref.h b/include/linux/kref.h
> >  > index 484604d..c3f8a0a 100644
> >  > --- a/include/linux/kref.h
> >  > +++ b/include/linux/kref.h
> >  > @@ -43,8 +43,10 @@ static inline void kref_get(struct kref *kref)
> >  >  	/* If refcount was 0 before incrementing then we have a race
> >  >  	 * condition when this kref is freeing by some other thread right now.
> >  >  	 * In this case one should use kref_get_unless_zero()
> >  > +	 *
> >  > +	 * Terminate the current thread to stop potential security exploits.
> >  >  	 */
> >  > -	WARN_ON_ONCE(atomic_inc_return(&kref->refcount) < 2);
> >  > +	BUG_ON(atomic_inc_return(&kref->refcount) < 2);
> > 
> > This isn't "terminating the thread", this is "lock up the box".
> 
> Only if kref_get holds a lock while encountering a refcount
> underflow, right?
>

Yes, and in a quick glance through the tree it seems we have several
codesites where we can find such condition likely to happen,
unfortunately.



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-02-21 12:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-02-20 17:44 [PATCH] kref: oops on zero or negative refcount Mateusz Guzik
2014-02-20 18:14 ` Dave Jones
2014-02-20 18:17   ` Rik van Riel
2014-02-21 12:21     ` Rafael Aquini
2014-02-21 12:05   ` Mateusz Guzik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox