Skip to contentSkip to conversation

refcount: Use CONFIG_BUG_ON_DATA_CORRUPTION for UAF-related errors

PATCH Applied Jann Horn 4 participants 5 messages Aug 12 – Oct 2 Library code

lore
Share & tools
lore

Jann Horn

author
#0 lore raw

PATCH 1/1 refcount: Use CONFIG_BUG_ON_DATA_CORRUPTION for UAF-related errors

Some refcount issues are not necessarily associated with memory corruption, but REFCOUNT_ADD_UAF suggests that a UAF either just happened or is about to happen.

REFCOUNT_SUB_UAF is also an indicator that reference counting is wrong, and suggests (less strongly) that a UAF access might have happened recently.

In these cases, BUG() is appropriate if CONFIG_BUG_ON_DATA_CORRUPTION is set.

---
MAINTAINERS specifies no specific maintainer for lib/refcount.c, but it does have an entry for include/linux/refcount.h, so I guess I should route this patch based on that.

I decided to send this patch after wondering how exploitable it would be to have a refcount_inc() call on an object which has reached refcount 0, but is not yet freed because of something like an RCU grace period.

---
 lib/refcount.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
lib/refcount.c +12 -3
index a207a8f22b3c..c0f0dc5296eb 100644
@@ -10,6 +10,15 @@
 
 #define REFCOUNT_WARN(str)	WARN_ONCE(1, "refcount_t: " str ".\n")
 
+#ifdef CONFIG_BUG_ON_DATA_CORRUPTION
+#define REFCOUNT_CORRUPTION(str) ({		\
+	pr_err("refcount_t: " str ".\n");	\
+	BUG();					\
+})
+#else
+#define REFCOUNT_CORRUPTION(str) REFCOUNT_WARN(str)
+#endif
+
 void refcount_warn_saturate(refcount_t *r, enum refcount_saturation_type t)
 {
 	refcount_set(r, REFCOUNT_SATURATED);
@@ -22,10 +31,10 @@ void refcount_warn_saturate(refcount_t *r, enum refcount_saturation_type t)
 		REFCOUNT_WARN("saturated; leaking memory");
 		break;
 	case REFCOUNT_ADD_UAF:
-		REFCOUNT_WARN("addition on 0; use-after-free");
+		REFCOUNT_CORRUPTION("addition on 0; use-after-free");
 		break;
 	case REFCOUNT_SUB_UAF:
-		REFCOUNT_WARN("underflow; use-after-free");
+		REFCOUNT_CORRUPTION("underflow; use-after-free");
 		break;
 	case REFCOUNT_DEC_LEAK:
 		REFCOUNT_WARN("decrement hit 0; leaking memory");
@@ -84,7 +93,7 @@ bool refcount_dec_not_one(refcount_t *r)
 
 		new = val - 1;
 		if (new > val) {
-			WARN_ONCE(new > val, "refcount_t: underflow; use-after-free.\n");
+			REFCOUNT_CORRUPTION("underflow; use-after-free");
 			return true;
 		}
 

---
base-commit: db2ddb87143519e20a95aa36c60b36107b736a58
change-id: 20260812-refcount-bug-on-data-corruption-f1a566f771e6

Best regards,

Signature · 1 line
Jann Horn <[email protected]>
Acked-by on v1 1/1
#1 lore raw
Quoted 29 lines · Jann Horn

Some refcount issues are not necessarily associated with memory corruption, but REFCOUNT_ADD_UAF suggests that a UAF either just happened or is about to happen.

REFCOUNT_SUB_UAF is also an indicator that reference counting is wrong, and suggests (less strongly) that a UAF access might have happened recently.

In these cases, BUG() is appropriate if CONFIG_BUG_ON_DATA_CORRUPTION is set.

---
MAINTAINERS specifies no specific maintainer for lib/refcount.c, but it does have an entry for include/linux/refcount.h, so I guess I should route this patch based on that.

I decided to send this patch after wondering how exploitable it would be to have a refcount_inc() call on an object which has reached refcount 0, but is not yet freed because of something like an RCU grace period.

---
 lib/refcount.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
lib/refcount.c +0 -0
index a207a8f22b3c..c0f0dc5296eb 100644
@@ -10,6 +10,15 @@
 

lib/refcount.c line 20

Quoted 10 lines
lib/refcount.c +8 -0
 #define REFCOUNT_WARN(str)	WARN_ONCE(1, "refcount_t: " str ".\n")
 
+#ifdef CONFIG_BUG_ON_DATA_CORRUPTION
+#define REFCOUNT_CORRUPTION(str) ({		\
+	pr_err("refcount_t: " str ".\n");	\
+	BUG();					\
+})
+#else
+#define REFCOUNT_CORRUPTION(str) REFCOUNT_WARN(str)
+#endif

Can you use CHECK_DATA_CORRUPTION() here instead of open-coding the BUG()?

Either way:

Will

author
#2 lore raw
Quoted 32 lines · Will Deacon
Quoted 31 lines · Jann Horn

Some refcount issues are not necessarily associated with memory corruption, but REFCOUNT_ADD_UAF suggests that a UAF either just happened or is about to happen.

REFCOUNT_SUB_UAF is also an indicator that reference counting is wrong, and suggests (less strongly) that a UAF access might have happened recently.

