Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-5796

bson to json recursion limit defeated by legacy codewscope

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • None
    • C Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Summary

      libbson's bson-as-json serialization tries to limit recursion depth to 200, in order to limit the otherwise hazardous stack memory usage and algorithmic overhead that would result from unbounded recursion. This limit is rendered ineffective by nested documents within legacy code-with-scope scopes. Documents of less than 1MB can be constructed that use all available stack space on a default php interpreter. With unlimited stack space, a full 16MB worst-case document takes over ten minutes of CPU time and 1GB of RAM to serialize.

      Notably the server-side equivalent (bson_validate.cpp) does NOT contain the same bug, so this is not possible to trigger via any BSON document that could be stored inside MongoDB, only by other types of documents that are potentially read from an untrusted source.

      Environment

      Issue is not platform-specific, but the ramifications will be. I tested this on Linux (WSL2) especially in the php driver, with a default 8MB stack and with a stack increased by ulimit.

      Tested on driver version 1.28.0 and on latest master. A mongod connection is not required, this issue is in libbson.

      How to Reproduce

      Example is attached, written in PHP. See the comments, it will generate a worst-case document of any requested size.

      Additional Background

      I don't think we have a clear broadly applicable specification for the allowed BSON recursion depth and how/where it should be enforced; that would be good to establish.

      It is additionally feasible to rewrite bson-as-json serialization to avoid recursion entirely, and/or to have a stack overhead about 100x lower. These solutions would allow bson-as-json serialization to efficiently handle deeply nested documents, but without guidance toward consistent support for deeply nested documents across mongodb this would have limited use.

            Assignee:
            Unassigned Unassigned
            Reporter:
            micah.scott@mongodb.com Micah Scott
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: