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