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.

Signed-off-by: Jann Horn <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Acked-by: Will Deacon <[email protected]>
Link: https://patch.msgid.link/20260812-refcount-bug-on-data-corruption-v1-1-c6da63566101@google.com
This commit is contained in:
Jann Horn
2026-10-01 14:00:40 +02:00
committed by Peter Zijlstra
parent 72d3fcf802
commit 5b52242207
+12 -3
View File
@@ -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;
}