libceph: fix potential out-of-bounds read in decode_new_up_state_weight()
Subsystems (2)
PATCH 1/1 libceph: fix potential out-of-bounds read in decode_new_up_state_weight()
The new_state section of an incremental OSD map is validated and skipped using a byte count computed as
len *= sizeof(u32) + (struct_v >= 5 ? sizeof(u32) : sizeof(u8));
The multiplication is evaluated in size_t, but the result is stored back into the u32 "len", truncating it. A malicious or corrupted incremental map can supply a new_state element count >= 0x20000000 (struct_v >= 5) so that len * 8 wraps modulo 2^32 to a small value. The following ceph_decode_need() then validates far fewer bytes than the section actually occupies.
new_state is then reprocessed with the unchecked ceph_decode_32() and ceph_decode_8() helpers, which have no per-iteration bounds check and rely entirely on that truncated up-front validation. This can lead to a kernel out-of-bounds read past "end".
Compute the byte count in u64 and bounds-check it against the remaining buffer before skipping, mirroring the size_t-typed length checks used elsewhere in this file (e.g. decode_crush_names(), decode_pg_mapping()). The osd index used for the osd_state[] write is already bounds-checked against map->max_osd, so this is an out-of-bounds read, not a write.
--- net/ceph/osdmap.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
net/ceph/osdmap.c +5 -3
@@ -1842,6 +1842,7 @@ static int decode_new_up_state_weight(void **p, void *end, u8 struct_v,void *new_up_client;void *new_state;void *new_weight_end; +u64 skip_len;u32 len;int ret;int i;
@@ -1862,9 +1863,10 @@ static int decode_new_up_state_weight(void **p, void *end, u8 struct_v, new_state = *p;ceph_decode_32_safe (p, end, len, e_inval); - len *=sizeof (u32) + (struct_v >=5 ?sizeof (u32) :sizeof (u8)); -ceph_decode_need (p, end, len, e_inval); - *p += len; + skip_len = (u64 )len * (sizeof (u32) + (struct_v >=5 ?sizeof (u32) :sizeof (u8))); +if (skip_len > end - *p) + goto e_inval; + *p += skip_len;/* new_weight */ ceph_decode_32_safe (p, end, len, e_inval);
---
base-commit: dbe8d05c9750b107b10c15361aad40fbb350bedb
change-id: 20260606-ceph-fix-final-16f4e1df5a5e
Best regards,
Signature · 1 line
Zhenhao Wan <[email protected]>
Quoted 62 lines · Zhenhao Wan
The
new_statesection of an incremental OSD map is validated and skipped using a byte count computed aslen *= sizeof(u32) + (struct_v >= 5 ? sizeof(u32) : sizeof(u8));The multiplication is evaluated in
size_t, but the result is stored back into the u32 "len", truncating it. A malicious or corrupted incremental map can supply anew_stateelement count >=0x20000000(struct_v>= 5) so that len * 8 wraps modulo 2^32 to a small value. The followingceph_decode_need()then validates far fewer bytes than the section actually occupies.
new_stateis then reprocessed with the uncheckedceph_decode_32()andceph_decode_8()helpers, which have no per-iteration bounds check and rely entirely on that truncated up-front validation. This can lead to a kernel out-of-bounds read past "end".Compute the byte count in u64 and bounds-check it against the remaining buffer before skipping, mirroring the size_t-typed length checks used elsewhere in this file (e.g.
decode_crush_names(),decode_pg_mapping()). The osd index used for the osd_state[] write is already bounds-checked againstmap->max_osd, so this is an out-of-bounds read, not a write.Fixes 930c53286977 ("libceph: apply new_state before new_up_client on incrementals")Reported-by Yuhao Jiang <[email protected]>Signed-off-by Zhenhao Wan <[email protected]>--- net/ceph/osdmap.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)net/ceph/osdmap.c +5
-4@@ -1842,6 +1842,7 @@ static int decode_new_up_state_weight(void **p, void *end, u8 struct_v,void *new_up_client;void *new_state;void *new_weight_end; +u64 skip_len;u32 len;int ret;int i;@@ -1862,9 +1863,10 @@ static int decode_new_up_state_weight(void **p, void *end, u8 struct_v, new_state = *p;ceph_decode_32_safe (p, end, len, e_inval); - len *=sizeof (u32) + (struct_v >=5 ?sizeof (u32) :sizeof (u8)); -ceph_decode_need (p, end, len, e_inval); - *p += len; + skip_len = (u64 )len * (sizeof (u32) + (struct_v >=5 ?sizeof (u32) :sizeof (u8))); +if (skip_len > end - *p) + goto e_inval; + *p += skip_len;/* new_weight */ ceph_decode_32_safe (p, end, len, e_inval); ---base-commit:
dbe8d05c9750b107b10c15361aad40fbb350bedb
change-id: 20260606-ceph-fix-final-16f4e1df5a5e
Quoted 3 lines
Best regards,
--
Zhenhao Wan <[email protected]>
Hi,
a patch for this issue has already been discussed on the ceph-devel mailing list here:
https://lore.kernel.org/ceph-devel/[email protected]/T/#t
Best regards,
Raphael
Alex Markuze
maintainerHi Zhenhao,
NACK for now. Thanks for the patch, but I have some concerns that need a v2:
1. [major] (process): Competing patch unresolved on-list
2. [nit] net/ceph/osdmap.c:1866: Open-coded bounds check deserves a comment
Signature · 1 line
Alex Markuze