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