Skip to contentSkip to conversation

refcount: Create unchecked atomic_t implementation

PATCH Superseded Kees Cook 8 participants 9 messages Jun 8, 2017 – Jun 21, 2017

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v2 1/1 refcount: Create unchecked atomic_t implementation

Many subsystems will not use refcount_t unless there is a way to build the kernel so that there is no regression in speed compared to atomic_t. This adds CONFIG_REFCOUNT_FULL to enable the full refcount_t implementation which has the validation but is slightly slower. When not enabled, refcount_t uses the basic unchecked atomic_t routines, which results in no code changes compared to just using atomic_t directly.

---
This is v2 of this patch, which I've split from the arch-specific alternative implementation for x86. Getting this patch in will unblock atomic_t -> refcount_t conversion, and the x86 alternative implementation can be developed in parallel. Changes from v1: use better atomic ops, thanks to Elena and Peter.

---
 arch/Kconfig             |  9 +++++++++
 include/linux/refcount.h | 44 ++++++++++++++++++++++++++++++++++++++++++++
 lib/refcount.c           |  3 +++
 3 files changed, 56 insertions(+)
arch/Kconfig +9 -0
index 6c00e5b00f8b..fba3bf186728 100644
@@ -867,4 +867,13 @@ config STRICT_MODULE_RWX
 config ARCH_WANT_RELAX_ORDER
 	bool
 
+config REFCOUNT_FULL
+	bool "Perform full reference count validation at the expense of speed"
+	help
+	  Enabling this switches the refcounting infrastructure from a fast
+	  unchecked atomic_t implementation to a fully state checked
+	  implementation, which can be slower but provides protections
+	  against various use-after-free conditions that can be used in
+	  security flaw exploits.
+
 source "kernel/gcov/Kconfig"
include/linux/refcount.h +44 -0

56 lines not on this page · raw message

lib/refcount.c +3 -0
index 9f906783987e..5d0582a9480c 100644
@@ -37,6 +37,8 @@
 #include <linux/refcount.h>
 #include <linux/bug.h>
 
