Skip to content

Unsoundness in scaly #2594

@xizheyin

Description

@xizheyin

I discovered an unsoundness in scaly, but I couldn't locate its repository, so I'm submitting it here. Implementation of Index::index did not check the bound.

  82 | impl<T: Copy> Index<usize> for Array<T> {
  83 |     type Output = T;
  84 |     fn index(&self, offset: usize) -> &Self::Output {
  85 |         unsafe { &*self.vector.data.offset(offset as isize) }
  86 |     }
  87 | }

reproduce case:

use scaly::Array;

fn main() {
    let mut a: Array<u8> = Array::new();
    a.add(1);

    // Safe API OOB: Index<usize> does unchecked pointer offset
    // This should be UB (Miri will flag).
    let v = a[1];
    std::hint::black_box(v);
}

miri reports:

error: Undefined Behavior: pointer not dereferenceable: pointer must be dereferenceable for 16 bytes, but got 0x20000[noalloc] which is a dangling pointer (it has no provenance)
  --> /home/test/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/scaly-0.0.37/src/containers/array.rs:58:38
   |
58 |                 let exclusive_page = (*_own_page).allocate_exclusive_page();
   |                                      ^^^^^^^^^^^^ Undefined Behavior occurred here
   |
...

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