Skip to content

Commit 4fb91d9

Browse files
bugfix: shdict ffi store/incr silently truncated keys > 65535 bytes.
ngx_stream_lua_shdict_node_t::key_len is a u_short (uint16). The ngx_stream_lua_ffi_shdict_store() and ngx_stream_lua_ffi_shdict_incr() entry points accepted key_len as size_t and assigned it through a (u_short) cast without bounds-checking, so a key longer than 65535 bytes was silently truncated when stored on the node, while the slab allocation and the key memcpy still used the untruncated length. The truncated sd->key_len was later used as an offset to locate the value, landing inside the key bytes and reading the wrong data or going past the value. Mirror the > 65535 check already enforced by the Lua-stack API path (see ngx_stream_lua_shdict_set_helper) at the FFI entry points so the truncating cast is unreachable. Mirrors the fix in lua-nginx-module.
1 parent ae32ae6 commit 4fb91d9

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

src/ngx_stream_lua_shdict.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,11 @@ ngx_stream_lua_ffi_shdict_store(ngx_shm_zone_t *zone, int op, u_char *key,
12441244

12451245
dd("exptime: %ld", exptime);
12461246

1247+
if (key_len > 65535) {
1248+
*errmsg = "key too long";
1249+
return NGX_ERROR;
1250+
}
1251+
12471252
ctx = zone->data;
12481253

12491254
*forcible = 0;
@@ -1651,6 +1656,11 @@ ngx_stream_lua_ffi_shdict_incr(ngx_shm_zone_t *zone, u_char *key,
16511656
tp = ngx_timeofday();
16521657
}
16531658

1659+
if (key_len > 65535) {
1660+
*err = "key too long";
1661+
return NGX_ERROR;
1662+
}
1663+
16541664
ctx = zone->data;
16551665

16561666
*forcible = 0;

0 commit comments

Comments
 (0)