skb_scrub_packet() drops all skb extensions unconditionally via skb_ext_reset(). It runs on tunnel encap/decap (ip_tunnel_rcv, vxlan_rcv, etc.) and cross-netns forwarding (dev_forward_skb). This makes it impossible for a BPF program to pass metadata via bpf_skb_ext through a tunnel or across a netns boundary. The extension is always lost at the scrub point. Introduce skb_ext_scrub() which consults each active extension before discarding it. Extensions that request preservation are kept while the rest are torn down. When the extension slab is shared with clones, COW ensures isolation. Replace the skb_ext_reset() call in skb_scrub_packet() with skb_ext_scrub(). Expose the opt-in mechanism to BPF via the BPF_SKB_EXT_F_NO_SCRUB flag for bpf_dynptr_from_skb_ext(). A program that sets this flag when creating the extension signals that its metadata should survive scrubbing. Signed-off-by: Jakub Sitnicki --- include/linux/bpf.h | 1 + include/linux/skbuff.h | 2 ++ include/uapi/linux/bpf.h | 3 +- net/core/filter.c | 11 +++++-- net/core/skbuff.c | 82 ++++++++++++++++++++++++++++++++++++++++++------ net/ipv4/udp.c | 2 +- 6 files changed, 86 insertions(+), 15 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 6b918a5b61bf..a46ca53c5b27 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -4214,6 +4214,7 @@ static inline int bpf_map_check_op_flags(struct bpf_map *map, u64 flags, u64 all #ifdef CONFIG_BPF_SKB_EXT struct bpf_skb_ext { + u64 flags; u8 buf[CONFIG_BPF_SKB_EXT_SIZE] __aligned(8); }; diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 584d8440d352..66afa5489007 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -5063,6 +5063,7 @@ void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id); void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id); void __skb_ext_put(struct skb_ext *ext); +void skb_ext_scrub(struct sk_buff *skb); static inline void skb_ext_put(struct sk_buff *skb) { @@ -5132,6 +5133,7 @@ static inline bool skb_has_extensions(struct sk_buff *skb) static inline void __skb_ext_put(struct skb_ext *ext) {} static inline void skb_ext_put(struct sk_buff *skb) {} static inline void skb_ext_reset(struct sk_buff *skb) {} +static inline void skb_ext_scrub(struct sk_buff *skb) {} static inline void skb_ext_del(struct sk_buff *skb, int unused) {} static inline void __skb_ext_copy(struct sk_buff *d, const struct sk_buff *s) {} static inline void skb_ext_copy(struct sk_buff *dst, const struct sk_buff *s) {} diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 3eee4467422d..02da170205de 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7734,7 +7734,8 @@ struct bpf_insn_array_value { /* Flags to control bpf_dynptr_from_skb_ext() behavior. */ enum { - BPF_SKB_EXT_F_CREATE = (1ULL << 0), + BPF_SKB_EXT_F_CREATE = (1ULL << 0), + BPF_SKB_EXT_F_NO_SCRUB = (1ULL << 1), }; #endif /* _UAPI__LINUX_BPF_H__ */ diff --git a/net/core/filter.c b/net/core/filter.c index b0b039c64b02..eca07976ab1c 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -12319,6 +12319,7 @@ __bpf_kfunc int bpf_dynptr_from_skb(struct __sk_buff *s, u64 flags, * @skb_: socket buffer to attach the extension to * @size: dynptr size in bytes, 0 for maximum (BPF_SKB_EXT_SIZE_MAX) * @flags: BPF_SKB_EXT_F_CREATE to create/COW (read-write), 0 to find (read-only) + * BPF_SKB_EXT_F_NO_SCRUB to keep the extension across skb_scrub_packet() * @ptr__uninit: dynptr to initialize * * Return: @@ -12335,14 +12336,16 @@ __bpf_kfunc int bpf_dynptr_from_skb_ext(struct __sk_buff *skb_, u32 size, struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)ptr__uninit; struct sk_buff *skb = (struct sk_buff *)skb_; bool create = flags & BPF_SKB_EXT_F_CREATE; + bool no_scrub = flags & BPF_SKB_EXT_F_NO_SCRUB; struct bpf_skb_ext *ext; bool exists; int err; - if (flags & ~BPF_SKB_EXT_F_CREATE) { - err = -EINVAL; + err = -EINVAL; + if (flags & ~(BPF_SKB_EXT_F_CREATE | BPF_SKB_EXT_F_NO_SCRUB)) + goto error; + if (!create && no_scrub) goto error; - } if (size > ARRAY_SIZE(ext->buf)) { err = -E2BIG; @@ -12367,6 +12370,8 @@ __bpf_kfunc int bpf_dynptr_from_skb_ext(struct __sk_buff *skb_, u32 size, } if (!exists) memset(ext, 0, sizeof(*ext)); + if (no_scrub) + ext->flags |= BPF_SKB_EXT_F_NO_SCRUB; out: bpf_dynptr_init(ptr, skb, BPF_DYNPTR_TYPE_SKB_EXT, 0, size); if (!create) diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 4efc06c426b2..46b5bd49a110 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -83,6 +83,7 @@ #include #include #include +#include #include #include @@ -6278,7 +6279,7 @@ void skb_scrub_packet(struct sk_buff *skb, bool xnet) skb->skb_iif = 0; skb->ignore_df = 0; skb_dst_drop(skb); - skb_ext_reset(skb); + skb_ext_scrub(skb); nf_reset_ct(skb); nf_reset_trace(skb); @@ -7252,6 +7253,20 @@ void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) } EXPORT_SYMBOL(__skb_ext_del); +static void skb_ext_put_each(struct skb_ext *ext, unsigned int skip) +{ +#ifdef CONFIG_XFRM + if (!(skip & (1 << SKB_EXT_SEC_PATH)) && + __skb_ext_exist(ext, SKB_EXT_SEC_PATH)) + skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); +#endif +#ifdef CONFIG_MCTP_FLOWS + if (!(skip & (1 << SKB_EXT_MCTP)) && + __skb_ext_exist(ext, SKB_EXT_MCTP)) + skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP)); +#endif +} + void __skb_ext_put(struct skb_ext *ext) { /* If this is last clone, nothing can increment @@ -7263,18 +7278,65 @@ void __skb_ext_put(struct skb_ext *ext) if (!refcount_dec_and_test(&ext->refcnt)) return; free_now: -#ifdef CONFIG_XFRM - if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) - skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); -#endif -#ifdef CONFIG_MCTP_FLOWS - if (__skb_ext_exist(ext, SKB_EXT_MCTP)) - skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP)); -#endif - + skb_ext_put_each(ext, 0); kmem_cache_free(skbuff_ext_cache, ext); } EXPORT_SYMBOL(__skb_ext_put); + +static unsigned int skb_ext_no_scrub(struct skb_ext *ext) +{ + unsigned int keep = 0; + +#if IS_ENABLED(CONFIG_BPF_SKB_EXT) + if (__skb_ext_exist(ext, SKB_EXT_BPF)) { + struct bpf_skb_ext *bpf = skb_ext_get_ptr(ext, SKB_EXT_BPF); + + if (bpf->flags & BPF_SKB_EXT_F_NO_SCRUB) + keep |= (1 << SKB_EXT_BPF); + } +#endif + return keep; +} + +static int __skb_ext_scrub(struct sk_buff *skb, unsigned int keep) +{ + struct skb_ext *old = skb->extensions; + struct skb_ext *ext; + int i; + + if (refcount_read(&old->refcnt) == 1) { + skb_ext_put_each(old, keep); + ext = old; + } else { + ext = skb_ext_maybe_cow(old, keep); + if (!ext) + return -ENOMEM; + skb->extensions = ext; + } + + for (i = 0; i < SKB_EXT_NUM; i++) { + if (!(keep & (1 << i))) + ext->offset[i] = 0; + } + skb->active_extensions = keep; + return 0; +} + +void skb_ext_scrub(struct sk_buff *skb) +{ + unsigned int keep; + + if (likely(!skb->active_extensions)) + return; + + keep = skb_ext_no_scrub(skb->extensions); + if (keep && !__skb_ext_scrub(skb, keep)) + return; + + skb_ext_reset(skb); +} +EXPORT_SYMBOL(skb_ext_scrub); + #endif /* CONFIG_SKB_EXTENSIONS */ static void kfree_skb_napi_cache(struct sk_buff *skb) diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 70f6cbd4ef73..08357f2621da 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -1536,7 +1536,7 @@ static bool udp_try_make_stateless(struct sk_buff *skb) return true; if (!secpath_exists(skb)) { - skb_ext_reset(skb); + skb_ext_scrub(skb); return true; } -- 2.43.0