Skip to contentSkip to conversation

kref: oops on zero or negative refcount

PATCH In review Mateusz Guzik 4 participants 5 messages Feb 20, 2014 – Feb 21, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kref: oops on zero or negative refcount

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.

---
 include/linux/kref.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/linux/kref.h +3 -1
index 484604d..c3f8a0a 100644
@@ -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);
 }
 
 /**
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 9 lines · Mateusz Guzik

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.

include/linux/kref.h +0 -0
index 484604d..c3f8a0a 100644

include/linux/kref.h static inline void kref_get(struct kref *kref) · line 49

Quoted 8 lines
include/linux/kref.h +3 -1
@@ -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

#2 lore raw
Quoted 5 lines · Dave Jones
Quoted 4 lines · Mateusz Guzik

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.

Quoted 15 lines

include/linux/kref.h static inline void kref_get(struct kref *kref) · line 49

Quoted 13 lines
include/linux/kref.h +3 -1
index 484604d..c3f8a0a 100644
@@ -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?

Signature · 1 line
All rights reversed
#3 lore raw
Quoted 20 lines · Rik van Riel
Quoted 19 lines · Dave Jones
Quoted 17 lines · Mateusz Guzik

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.

include/linux/kref.h +3 -1
index 484604d..c3f8a0a 100644
@@ -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);
Quoted 5 lines
Quoted 1 line

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.

#4 lore raw
In reply to Dave Jones
Quoted 5 lines · Dave Jones
Quoted 4 lines · Mateusz Guzik

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.

Quoted 16 lines

include/linux/kref.h static inline void kref_get(struct kref *kref) · line 49

Quoted 13 lines
include/linux/kref.h +3 -1
index 484604d..c3f8a0a 100644
@@ -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,

Signature · 1 line
Mateusz Guzik