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

reviewable/pr1340/r5
Michal R 5 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; mod args;
pub use args::{PtRegs, RawTracepointArgs}; pub use args::{PtRegs, RawTracepointArgs};
pub mod error;
#[expect(clippy::missing_safety_doc, unsafe_op_in_unsafe_fn)] #[expect(clippy::missing_safety_doc, unsafe_op_in_unsafe_fn)]
pub mod helpers; pub mod helpers;
pub mod maps; pub mod maps;

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

Loading…
Cancel
Save