CVE-2019-3558: Improper Handling of Exceptional Conditions (CWE-755)
value: Python Facebook Thrift servers would not error upon receiving messages with containers of fields of unknown type. As a result, malicious clients could send short messages which would take a long time for the server to parse, potentially leading to denial of service. This issue affects Facebook Thrift prior to v2019.02.18.00.
CWE description: The product does not handle or incorrectly handles an exceptional condition.
CVSS v3 base score: 7.5
Published: 2019-05-06T16:29Z
Repository: https://github.com/facebook/fbthrift
Language: C++
Files changed: thrift/lib/cpp/protocol/TProtocolException.cpp, thrift/lib/cpp/protocol/TProtocolException.h, thrift/lib/cpp2/protocol/Protocol.h, thrift/lib/py/protocol/TProtocol.py, thrift/test/ProtocolSkipTest.cpp
Commit date: 2019-02-14 14:38:42 -0800
Commit message: Throw on bad types during skipping data

Summary:
The current code silently returns on bad types. In case when we have an invalid data, we may get a container of a large size with a bad type, this would lead to us running long loop doing nothing (though we already can say that the data is invalid).

The new code would throw an exception as soon as we try to skip a value of invalid type.

Fixes CVE-2019-3552

Reviewed By: yfeldblum, stevegury

Differential Revision: D8344920

fbshipit-source-id: f12e8f3442f7ad5e1a81d822380701e929b80f0d

Diff: diff --git a/thrift/lib/cpp/protocol/TProtocolException.cpp b/thrift/lib/cpp/protocol/TProtocolException.cpp
index d0ad894502..ab84de2a72 100644
--- a/thrift/lib/cpp/protocol/TProtocolException.cpp
+++ b/thrift/lib/cpp/protocol/TProtocolException.cpp
@@ -55,6 +55,14 @@ namespace apache { namespace thrift { namespace protocol {
       TProtocolException::INVALID_DATA,
       folly::sformat(
           "Attempt to interpret value {} as bool, probably the data is corrupted",
           value));
 }
+
+[[noreturn]] void TProtocolException::throwInvalidSkipType(TType type) {
+  throw TProtocolException(
+      TProtocolException::INVALID_DATA,
+      folly::sformat(
+          "Encountered invalid field/element type ({}) during skipping",
+          static_cast<uint8_t>(type)));
+}
 }}}
diff --git a/thrift/lib/cpp/protocol/TProtocolException.h b/thrift/lib/cpp/protocol/TProtocolException.h
index 874654fa40..193d25d784 100644
--- a/thrift/lib/cpp/protocol/TProtocolException.h
+++ b/thrift/lib/cpp/protocol/TProtocolException.h
@@ -21,10 +21,11 @@
 
 #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_
 #define _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ 1
 
 #include <thrift/lib/cpp/Thrift.h>
