Skip to content

Various soundness bugs related to MaybeUninit::uninit, partial initialization, then .assume_init() #2720

@tgross35

Description

@tgross35

This is similar to what is reported in #2693, but more general.

The first example is the most problematic, that UB caused a (fairly innocuous) issue in Fedora's CI rust-lang/libc#4892 (comment). This happened to be fixed in the latest libc, but that was due to a change in internal details that shouldn't be relied upon.

In general, it's really not ever robust to use MaybeUninit::uninit with libc types if .assume_init() will be called. For anything that has padding fields now or may add fields in the future, it's easy to wind up in a situation where a system libc/kernel is only partially initializing the type and assume_init creates UB. I doubt the cost of zeroing will be noticeable when there is usually a syscall overhead.

(It's fine to leave the type wrapped in MaybeUninit and only access useful fields via raw pointers, same as in C, but that's not ergonomic.)

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