From 890e8c9340d46f931dbbaf43d884593e012ef9f9 Mon Sep 17 00:00:00 2001 From: Tuetuopay Date: Thu, 29 Dec 2022 16:43:02 +0100 Subject: [PATCH] bpf: fix set_mark by not copying __sk_buff Such an assignment in two parts (first deref in `unsafe`, then field access outside of `unsafe`) is bogus: the deref "returned" by the `unsafe` block actually creates a copy of the `__sk_buff` struct because it implements `Copy`. The mark value is written to the `mark` field of the copy, and not the real `__sk_buff`. Change it to do it all in the `unsafe` block. The same is done for the `.len()` getter to avoid copying the whole `__sk_buff` struct for a 32 bit field. Although such a copy should be optimized out by the compiler, it's better to help it do so. --- bpf/aya-bpf/src/programs/sk_buff.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bpf/aya-bpf/src/programs/sk_buff.rs b/bpf/aya-bpf/src/programs/sk_buff.rs index 38b0177d..3ae6cf3d 100644 --- a/bpf/aya-bpf/src/programs/sk_buff.rs +++ b/bpf/aya-bpf/src/programs/sk_buff.rs @@ -25,7 +25,7 @@ impl SkBuff { #[allow(clippy::len_without_is_empty)] #[inline] pub fn len(&self) -> u32 { - unsafe { *self.skb }.len + unsafe { (*self.skb).len } } #[inline] @@ -40,7 +40,7 @@ impl SkBuff { #[inline] pub fn set_mark(&mut self, mark: u32) { - unsafe { *self.skb }.mark = mark; + unsafe { (*self.skb).mark = mark } } #[inline]