Mitigate possibility of DoS attack from throwing bad_alloc 02/309902/1
authorYonggoo Kang <ygace.kang@samsung.com>
Thu, 18 Apr 2024 05:33:57 +0000 (14:33 +0900)
committerYonggoo Kang <ygace.kang@samsung.com>
Thu, 18 Apr 2024 05:33:57 +0000 (14:33 +0900)
Change-Id: I5c45e0a2e11915f5a4388ec4d960fa1051b7eb20

srcs/common/message-buffer.h

index 8ed2657b2e324e2106b01410b27d225f1afcdfc5..1a13a70b109809c9fc54b01fbf3e7dbb1a3da21f 100644 (file)
@@ -253,8 +253,14 @@ public:
         }
 
         if (messageSize > m_bufferSize)
-            ReserveMessageSize(messageSize);
-
+        {
+            try{
+                ReserveMessageSize(messageSize);
+            } catch (const std::bad_alloc &e) {
+                LogError("Bad allocation while realloc: " << e.what());
+                return InputResult::ProtocolBroken;
+            }
+        }
         return InputResult::Pending;
     }
 
@@ -345,8 +351,14 @@ public:
         auto payloadSize = *reinterpret_cast<size_t*>(m_buffer);
 
         if (payloadSize > m_bufferSize)
-            ReserveMessageSize(payloadSize);
-
+        {
+            try{
+                ReserveMessageSize(payloadSize);
+            } catch (const std::bad_alloc &e) {
+                LogError("Bad allocation while realloc: " << e.what());
+                return 0;
+            }
+        }
         return payloadSize;
     }
 private: