Skip to content

Heap buffer overflow in ReplaceTagsWithSubs #2424

Backlog

Description

@x4evexnol

pixie-io_pixie_001_heap-oob-write_artifacts.zip

Here's another bug I found, a heap buffer overflow in ReplaceTagsWithSubs caused by overlapping PII tags undercounting the allocated output string size.

  • Commit: 1d024d972e7bc1ad7a27d76c997325eebe5e72a1 (current main when tested)
  • Location: src/carnot/funcs/builtins/pii_ops.cc:348-391 (ReplaceTagsWithSubs; crash at line 387)

Vulnerable Code

In src/carnot/funcs/builtins/pii_ops.cc (lines 371 to 391), new_string_size is computed assuming non_overlapping_tags contains no overlaps, and sub_str.copy writes each replacement into output:

    371  
    372    // Calculate new string size.
    373    size_t new_string_size = input.size();
    374    for (auto tag : non_overlapping_tags) {
    375      new_string_size = new_string_size + type_to_sub_str_[tag.tag_type].size() - tag.size;
    376    }
    377    // Build new string from old string and non overlapping tags.
    378    std::string output(new_string_size, 0);
    379    int input_idx = 0;
    380    auto data_ptr = output.data();
    381    for (auto tag : non_overlapping_tags) {
    382      auto n_copy = input.copy(data_ptr, tag.start_idx - input_idx, input_idx);
    383      data_ptr += n_copy;
    384      input_idx = tag.start_idx;
    385  
    386      auto sub_str = type_to_sub_str_[tag.tag_type];
>   387      n_copy = sub_str.copy(data_ptr, sub_str.length());
    388      data_ptr += n_copy;
    389      input_idx += tag.size;
    390    }
    391    input.copy(data_ptr, input.length() - input_idx, input_idx);

How to Reproduce

Call RedactPIIUDF::Init(nullptr) once, then pass the input to RedactPIIUDF::Exec(nullptr, px::types::StringValue(input)).

  • Primary input (x2-overlap): GB82WEST12345698765432@255.255.255.255
  • Variant (x4-short): GB82WEST12345698765432@10.0.0.1
  • Benign controls: hello@pixie.ai (x1-benign) and connect to 1.2.3.4 please (x3-benign)

The four raw inputs are in artifacts/PoC/; the .param files only add the replay harness's input prefix.

For the primary input, the taggers produce IBAN(0, 22), EMAIL(0, 38), and IPv4(23, 15). The de-overlap pass groups only IBAN and EMAIL, keeps EMAIL, and incorrectly leaves IPv4 as a second "non-overlapping" tag even though it starts inside EMAIL.

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). The replay harness compiled pii_ops.cc and pii_ops.h verbatim against stub headers plus the real re2 and absl libraries with -fsanitize=address,undefined -fno-omit-frame-pointer -g -O1 -std=c++17.

Crash Output

AddressSanitizer reports a heap-buffer-overflow write of size 15 at pii_ops.cc:387 in ReplaceTagsWithSubs (called from RedactPIIUDF::Exec at pii_ops.cc:403), 0 bytes past the 17-byte std::string allocation for output.

Top frames:

  • #3 ReplaceTagsWithSubs (src/carnot/funcs/builtins/pii_ops.cc:387)
  • #4 px::carnot::builtins::RedactPIIUDF::Exec(...) (src/carnot/funcs/builtins/pii_ops.cc:403)
  • #5 main (harness.cc:31)

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

[harness] calling Exec
=================================================================
==12==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6030000100d1 at pc 0x7ffff798baef bp 0x7fffffffda90 sp 0x7fffffffd250
WRITE of size 15 at 0x6030000100d1 thread T0
    #0 0x7ffff798baee in __interceptor_memcpy (/usr/local/lib64/libasan.so.8+0x70aee)
    #1 0x413fbc in std::char_traits<char>::copy(char*, char const*, unsigned long) /usr/local/include/c++/13.5.0/bits/char_traits.h:435
    #2 0x413fbc in std::basic_string_view<char, std::char_traits<char> >::copy(char*, unsigned long, unsigned long) const /usr/local/include/c++/13.5.0/string_view:328
    #3 0x413fbc in ReplaceTagsWithSubs /src/tree/src/carnot/funcs/builtins/pii_ops.cc:387
    #4 0x413fbc in px::carnot::builtins::RedactPIIUDF::Exec(px::carnot::udf::FunctionContext*, px::types::StringValue) /src/tree/src/carnot/funcs/builtins/pii_ops.cc:403
    #5 0x442bb5 in main /src/harness.cc:31
    #6 0x7ffff6d58249  (/lib/x86_64-linux-gnu/libc.so.6+0x27249) (BuildId: 93ac61ec5a8eb1396f9fbd350e3169a558528a40)
    #7 0x7ffff6d58304 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x27304) (BuildId: 93ac61ec5a8eb1396f9fbd350e3169a558528a40)
    #8 0x4046f0 in _start (/src/pocbin+0x4046f0)

0x6030000100d1 is located 0 bytes after 17-byte region [0x6030000100c0,0x6030000100d1)
allocated by thread T0 here:
    #0 0x7ffff79f6f28 in operator new(unsigned long) (/usr/local/lib64/libasan.so.8+0xdbf28)
    #1 0x7ffff7775a6b in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct(unsigned long, char) (/usr/local/lib64/libstdc++.so.6+0x14fa6b)

SUMMARY: AddressSanitizer: heap-buffer-overflow (/usr/local/lib64/libasan.so.8+0x70aee) in __interceptor_memcpy
Shadow bytes around the buggy address:
  0x60300000fe00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x60300000fe80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x60300000ff00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x60300000ff80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x603000010000: 00 00 00 fa fa fa 00 00 00 fa fa fa fd fd fd fd
=>0x603000010080: fa fa 00 00 00 00 fa fa 00 00[01]fa fa fa fa fa
  0x603000010100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa

Impact

The de-overlap loop at pii_ops.cc:348-370 groups tags against the first tag's extent rather than the maximum tag's extent, yet emits the largest tag. As a result, a later tag starting before the emitted tag ends survives into non_overlapping_tags. This causes new_string_size to be undercounted: for x2-overlap, 38 + (16 - 38) + (16 - 15) = 17. The loop then writes the 15-byte IPv4 replacement at offset 16 of that 17-byte output, overflowing the heap allocation at pii_ops.cc:387.

The allocation undercount and overflow extent depend deterministically on which PII patterns match and overlap in the input. Because px.redact_pii_best_effort is automatically inserted on http_events req_body and resp_body when RedactionOptions is set (restrict_columns_rule.cc:94-115) and used in pii_cluster_egress.pxl:201, attacker-influenced network traffic can trigger the overflow.

Suggested Fix

Fix the de-overlap pass so it tracks the maximum end of the current transitive overlap group, not just the first tag's end (artifacts/fix_patch.diff, src/carnot/funcs/builtins/pii_ops.cc, 1 hunk):

+    size_t group_end = static_cast<size_t>(it->start_idx) + it->size;
+    auto next = it + 1;
+    while (next != tags->end() && next->start_idx < group_end) {
+      if (next->size > max_size) {
+        max_size_tag = *next;
+        max_size = next->size;
+      }
+      group_end = std::max(group_end, static_cast<size_t>(next->start_idx) + next->size);
+      ++next;
+    }

This guarantees the emitted spans are disjoint, so the size calculation and copy loop are valid. I rebuilt with the patch and replayed all four inputs; each exits 0 with no ASan or UBSan diagnostics.

Thanks again.

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