Skip to content

Critical Rust memory safety bug: returning pointer to stack-allocated PMTEntry #1986

@THE-Amrit-mahto-05

Description

@THE-Amrit-mahto-05

Description

While reviewing the Rust FFI layer, I found a use-after-scope (dangling pointer) bug in ctorust.rs.

An implementation of FromCType<*mut PMT_entry> returns a raw pointer derived from a stack-allocated Rust value, which becomes invalid immediately after the function returns.

This is undefined behavior in Rust, independent of C-side invariants, test coverage, or calling order.

Affected File: src/rust/src/ctorust.rs

Problematic Code

let mut pmt_entry = PMTEntry {
    program_number,
    elementary_pid,
    stream_type,
    printable_stream_type,
};

Some(&mut pmt_entry as *mut PMTEntry)

here

  • pmt_entry is allocated on the stack
  • A pointer to it is returned
  • The function returns
  • The stack frame is destroyed
  • The returned pointer now points to invalid memory

Proposed Fix

let pmt_entry = PMTEntry { ... };
Some(Box::into_raw(Box::new(pmt_entry)))

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