Skip to content

Commit 4f83876

Browse files
recepasaniluuu1994
authored andcommitted
Fix out-of-bounds write in ext-bcmath bccomp() via bc_str2num()
When a fraction is truncated to a caller-supplied scale and then re-trimmed of trailing zeros, str_scale was reduced but fractional_end was not, so bc_copy_and_toggle_bcd() copied more bytes than were reserved by bc_new_num_nonzeroed(). With small numbers allocated from the 256-byte stack arena this is a stack-based OOB write reachable via bccomp($num1, $num2, $scale), e.g. bccomp("1.901", "0", 2). Keep fractional_end in sync with str_scale so the copy length matches the reserved length. Fixes GHSA-x692-q9x7-8c3f
1 parent d3f361c commit 4f83876

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

ext/bcmath/libbcmath/src/str2num.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ bool bc_str2num(bc_num *num, const char *str, const char *end, size_t scale, siz
181181
if (str_scale > 0) {
182182
const char *fractional_new_end = bc_skip_zero_reverse(fractional_end, fractional_ptr);
183183
str_scale -= fractional_end - fractional_new_end; /* fractional_end >= fractional_new_end */
184+
fractional_end = fractional_new_end;
184185
}
185186
}
186187
} else {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
GHSA-x692-q9x7-8c3f: bccomp() out-of-bounds write
3+
--CREDITS--
4+
Recep Asan (recepasan)
5+
--FILE--
6+
<?php
7+
8+
$n = '1.' . '9' . str_repeat('0', 300) . '1';
9+
var_dump(bccomp($n, '0', 300));
10+
11+
?>
12+
--EXPECT--
13+
int(1)

0 commit comments

Comments
 (0)