Skip to contentSkip to conversation

ocfs2: dlm: validate query region message length and fields

PATCH New Jenny Guanni Qu 1 participant 1 message Mar 13 OCFS2

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocfs2: dlm: validate query region message length and fields

dlm_query_region_handler() casts msg->buf to struct dlm_query_region and accesses its fields without verifying that the received message length is at least sizeof(struct dlm_query_region). A short message from a cluster peer can cause out-of-bounds reads.

Additionally, qr->qr_namelen is not validated before being passed to __dlm_lookup_domain_full() which uses it as a memcmp length, qr->qr_numregions is used as a loop bound for iterating qr_regions[] without checking it is within O2NM_MAX_REGIONS, and qr->qr_domain is logged with %s which reads until a NUL terminator that may not exist within the received payload.

Add a message length check, validate qr_namelen against O2NM_MAX_NAME_LEN, validate qr_numregions against O2NM_MAX_REGIONS, and use %.*s for bounded logging.

The OOB read pattern was confirmed with KASAN using a test module that accesses fields past a short kmalloc'd buffer matching the handler's cast pattern.

Fixes ea2034416b54 ("ocfs2/dlm: Add message DLM_QUERY_REGION")
---
 fs/ocfs2/dlm/dlmdomain.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
fs/ocfs2/dlm/dlmdomain.c +11 -2
index 2018501b2249..d81e41bd231b 100644
@@ -1100,10 +1100,19 @@ static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
 	char *local = NULL;
 	int status = 0;
 
+	if (len < sizeof(struct dlm_query_region))
+		return -EINVAL;
+
 	qr = (struct dlm_query_region *) msg->buf;
 
-	mlog(0, "Node %u queries hb regions on domain %s\n", qr->qr_node,
-	     qr->qr_domain);
+	if (qr->qr_namelen >= O2NM_MAX_NAME_LEN)
+		return -EINVAL;
+
+	if (qr->qr_numregions >= O2NM_MAX_REGIONS)
+		return -EINVAL;
+
+	mlog(0, "Node %u queries hb regions on domain %.*s\n", qr->qr_node,
+	     qr->qr_namelen, qr->qr_domain);
 
 	/* buffer used in dlm_mast_regions() */
 	local = kmalloc(sizeof(qr->qr_regions), GFP_KERNEL);
Signature · 1 line
2.34.1