Updated ntdll-RtlIpv4StringToAddress patchset

This commit is contained in:
Alistair Leslie-Hughes 2020-03-01 13:31:46 +11:00
parent deeaa04aa4
commit 4ff3984c49

View File

@ -1,16 +1,16 @@
From 18fd7a7718f017b798f40de032ea60730d6f4bfa Mon Sep 17 00:00:00 2001
From f19447eff25e8823df559a8c9a2506b404f4da4b Mon Sep 17 00:00:00 2001
From: Alex Henrie <alexhenrie24@gmail.com>
Date: Sat, 1 Feb 2020 12:38:21 -0700
Subject: [PATCH 1/5] ntdll: Implement RtlIpv6StringToAddress(Ex)[AW]
Date: Wed, 26 Feb 2020 00:31:18 -0700
Subject: [PATCH] ntdll: Implement RtlIpv6StringToAddress(Ex)[AW]
Signed-off-by: Alex Henrie <alexhenrie24@gmail.com>
---
dlls/ntdll/ntdll.spec | 6 +-
dlls/ntdll/rtl.c | 259 +++++++++++++++++++++++++++++++++++++-----
2 files changed, 236 insertions(+), 29 deletions(-)
dlls/ntdll/rtl.c | 219 +++++++++++++++++++++++++++++++++++++++---
2 files changed, 211 insertions(+), 14 deletions(-)
diff --git a/dlls/ntdll/ntdll.spec b/dlls/ntdll/ntdll.spec
index fe36235bda..bcd9950a76 100644
index 57055ae34e..c715a1f612 100644
--- a/dlls/ntdll/ntdll.spec
+++ b/dlls/ntdll/ntdll.spec
@@ -778,10 +778,10 @@
@ -28,10 +28,10 @@ index fe36235bda..bcd9950a76 100644
@ stdcall RtlIsCriticalSectionLocked(ptr)
@ stdcall RtlIsCriticalSectionLockedByThread(ptr)
diff --git a/dlls/ntdll/rtl.c b/dlls/ntdll/rtl.c
index 15ff037fef..69f2f52743 100644
index 15ff037fef..11e76ed295 100644
--- a/dlls/ntdll/rtl.c
+++ b/dlls/ntdll/rtl.c
@@ -98,6 +98,21 @@ static const DWORD CRC_table[256] =
@@ -98,6 +98,16 @@ static const DWORD CRC_table[256] =
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
@ -44,21 +44,13 @@ index 15ff037fef..69f2f52743 100644
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5F */
+ -1, 10, 11, 12, 13, 14, 15 /* 0x60-0x66 */
+};
+
+static BOOL is_hex(WCHAR c)
+{
+ return c < ARRAY_SIZE(hex_table) && hex_table[c] != -1;
+}
+
/*
* resource functions
*/
@@ -884,18 +899,9 @@ void WINAPI RtlCopyLuidAndAttributesArray(
for (i = 0; i < Count; i++) Dest[i] = Src[i];
}
@@ -886,15 +896,6 @@ void WINAPI RtlCopyLuidAndAttributesArray(
-static BOOL parse_ipv4_component(const WCHAR **str, BOOL strict, ULONG *value)
+static BOOL parse_ip_component(const WCHAR **str, int base, BOOL strict, ULONG *value)
static BOOL parse_ipv4_component(const WCHAR **str, BOOL strict, ULONG *value)
{
- static const int hex_table[] = {
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
@ -69,81 +61,30 @@ index 15ff037fef..69f2f52743 100644
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5F */
- -1, 10, 11, 12, 13, 14, 15 /* 0x60-0x66 */
- };
- int base = 10, d;
+ int d;
int base = 10, d;
WCHAR c;
ULONG cur_value, prev_value = 0;
BOOL success = FALSE;
@@ -906,19 +912,23 @@ static BOOL parse_ipv4_component(const WCHAR **str, BOOL strict, ULONG *value)
return FALSE;
}
- if ((*str)[0] == '0')
+ if (!base)
{
- if ((*str)[1] == 'x' || (*str)[1] == 'X')
+ base = 10;
+ if ((*str)[0] == '0')
{
- *str += 2;
- if (strict) return FALSE;
- base = 16;
- }
- else if ((*str)[1] >= '0' && (*str)[1] <= '9')
- {
- *str += 1;
- if (strict) return FALSE;
- base = 8;
+ if ((*str)[1] == 'x' || (*str)[1] == 'X')
+ {
+ *str += 2;
+ if (strict) return FALSE;
+ base = 16;
+ }
+ else if ((*str)[1] >= '0' && (*str)[1] <= '9')
+ {
+ *str += 1;
+ if (strict) return FALSE;
+ base = 8;
+ }
}
}
@@ -946,7 +956,7 @@ static NTSTATUS ipv4_string_to_address(const WCHAR *str, BOOL strict,
for (;;)
{
- if (!parse_ipv4_component(&str, strict, &fields[n]))
+ if (!parse_ip_component(&str, 0, strict, &fields[n]))
goto error;
n++;
if (*str != '.')
@@ -1000,7 +1010,7 @@ static NTSTATUS ipv4_string_to_address(const WCHAR *str, BOOL strict,
if (*str == ':')
{
str++;
- if (!parse_ipv4_component(&str, FALSE, &fields[0]))
+ if (!parse_ip_component(&str, 0, FALSE, &fields[0]))
goto error;
if (!fields[0] || fields[0] > 0xFFFF || *str)
goto error;
@@ -1074,13 +1084,210 @@ NTSTATUS WINAPI RtlIpv4StringToAddressA(const char *str, BOOLEAN strict, const c
@@ -1074,13 +1075,209 @@ NTSTATUS WINAPI RtlIpv4StringToAddressA(const char *str, BOOLEAN strict, const c
return ret;
}
+static BOOL parse_ipv6_component(const WCHAR **str, int base, ULONG *value)
+{
+ WCHAR *terminator;
+ if (**str >= ARRAY_SIZE(hex_table) || hex_table[**str] == -1) return FALSE;
+ *value = min(strtoulW(*str, &terminator, base), 0x7FFFFFFF);
+ if (terminator == *str) return FALSE;
+ *str = terminator;
+ return TRUE;
+}
+
+static NTSTATUS ipv6_string_to_address(const WCHAR *str, BOOL ex,
+ const WCHAR **terminator, IN6_ADDR *address, ULONG *scope, USHORT *port)
+{
+ static const WCHAR hex_digits[] = {
+ '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','A','B','C','D','E','F',0
+ };
+ BOOL expecting_port = FALSE, ipv4_in_hex = FALSE;
+ BOOL expecting_port = FALSE, has_0x = FALSE, too_big = FALSE;
+ int n_bytes = 0, n_ipv4_bytes = 0, gap = -1;
+ ULONG ip_component, scope_component = 0, port_component = 0;
+ const WCHAR *component_terminator;
+
+ size_t component_len;
+ WCHAR delimiter;
+ const WCHAR *prev_str;
+
+ if (str[0] == '[')
+ {
@ -168,74 +109,69 @@ index 15ff037fef..69f2f52743 100644
+ /* double colon */
+ if (gap != -1) goto error;
+ str++;
+ if (is_hex(*str))
+ {
+ gap = -2; /* set the gap after the next component is successfully parsed */
+ }
+ else
+ {
+ gap = n_bytes;
+ break;
+ }
+ }
+ component_len = strspnW(str, hex_digits);
+ delimiter = str[component_len];
+ if (!n_ipv4_bytes && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') && is_hex(str[2]))
+ {
+ /* IPv4 address as single hex value */
+ if (n_bytes > 12) goto error;
+ component_terminator = str + 2;
+ if (!parse_ip_component(&component_terminator, 16, TRUE, &ip_component)) goto error;
+ if (terminator) *terminator = str + 1; /* Windows bug: terminator is off by one */
+ if (gap == -2) gap = n_bytes;
+ if (gap == -1 && *component_terminator == ':') return STATUS_INVALID_PARAMETER;
+ address->u.Byte[n_bytes++] = (ip_component & 0xFF000000) >> 24;
+ address->u.Byte[n_bytes++] = (ip_component & 0x00FF0000) >> 16;
+ address->u.Byte[n_bytes++] = (ip_component & 0x0000FF00) >> 8;
+ address->u.Byte[n_bytes++] = (ip_component & 0x000000FF);
+ str = component_terminator;
+ ipv4_in_hex = TRUE;
+ goto fill_gap;
+ }
+ if (!n_ipv4_bytes && delimiter == '.' && n_bytes <= 12)
+ {
+ component_terminator = str;
+ if (parse_ip_component(&component_terminator, 10, TRUE, &ip_component))
+ if (*component_terminator == delimiter)
+ n_ipv4_bytes = 1;
+ }
+ if (n_ipv4_bytes)
+ {
+ /* dotted IPv4 address */
+ if (!parse_ip_component(&str, 10, TRUE, &ip_component) || *str != delimiter) goto error;
+ if (delimiter != '.' && (n_ipv4_bytes < 4 || (n_bytes < 15 && gap == -1))) goto error;
+ if (delimiter != '.' && terminator) *terminator = str;
+ if (ip_component > 255) return STATUS_INVALID_PARAMETER;
+ if (gap == -2) gap = n_bytes;
+ address->u.Byte[n_bytes] = ip_component;
+ n_bytes++;
+ if (n_ipv4_bytes == 4) break;
+ n_ipv4_bytes++;
+ if (delimiter != '.') break;
+ prev_str = str;
+ gap = n_bytes;
+ if (n_bytes == 14 || !parse_ipv6_component(&str, 16, &ip_component)) break;
+ str = prev_str;
+ }
+ else
+ {
+ /* pure IPv6 */
+ if (!parse_ip_component(&str, 16, TRUE, &ip_component)) goto error;
+ if (delimiter != ':' && n_bytes < 14 && gap == -1) goto error;
+ if (delimiter != ':' && terminator) *terminator = str;
+ if (component_len > 4) return STATUS_INVALID_PARAMETER;
+ if (gap == -2) gap = n_bytes;
+ address->u.Word[n_bytes/2] = htons(ip_component);
+ n_bytes += 2;
+ if (delimiter != ':') break;
+ prev_str = str;
+ }
+ if (n_bytes == 16 || (n_bytes == 14 && gap != -1)) break;
+ if (gap != -1 && str[0] == ':' && str[1] == ':') break;
+
+ if (!n_ipv4_bytes && n_bytes <= (gap != -1 ? 10 : 12))
+ {
+ if (parse_ipv6_component(&str, 10, &ip_component) && *str == '.')
+ n_ipv4_bytes = 1;
+ str = prev_str;
+ }
+
+ if (n_ipv4_bytes)
+ {
+ /* IPv4 component */
+ if (!parse_ipv6_component(&str, 10, &ip_component)) goto error;
+ if (str - prev_str > 3 || ip_component > 255)
+ {
+ too_big = TRUE;
+ }
+ else
+ {
+ if (*str != '.' && (n_ipv4_bytes < 4 || (n_bytes < 15 && gap == -1))) goto error;
+ address->u.Byte[n_bytes] = ip_component;
+ n_bytes++;
+ }
+ if (n_ipv4_bytes == 4 || *str != '.') break;
+ n_ipv4_bytes++;
+ }
+ else
+ {
+ /* IPv6 component */
+ if (!parse_ipv6_component(&str, 16, &ip_component)) goto error;
+ if (prev_str[0] == '0' && (prev_str[1] == 'x' || prev_str[1] == 'X'))
+ {
+ /* Windows "feature": the last IPv6 component can start with "0x" and be longer than 4 digits */
+ if (terminator) *terminator = prev_str + 1; /* Windows says that the "x" is the terminator */
+ if (n_bytes < 14 && gap == -1) return STATUS_INVALID_PARAMETER;
+ address->u.Word[n_bytes/2] = htons(ip_component);
+ n_bytes += 2;
+ has_0x = TRUE;
+ goto fill_gap;
+ }
+ if (*str != ':' && n_bytes < 14 && gap == -1) goto error;
+ if (str - prev_str > 4)
+ too_big = TRUE;
+ else
+ address->u.Word[n_bytes/2] = htons(ip_component);
+ n_bytes += 2;
+ if (*str != ':' || (gap != -1 && str[1] == ':')) break;
+ }
+ if (n_bytes == (gap != -1 ? 14 : 16)) break;
+ if (too_big) return STATUS_INVALID_PARAMETER;
+ str++;
+ }
+
+ if (terminator) *terminator = str;
+ if (too_big) return STATUS_INVALID_PARAMETER;
+
+fill_gap:
+ if (gap == -1)
@ -250,12 +186,12 @@ index 15ff037fef..69f2f52743 100644
+
+ if (ex)
+ {
+ if (ipv4_in_hex) goto error;
+ if (has_0x) goto error;
+
+ if (*str == '%')
+ {
+ str++;
+ if (!parse_ip_component(&str, 10, TRUE, &scope_component)) goto error;
+ if (!parse_ipv4_component(&str, TRUE, &scope_component)) goto error;
+ }
+
+ if (expecting_port)
@ -265,7 +201,7 @@ index 15ff037fef..69f2f52743 100644
+ if (*str == ':')
+ {
+ str++;
+ if (!parse_ip_component(&str, 0, FALSE, &port_component)) goto error;
+ if (!parse_ipv4_component(&str, FALSE, &port_component)) goto error;
+ if (!port_component || port_component > 0xFFFF || *str) goto error;
+ port_component = htons(port_component);
+ }
@ -341,5 +277,5 @@ index 15ff037fef..69f2f52743 100644
/***********************************************************************
--
2.25.0
2.25.1