From: "flichtenheld (Code Review)" Date: Fri, 12 Jan 2024 14:14:06 +0000 References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: <1b0f33af4b5532e492ab0c48b11f4f28bf456819-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="I+xGdGOU+OA="; charset=UTF-8 Subject: [Openvpn-devel] [S] Change in openvpn[master]: NTLM: increase size of phase 2 response we can handle List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos Cc: openvpn-devel --I+xGdGOU+OA= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/497?usp=3Demail to review the following change. Change subject: NTLM: increase size of phase 2 response we can handle ...................................................................... NTLM: increase size of phase 2 response we can handle With NTLMv2 the target information buffer can be rather large even with normal domain setups. In my test setup it was 152 bytes starting at offset 71. Overall the base64 encode phase 2 response was 300 byte long. The linked documentation has 98 bytes at offset 60. 128 byte is clearly too low. While here improve the error messaging, so that if the buffer is too small at least one can determine that in the log. Change-Id: Iefa4930cb1e8c4135056a17ceb4283fc13cc75c8 Signed-off-by: Frank Lichtenheld --- M src/openvpn/ntlm.c M src/openvpn/proxy.c 2 files changed, 10 insertions(+), 11 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/97/497/1 diff --git a/src/openvpn/ntlm.c b/src/openvpn/ntlm.c index 99d4ae7..2c6a69d 100644 --- a/src/openvpn/ntlm.c +++ b/src/openvpn/ntlm.c @@ -205,7 +205,7 @@ uint8_t challenge[8]; int i, ret_val; - uint8_t ntlmv2_response[144]; + uint8_t ntlmv2_response[256]; char userdomain_u[256]; /* for uppercase unicode username and doma= in */ char userdomain[128]; /* the same as previous but ascii */ uint8_t ntlmv2_hash[MD5_DIGEST_LENGTH]; @@ -255,17 +255,15 @@ * the missing bytes will be NULL, as buf2 is known to be zeroed * when this decode happens. */ - uint8_t buf2[128]; /* decoded reply from proxy */ + uint8_t buf2[512]; /* decoded reply from proxy */ CLEAR(buf2); ret_val =3D openvpn_base64_decode(phase_2, buf2, -1); if (ret_val < 0) { + msg(M_WARN, "NTLM: base64 decoding of phase 2 response failed"); return NULL; } - /* we can be sure that phase_2 is less than 128 - * therefore buf2 needs to be (3/4 * 128) */ - /* extract the challenge from bytes 24-31 */ for (i =3D 0; i<8; i++) { @@ -284,7 +282,7 @@ } else { - msg(M_INFO, "Warning: Username or domain too long"); + msg(M_INFO, "NTLM: Username or domain too long"); } unicodize(userdomain_u, userdomain); gen_hmac_md5((uint8_t *)userdomain_u, 2 * strlen(userdomain), md4_hash, @@ -319,9 +317,10 @@ if ((flags & 0x00800000) =3D=3D 0x00800000) { tib_len =3D buf2[0x28]; /* Get Target Information block= size */ - if (tib_len > 96) + if (tib_len + 0x1c + 16 > sizeof(ntlmv2_response)) { - tib_len =3D 96; + msg(M_WARN, "NTLM: target information buffer too long for resp= onse (len=3D%d)", tib_len); + return NULL; } { @@ -329,6 +328,7 @@ uint8_t tib_pos =3D buf2[0x2c]; if (tib_pos + tib_len > sizeof(buf2)) { + msg(M_ERR, "NTLM: phase 2 response from server too long (n= eed %d bytes at offset %u)", tib_len, tib_pos); return NULL; } /* Get Target Information block pointer */ diff --git a/src/openvpn/proxy.c b/src/openvpn/proxy.c index 5a1b4e1..e081532 100644 --- a/src/openvpn/proxy.c +++ b/src/openvpn/proxy.c @@ -752,8 +752,7 @@ { #if NTLM /* look for the phase 2 response */ - char buf2[129]; - + char buf2[512]; while (true) { if (!recv_line(sd, buf, sizeof(buf), get_server_poll_remai= ning_time(server_poll_timeout), true, NULL, signal_received)) @@ -764,9 +763,9 @@ msg(D_PROXY, "HTTP proxy returned: '%s'", buf); char get[80]; + CLEAR(buf2); openvpn_snprintf(get, sizeof(get), "%%*s NTLM %%%zus", siz= eof(buf2) - 1); nparms =3D sscanf(buf, get, buf2); - buf2[128] =3D 0; /* we only need the beginning - ensure it= 's null terminated. */ /* check for "Proxy-Authenticate: NTLM TlRM..." */ if (nparms =3D=3D 1) -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/497?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Iefa4930cb1e8c4135056a17ceb4283fc13cc75c8 Gerrit-Change-Number: 497 Gerrit-PatchSet: 1 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-MessageType: newchange --I+xGdGOU+OA= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: plaisthos.

flichtenheld would like plaisthos to review this change.

Vi= ew Change

NTLM: increase size of phase 2 response we can handle

= With NTLMv2 the target information buffer can be rather large
even with = normal domain setups.

In my test setup it was 152 bytes starting at = offset 71.
Overall the base64 encode phase 2 response was 300 byte long.=
The linked documentation has 98 bytes at offset 60. 128 byte
is clea= rly too low.

While here improve the error messaging, so that if the = buffer
is too small at least one can determine that in the log.

C= hange-Id: Iefa4930cb1e8c4135056a17ceb4283fc13cc75c8
Signed-off-by: Frank= Lichtenheld <frank@...2641...>
---
M src/openvpn/ntlm.c
M s= rc/openvpn/proxy.c
2 files changed, 10 insertions(+), 11 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/97/497=
/1
diff --git a/src/openvpn/ntlm.c b/src/openvpn/ntlm.c
<= span>index 99d4ae7..2c6a69d 100644
--- a/src/openvpn/ntlm.c=
+++ b/src/openvpn/ntlm.c
@@ -205,7 +205,7 = @@
uint8_t challenge[8];
int i, r= et_val;

- uint8_t ntlmv2_response[144];
+ uint8_t ntlmv2_response[256];
ch= ar userdomain_u[256]; /* for uppercase unicode username and domain */
char userdomain[128]; /* the same as previous but= ascii */
uint8_t ntlmv2_hash[MD5_DIGEST_LENGTH];
@@ -255,17 +255,15 @@
* the missing bytes= will be NULL, as buf2 is known to be zeroed
* when t= his decode happens.
*/
- uint8_t buf2[128]; /* decoded reply from proxy *= /
+ uint8_t buf2[5= 12]; /* decoded reply from proxy */
CLEAR(buf2);
ret_val =3D openvpn_base64_decode(phase_2, buf2, -1);
if (ret_val < 0)
{
+ msg(M_WARN, "NTLM: base= 64 decoding of phase 2 response failed");
ret= urn NULL;
}

- /* we can be sure that phase_2 is less tha= n 128
- * therefore= buf2 needs to be (3/4 * 128) */
-
/* extract the challenge from bytes 24-31 = */
for (i =3D 0; i<8; i++)
{
@@ -284,7 +282,7 @@
}
= else
{
- msg(M_INFO, "Warning: Username or domain too long&qu= ot;);
+ msg(M_= INFO, "NTLM: Username or domain too long");
= }
unicodize(userdomain_u, userdomain);
gen_hmac_md5((uint8_t *)userdomain_u, 2 * strlen(userdomain), md4_has= h,

@@ -319,9 +317,10 @@
if ((flags &am= p; 0x00800000) =3D=3D 0x00800000)
{
= tib_len =3D buf2[0x28]; /* Get Target Information block s= ize */
- if (tib= _len > 96)
+ = if (tib_len + 0x1c + 16 > sizeof(ntlmv2_response))
= {
- ti= b_len =3D 96;
+ = msg(M_WARN, "NTLM: target information buffer too long for respon= se (len=3D%d)", tib_len);
+ return NULL;
}

{
@@ -329,6 +328,7 @@=
uint8_t tib_pos =3D buf2[0x2c];
= if (tib_pos + tib_len > sizeof(buf2))
= {
+ = msg(M_ERR, "NTLM: phase 2 response from server too long (need %d byt= es at offset %u)", tib_len, tib_pos);
= return NULL;
}
/= * Get Target Information block pointer */
diff --git a/src/= openvpn/proxy.c b/src/openvpn/proxy.c
index 5a1b4e1..e08153= 2 100644
--- a/src/openvpn/proxy.c
+++ b/sr= c/openvpn/proxy.c
@@ -752,8 +752,7 @@
= {
#if NTLM
/* look for th= e phase 2 response */
-= char buf2[129];
-
+ c= har buf2[512];
while (true)
= {
if (!recv_line(sd, buf, sizeo= f(buf), get_server_poll_remaining_time(server_poll_timeout), true, NULL, si= gnal_received))
@@ -764,9 +763,9 @@
= msg(D_PROXY, "HTTP proxy returned: '%s'", buf);=

char get[80];+ CLEAR(buf2);<= /span>
openvpn_snprintf(get, sizeof(get), "%= %*s NTLM %%%zus", sizeof(buf2) - 1);
= nparms =3D sscanf(buf, get, buf2);
- buf2[128] =3D 0; /* we only need the beginning= - ensure it's null terminated. */

= /* check for "Proxy-Authenticate: NTLM TlRM..." */=
if (nparms =3D=3D 1)

To view, visit change 497. To unsubscribe, or for help writing m= ail filters, visit settings<= /a>.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Iefa4930cb1e8c4135056a17ceb4= 283fc13cc75c8
Gerrit-Change-Number: 497
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-MessageType: newchange
--I+xGdGOU+OA=--