Skip to content

Possible unsoundness with uninit to_guest: &mut [u32] for syscalls #2853

@thaliaarchi

Description

@thaliaarchi

I think it may be unsound for Syscall to send data from the host to guest via to_guest: &mut [u32], when the pointer given by the guest is to an uninitialized allocation. I think use of sys_read or sys_read_words with uninitialized buffers could trigger UB. I think it should instead be &mut [MaybeUninit<u32>].

My motivation for this comes from wanting to implement Read::read_buf for the RISC Zero version of std::io::Stdin (tracked in rust-lang/rust#136756).

If this sounds good, I could submit a PR here similar to what I did for the Hermit unikernel in hermit-os/kernel#1606 (Use uninitialized buffers for read and recvfrom). However, since each file descriptor is associated with a dyn Read and Read::read_buf is currently unstable, this might not be possible.

On the other hand, Rust tentatively does not have full recursive validity for references, so as long as uninitialized data is not read, you might be fine. @RalfJung?

Steps to reproduce

This patch to std, which implements Read::read_buf for RISC Zero stdin, would encounter this issue.

Since I know little about RISC Zero, here's my understanding of the relevant bits, so you can correct me if I'm wrong:

Trace from the C FFI to the underlying Read::read calls

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions