Skip to content

backends: fix efi_rng error check, esp_idf FFI signature, and wasi_p1 negation - #885

Open
devonjonte wants to merge 1 commit into
rust-random:masterfrom
devonjonte:fix/efi-esp-wasi-edge-cases
Open

devonjonte wants to merge 1 commit into
rust-random:masterfrom
devonjonte:fix/efi-esp-wasi-edge-cases

Conversation

@devonjonte

Copy link
Copy Markdown

Fixes three edge-case defects found while auditing getrandom 0.4.3:

  1. efi_rng: high-bit UEFI error check (src/backends/efi_rng.rs)

    • efi_rng checked ret.is_error() via r_efi::efi::Status::is_error(), which masks the top two bits (0xc0) and requires 0x80 (r-efi/src/base.rs). Any high-bit status in the 0xC0.. reserved range returns false from is_error(), even though EDK2's EFI_ERROR(s) (MdePkg/Include/Base.h) and getrandom's own Error::from_uefi_code (src/error.rs) classify all high-bit-set statuses as errors.
    • Treating a 0xC0.. error as success causes init() to call protocol.assume_init() on potentially uninitialized memory and fill_inner() to return Ok(()) without filling dest (leading to uninitialized reads in fill_uninit).
    • Testing status.as_usize() & (1 << (usize::BITS - 1)) != 0 directly keeps efi_rng consistent with Error::from_uefi_code and fails closed.
  2. esp_idf: C FFI return type (src/backends/esp_idf.rs)

    • ESP-IDF declares void esp_fill_random(void *buf, size_t len); (components/esp_hw_support/include/esp_random.h), whereas esp_idf.rs declared -> u32 (and discarded the value). Remove the bogus return type.
  3. wasi_p1: negative return-value negation overflow (src/backends/wasi_p1.rs)

    • code if code <= MAX_ERROR_CODE => Err(Error::from_neg_error_code(-code)) matches all negative values, so i32::MIN overflows -code (panicking with overflow checks enabled). Match 1..=MAX_ERROR_CODE instead so negative return values map to Error::UNEXPECTED.

… negation

- efi_rng: test the UEFI high error bit directly (matching EDK2 EFI_ERROR
  and Error::from_uefi_code) instead of r_efi::Status::is_error(), which
  masks the top two bits (0xc0) and classifies 0xC0.. high-bit statuses as
  success, failing open into MaybeUninit::assume_init() / unfilled buffers.
- esp_idf: declare esp_fill_random returning void instead of u32 to match
  ESP-IDF's C prototype (void esp_fill_random(void *buf, size_t len)).
- wasi_p1: match 1..=MAX_ERROR_CODE instead of code <= MAX_ERROR_CODE so
  negative values (in particular i32::MIN) return Error::UNEXPECTED instead
  of overflowing -code.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant