Skip to contentSkip to conversation

ceph: fix potential overflow in parse_reply_info_dir()

PATCH New Viacheslav Dubeyko 1 participant 1 message Sep 16, 2025

Subsystems (2)
lore
Share & tools
lore

Viacheslav Dubeyko

author maintainer
#0 lore raw

PATCH 1/1 ceph: fix potential overflow in parse_reply_info_dir()

From: Viacheslav Dubeyko <[email protected]>

The parse_reply_info_dir() logic tries to parse a dir fragment:

struct ceph_mds_reply_dirfrag {

	__le32 frag;            /* fragment */
	__le32 auth;            /* auth mds, if this is a delegation point */
	__le32 ndist;           /* number of mds' this is replicated on */
	__le32 dist[];
} __attribute__ ((packed));

Potentially, ndist field could be corrupted or to have invalid or malicious value. As a result, this logic could result in overflow:

*p += sizeof(**dirfrag) + sizeof(u32) * le32_to_cpu((*dirfrag)->ndist);

Al Viro suggested the initial vision of the fix. The suggested fix was partially reworked.

This patch adds the checking that ndist is not bigger than (U32_MAX / sizeof(u32)) and to check that we have enough space in memory buffer by means of ceph_decode_need().

---
 fs/ceph/mds_client.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
fs/ceph/mds_client.c +13 -3
index 3bc72b47fe4d..245f5fb97f2e 100644
@@ -283,6 +283,11 @@ static int parse_reply_info_dir(void **p, void *end,
 				struct ceph_mds_reply_dirfrag **dirfrag,
 				u64 features)
 {
+	size_t item_size = sizeof(u32);
+	size_t dirfrag_size;
+	u32 ndist;
+	u32 array_size;
+
 	if (features == (u64)-1) {
 		u8 struct_v, struct_compat;
 		u32 struct_len;
@@ -297,11 +302,16 @@ static int parse_reply_info_dir(void **p, void *end,
 		end = *p + struct_len;
 	}
 
-	ceph_decode_need(p, end, sizeof(**dirfrag), bad);
+	dirfrag_size = sizeof(**dirfrag);
+	ceph_decode_need(p, end, dirfrag_size, bad);
 	*dirfrag = *p;
-	*p += sizeof(**dirfrag) + sizeof(u32) * le32_to_cpu((*dirfrag)->ndist);
-	if (unlikely(*p > end))
+	*p += dirfrag_size;
+	ndist = le32_to_cpu((*dirfrag)->ndist);
+	if (unlikely(ndist > (U32_MAX / item_size)))
 		goto bad;
+	array_size = ndist * item_size;
+	ceph_decode_need(p, end, array_size, bad);
+	*p += array_size;
 	if (features == (u64)-1)
 		*p = end;
 	return 0;
Signature · 1 line
2.51.0