Skip to content

Commit c7ed4dd

Browse files
defanatorFelipe Zimmerle
authored andcommitted
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 4846528 commit c7ed4dd

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
@@ -279,9 +279,13 @@ ngx_http_modsecurity_load_request(ngx_http_request_t *r)
279279
{
280280
ngx_http_modsecurity_ctx_t *ctx;
281281
request_rec *req;
282-
ngx_str_t str;
283282
size_t root;
284283
ngx_str_t path;
284+
ngx_uint_t port;
285+
struct sockaddr_in *sin;
286+
#if (NGX_HAVE_INET6)
287+
struct sockaddr_in6 *sin6;
288+
#endif
285289

286290
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity);
287291
req = ctx->req;
@@ -324,10 +328,30 @@ ngx_http_modsecurity_load_request(ngx_http_request_t *r)
324328
req->parsed_uri.path = (char *)ngx_pstrdup0(r->pool, &r->uri);
325329
req->parsed_uri.is_initialized = 1;
326330

327-
str.data = r->port_start;
328-
str.len = r->port_end - r->port_start;
329-
req->parsed_uri.port = ngx_atoi(str.data, str.len);
330-
req->parsed_uri.port_str = (char *)ngx_pstrdup0(r->pool, &str);
331+
switch (r->connection->local_sockaddr->sa_family) {
332+
333+
#if (NGX_HAVE_INET6)
334+
case AF_INET6:
335+
sin6 = (struct sockaddr_in6 *) r->connection->local_sockaddr;
336+
port = ntohs(sin6->sin6_port);
337+
break;
338+
#endif
339+
340+
#if (NGX_HAVE_UNIX_DOMAIN)
341+
case AF_UNIX:
342+
port = 0;
343+
break;
344+
#endif
345+
346+
default: /* AF_INET */
347+
sin = (struct sockaddr_in *) r->connection->local_sockaddr;
348+
port = ntohs(sin->sin_port);
349+
break;
350+
}
351+
352+
req->parsed_uri.port = port;
353+
req->parsed_uri.port_str = ngx_pnalloc(r->pool, sizeof("65535"));
354+
(void) ngx_sprintf((u_char *)req->parsed_uri.port_str, "%ui%c", port, '\0');
331355

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

0 commit comments

Comments
 (0)