Sashiko pointed out [1]:
The hdr pointer passed to bpf_lwt_push_ip_encap() can point to concurrently
mutable memory such as a BPF map value.

So, the memory of hdr pointer can be updated after skb_postpush_rcsum().

To fix it, memcpy() the hdr to a local buffer, which will be used for the
following checks and updates.

[1] https://lore.kernel.org/bpf/[email protected]/

Fixes: 52f278774e79 ("bpf: implement BPF_LWT_ENCAP_IP mode in 
bpf_lwt_push_encap")
Signed-off-by: Leon Hwang <[email protected]>
---
 net/core/lwt_bpf.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/net/core/lwt_bpf.c b/net/core/lwt_bpf.c
index f71ef82a5f3d..8009e427851f 100644
--- a/net/core/lwt_bpf.c
+++ b/net/core/lwt_bpf.c
@@ -599,6 +599,7 @@ static int handle_gso_encap(struct sk_buff *skb, bool ipv4, 
int encap_len)
 
 int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool 
ingress)
 {
+       u8 buff[LWT_BPF_MAX_HEADROOM];
        struct iphdr *iph;
        bool ipv4;
        int err;
@@ -606,8 +607,10 @@ int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, 
u32 len, bool ingress)
        if (unlikely(len < sizeof(struct iphdr) || len > LWT_BPF_MAX_HEADROOM))
                return -EINVAL;
 
+       memcpy(buff, hdr, len);
+
        /* validate protocol and length */
-       iph = (struct iphdr *)hdr;
+       iph = (struct iphdr *)buff;
        if (iph->version == 4) {
                ipv4 = true;
                if (unlikely(len < iph->ihl * 4))
@@ -637,7 +640,7 @@ int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, 
u32 len, bool ingress)
        if (ingress)
                skb_postpush_rcsum(skb, iph, len);
        skb_reset_network_header(skb);
-       memcpy(skb_network_header(skb), hdr, len);
+       memcpy(skb_network_header(skb), buff, len);
        bpf_compute_data_pointers(skb);
        skb_clear_hash(skb);
 
-- 
2.54.0


Reply via email to