[PATCH] Core: fix conversion of IPv4-mapped IPv6 addresses

Piotr Sikora piotr at aviatrix.com
Wed Feb 28 01:21:49 UTC 2024


# HG changeset patch
# User Piotr Sikora <piotr at aviatrix.com>
# Date 1708977626 0
#      Mon Feb 26 20:00:26 2024 +0000
# Branch patch007
# Node ID 5584232259d28489efba149f2f5ae730691ff0d4
# Parent  03e5549976765912818120e11f6b08410a2af6a9
Core: fix conversion of IPv4-mapped IPv6 addresses.

Found with UndefinedBehaviorSanitizer (shift).

Signed-off-by: Piotr Sikora <piotr at aviatrix.com>

diff -r 03e554997676 -r 5584232259d2 src/core/ngx_inet.c
--- a/src/core/ngx_inet.c	Mon Feb 26 20:00:23 2024 +0000
+++ b/src/core/ngx_inet.c	Mon Feb 26 20:00:26 2024 +0000
@@ -507,10 +507,10 @@
 
             p = inaddr6->s6_addr;
 
-            inaddr = p[12] << 24;
-            inaddr += p[13] << 16;
-            inaddr += p[14] << 8;
-            inaddr += p[15];
+            inaddr = (in_addr_t) p[12] << 24;
+            inaddr += (in_addr_t) p[13] << 16;
+            inaddr += (in_addr_t) p[14] << 8;
+            inaddr += (in_addr_t) p[15];
 
             inaddr = htonl(inaddr);
         }
diff -r 03e554997676 -r 5584232259d2 src/http/modules/ngx_http_access_module.c
--- a/src/http/modules/ngx_http_access_module.c	Mon Feb 26 20:00:23 2024 +0000
+++ b/src/http/modules/ngx_http_access_module.c	Mon Feb 26 20:00:26 2024 +0000
@@ -148,10 +148,10 @@
         p = sin6->sin6_addr.s6_addr;
 
         if (alcf->rules && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
-            addr = p[12] << 24;
-            addr += p[13] << 16;
-            addr += p[14] << 8;
-            addr += p[15];
+            addr = (in_addr_t) p[12] << 24;
+            addr += (in_addr_t) p[13] << 16;
+            addr += (in_addr_t) p[14] << 8;
+            addr += (in_addr_t) p[15];
             return ngx_http_access_inet(r, alcf, htonl(addr));
         }
 
