Project

General

Profile

Actions

Security #9049

closed
VJ VJ

luaxform: script-supplied return length not validated against the Lua string -> heap OOB read

Security #9049: luaxform: script-supplied return length not validated against the Lua string -> heap OOB read

Added by Victor Julien 2 months ago. Updated 7 days ago.

Status:
Closed
Priority:
Normal
Assignee:
Target version:
Affected Versions:
Label:
CVE:
Git IDs:

c545bfaef748536ef4d4c74866d4e1675832cc62

Severity:
LOW
Disclosure Date:

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.


Subtasks 1 (0 open — 1 closed)

Security #9299: luaxform: script-supplied return length not validated against the Lua string -> heap OOB read (8.0.x backport)ClosedVictor JulienActions

LS Updated by Lukas Sismis 2 months ago Actions #1

Also found by my tooling:

# L8 — luaxform byte-count heap OOB read

## Finding

- **File:** `src/detect-transform-luaxform.c`
- **Lines:** ~358–363 (`TransformLuaxform`)
- **Bug class:** Heap out-of-bounds READ (CWE-125), script-controlled length

`TransformLuaxform()` calls the rule's Lua `transform()` function with
`lua_pcall(state, 2, 2, 0)` — the script returns two values: a string and an
integer. The C code then does:

```c
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);
```

The count returned by the script is **trusted** and never validated against
the actual length of the returned string (which Lua reports via
`lua_rawlen`/`lua_tolstring`'s out-parameter). `InspectionBufferCopy()` does
`memcpy(buffer->buf, buf, count)` — so a script that returns `("B", 1000000)`
makes Suricata read 1,000,000 bytes out of a 1-byte Lua-allocated string.

## Impact

A rule writer (or anyone able to inject a rule + Lua script) can cause a heap
OOB read of arbitrary size. In a non-ASan build this reads adjacent heap
memory into the inspection buffer — the over-read data can then be matched
against `content`/`pcre` keywords in the same rule and appear in alert output
(eve.json `http.uri` etc.), i.e. an information-disclosure primitive, plus a
crash (SIGSEGV) if the read crosses into unmapped memory. The Lua sandbox
does not mitigate this: the sandbox limits instructions/allocations, not the
integrity of return values.

## How it reproduces

1. A rule uses `luaxform:transform.lua` on a sticky buffer (`http.uri` here).
2. `transform.lua`'s `transform()` returns `return "B", 1000000` — a 1-byte
   string with an inflated byte count.
3. A pcap containing an HTTP request drives `http.uri` inspection, the
   transform runs, and `InspectionBufferCopy` memcpy's 1e6 bytes from the
   1-byte Lua string.

## Expected signal

ASan (build at `/home/local/xsismi01/suricata-asan/src/suricata`):

```
ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 1000000
    #1 InspectionBufferCopy src/detect-engine-inspect-buffer.c:252
    #2 TransformLuaxform src/detect-transform-luaxform.c:362
0x... is located 0 bytes after 26-byte region ... allocated by LuaAlloc
```

exit code 134 (abort_on_error=1).

## How to run

L8 SV test

## Suggested fix direction

Validate the count against the string's real length, e.g.:

```c
size_t len = 0;
const char *s = lua_tolstring(tlua->luastate, -2, &len);
lua_Integer count = lua_tointeger(tlua->luastate, -1);
if (s && count > 0)
    InspectionBufferCopy(buffer, (uint8_t *)s, (uint32_t)MIN((size_t)count, len));
```

LS Updated by Lukas Sismis 2 months ago Actions #2

  • Affected Versions 8.0.7 added

VJ Updated by Victor Julien about 2 months ago Actions #3

  • Status changed from New to In Progress
  • Assignee changed from OISF Dev to Victor Julien

JI Updated by Jason Ish about 1 month ago Actions #4

  • Status changed from In Progress to In Review

MR1382

JI Updated by Jason Ish 26 days ago Actions #5

  • Subtask #9299 added

JI Updated by Jason Ish 26 days ago Actions #6

  • Severity set to LOW

JI Updated by Jason Ish 26 days ago Actions #7

  • GHSA set to GHSA-g834-qffv-4h52

JI Updated by Jason Ish 25 days ago Actions #8

  • Git IDs updated (diff)

JI Updated by Jason Ish 25 days ago Actions #9

  • Status changed from In Review to Closed

JI Updated by Jason Ish 7 days ago Actions #10

  • Private changed from Yes to No
Actions

Also available in: PDF Atom