foniod / redbpf

Rust library for building and running BPF/eBPF modules
Apache License 2.0
1.71k stars 136 forks source link

ub in RingBufPayload #333

Open Sherlock-Holo opened 2 years ago

Sherlock-Holo commented 2 years ago

in rust, we can't get the &mut from a pointer *mut, even it is not null

https://github.com/foniod/redbpf/blob/eed2cdc62c08633d83215af696445ec91f30649b/redbpf-probes/src/ringbuf.rs#L167=

here redbpf get the &mut from the *mut, we don't know if kernel init the memory with zero or not, but in rust, memory filled with zero should not be seems as initialize

and the as_mut also says

If the value may be uninitialized, as_uninit_mut must be used instead.

I recommand return the ptr directly and remove the unsafe mark, user who want to use it to init field should use https://doc.rust-lang.org/core/ptr/macro.addr_of_mut.html and use ptr::write to init field

Sherlock-Holo commented 2 years ago

or return the NonNull tell user the pointer is not null