Skip to contentSkip to conversation

ocfs2: dlm: validate bit index in refmap helpers

PATCH In review Jenny Guanni Qu 2 participants 2 messages Mar 13 – Mar 16 OCFS2

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocfs2: dlm: validate bit index in refmap helpers

dlm_lockres_set_refmap_bit() and dlm_lockres_clear_refmap_bit() pass the bit parameter directly to set_bit()/clear_bit() on res->refmap without checking that it is within O2NM_MAX_NODES. Since refmap is sized for exactly O2NM_MAX_NODES bits, an out-of-range value causes an out-of-bounds write past the bitmap.

The bit value originates from request->node_idx in network message handlers such as dlm_master_request_handler(), which do not validate node_idx before calling set_refmap_bit. In contrast, dlm_deref_lockres_handler() already checks node_idx >= O2NM_MAX_NODES.

Add bounds checks in both refmap helpers to prevent OOB writes from any caller.

The OOB write pattern was confirmed with KASAN using a test module that calls set_bit() with an out-of-range index on a kmalloc'd bitmap matching the refmap layout.

Fixes 8d400b81cc83 ("ocfs2/dlm: Clean up refmap helpers")
---
 fs/ocfs2/dlm/dlmmaster.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
fs/ocfs2/dlm/dlmmaster.c +10 -0
index d610da8e2f24..08746e118466 100644
@@ -606,6 +606,11 @@ void dlm_lockres_set_refmap_bit(struct dlm_ctxt *dlm,
 	mlog(0, "res %.*s, set node %u, %ps()\n", res->lockname.len,
 	     res->lockname.name, bit, __builtin_return_address(0));
 
+	if (bit >= O2NM_MAX_NODES) {
+		mlog(ML_ERROR, "bit %d >= O2NM_MAX_NODES (%d)\n",
+		     bit, O2NM_MAX_NODES);
+		return;
+	}
 	set_bit(bit, res->refmap);
 }
 
@@ -617,6 +622,11 @@ void dlm_lockres_clear_refmap_bit(struct dlm_ctxt *dlm,
 	mlog(0, "res %.*s, clr node %u, %ps()\n", res->lockname.len,
 	     res->lockname.name, bit, __builtin_return_address(0));
 
+	if (bit >= O2NM_MAX_NODES) {
+		mlog(ML_ERROR, "bit %d >= O2NM_MAX_NODES (%d)\n",
+		     bit, O2NM_MAX_NODES);
+		return;
+	}
 	clear_bit(bit, res->refmap);
 }
 
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 11 lines · Jenny Guanni Qu

dlm_lockres_set_refmap_bit() and dlm_lockres_clear_refmap_bit() pass the bit parameter directly to set_bit()/clear_bit() on res->refmap without checking that it is within O2NM_MAX_NODES. Since refmap is sized for exactly O2NM_MAX_NODES bits, an out-of-range value causes an out-of-bounds write past the bitmap.

The bit value originates from request->node_idx in network message handlers such as dlm_master_request_handler(), which do not validate node_idx before calling set_refmap_bit. In contrast, dlm_deref_lockres_handler() already checks node_idx >= O2NM_MAX_NODES.

Quoted 7 lines

Add bounds checks in both refmap helpers to prevent OOB writes from any caller.

The OOB write pattern was confirmed with KASAN using a test module that calls set_bit() with an out-of-range index on a kmalloc'd bitmap matching the refmap layout.

Do you have a real reproducer to trigger this OOB access?

Thanks,
Joseph