$function missing advanceGeneration() causes use-after-free on retained BSON args

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Major - P3
    • 9.0.0-rc0, 8.2.12, 8.0.28, 7.0.39, 8.3.7
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Integration
    • Fully Compatible
    • ALL
    • v8.3, v8.2, v8.0, v7.0
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      In src/mongo/db/exec/expression/evaluate_javascript.cpp, $function serializes arguments into a stack-local BSONObjBuilder and passes bob.done() (unowned BSONObj) to MozJS. Unlike $where (which calls scope->advanceGeneration() before each invocation), $function never invalidates old unowned BSON wrappers between documents. A JS function can retain an argument in a global variable across invocations, then access freed stack memory on the next call.

      Impact: A user with read privileges can crash mongod or leak heap contents via a crafted aggregation pipeline.

      Suggested Fix: Invoke scope->advanceGeneration() before building arguments in the $function evaluate path, matching the existing $where pattern in js_function.cpp

            Assignee:
            Aditya Deshpande
            Reporter:
            Aditya Deshpande
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: