f47198
commit 4031c6c2b004190fdffbc19dab7cd0305a2025b7 (refs/remotes/origin/v4, refs/remotes/github/v4, refs/heads/v4)
f47198
Author: Amos Jeffries <yadij@users.noreply.github.com>
f47198
Date:   2022-08-09 23:34:54 +0000
f47198
f47198
    Bug 3193 pt2: NTLM decoder truncating strings (#1114)
f47198
    
f47198
    The initial bug fix overlooked large 'offset' causing integer
f47198
    wrap to extract a too-short length string.
f47198
    
f47198
    Improve debugs and checks sequence to clarify cases and ensure
f47198
    that all are handled correctly.
f47198
f47198
diff --git a/lib/ntlmauth/ntlmauth.cc b/lib/ntlmauth/ntlmauth.cc
f47198
index 5d9637290..f00fd51f8 100644
f47198
--- a/lib/ntlmauth/ntlmauth.cc
f47198
+++ b/lib/ntlmauth/ntlmauth.cc
f47198
@@ -107,10 +107,19 @@ ntlm_fetch_string(const ntlmhdr *packet, const int32_t packet_size, const strhdr
f47198
     int32_t o = le32toh(str->offset);
f47198
     // debug("ntlm_fetch_string(plength=%d,l=%d,o=%d)\n",packet_size,l,o);
f47198
 
f47198
-    if (l < 0 || l > NTLM_MAX_FIELD_LENGTH || o + l > packet_size || o == 0) {
f47198
-        debug("ntlm_fetch_string: insane data (pkt-sz: %d, fetch len: %d, offset: %d)\n", packet_size,l,o);
f47198
+    if (l < 0 || l > NTLM_MAX_FIELD_LENGTH) {
f47198
+        debug("ntlm_fetch_string: insane string length (pkt-sz: %d, fetch len: %d, offset: %d)\n", packet_size,l,o);
f47198
         return rv;
f47198
     }
f47198
+    else if (o <= 0 || o > packet_size) {
f47198
+        debug("ntlm_fetch_string: insane string offset (pkt-sz: %d, fetch len: %d, offset: %d)\n", packet_size,l,o);
f47198
+        return rv;
f47198
+    }
f47198
+    else if (l > packet_size - o) {
f47198
+        debug("ntlm_fetch_string: truncated string data (pkt-sz: %d, fetch len: %d, offset: %d)\n", packet_size,l,o);
f47198
+        return rv;
f47198
+    }
f47198
+
f47198
     rv.str = (char *)packet + o;
f47198
     rv.l = 0;
f47198
     if ((flags & NTLM_NEGOTIATE_ASCII) == 0) {