Lua v5.4.0及之前版本lsys_load()栈溢出漏洞

 

漏洞描述:

Lua是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放。

Lua v5.4.0 及之前版本 lsys_load()函数加载动态链接库时没有正确处理文件名的长度,造成栈溢出漏洞,可造成拒绝服务问题。

 

影响版本:

Lua <= v5.4.0

 

漏洞复现:

$ git clone https://github.com/lua/lua.git
$ make
$ ./lua ./Stack_overflow_lsys_load.lua
AddressSanitizer:DEADLYSIGNAL
=================================================================
==90451==ERROR: AddressSanitizer: stack-overflow on address 0x7ffc56ae1e38 (pc 0x7f84c442acf7 bp 0x7ffc573773f0 sp 0x7ffc56ae1e40 T0)
    #0 0x7f84c442acf6  (/lib64/ld-linux-x86-64.so.2+0x7cf6)
    #1 0x7f84c442cd4d  (/lib64/ld-linux-x86-64.so.2+0x9d4d)
    #2 0x7f84c44379c3  (/lib64/ld-linux-x86-64.so.2+0x149c3)
    #3 0x7f84c31d148e in _dl_catch_exception (/lib/x86_64-linux-gnu/libc.so.6+0x15c48e)
    #4 0x7f84c44372c5  (/lib64/ld-linux-x86-64.so.2+0x142c5)
    #5 0x7f84c34a9255  (/lib/x86_64-linux-gnu/libdl.so.2+0x1255)
    #6 0x7f84c31d148e in _dl_catch_exception (/lib/x86_64-linux-gnu/libc.so.6+0x15c48e)
    #7 0x7f84c31d151e in _dl_catch_error (/lib/x86_64-linux-gnu/libc.so.6+0x15c51e)
    #8 0x7f84c34a9a24  (/lib/x86_64-linux-gnu/libdl.so.2+0x1a24)
    #9 0x7f84c34a92e5 in dlopen (/lib/x86_64-linux-gnu/libdl.so.2+0x12e5)
    #10 0x7f84c36c9a33  (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x8ea33)
    #11 0x56529c0e5908 in lsys_load /home/test/Lua/Test_lua/lua/loadlib.c:134
    #12 0x56529c0e5908 in lookforfunc /home/test/Lua/Test_lua/lua/loadlib.c:391
    #13 0x56529c0e5a14 in ll_loadlib /home/test/Lua/Test_lua/lua/loadlib.c:412
    #14 0x56529c09025c in luaD_call /home/test/Lua/Test_lua/lua/ldo.c:481
    #15 0x56529c0b8824 in luaV_execute /home/test/Lua/Test_lua/lua/lvm.c:1614
    #16 0x56529c0b8824 in luaV_execute /home/test/Lua/Test_lua/lua/lvm.c:1614
    #17 0x56529c090605 in luaD_callnoyield /home/test/Lua/Test_lua/lua/ldo.c:525
    #18 0x56529c08dc51 in luaD_rawrunprotected /home/test/Lua/Test_lua/lua/ldo.c:148
    #19 0x56529c0911e0 in luaD_pcall /home/test/Lua/Test_lua/lua/ldo.c:749
    #20 0x56529c086f8f in lua_pcallk /home/test/Lua/Test_lua/lua/lapi.c:1031
    #21 0x56529c08042a in docall /home/test/Lua/Test_lua/lua/lua.c:139
    #22 0x56529c08179d in handle_script /home/test/Lua/Test_lua/lua/lua.c:228
    #23 0x56529c08179d in pmain /home/test/Lua/Test_lua/lua/lua.c:603
    #24 0x56529c09025c in luaD_call /home/test/Lua/Test_lua/lua/ldo.c:481
    #25 0x56529c090605 in luaD_callnoyield /home/test/Lua/Test_lua/lua/ldo.c:525
    #26 0x56529c08dc51 in luaD_rawrunprotected /home/test/Lua/Test_lua/lua/ldo.c:148
    #27 0x56529c0911e0 in luaD_pcall /home/test/Lua/Test_lua/lua/ldo.c:749
    #28 0x56529c086f8f in lua_pcallk /home/test/Lua/Test_lua/lua/lapi.c:1031
    #29 0x56529c07fbda in main /home/test/Lua/Test_lua/lua/lua.c:629
    #30 0x7f84c309909a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2409a)
    #31 0x56529c080239 in _start (/home/test/Lua/Test_lua/lua/lua+0x18239)
SUMMARY: AddressSanitizer: stack-overflow (/lib64/ld-linux-x86-64.so.2+0x7cf6) 
==90451==ABORTING

 

漏洞分析:

漏洞发生在package.loadlib函数中,当用package.loadlib加载一个名字很长的动态链接库时,造成栈溢出:

 static void *lsys_load (lua_State *L, const char *path, int seeglb) {
    void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));  ---->crash
    if (lib == NULL) lua_pushstring(L, dlerror());
    return lib;
}

修复建议:

在加载动态链接库时限制动态链接库的长度。

漏洞演示:

https://www.bilibili.com/video/BV1s5411b7NT

(完)