]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
smb: transfer debugassert to real check
authorStefan Eissing <stefan@eissing.org>
Fri, 10 Oct 2025 09:28:29 +0000 (11:28 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Sun, 12 Oct 2025 13:27:05 +0000 (15:27 +0200)
That also works for non-debug builds.

Reported-by: Joshua Rogers
Cloes #19003

lib/smb.c

index f7d06eb4900eb2db0b1c7a12d92fda468fe81b47..2aa8e96644a9168500d8c4a01a3cef543caaf24e 100644 (file)
--- a/lib/smb.c
+++ b/lib/smb.c
@@ -545,7 +545,7 @@ static CURLcode smb_recv_message(struct Curl_easy *data,
   char *buf = smbc->recv_buf;
   size_t bytes_read;
   size_t nbt_size;
-  size_t msg_size;
+  size_t msg_size = sizeof(struct smb_header);
   size_t len = MAX_MESSAGE_SIZE - smbc->got;
   CURLcode result;
 
@@ -565,10 +565,19 @@ static CURLcode smb_recv_message(struct Curl_easy *data,
   nbt_size = Curl_read16_be((const unsigned char *)
                             (buf + sizeof(unsigned short))) +
     sizeof(unsigned int);
+  if(nbt_size > MAX_MESSAGE_SIZE) {
+    failf(data, "too large NetBIOS frame size %zu", nbt_size);
+    return CURLE_RECV_ERROR;
+  }
+  else if(nbt_size < msg_size) {
+    /* Each SMB message must be at least this large, e.g. 32 bytes */
+    failf(data, "too small NetBIOS frame size %zu", nbt_size);
+    return CURLE_RECV_ERROR;
+  }
+
   if(smbc->got < nbt_size)
     return CURLE_OK;
 
-  msg_size = sizeof(struct smb_header);
   if(nbt_size >= msg_size + 1) {
     /* Add the word count */
     msg_size += 1 + ((unsigned char) buf[msg_size]) * sizeof(unsigned short);
@@ -577,7 +586,7 @@ static CURLcode smb_recv_message(struct Curl_easy *data,
       msg_size += sizeof(unsigned short) +
         Curl_read16_le((const unsigned char *)&buf[msg_size]);
       if(nbt_size < msg_size)
-        return CURLE_READ_ERROR;
+        return CURLE_RECV_ERROR;
     }
   }
 
@@ -661,7 +670,10 @@ static CURLcode smb_send_message(struct Curl_easy *data,
 {
   smb_format_message(smbc, req, (struct smb_header *)smbc->send_buf,
                      cmd, msg_len);
-  DEBUGASSERT((sizeof(struct smb_header) + msg_len) <= MAX_MESSAGE_SIZE);
+  if((sizeof(struct smb_header) + msg_len) > MAX_MESSAGE_SIZE) {
+    DEBUGASSERT(0);
+    return CURLE_SEND_ERROR;
+  }
   memcpy(smbc->send_buf + sizeof(struct smb_header), msg, msg_len);
 
   return smb_send(data, smbc, sizeof(struct smb_header) + msg_len, 0);