⬢ DragonFlyBSD Kernel Audit
DF-2987 / fix.diff
← back to finding ↓ download raw
--- a/sys/sys/refcount.h	2026-06-29 12:51:19.000000000 +0000
+++ b/sys/sys/refcount.h	2026-10-26 03:27:38.934321340 +0000
@@ -60,13 +60,21 @@
 static __inline int
 refcount_release(volatile u_int *countp)
 {
-	return ((atomic_fetchadd_int(countp, -1) & ~REFCNTF_WAITING) == 1);
+	u_int old = atomic_fetchadd_int(countp, -1);
+
+	KASSERT((old & ~REFCNTF_WAITING) != 0,
+	    ("refcount_release: counter is zero, double-put?"));
+	return ((old & ~REFCNTF_WAITING) == 1);
 }
 
 static __inline int
 refcount_release_n(volatile u_int *countp, u_int i)
 {
-	return ((atomic_fetchadd_int(countp, -i) & ~REFCNTF_WAITING) == i);
+	u_int old = atomic_fetchadd_int(countp, -i);
+
+	KASSERT(i > 0 && (old & ~REFCNTF_WAITING) >= i,
+	    ("refcount_release_n: underflow, double-put?"));
+	return ((old & ~REFCNTF_WAITING) == i);
 }
 
 /*