Fix segfault when fake request interacts with ngx_http_realip_module#712
Open
DenGontsov wants to merge 1 commit into
Open
Fix segfault when fake request interacts with ngx_http_realip_module#712DenGontsov wants to merge 1 commit into
DenGontsov wants to merge 1 commit into
Conversation
Initialize sockaddr for fake connection to prevent null pointer dereference in ngx_cidr_match() called by ngx_http_realip_module. Problem: - nchan creates fake requests with sockaddr == NULL - realip module calls ngx_cidr_match(sa=0x0) causing segfault Solution: - Allocate and initialize sockaddr in nchan_create_fake_connection() - Use loopback address (127.0.0.1) as dummy sockaddr Tested with: - nginx 1.30.1 - nchan 1.3.8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Nchan creates fake requests with
r->connection->sockaddr == NULL. When any module that performs IP-based access checks processes such a request (e.g.,ngx_http_access_modulewithallow/deny,ngx_http_realip_module,ngx_http_geo_module), it callsngx_cidr_match(sa=0x0), causing a segmentation fault.Steps to Reproduce
allow 1.1.1.1; deny all;ngx_cidr_match (sa=0x0, ...)Backtrace (GDB)
Root Cause
The
allow/denydirective fromngx_http_access_module(and similar IP-checking modules likerealip,geo) callsngx_cidr_match()to validate the client IP address. When Nchan creates a fake request for internal subrequests, thesockaddrfield of the connection isNULL. This causes a null pointer dereference inngx_cidr_match()when it attempts to accesssa->sa_family.Solution
Initialize
sockaddrfor fake connections innchan_create_fake_connection()to a valid loopback address:struct sockaddr_infrom the connection poolsin_family = AF_INETsin_addr.s_addr = htonl(INADDR_LOOPBACK)(127.0.0.1)c->socklen = sizeof(struct sockaddr_in)This ensures that any module calling
ngx_cidr_match()receives a validsockaddrpointer instead ofNULL.Testing
allow/denyallow/denyallow/denyrealipenabledTest Environment
Affected Versions
All nchan versions that create fake requests with
sockaddr == NULL(likely all versions) are affected when used with:ngx_http_access_module(allow/deny)ngx_http_realip_modulengx_http_geo_modulengx_cidr_match()or accessesr->connection->sockaddrAdditional Notes
This fix is backward compatible and does not affect existing functionality. The loopback address is a safe dummy value because fake requests are internal and their IP address is never used for actual network routing.
Please also give credit to Nazim Khalilov for researching this bug.