diff -r 03e554997676 -r 5584232259d2 src/http/modules/ngx_http_geo_module.c
--- a/src/http/modules/ngx_http_geo_module.c	Mon Feb 26 20:00:23 2024 +0000
+++ b/src/http/modules/ngx_http_geo_module.c	Mon Feb 26 20:00:26 2024 +0000
@@ -199,10 +199,10 @@
         p = inaddr6->s6_addr;
 
         if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {
-            inaddr = p[12] << 24;
-            inaddr += p[13] << 16;
-            inaddr += p[14] << 8;
-            inaddr += p[15];
+            inaddr = (in_addr_t) p[12] << 24;
+            inaddr += (in_addr_t) p[13] << 16;
+            inaddr += (in_addr_t) p[14] << 8;
+            inaddr += (in_addr_t) p[15];
 
             vv = (ngx_http_variable_value_t *)
                       ngx_radix32tree_find(ctx->u.trees.tree, inaddr);
@@ -272,10 +272,10 @@
             if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {
                 p = inaddr6->s6_addr;
 
-                inaddr = p[12] << 24;
-                inaddr += p[13] << 16;
-                inaddr += p[14] << 8;
-                inaddr += p[15];
+                inaddr = (in_addr_t) p[12] << 24;
+                inaddr += (in_addr_t) p[13] << 16;
+                inaddr += (in_addr_t) p[14] << 8;
+                inaddr += (in_addr_t) p[15];
 
             } else {
                 inaddr = INADDR_NONE;
diff -r 03e554997676 -r 5584232259d2 src/http/modules/ngx_http_geoip_module.c
--- a/src/http/modules/ngx_http_geoip_module.c	Mon Feb 26 20:00:23 2024 +0000
+++ b/src/http/modules/ngx_http_geoip_module.c	Mon Feb 26 20:00:26 2024 +0000
@@ -266,10 +266,10 @@
         if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {
             p = inaddr6->s6_addr;
 
-            inaddr = p[12] << 24;
-            inaddr += p[13] << 16;
-            inaddr += p[14] << 8;
-            inaddr += p[15];
+            inaddr = (in_addr_t) p[12] << 24;
+            inaddr += (in_addr_t) p[13] << 16;
+            inaddr += (in_addr_t) p[14] << 8;
+            inaddr += (in_addr_t) p[15];
 
             return inaddr;
         }
diff -r 03e554997676 -r 5584232259d2 src/stream/ngx_stream_access_module.c
--- a/src/stream/ngx_stream_access_module.c	Mon Feb 26 20:00:23 2024 +0000
+++ b/src/stream/ngx_stream_access_module.c	Mon Feb 26 20:00:26 2024 +0000
@@ -144,10 +144,10 @@
         p = sin6->sin6_addr.s6_addr;
 
         if (ascf->rules && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
-            addr = p[12] << 24;
-            addr += p[13] << 16;
-            addr += p[14] << 8;
-            addr += p[15];
+            addr = (in_addr_t) p[12] << 24;
+            addr += (in_addr_t) p[13] << 16;
+            addr += (in_addr_t) p[14] << 8;
+            addr += (in_addr_t) p[15];
             return ngx_stream_access_inet(s, ascf, htonl(addr));
         }
 
diff -r 03e554997676 -r 5584232259d2 src/stream/ngx_stream_geo_module.c
--- a/src/stream/ngx_stream_geo_module.c	Mon Feb 26 20:00:23 2024 +0000
+++ b/src/stream/ngx_stream_geo_module.c	Mon Feb 26 20:00:26 2024 +0000
@@ -190,10 +190,10 @@
         p = inaddr6->s6_addr;
 
         if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {
-            inaddr = p[12] << 24;
-            inaddr += p[13] << 16;
-            inaddr += p[14] << 8;
-            inaddr += p[15];
+            inaddr = (in_addr_t) p[12] << 24;
+            inaddr += (in_addr_t) p[13] << 16;
+            inaddr += (in_addr_t) p[14] << 8;
+            inaddr += (in_addr_t) p[15];
 
             vv = (ngx_stream_variable_value_t *)
                       ngx_radix32tree_find(ctx->u.trees.tree, inaddr);
@@ -263,10 +263,10 @@
             if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {
                 p = inaddr6->s6_addr;
 
-                inaddr = p[12] << 24;
-                inaddr += p[13] << 16;
-                inaddr += p[14] << 8;
-                inaddr += p[15];
+                inaddr = (in_addr_t) p[12] << 24;
+                inaddr += (in_addr_t) p[13] << 16;
+                inaddr += (in_addr_t) p[14] << 8;
+                inaddr += (in_addr_t) p[15];
 
             } else {
                 inaddr = INADDR_NONE;
diff -r 03e554997676 -r 5584232259d2 src/stream/ngx_stream_geoip_module.c
--- a/src/stream/ngx_stream_geoip_module.c	Mon Feb 26 20:00:23 2024 +0000
+++ b/src/stream/ngx_stream_geoip_module.c	Mon Feb 26 20:00:26 2024 +0000
@@ -236,10 +236,10 @@
         if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {
             p = inaddr6->s6_addr;
 
-            inaddr = p[12] << 24;
-            inaddr += p[13] << 16;
-            inaddr += p[14] << 8;
-            inaddr += p[15];
+            inaddr = (in_addr_t) p[12] << 24;
+            inaddr += (in_addr_t) p[13] << 16;
+            inaddr += (in_addr_t) p[14] << 8;
+            inaddr += (in_addr_t) p[15];
 
             return inaddr;
         }


More information about the nginx-devel mailing list