Skip to content

Out-of-bounds vector access in CoresetTree::Merge #2423

Backlog

Description

@x4evexnol

pixie-io_pixie_000_heap-oob-write_artifacts.zip

I found an out-of-bounds vector access in CoresetTree<TCoreset>::Merge when merging a CoresetTree that has empty levels below populated ones.

  • Commit: 1d024d972e7bc1ad7a27d76c997325eebe5e72a1 (current main when tested)
  • Location: src/carnot/exec/ml/coreset.h:203-208 (CoresetTree<TCoreset>::Merge, crash at line 206)

Vulnerable Code

In src/carnot/exec/ml/coreset.h (lines 200 to 211), the second merge loop increments i unconditionally while levels_.emplace_back() only executes when other.levels_[i].size() > 0. If any lower level in other is empty, levels_.size() falls behind i, and levels_[i] indexes out of bounds:

    200          levels_[i].insert(levels_[i].end(), other.levels_[i].begin(), other.levels_[i].end());
    201        }
    202      }
    203      for (auto i = levels_.size(); i < other.levels_.size(); i++) {
    204        if (other.levels_[i].size() > 0) {
    205          levels_.emplace_back();
>   206          levels_[i].insert(levels_[i].end(), other.levels_[i].begin(), other.levels_[i].end());
    207        }
    208      }
    209  
    210      // Fix the r-way tree by coresetting any levels that have r or more buckets after merge.
    211      for (auto i = 0UL; i < levels_.size(); i++) {

How to Reproduce

Deserializing {"coreset_size":100,"r":2,"levels":{"1":[{"points":[[1.0,2.0],[3.0,4.0]],"weights":[1.0,1.0]}]}} (p2-gap) via FromJSON and merging it into a default-constructed CoresetTree triggers the out-of-bounds access. Additional tests confirmed:

  • p1-benign: exits 0 with no sanitizer errors.
  • p3-crafted: demonstrates a 3-level overshoot past levels_.
  • p4-producer: shows the bug triggers with no crafted input at all, because a normal Update(), ToJSON(), and FromJSON() round trip on an honest producer emits and re-materializes the same [empty, nonempty] level state and crashes at line 206.

The four case files are in artifacts/PoC/:

  • p2-gap.param is the primary crafted JSON.
  • p3-crafted.param demonstrates a three-level overshoot.
  • p4-producer.param runs the honest Update() -> ToJSON() -> FromJSON() -> Merge() path with no crafted JSON.
  • p1-benign.param is the non-gapped control.

The reproduction harness compiles src/carnot/exec/ml/coreset.h verbatim with -fsanitize=address,undefined -fno-omit-frame-pointer -g -O1 -std=c++17, using the real rapidjson, absl, and Eigen headers.

The crash reproduced in 3 out of 3 fresh ASan/UBSan runs (ASAN_OPTIONS=detect_leaks=0:abort_on_error=1 UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=0).

Crash Output

AddressSanitizer reports a heap-buffer-overflow read of size 8 at coreset.h:206 in CoresetTree<TestCoreset>::Merge when evaluating levels_[i].end() on the one-past-the-end Level slot, 8 bytes past the 24-byte std::vector<Level> allocation.

Top frames:

  • #2 px::carnot::exec::ml::CoresetTree<TestCoreset>::Merge(...) (src/carnot/exec/ml/coreset.h:206)
  • #3 main (harness.cc:104)

Excerpt (full output in artifacts/crash_output.txt, 5 log sections):

[harness] receiver.Merge(other)
=================================================================
==12==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6030000000c0 at pc 0x00000044a050 bp 0x7fffffffdf40 sp 0x7fffffffdf38
READ of size 8 at 0x6030000000c0 thread T0
    #0 0x44a04f in __gnu_cxx::__normal_iterator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>*, std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > > >::__normal_iterator(std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>* const&) /usr/local/include/c++/13.5.0/bits/stl_iterator.h:1077
    #1 0x44a04f in std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > >::end() /usr/local/include/c++/13.5.0/bits/stl_vector.h:894
    #2 0x44a04f in px::carnot::exec::ml::CoresetTree<px::carnot::exec::ml::TestCoreset>::Merge(px::carnot::exec::ml::CoresetTree<px::carnot::exec::ml::TestCoreset> const&) /src/tree/src/carnot/exec/ml/coreset.h:206
    #3 0x405b45 in main /src/harness.cc:104
    #4 0x7ffff6db1249  (/lib/x86_64-linux-gnu/libc.so.6+0x27249) (BuildId: 93ac61ec5a8eb1396f9fbd350e3169a558528a40)
    #5 0x7ffff6db1304 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x27304) (BuildId: 93ac61ec5a8eb1396f9fbd350e3169a558528a40)
    #6 0x403510 in _start (/src/pocbin+0x403510)

0x6030000000c0 is located 8 bytes after 24-byte region [0x6030000000a0,0x6030000000b8)
allocated by thread T0 here:
    #0 0x7ffff79f6f28 in operator new(unsigned long) (/usr/local/lib64/libasan.so.8+0xdbf28)
    #1 0x41adae in std::__new_allocator<std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > > >::allocate(unsigned long, void const*) /usr/local/include/c++/13.5.0/bits/new_allocator.h:151
    #2 0x41adae in std::allocator_traits<std::allocator<std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > > > >::allocate(std::allocator<std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > > >&, unsigned long) /usr/local/include/c++/13.5.0/bits/alloc_traits.h:482
    #3 0x41adae in std::_Vector_base<std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > >, std::allocator<std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > > > >::_M_allocate(unsigned long) /usr/local/include/c++/13.5.0/bits/stl_vector.h:381
    #4 0x41adae in void std::vector<std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > >, std::allocator<std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > > > >::_M_realloc_insert<>(__gnu_cxx::__normal_iterator<std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > >*, std::vector<std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > >, std::allocator<std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > > > > >) /usr/local/include/c++/13.5.0/bits/vector.tcc:459
    #5 0x41cddc in std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > >& std::vector<std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > >, std::allocator<std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > > > >::emplace_back<>() /usr/local/include/c++/13.5.0/bits/vector.tcc:123
    #6 0x4496e1 in px::carnot::exec::ml::CoresetTree<px::carnot::exec::ml::TestCoreset>::Merge(px::carnot::exec::ml::CoresetTree<px::carnot::exec::ml::TestCoreset> const&) /src/tree/src/carnot/exec/ml/coreset.h:205
    #7 0x405b45 in main /src/harness.cc:104
    #8 0x7ffff6db1249  (/lib/x86_64-linux-gnu/libc.so.6+0x27249) (BuildId: 93ac61ec5a8eb1396f9fbd350e3169a558528a40)

SUMMARY: AddressSanitizer: heap-buffer-overflow /usr/local/include/c++/13.5.0/bits/stl_iterator.h:1077 in __gnu_cxx::__normal_iterator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>*, std::vector<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>, std::allocator<std::shared_ptr<px::carnot::exec::ml::WeightedPointSet> > > >::__normal_iterator(std::shared_ptr<px::carnot::exec::ml::WeightedPointSet>* const&)

Impact

While ASan catches the initial end() read on the out-of-bounds Level slot, in a non-sanitized build levels_[i].insert(...) attempts to copy WeightedPointSet shared_ptr elements through the garbage vector state at offset vector_base + 24*i and mutate its end_ pointer. The level overshoot i - levels_.size() is controlled via the JSON levels keys (tested up to a 3-level overshoot in p3-crafted). Because serialized UDA state crosses the PEM-to-Kelvin boundary (AggNode::DeserializeAndMergeRow), either a compromised PEM or a normal ToJSON/FromJSON round trip can trigger heap corruption or a crash in Kelvin.

Suggested Fix

Keep levels_ aligned with other.levels_ before the indexed insert (artifacts/fix_patch.diff, src/carnot/exec/ml/coreset.h, 1 hunk):

     for (auto i = levels_.size(); i < other.levels_.size(); i++) {
+      levels_.emplace_back();
       if (other.levels_[i].size() > 0) {
-        levels_.emplace_back();
         levels_[i].insert(levels_[i].end(), other.levels_[i].begin(), other.levels_[i].end());
       }
     }

This preserves the populated higher levels instead of silently dropping them. I rebuilt with the patch and replayed all four inputs; each exits 0 with no ASan or UBSan diagnostics.

Thanks for taking a look at this report.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions