Security #9049
closedluaxform: script-supplied return length not validated against the Lua string -> heap OOB read
Description
- Status: present in
main(9b7938aec); confirmed with ASAN - Class: heap out-of-bounds read (script-supplied length not validated against the Lua string)
- Component:
src/detect-transform-luaxform.c(TransformLuaxform)
Description¶
The luaxform transform expects the script to return (buffer, length) and copies the result into the inspection buffer without validating length against the real Lua string length (src/detect-transform-luaxform.c:325-345):
if (lua_isstring(tlua->luastate, -2)) {
const char *transformed_buffer = lua_tostring(tlua->luastate, -2);
lua_Integer transformed_buffer_byte_count = lua_tointeger(tlua->luastate, -1);
if (transformed_buffer != NULL && transformed_buffer_byte_count > 0)
InspectionBufferCopy(buffer, (uint8_t *)transformed_buffer,
(uint32_t)transformed_buffer_byte_count);
}
InspectionBufferCopy() expands the destination to buf_len and memcpy@s @MIN(buf_len, buffer->size) bytes from the source (src/detect-engine-inspect-buffer.c:251-259), so a returned length larger than the actual string reads past the end of the Lua string heap object. The (uint32_t) cast also truncates lua_Integer values above 4 GiB.
This is the same class as the fixed LuaFlowvarSet length bug (e281de36c), and the length is script-controlled and can be derived from packet data.
ASAN evidence (main \@ 9b7938aec)¶
Script function transform(data, args) return "A", 100000 end, rule http.uri; content:"/"; luaxform:xform.lua;:
==25791==ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 100000
#0 memcpy
#2 InspectionBufferCopy src/detect-engine-inspect-buffer.c:257
#3 TransformLuaxform src/detect-transform-luaxform.c:362
#4 InspectionBufferApplyTransformsInternal
...
Fix direction¶
Validate the returned count against the real string length and clamp the copy:
if (lua_isstring(tlua->luastate, -2)) {
size_t real_len = 0;
const char *data = lua_tolstring(tlua->luastate, -2, &real_len);
lua_Integer n = lua_tointeger(tlua->luastate, -1);
if (data != NULL && n > 0 && (size_t)n <= real_len && (size_t)n <= UINT32_MAX)
InspectionBufferCopy(buffer, (uint8_t *)data, (uint32_t)n);
}
Regression test: lua-transform-length-mismatch (return length > string length, negative, float, zero) under ASAN; assert no OOB.