+#include <thrift/lib/cpp/protocol/TType.h>
 
 #include <string>
 
 namespace apache { namespace thrift { namespace protocol {
 
@@ -104,10 +105,11 @@ class TProtocolException : public apache::thrift::TLibraryException {
   [[noreturn]] static void throwExceededSizeLimit();
   [[noreturn]] static void throwMissingRequiredField(
       folly::StringPiece field,
       folly::StringPiece type);
   [[noreturn]] static void throwBoolValueOutOfRange(uint8_t value);
+  [[noreturn]] static void throwInvalidSkipType(TType type);
 
  protected:
   /**
    * Error code
    */
diff --git a/thrift/lib/cpp2/protocol/Protocol.h b/thrift/lib/cpp2/protocol/Protocol.h
index d1cc63ed63..033668ac7b 100644
--- a/thrift/lib/cpp2/protocol/Protocol.h
+++ b/thrift/lib/cpp2/protocol/Protocol.h
@@ -174,12 +174,13 @@ void skip(Protocol_& prot, TType arg_type) {
         apache::thrift::skip(prot, elemType);
       }
       prot.readListEnd();
       return;
     }
-    default:
-      return;
+    default: {
+      TProtocolException::throwInvalidSkipType(arg_type);
+    }
   }
 }
 
 template <class StrType>
 struct StringTraits {
diff --git a/thrift/lib/py/protocol/TProtocol.py b/thrift/lib/py/protocol/TProtocol.py
index a229eb0254..252b53db43 100644
--- a/thrift/lib/py/protocol/TProtocol.py
+++ b/thrift/lib/py/protocol/TProtocol.py
@@ -175,13 +175,11 @@ class TProtocolBase:
 
     def readString(self):
         pass
 
     def skip(self, type):
-        if type == TType.STOP:
-            return
-        elif type == TType.BOOL:
+        if type == TType.BOOL:
             self.readBool()
         elif type == TType.BYTE:
             self.readByte()
         elif type == TType.I16:
             self.readI16()
@@ -218,10 +216,15 @@ class TProtocolBase:
         elif type == TType.LIST:
             (etype, size) = self.readListBegin()
             for _ in range(size):
                 self.skip(etype)
             self.readListEnd()
+        else:
+            raise TProtocolException(
+                TProtocolException.INVALID_DATA,
+                "Unexpected type for skipping {}".format(type)
+            )
 
     def readIntegral(self, type):
         if type == TType.BOOL:
             return self.readBool()
         elif type == TType.BYTE:
diff --git a/thrift/test/ProtocolSkipTest.cpp b/thrift/test/ProtocolSkipTest.cpp
new file mode 100644
index 0000000000..762ec64a8d
--- /dev/null
+++ b/thrift/test/ProtocolSkipTest.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2004-present Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <thrift/lib/cpp2/protocol/CompactProtocol.h>
+
+using namespace apache::thrift;
+
+TEST(ProtocolSkipTest, SkipInt) {
+  IOBufQueue queue;
+  CompactProtocolWriter writer;
+  writer.setOutput(&queue);
+  writer.writeI32(123);
+  auto buf = queue.move();
+  CompactProtocolReader reader;
+  reader.setInput(buf.get());
+  reader.skip(TType::T_I32);
+}
+
+TEST(ProtocolSkipTest, SkipStop) {
+  IOBufQueue queue;
+  CompactProtocolWriter writer;
+  writer.setOutput(&queue);
+  writer.writeFieldStop();
+  auto buf = queue.move();
+  CompactProtocolReader reader;
+  reader.setInput(buf.get());
+  bool thrown = false;
+  try {
+    reader.skip(TType::T_STOP);
+  } catch (const TProtocolException& ex) {
+    EXPECT_EQ(TProtocolException::INVALID_DATA, ex.getType());
+    thrown = true;
+  }
+  EXPECT_TRUE(thrown);
+}
+
+TEST(ProtocolSkipTest, SkipStopInContainer) {
+  IOBufQueue queue;
+  CompactProtocolWriter writer;
+  writer.setOutput(&queue);
+  writer.writeListBegin(TType::T_STOP, 1u << 30);
+  auto buf = queue.move();
+  CompactProtocolReader reader;
+  reader.setInput(buf.get());
+  bool thrown = false;
+  try {
+    reader.skip(TType::T_LIST);
+  } catch (const TProtocolException& ex) {
+    EXPECT_EQ(TProtocolException::INVALID_DATA, ex.getType());
+    thrown = true;
+  }
+  EXPECT_TRUE(thrown);
+}

Vulnerable code:     default:
      return;
        if type == TType.STOP:
            return
        elif type == TType.BOOL:
Fixed code: 
[[noreturn]] void TProtocolException::throwInvalidSkipType(TType type) {
  throw TProtocolException(
      TProtocolException::INVALID_DATA,
      folly::sformat(
          "Encountered invalid field/element type ({}) during skipping",
          static_cast<uint8_t>(type)));
}
#include <thrift/lib/cpp/protocol/TType.h>
  [[noreturn]] static void throwInvalidSkipType(TType type);
    default: {
      TProtocolException::throwInvalidSkipType(arg_type);
    }
        if type == TType.BOOL:
        else:
            raise TProtocolException(
                TProtocolException.INVALID_DATA,
                "Unexpected type for skipping {}".format(type)
            )
/*
 * Copyright 2004-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>

#include <thrift/lib/cpp2/protocol/CompactProtocol.h>

using namespace apache::thrift;

TEST(ProtocolSkipTest, SkipInt) {
  IOBufQueue queue;
  CompactProtocolWriter writer;
  writer.setOutput(&queue);
  writer.writeI32(123);
  auto buf = queue.move();
  CompactProtocolReader reader;
  reader.setInput(buf.get());
  reader.skip(TType::T_I32);
}

TEST(ProtocolSkipTest, SkipStop) {
  IOBufQueue queue;
  CompactProtocolWriter writer;
  writer.setOutput(&queue);
  writer.writeFieldStop();
  auto buf = queue.move();
  CompactProtocolReader reader;
  reader.setInput(buf.get());
  bool thrown = false;
  try {
    reader.skip(TType::T_STOP);
  } catch (const TProtocolException& ex) {
    EXPECT_EQ(TProtocolException::INVALID_DATA, ex.getType());
    thrown = true;
  }
  EXPECT_TRUE(thrown);
}

TEST(ProtocolSkipTest, SkipStopInContainer) {
  IOBufQueue queue;
  CompactProtocolWriter writer;
  writer.setOutput(&queue);
  writer.writeListBegin(TType::T_STOP, 1u << 30);
  auto buf = queue.move();
  CompactProtocolReader reader;
  reader.setInput(buf.get());
  bool thrown = false;
  try {
    reader.skip(TType::T_LIST);
  } catch (const TProtocolException& ex) {
    EXPECT_EQ(TProtocolException::INVALID_DATA, ex.getType());
    thrown = true;
  }
  EXPECT_TRUE(thrown);
}