libceph: avoid infinite loop in ceph_pg_is_split()
Subsystems (2)
Alex Markuze
maintainerPATCH 1/1 libceph: avoid infinite loop in ceph_pg_is_split()
old_pg_num and new_pg_num come from the OSD map. When old_pg_num is at least 2^31, calc_bits_of() returns 32 and both (1 << 32) and n << (old_bits - 1) are undefined in 32-bit signed arithmetic. On machines that mask the shift count, next_bit toggles between 0 and 2^31 so s never reaches new_pg_num and the loop never exits. old_pg_num == 0 hits n << -1, which is also undefined.
Do the shift in u64, build the 2^n-1 mask without a 32-bit overflow, treat old_pg_num == 0 as not-a-split, and compute stable_mod in unsigned so seeds in the high half of the u32 range stay well-defined. A hostile map can no longer pin the client in this helper.
--- net/ceph/osdmap.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)
net/ceph/osdmap.c +24 -10
@@ -2307,25 +2307,39 @@ void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src)bool ceph_pg_is_split (const struct ceph_pg *pgid,u32 old_pg_num,u32 new_pg_num) { -int old_bits =calc_bits_of (old_pg_num); -int old_mask = (1 << old_bits) -1 ; -int n; +unsigned int old_bits; +u32 old_mask, n; -WARN_ON (pgid->seed >= old_pg_num); -if (new_pg_num <= old_pg_num) +if (!old_pg_num || new_pg_num <= old_pg_num)return false; -for (n =1 ; ; n++) { -int next_bit = n << (old_bits -1 ); -u32 s = next_bit | pgid->seed; +WARN_ON (pgid->seed >= old_pg_num); + + old_bits =calc_bits_of (old_pg_num); +if (old_bits >=32 ) + old_mask = ~0U ; +else + old_mask = (1U << old_bits) -1 ; + +for (n =1 ; n; n++) { +u64 next_bit, s; +u32 parent; + + next_bit = (u64 )n << (old_bits -1 ); +if (next_bit >= new_pg_num) +break ; + s = next_bit | pgid->seed;if (s < old_pg_num || s == pgid->seed)continue ;if (s >= new_pg_num)break ; - s =ceph_stable_mod (s, old_pg_num, old_mask); -if (s == pgid->seed) +if ((s & old_mask) < old_pg_num) + parent = s & old_mask; +else + parent = s & (old_mask >>1 ); +if (parent == pgid->seed)return true; }
Signature · 1 line
2.34.1
Hi Alex,
This also LGTM.
Thanks
- Xiubo
Quoted 79 lines · Alex Markuze
old_pg_numandnew_pg_numcome from the OSD map. Whenold_pg_numis at least 2^31,calc_bits_of()returns 32 and both (1 << 32) and n << (old_bits- 1) are undefined in 32-bit signed arithmetic. On machines that mask the shift count,next_bittoggles between 0 and 2^31 so s never reachesnew_pg_numand the loop never exits.old_pg_num== 0 hits n << -1, which is also undefined.Do the shift in u64, build the 2^n-1 mask without a 32-bit overflow, treat
old_pg_num== 0 as not-a-split, and computestable_modin unsigned so seeds in the high half of the u32 range stay well-defined. A hostile map can no longer pin the client in this helper.Reported-by Raphael Zimmer <[email protected]>Fixes 63244fa123a7 ("libceph: introduce ceph_osd_request_target, calc_target()")Signed-off-by Alex Markuze <[email protected]>--- net/ceph/osdmap.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)net/ceph/osdmap.c +24
-11@@ -2307,25 +2307,39 @@ void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src)bool ceph_pg_is_split (const struct ceph_pg *pgid,u32 old_pg_num,u32 new_pg_num) { -int old_bits =calc_bits_of (old_pg_num); -int old_mask = (1 << old_bits) -1 ; -int n; +unsigned int old_bits; +u32 old_mask, n; -WARN_ON (pgid->seed >= old_pg_num); -if (new_pg_num <= old_pg_num) +if (!old_pg_num || new_pg_num <= old_pg_num)return false; -for (n =1 ; ; n++) { -int next_bit = n << (old_bits -1 ); -u32 s = next_bit | pgid->seed; +WARN_ON (pgid->seed >= old_pg_num); + + old_bits =calc_bits_of (old_pg_num); +if (old_bits >=32 ) + old_mask = ~0U ; +else + old_mask = (1U << old_bits) -1 ; + +for (n =1 ; n; n++) { +u64 next_bit, s; +u32 parent; + + next_bit = (u64 )n << (old_bits -1 ); +if (next_bit >= new_pg_num) +break ; + s = next_bit | pgid->seed;if (s < old_pg_num || s == pgid->seed)continue ;if (s >= new_pg_num)break ; - s =ceph_stable_mod (s, old_pg_num, old_mask); -if (s == pgid->seed) +if ((s & old_mask) < old_pg_num) + parent = s & old_mask; +else + parent = s & (old_mask >>1 ); +if (parent == pgid->seed)return true; } --2.34.1