In these cases, BUG() is appropriate if CONFIG_BUG_ON_DATA_CORRUPTION is set.

---
MAINTAINERS specifies no specific maintainer for lib/refcount.c, but it does have an entry for include/linux/refcount.h, so I guess I should route this patch based on that.

I decided to send this patch after wondering how exploitable it would be to have a refcount_inc() call on an object which has reached refcount 0, but is not yet freed because of something like an RCU grace period.

---
 lib/refcount.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
lib/refcount.c +0 -0
index a207a8f22b3c..c0f0dc5296eb 100644
@@ -10,6 +10,15 @@
 
 #define REFCOUNT_WARN(str)   WARN_ONCE(1, "refcount_t: " str ".\n")
 
Quoted 10 lines

lib/refcount.c line 20

Quoted 8 lines
lib/refcount.c +8 -0
+#ifdef CONFIG_BUG_ON_DATA_CORRUPTION
+#define REFCOUNT_CORRUPTION(str) ({          \
+     pr_err("refcount_t: " str ".\n");       \
+     BUG();                                  \
+})
+#else
+#define REFCOUNT_CORRUPTION(str) REFCOUNT_WARN(str)
+#endif

Can you use CHECK_DATA_CORRUPTION() here instead of open-coding the BUG()?

I was considering that, but decided not to for two reasons:

1. CHECK_DATA_CORRUPTION() uses WARN(), while the refcounting code uses WARN_ONCE() so that a saturated refcount doesn't WARN() every time it is touched.
2. CHECK_DATA_CORRUPTION() is currently marked with __must_check (which can't reliably be suppressed on all compilers by casting the result to void), see
https://lore.kernel.org/all/[email protected]/
.

Quoted 3 lines

Either way:

Thanks!

#3 lore raw
In reply to Jann Horn
Quoted 7 lines · Jann Horn

Some refcount issues are not necessarily associated with memory corruption, but REFCOUNT_ADD_UAF suggests that a UAF either just happened or is about to happen.

REFCOUNT_SUB_UAF is also an indicator that reference counting is wrong, and suggests (less strongly) that a UAF access might have happened recently.

Quoted 8 lines

In these cases, BUG() is appropriate if CONFIG_BUG_ON_DATA_CORRUPTION is set.

---
MAINTAINERS specifies no specific maintainer for lib/refcount.c, but it does have an entry for include/linux/refcount.h, so I guess I should route this patch based on that.

Right, I'll stick this in tip/locking/core, where most all atomic things go. Thanks!

merge bot Applied
#4 lore raw
In reply to Jann Horn
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     5b5224220747029bbaee8ab851cc6c599c48eded
Gitweb:        https://git.kernel.org/tip/5b5224220747029bbaee8ab851cc6c599c48eded
Author:        Jann Horn <[email protected]>
AuthorDate:    Wed, 12 Aug 2026 20:56:38 +02:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Thu, 01 Oct 2026 14:00:40 +02:00

refcount: Use CONFIG_BUG_ON_DATA_CORRUPTION for UAF-related errors

Some refcount issues are not necessarily associated with memory corruption, but REFCOUNT_ADD_UAF suggests that a UAF either just happened or is about to happen.

REFCOUNT_SUB_UAF is also an indicator that reference counting is wrong, and suggests (less strongly) that a UAF access might have happened recently.

In these cases, BUG() is appropriate if CONFIG_BUG_ON_DATA_CORRUPTION is set.

---
 lib/refcount.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
lib/refcount.c +12 -3
index a207a8f..c0f0dc5 100644
@@ -10,6 +10,15 @@
 
 #define REFCOUNT_WARN(str)	WARN_ONCE(1, "refcount_t: " str ".\n")
 
+#ifdef CONFIG_BUG_ON_DATA_CORRUPTION
+#define REFCOUNT_CORRUPTION(str) ({		\
+	pr_err("refcount_t: " str ".\n");	\
+	BUG();					\
+})
+#else
+#define REFCOUNT_CORRUPTION(str) REFCOUNT_WARN(str)
+#endif
+
 void refcount_warn_saturate(refcount_t *r, enum refcount_saturation_type t)
 {
 	refcount_set(r, REFCOUNT_SATURATED);
@@ -22,10 +31,10 @@ void refcount_warn_saturate(refcount_t *r, enum refcount_saturation_type t)
 		REFCOUNT_WARN("saturated; leaking memory");
 		break;
 	case REFCOUNT_ADD_UAF:
-		REFCOUNT_WARN("addition on 0; use-after-free");
+		REFCOUNT_CORRUPTION("addition on 0; use-after-free");
 		break;
 	case REFCOUNT_SUB_UAF:
-		REFCOUNT_WARN("underflow; use-after-free");
+		REFCOUNT_CORRUPTION("underflow; use-after-free");
 		break;
 	case REFCOUNT_DEC_LEAK:
 		REFCOUNT_WARN("decrement hit 0; leaking memory");
@@ -84,7 +93,7 @@ bool refcount_dec_not_one(refcount_t *r)
 
 		new = val - 1;
 		if (new > val) {
-			WARN_ONCE(new > val, "refcount_t: underflow; use-after-free.\n");
+			REFCOUNT_CORRUPTION("underflow; use-after-free");
 			return true;
 		}