aya-ebpf: Validate the pointer alignment in `Array::get`

reviewable/pr1340/r5
Michal R 4 days ago
parent 1b2c61fcf4
commit 7f65983b2f

@ -0,0 +1,3 @@
use crate::cty::c_long;
pub const EINVAL: c_long = 22;

@ -23,6 +23,7 @@ pub use aya_ebpf_bindings::bindings;
mod args;
pub use args::{PtRegs, RawTracepointArgs};
pub mod error;
#[expect(clippy::missing_safety_doc, unsafe_op_in_unsafe_fn)]
pub mod helpers;
pub mod maps;

@ -4,6 +4,7 @@ use aya_ebpf_cty::c_long;
use crate::{
bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_ARRAY},
error::EINVAL,
insert, lookup,
maps::PinningType,
};
@ -48,9 +49,19 @@ impl<T> Array<T> {
}
#[inline(always)]
pub fn get(&self, index: u32) -> Option<&T> {
// FIXME: alignment
unsafe { self.lookup(index).map(|p| p.as_ref()) }
pub fn get(&self, index: u32) -> Result<Option<&T>, c_long> {
unsafe {
match self.lookup(index) {
Some(p) => {
if p.is_aligned() {
Ok(Some(p.as_ref()))
} else {
Err(-EINVAL)
}
}
None => Ok(None),
}
}
}
#[inline(always)]

Loading…
Cancel
Save