ocfs2: dlm: validate bit index in refmap helpers
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.
--- fs/ocfs2/dlm/dlmmaster.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
fs/ocfs2/dlm/dlmmaster.c +10 -0
@@ -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
Joseph Qi
maintainerQuoted 11 lines · Jenny Guanni Qu
dlm_lockres_set_refmap_bit()anddlm_lockres_clear_refmap_bit()pass the bit parameter directly to set_bit()/clear_bit() onres->refmapwithout checking that it is withinO2NM_MAX_NODES. Since refmap is sized for exactlyO2NM_MAX_NODESbits, an out-of-range value causes an out-of-bounds write past the bitmap.The bit value originates from
request->node_idxin network message handlers such asdlm_master_request_handler(), which do not validatenode_idxbefore callingset_refmap_bit. In contrast,dlm_deref_lockres_handler()already checksnode_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