Skip to content

August 5, 2026 · 2 min read

Lua 5.3 and 5.4 misread long string lengths on 32-bit builds

While sponsoring UIUCTF 2026 we found that string.unpack truncates 64-bit lengths on 32-bit builds of Lua 5.3 and 5.4.

While sponsoring UIUCTF 2026 we found a bug in Lua’s standard library. On 32-bit builds of Lua 5.3 and 5.4, string.unpack accepts string lengths that it should reject. We disclosed it and then built a challenge around it for the competition.

The bug

string.unpack can read a string that is preceded by its own length. With the format s8, that length is an 8-byte integer. Lua reads the eight bytes into a 64-bit integer and casts the result to size_t before checking it against the remaining data.

On builds where size_t is 32 bits wide and Lua integers are 64, such as wasm32 and i386, the cast discards the upper half of the length. A record that declares a length of 4,294,967,308 bytes is read as 12 bytes long.

The eight bytes of the length field. A 32-bit size_t keeps the low four bytes, reads 12, and accepts the record. A 64-bit size_t reads 4,294,967,308 and rejects it.

-- An 8-byte length of 0x10000000c, followed by 12 bytes of data.
local record = string.pack("<I8", 0x10000000c) .. "hello, world"

print((string.unpack("<s8", record)))
-- 64-bit build:  error, data string too short
-- wasm32, i386:  hello, world

Affected versions

Every release from 5.3.0 to 5.4.9 is affected when built with a 32-bit size_t and 64-bit Lua integers. The truncating cast is in the source of all 17 releases. We ran 5.3.6, 5.4.0, 5.4.8, and 5.4.9 on wasm32, and each one accepts the bad length.

Lua 5.5.0 and 5.5.1 are not affected. On wasm32 both raise data string too short. 64-bit builds and LUA_32BITS builds of the older releases reject the length as well.

Severity

The truncated length is still checked against the remaining data, so Lua never reads past the end of the string and no memory is corrupted.

The consequence is that an affected build accepts a malformed record as valid, and that 32-bit and 64-bit builds decode the same bytes differently. A system that validates a record with one build and processes it with the other can be given input that each of them interprets its own way.

Disclosure

We disclosed the issue before writing the challenge. Upstream had already fixed it in commit ec65ab8 on June 21, 2024, and the fix shipped in Lua 5.5.0. It is not listed on the Lua bugs page, which calls 5.4.9 the last release of Lua 5.4. The 5.4 line never received the fix and has ended, so every 5.3 and 5.4 release remains affected.

Mitigation

We recommend upgrading to Lua 5.5.

If you embed Lua 5.4 on a 32-bit target and cannot upgrade yet, a five-line patch equivalent to the upstream change applies to 5.4.8 and 5.4.9. Patched builds reject the bad lengths on wasm32, i386, and x86_64 and still decode valid records. The patch does not apply to 5.3.x. Email [email protected] and we will send it to you.

The UIUCTF challenge

After the disclosure we wrote “Sparxie: Vanishing Encore”, a pwn challenge for UIUCTF 2026 that runs on a 32-bit Lua runtime. Competitors have to find the same truncation and use it to get a record with an out-of-range length accepted.

We also sponsored the King of the Hill challenges by providing compute on our network.

  • Research
  • Disclosure
  • CTF
All posts