Skip to content

Commit 5335587

Browse files
defanatorFelipe Zimmerle
authored and
Felipe Zimmerle
committed
Obtain port from r->connection->local_sockaddr.
This eliminates segfaults caused by unset (NULL) r->port_start and non-NULL r->port_end. In fact, r->port_start is always NULL, so it is useless to rely on this pointer.
1 parent 9c0229c commit 5335587

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

nginx/modsecurity/ngx_http_modsecurity.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,13 @@ ngx_http_modsecurity_load_request(ngx_http_request_t *r)
207207
{
208208
ngx_http_modsecurity_ctx_t *ctx;
209209
request_rec *req;
210-
ngx_str_t str;
211210
size_t root;
212211
ngx_str_t path;
212+
ngx_uint_t port;
213+
struct sockaddr_in *sin;
214+
#if (NGX_HAVE_INET6)
215+
struct sockaddr_in6 *sin6;
216+
#endif
213217

214218
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity);
215219
req = ctx->req;
@@ -252,10 +256,30 @@ ngx_http_modsecurity_load_request(ngx_http_request_t *r)
252256
req->parsed_uri.path = (char *)ngx_pstrdup0(r->pool, &r->uri);
253257
req->parsed_uri.is_initialized = 1;
254258

255-
str.data = r->port_start;
256-
str.len = r->port_end - r->port_start;
257-
req->parsed_uri.port = ngx_atoi(str.data, str.len);
258-
req->parsed_uri.port_str = (char *)ngx_pstrdup0(r->pool, &str);
259+
switch (r->connection->local_sockaddr->sa_family) {
260+
261+
#if (NGX_HAVE_INET6)
262+
case AF_INET6:
263+
sin6 = (struct sockaddr_in6 *) r->connection->local_sockaddr;
264+
port = ntohs(sin6->sin6_port);
265+
break;
266+
#endif
267+
268+
#if (NGX_HAVE_UNIX_DOMAIN)
269+
case AF_UNIX:
270+
port = 0;
271+
break;
272+
#endif
273+
274+
default: /* AF_INET */
275+
sin = (struct sockaddr_in *) r->connection->local_sockaddr;
276+
port = ntohs(sin->sin_port);
277+
break;
278+
}
279+
280+
req->parsed_uri.port = port;
281+
req->parsed_uri.port_str = ngx_pnalloc(r->pool, sizeof("65535"));
282+
(void) ngx_sprintf((u_char *)req->parsed_uri.port_str, "%ui%c", port, '\0');
259283

260284
req->parsed_uri.query = r->args.len ? req->args : NULL;
261285
req->parsed_uri.dns_looked_up = 0;

0 commit comments

Comments
 (0)