Skip to content

Commit dbf1ebd

Browse files
Lua library Integer overflow can cause the DragonFly crash. (CVE-2020-14147) (#5421)
* CVE-2020-14147 * test: fix LuaIntOverflow expectation (should fail on overflow)
1 parent 9b8901f commit dbf1ebd

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/core/interpreter_test.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,4 +522,8 @@ TEST_F(InterpreterTest, AvoidIntOverflow) {
522522
EXPECT_EQ("str(0000FFFF)", ser_.res);
523523
}
524524

525+
TEST_F(InterpreterTest, LuaIntOverflow) {
526+
EXPECT_FALSE(Execute("EVAL \"struct.pack('>I2147483648', '10')\" 0"));
527+
}
528+
525529
} // namespace dfly

src/redis/lua/struct/lua_struct.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,20 @@ typedef struct Header {
8282
} Header;
8383

8484

85-
static int getnum (const char **fmt, int df) {
85+
static int getnum (lua_State *L, const char **fmt, int df) {
8686
if (!isdigit(**fmt)) /* no number? */
8787
return df; /* return default value */
8888
else {
8989
int a = 0;
9090
do {
91+
if (a > (INT_MAX / 10) || a * 10 > (INT_MAX - (**fmt - '0')))
92+
luaL_error(L, "integral size overflow");
9193
a = a*10 + *((*fmt)++) - '0';
9294
} while (isdigit(**fmt));
9395
return a;
9496
}
9597
}
9698

97-
9899
#define defaultoptions(h) ((h)->endian = native.endian, (h)->align = 1)
99100

100101

@@ -108,9 +109,9 @@ static size_t optsize (lua_State *L, char opt, const char **fmt) {
108109
case 'f': return sizeof(float);
109110
case 'd': return sizeof(double);
110111
case 'x': return 1;
111-
case 'c': return getnum(fmt, 1);
112+
case 'c': return getnum(L, fmt, 1);
112113
case 'i': case 'I': {
113-
int sz = getnum(fmt, sizeof(int));
114+
int sz = getnum(L, fmt, sizeof(int));
114115
if (sz > MAXINTSIZE)
115116
luaL_error(L, "integral size %d is larger than limit of %d",
116117
sz, MAXINTSIZE);
@@ -143,7 +144,7 @@ static void controloptions (lua_State *L, int opt, const char **fmt,
143144
case '>': h->endian = BIG; return;
144145
case '<': h->endian = LITTLE; return;
145146
case '!': {
146-
int a = getnum(fmt, MAXALIGN);
147+
int a = getnum(L, fmt, MAXALIGN);
147148
if (!isp2(a))
148149
luaL_error(L, "alignment %d is not a power of 2", a);
149150
h->align = a;

0 commit comments

Comments
 (0)