Skip to content

Commit f6353e9

Browse files
Mike PallBuristan
Mike Pall
authored andcommitted
Fix zero stripping in %g number formatting.
Reported by pwnhacker0x18. (cherry picked from commit 343ce0e) LuaJIT uses `lj_strfmt_wfnum()` for number formatting. In the situation when the precision (`prec`) and amount of digits (`hilen`) for the decimal representation are the same and `ndhi` == 0, the `ndlo` part becomes 64 (the size of the `nd` stack buffer), and the overflow occurs. This patch adds the corresponding mask (0x3f == 63) for the `ndlo` incrementation result. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#9595
1 parent 022e3c7 commit f6353e9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/lj_strfmt_num.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ static char *lj_strfmt_wfnum(SBuf *sb, SFormat sf, lua_Number n, char *p)
454454
prec--;
455455
if (!i) {
456456
if (ndlo == ndhi) { prec = 0; break; }
457-
lj_strfmt_wuint9(tail, nd[++ndlo]);
457+
ndlo = (ndlo + 1) & 0x3f;
458+
lj_strfmt_wuint9(tail, nd[ndlo]);
458459
i = 9;
459460
}
460461
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local tap = require('tap')
2+
3+
-- Test file to demonstrate stack-buffer-overflow in the
4+
-- `lj_strfmt_wfnum()` call.
5+
-- See also: https://github.com/LuaJIT/LuaJIT/issues/1149.
6+
7+
local test = tap.test('lj-1149-g-number-formating-bufov')
8+
test:plan(1)
9+
10+
-- XXX: The test shows stack-buffer-overflow only under ASAN.
11+
-- The number value for the test has the same precision
12+
-- (`prec` = 5) and amount of digits (`hilen` = 5) for the decimal
13+
-- representation. Hence, with `ndhi` == 0, the `ndlo` part
14+
-- becomes 64 (the size of the `nd` stack buffer), and the overflow
15+
-- occurs.
16+
-- See details in the <src/lj_strfmt_num.c>:`lj_strfmt_wfnum()`.
17+
test:is(string.format('%7g', 0x1.144399609d407p+401), '5.5733e+120',
18+
'correct format %7g result')
19+
20+
test:done(true)

0 commit comments

Comments
 (0)