From df51dd7ae421e674756bd73b97d979b2efb6ad5f 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 20455b00..6037c90f 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. The iterator item type is `Result 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 fe45176e..a0c4af6a 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. The iterator item type is `Result 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 7907e949..ff3d753c 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. The iterator item type is