perf branch: Fix heap out-of-bounds write in branch_type_count()
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 7 lines · Yujun Dong
The
branch_type_count()function writes to st->new_counts[flags->new_type] whenflags->typeisPERF_BR_EXTEND_ABI. However, while the arrayst->new_countsis sized forPERF_BR_NEW_MAX(8) entries, the fieldflags->new_typeis a 4-bit unsigned value and may hold values up to 15.This mismatch allows crafted perf data to trigger a heap out-of-bounds write when
flags->new_type>= 8, leading to memory corruption.
Crafted ? How could flags->new_type >= 8 when PERF_BR_NEW_MAX is capped at 8. Is this a real scenario that happened on a system ?
Quoted 3 lines
Add a bounds check to ensure
flags->new_typeis less thanPERF_BR_NEW_MAXbefore accessing thenew_countsarray.
But it might make sense to add this check just to be on the safer side.
Quoted 27 lines
Fixes 0ddea8e2a0c2 ("perf branch: Extend branch type classification")Signed-off-by Yujun Dong <[email protected]>--- tools/perf/util/branch.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)tools/perf/util/branch.c +5
-3@@ -21,10 +21,12 @@ void branch_type_count(struct branch_type_stat *st, struct branch_flags *flags,if (flags->type ==PERF_BR_UNKNOWN || from ==0 )return ; -if (flags->type ==PERF_BR_EXTEND_ABI ) - st->new_counts[flags->new_type]++; -else +if (flags->type ==PERF_BR_EXTEND_ABI ) { +if (flags->new_type <PERF_BR_NEW_MAX ) + st->new_counts[flags->new_type]++; + }else { st->counts[flags->type]++; + }if (flags->type ==PERF_BR_COND ) {if (to > from)
Quoted 6 lines · Anshuman Khandual
Quoted 5 lines · Yujun Dong
The
branch_type_count()function writes to st->new_counts[flags->new_type] whenflags->typeisPERF_BR_EXTEND_ABI. However, while the arrayst->new_countsis sized forPERF_BR_NEW_MAX(8) entries, the fieldflags->new_typeis a 4-bit unsigned value and may hold values up to 15.
Quoted 6 lines
Quoted 2 lines
This mismatch allows crafted perf data to trigger a heap out-of-bounds write when
flags->new_type>= 8, leading to memory corruption.Crafted ? How could
flags->new_type>= 8 whenPERF_BR_NEW_MAXis capped at 8. Is this a real scenario that happened on a system ?
Thanks for your review and for raising that question. The new_type field in struct branch_flags is declared as a 4-bit bitfield (u64 new_type:4), meaning it can hold values from 0 to 15, even though PERF_BR_NEW_MAX is defined as 8. So, it's entirely possible for flags->new_type to be >= 8.
In fact, I've observed such cases when running real-world perf record/top, where perf.data produced contains invalid new_type values, likely due to other bugs or unexpected data corruption. Additionally, a maliciously crafted perf.data file can also force this out-of-bounds write.
Quoted 6 lines
Quoted 3 lines
Add a bounds check to ensure
flags->new_typeis less thanPERF_BR_NEW_MAXbefore accessing thenew_countsarray.But it might make sense to add this check just to be on the safer side.
Notably, new_type is only used in two places:
1. In branch_new_type_name(), where the bounds are already validated.
2. In branch_type_count(), where the current patch now adds the necessary check.
Admittedly, the mismatch between the bit-field width (0-15) and PERF_BR_NEW_MAX (8) is the root cause. While adjusting the bit-field to match PERF_BR_NEW_MAX would also resolve the mismatch, that risks breaking existing compatibility. Therefore, adding a bounds check at the use site is the least disruptive correction.
Quoted 28 lines
Quoted 27 lines
Fixes 0ddea8e2a0c2 ("perf branch: Extend branch type classification")Signed-off-by Yujun Dong <[email protected]>---
tools/perf/util/branch.c| 8 +++++---1 file changed, 5 insertions(+), 3 deletions(-)tools/perf/util/branch.c +5
-3@@ -21,10 +21,12 @@ void branch_type_count(struct branch_type_stat *st, struct branch_flags *flags, if (flags->type == PERF_BR_UNKNOWN || from == 0) return; - if (flags->type == PERF_BR_EXTEND_ABI) - st->new_counts[flags->new_type]++; - else + if (flags->type == PERF_BR_EXTEND_ABI) { + if (flags->new_type < PERF_BR_NEW_MAX) + st->new_counts[flags->new_type]++; + } else { st->counts[flags->type]++; + } if (flags->type == PERF_BR_COND) { if (to > from)
Quoted 10 lines · Yujun Dong
Quoted 9 lines · Anshuman Khandual
Quoted 7 lines · Yujun Dong
The
branch_type_count()function writes to st->new_counts[flags->new_type] whenflags->typeisPERF_BR_EXTEND_ABI. However, while the arrayst->new_countsis sized forPERF_BR_NEW_MAX(8) entries, the fieldflags->new_typeis a 4-bit unsigned value and may hold values up to 15.This mismatch allows crafted perf data to trigger a heap out-of-bounds write when
flags->new_type>= 8, leading to memory corruption.
Quoted 8 lines
Quoted 3 lines
Crafted ? How could
flags->new_type>= 8 whenPERF_BR_NEW_MAXis capped at 8. Is this a real scenario that happened on a system ?Thanks for your review and for raising that question. The
new_typefield in structbranch_flagsis declared as a 4-bit bitfield (u64 new_type:4), meaning it can hold values from 0 to 15, even thoughPERF_BR_NEW_MAXis defined as 8. So, it's entirely possible forflags->new_typeto be >= 8.
Sure it is possible but not probable I guess as new_type itself would be first guarded by PERF_BR_NEW_MAX.
Quoted 5 lines
In fact, I've observed such cases when running real-world perf record/top, where perf.data produced contains invalid
new_typevalues, likely due to other bugs or unexpected data corruption. Additionally, a maliciously crafted perf.data file can also force this out-of-bounds write.
Agreed.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Add a bounds check to ensure
flags->new_typeis less thanPERF_BR_NEW_MAXbefore accessing thenew_countsarray.
Quoted 7 lines
Quoted 2 lines
But it might make sense to add this check just to be on the safer side.
Notably,
new_typeis only used in two places:
1. Inbranch_new_type_name(), where the bounds are already validated.
2. Inbranch_type_count(), where the current patch now adds the necessary check.
Agreed - this change will ensure consistency across both the functions.
Quoted 6 lines
Admittedly, the mismatch between the bit-field width (0-15) and
PERF_BR_NEW_MAX(8) is the root cause. While adjusting the bit-field to matchPERF_BR_NEW_MAXwould also resolve the mismatch, that risks breaking existing compatibility. Therefore, adding a bounds check at the use site is the least disruptive correction.
Right, increasing PERF_BR_NEW_MAX to 15 will not be desirable.
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Fixes 0ddea8e2a0c2 ("perf branch: Extend branch type classification")Signed-off-by Yujun Dong <[email protected]>---
tools/perf/util/branch.c| 8 +++++---1 file changed, 5 insertions(+), 3 deletions(-)tools/perf/util/branch.c +5
-3@@ -21,10 +21,12 @@ void branch_type_count(struct branch_type_stat *st, struct branch_flags *flags, if (flags->type == PERF_BR_UNKNOWN || from == 0) return; - if (flags->type == PERF_BR_EXTEND_ABI) - st->new_counts[flags->new_type]++; - else + if (flags->type == PERF_BR_EXTEND_ABI) { + if (flags->new_type < PERF_BR_NEW_MAX) + st->new_counts[flags->new_type]++; + } else { st->counts[flags->type]++; + } if (flags->type == PERF_BR_COND) { if (to > from)Reviewed-by Anshuman Khandual <[email protected]>