From 5af04c458248e800302e22d3425c5765665171e1 Mon Sep 17 00:00:00 2001 From: Tuetuopay Date: Sat, 5 Aug 2023 00:16:32 +0200 Subject: [PATCH] maps/xdp: drop the possibly-unsafe {Cpu,Dev}MapValue conversions Making a `From` impl that hides an unsafe with some safety guarantee is actually unsafe, because while we know how we use this impl, we don't know how other will. And since it's on two public types, anything can be done. --- aya/src/maps/xdp/cpu_map.rs | 19 +++++++------------ aya/src/maps/xdp/dev_map.rs | 19 +++++++------------ aya/src/maps/xdp/dev_map_hash.rs | 8 +++++++- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/aya/src/maps/xdp/cpu_map.rs b/aya/src/maps/xdp/cpu_map.rs index 67507869..87fda798 100644 --- a/aya/src/maps/xdp/cpu_map.rs +++ b/aya/src/maps/xdp/cpu_map.rs @@ -77,7 +77,13 @@ impl> CpuMap { io_error, })?; let value: bpf_cpumap_val = value.ok_or(MapError::KeyNotFound)?; - Ok(value.into()) + + // SAFETY: map writes use fd, map reads use id. + // https://elixir.bootlin.com/linux/v6.2/source/include/uapi/linux/bpf.h#L6149 + Ok(CpuMapValue { + qsize: value.qsize, + prog_id: unsafe { value.bpf_prog.id }, + }) } /// An iterator over the elements of the map. @@ -148,14 +154,3 @@ pub struct CpuMapValue { pub qsize: u32, pub prog_id: u32, } - -impl From for CpuMapValue { - fn from(value: bpf_cpumap_val) -> Self { - // SAFETY: map writes use fd, map reads use id. - // https://elixir.bootlin.com/linux/v6.2/source/include/uapi/linux/bpf.h#L6149 - CpuMapValue { - qsize: value.qsize, - prog_id: unsafe { value.bpf_prog.id }, - } - } -} diff --git a/aya/src/maps/xdp/dev_map.rs b/aya/src/maps/xdp/dev_map.rs index 6e8fd441..38bf0853 100644 --- a/aya/src/maps/xdp/dev_map.rs +++ b/aya/src/maps/xdp/dev_map.rs @@ -71,7 +71,13 @@ impl> DevMap { io_error, })?; let value: bpf_devmap_val = value.ok_or(MapError::KeyNotFound)?; - Ok(value.into()) + + // SAFETY: map writes use fd, map reads use id. + // https://elixir.bootlin.com/linux/v6.2/source/include/uapi/linux/bpf.h#L6136 + Ok(DevMapValue { + ifindex: value.ifindex, + prog_id: unsafe { value.bpf_prog.id }, + }) } /// An iterator over the elements of the array. @@ -141,14 +147,3 @@ pub struct DevMapValue { pub ifindex: u32, pub prog_id: u32, } - -impl From for DevMapValue { - fn from(value: bpf_devmap_val) -> Self { - // SAFETY: map writes use fd, map reads use id. - // https://elixir.bootlin.com/linux/v6.2/source/include/uapi/linux/bpf.h#L6136 - DevMapValue { - ifindex: value.ifindex, - prog_id: unsafe { value.bpf_prog.id }, - } - } -} diff --git a/aya/src/maps/xdp/dev_map_hash.rs b/aya/src/maps/xdp/dev_map_hash.rs index 6b7aff80..40408fc4 100644 --- a/aya/src/maps/xdp/dev_map_hash.rs +++ b/aya/src/maps/xdp/dev_map_hash.rs @@ -60,7 +60,13 @@ impl> DevMapHash { io_error, })?; let value: bpf_devmap_val = value.ok_or(MapError::KeyNotFound)?; - Ok(value.into()) + + // SAFETY: map writes use fd, map reads use id. + // https://elixir.bootlin.com/linux/v6.2/source/include/uapi/linux/bpf.h#L6136 + Ok(DevMapValue { + ifindex: value.ifindex, + prog_id: unsafe { value.bpf_prog.id }, + }) } /// An iterator over the elements of the devmap in arbitrary order.