openresty / openresty/stream-lua-nginx-module
Potential `NULL` dereference issue in the function ngx_stream_lua_ngx_flush (ngx_stream_lua_output.c)
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 749
- Forks
- 210
- Avg merge
- 4h 31m
- Merged PRs (30d)
- 3
Description
Hello! I analyzed Nginx modules with Svace static analyzer. It found a potential problem in the code in /stream-lua-nginx-module/src/ngx_stream_lua_output.c
Brief Description
There is a potential NULL dereference issue in the function ngx_stream_lua_ngx_flush. Specifically, the return value of the function ngx_stream_lua_get_req(L) is used without checking for NULL. If ngx_stream_lua_get_req(L) returns NULL, subsequent operations on the pointer r will result in undefined behavior, likely causing a segmentation fault or crash.
The problematic code snippet is as follows:
r = ngx_stream_lua_get_req(L);
ctx = ngx_stream_lua_get_module_ctx(r, ngx_stream_lua_module);
Here, r is dereferenced without verifying that it is not NULL.
Solution
To address this issue, we need to add a check for NULL after calling ngx_stream_lua_get_req(L). If r is NULL, the function should return an appropriate error message using luaL_error.
Patch
Below is the patch to fix the issue:
diff --git a/src/ngx_stream_lua_ngx_flush.c b/src/ngx_stream_lua_ngx_flush.c
--- a/src/ngx_stream_lua_ngx_flush.c
+++ b/src/ngx_stream_lua_ngx_flush.c
@@ -16,6 +16,9 @@ ngx_stream_lua_ngx_flush(lua_State *L)
r = ngx_stream_lua_get_req(L);
+ if (r == NULL) {
+ return luaL_error(L, "no request found");
+ }
ctx = ngx_stream_lua_get_module_ctx(r, ngx_stream_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no request ctx found");
Explanation of the Patch
- Check for
NULL: After callingngx_stream_lua_get_req(L), the patch adds a check to ensure thatris notNULL.if (r == NULL) { return luaL_error(L, "no request found"); } - Error Handling: If
risNULL, the function immediately returns an error message ("no request found") usingluaL_error. This prevents further execution and avoids dereferencing aNULLpointer.
Found by Linux Verification Center (linuxtesting.org) with SVACE.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/ngx_stream_lua_ngx_flush.c at ngx_stream_lua_ngx_flush and compare it with the reported location in ngx_stream_lua_output.c. Trace ngx_stream_lua_get_req(L) and the existing context-error path, then verify that the reported NULL case is handled without changing normal request behavior. The issue does not name a test file, so confirm the project’s relevant test procedure before checking the result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, lua
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100