+#ifdef CONFIG_REFCOUNT_FULL
+
 /**
  * refcount_add_not_zero - add a value to a refcount unless it is 0
  * @i: the value to add to the refcount
@@ -225,6 +227,7 @@ void refcount_dec(refcount_t *r)
 	WARN_ONCE(refcount_dec_and_test(r), "refcount_t: decrement hit 0; leaking memory.\n");
 }
 EXPORT_SYMBOL(refcount_dec);
+#endif /* CONFIG_REFCOUNT_FULL */
 
 /**
  * refcount_dec_if_one - decrement a refcount if it is 1
Signature · 6 lines
2.7.4


-- 
Kees Cook
Pixel Security
Acked-by on v2 1/1
#1 lore raw
Quoted 8 lines · Kees Cook

Many subsystems will not use refcount_t unless there is a way to build the kernel so that there is no regression in speed compared to atomic_t. This adds CONFIG_REFCOUNT_FULL to enable the full refcount_t implementation which has the validation but is slightly slower. When not enabled, refcount_t uses the basic unchecked atomic_t routines, which results in no code changes compared to just using atomic_t directly.

#3 lore raw
In reply to Kees Cook
Quoted 7 lines · Kees Cook

Many subsystems will not use refcount_t unless there is a way to build the kernel so that there is no regression in speed compared to atomic_t. This adds CONFIG_REFCOUNT_FULL to enable the full refcount_t implementation which has the validation but is slightly slower. When not enabled, refcount_t uses the basic unchecked atomic_t routines, which results in no code changes compared to just using atomic_t directly.

Quoted 7 lines

---
This is v2 of this patch, which I've split from the arch-specific alternative implementation for x86. Getting this patch in will unblock atomic_t -> refcount_t conversion, and the x86 alternative implementation can be developed in parallel. Changes from v1: use better atomic ops, thanks to Elena and Peter.

Yeah, can we get this in ASAP? Without having to always incur the over this will allow us to convert subsystems to refcount_t broadly.

#4 lore raw
Quoted 16 lines · Kees Cook
Quoted 7 lines

Many subsystems will not use refcount_t unless there is a way to build the kernel so that there is no regression in speed compared to atomic_t. This adds CONFIG_REFCOUNT_FULL to enable the full refcount_t implementation which has the validation but is slightly slower. When not enabled, refcount_t uses the basic unchecked atomic_t routines, which results in no code changes compared to just using atomic_t directly.

Quoted 7 lines

---
This is v2 of this patch, which I've split from the arch-specific alternative implementation for x86. Getting this patch in will unblock atomic_t -> refcount_t conversion, and the x86 alternative implementation can be developed in parallel. Changes from v1: use better atomic ops, thanks to Elena and Peter.

Quoted 2 lines

Yeah, can we get this in ASAP? Without having to always incur the over this will allow us to convert subsystems to refcount_t broadly.

+1. If this gets in, I can refresh the rest of the patches in net, mm, ipc, block, etc. and send them for review again.

Best Regards,
Elena

#5 lore raw
Quoted 16 lines · Elena
Quoted 16 lines · Kees Cook
Quoted 14 lines

Many subsystems will not use refcount_t unless there is a way to build the kernel so that there is no regression in speed compared to atomic_t. This adds CONFIG_REFCOUNT_FULL to enable the full refcount_t implementation which has the validation but is slightly slower. When not enabled, refcount_t uses the basic unchecked atomic_t routines, which results in no code changes compared to just using atomic_t directly.

---
This is v2 of this patch, which I've split from the arch-specific alternative implementation for x86. Getting this patch in will unblock atomic_t -> refcount_t conversion, and the x86 alternative implementation can be developed in parallel. Changes from v1: use better atomic ops, thanks to Elena and Peter.

Quoted 4 lines
Quoted 2 lines

Yeah, can we get this in ASAP? Without having to always incur the over this will allow us to convert subsystems to refcount_t broadly.

+1. If this gets in, I can refresh the rest of the patches in net, mm, ipc, block, etc. and send them for review again.

Yes, this would be a prerequisite for ipc; which I initially thought didn't take a performance hit.

Thanks,
Davidlohr

#6 lore raw

Hi Davidlohr,

Quoted 4 lines · Davidlohr Bueso

Yes, this would be a prerequisite for ipc; which I initially thought didn't take a performance hit.

Did you see a regression for ipc?

The fast paths don't use the refcount, it is only used for rare situations:
- GETALL, SETALL for large arrays
- alloc undo

--
Manfred

maintainer
#7 lore raw
Quoted 9 lines · Manfred Spraul

Hi Davidlohr,

Quoted 5 lines · Davidlohr Bueso

Yes, this would be a prerequisite for ipc; which I initially thought didn't
take a performance hit.

Did you see a regression for ipc?

I'd be most interested in having a benchmark that shows a regression. Please share.

author
#2 lore raw
In reply to Greg KH
Quoted 11 lines · Greg KH
Quoted 8 lines · Kees Cook

Many subsystems will not use refcount_t unless there is a way to build the kernel so that there is no regression in speed compared to atomic_t. This adds CONFIG_REFCOUNT_FULL to enable the full refcount_t implementation which has the validation but is slightly slower. When not enabled, refcount_t uses the basic unchecked atomic_t routines, which results in no code changes compared to just using atomic_t directly.

Ping. tip maintainers, can you please take this patch to unblock the refcount_t conversions?

Thanks!

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#8 lore raw
In reply to Kees Cook
Quoted 14 lines · Kees Cook

Many subsystems will not use refcount_t unless there is a way to build the kernel so that there is no regression in speed compared to atomic_t. This adds CONFIG_REFCOUNT_FULL to enable the full refcount_t implementation which has the validation but is slightly slower. When not enabled, refcount_t uses the basic unchecked atomic_t routines, which results in no code changes compared to just using atomic_t directly.

---
This is v2 of this patch, which I've split from the arch-specific alternative implementation for x86. Getting this patch in will unblock atomic_t -> refcount_t conversion, and the x86 alternative implementation can be developed in parallel. Changes from v1: use better atomic ops, thanks to Elena and Peter.

Quoted 5 lines
---
 arch/Kconfig             |  9 +++++++++
 include/linux/refcount.h | 44 ++++++++++++++++++++++++++++++++++++++++++++
 lib/refcount.c           |  3 +++
 3 files changed, 56 insertions(+)

Looks almost good - sans a few stylistic nits:

Quoted 32 lines
arch/Kconfig +9 -0
index 6c00e5b00f8b..fba3bf186728 100644
@@ -867,4 +867,13 @@ config STRICT_MODULE_RWX
 config ARCH_WANT_RELAX_ORDER
 	bool
 
+config REFCOUNT_FULL
+	bool "Perform full reference count validation at the expense of speed"
+	help
+	  Enabling this switches the refcounting infrastructure from a fast
+	  unchecked atomic_t implementation to a fully state checked
+	  implementation, which can be slower but provides protections
+	  against various use-after-free conditions that can be used in
+	  security flaw exploits.
+
 source "kernel/gcov/Kconfig"
include/linux/refcount.h +1 -0
index b34aa649d204..099c32bd07b2 100644
@@ -41,6 +41,7 @@ static inline unsigned int refcount_read(const refcount_t *r)
 	return atomic_read(&r->refs);
 }
 
+#ifdef CONFIG_REFCOUNT_FULL
 extern __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r);
 extern void refcount_add(unsigned int i, refcount_t *r);
 

include/linux/refcount.h extern void refcount_sub(unsigned int i, refcount_t *r); · line 58

Quoted 6 lines
include/linux/refcount.h +3 -0
@@ -52,6 +53,49 @@ extern void refcount_sub(unsigned int i, refcount_t *r);
 
 extern __must_check bool refcount_dec_and_test(refcount_t *r);
 extern void refcount_dec(refcount_t *r);
+#else
+static inline __must_check bool refcount_add_not_zero(unsigned int i,
+						      refcount_t *r)

Please keep it on a single, slighly over-long line instead of the ugly line break in the middle of the list of parameters ...

There's other such uglies in the patch as well.

Thanks,

Ingo