Skip to content

Commit 392d498

Browse files
tohojomehmetb0
authored andcommitted
bpf: Make sure internal and UAPI bpf_redirect flags don't overlap
BugLink: https://bugs.launchpad.net/bugs/2089272 [ Upstream commit 09d88791c7cd888d5195c84733caf9183dcfbd16 ] The bpf_redirect_info is shared between the SKB and XDP redirect paths, and the two paths use the same numeric flag values in the ri->flags field (specifically, BPF_F_BROADCAST == BPF_F_NEXTHOP). This means that if skb bpf_redirect_neigh() is used with a non-NULL params argument and, subsequently, an XDP redirect is performed using the same bpf_redirect_info struct, the XDP path will get confused and end up crashing, which syzbot managed to trigger. With the stack-allocated bpf_redirect_info, the structure is no longer shared between the SKB and XDP paths, so the crash doesn't happen anymore. However, different code paths using identically-numbered flag values in the same struct field still seems like a bit of a mess, so this patch cleans that up by moving the flag definitions together and redefining the three flags in BPF_F_REDIRECT_INTERNAL to not overlap with the flags used for XDP. It also adds a BUILD_BUG_ON() check to make sure the overlap is not re-introduced by mistake. Fixes: e624d4e ("xdp: Extend xdp_redirect_map with broadcast support") Reported-by: [email protected] Signed-off-by: Toke Høiland-Jørgensen <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Closes: https://syzkaller.appspot.com/bug?extid=cca39e6e84a367a7e6f6 Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Sasha Levin <[email protected]> CVE-2024-50163 Signed-off-by: Manuel Diewald <[email protected]> Signed-off-by: Mehmet Basaran <[email protected]>
1 parent 27504b8 commit 392d498

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

include/uapi/linux/bpf.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5108,11 +5108,6 @@ enum {
51085108
BPF_F_MARK_ENFORCE = (1ULL << 6),
51095109
};
51105110

5111-
/* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
5112-
enum {
5113-
BPF_F_INGRESS = (1ULL << 0),
5114-
};
5115-
51165111
/* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
51175112
enum {
51185113
BPF_F_TUNINFO_IPV6 = (1ULL << 0),
@@ -5251,10 +5246,12 @@ enum {
52515246
BPF_F_BPRM_SECUREEXEC = (1ULL << 0),
52525247
};
52535248

5254-
/* Flags for bpf_redirect_map helper */
5249+
/* Flags for bpf_redirect and bpf_redirect_map helpers */
52555250
enum {
5256-
BPF_F_BROADCAST = (1ULL << 3),
5257-
BPF_F_EXCLUDE_INGRESS = (1ULL << 4),
5251+
BPF_F_INGRESS = (1ULL << 0), /* used for skb path */
5252+
BPF_F_BROADCAST = (1ULL << 3), /* used for XDP path */
5253+
BPF_F_EXCLUDE_INGRESS = (1ULL << 4), /* used for XDP path */
5254+
#define BPF_F_REDIRECT_FLAGS (BPF_F_INGRESS | BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS)
52585255
};
52595256

52605257
#define __bpf_md_ptr(type, name) \

net/core/filter.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,9 +2405,9 @@ static int __bpf_redirect_neigh(struct sk_buff *skb, struct net_device *dev,
24052405

24062406
/* Internal, non-exposed redirect flags. */
24072407
enum {
2408-
BPF_F_NEIGH = (1ULL << 1),
2409-
BPF_F_PEER = (1ULL << 2),
2410-
BPF_F_NEXTHOP = (1ULL << 3),
2408+
BPF_F_NEIGH = (1ULL << 16),
2409+
BPF_F_PEER = (1ULL << 17),
2410+
BPF_F_NEXTHOP = (1ULL << 18),
24112411
#define BPF_F_REDIRECT_INTERNAL (BPF_F_NEIGH | BPF_F_PEER | BPF_F_NEXTHOP)
24122412
};
24132413

@@ -2417,6 +2417,8 @@ BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
24172417
struct sk_buff *clone;
24182418
int ret;
24192419

2420+
BUILD_BUG_ON(BPF_F_REDIRECT_INTERNAL & BPF_F_REDIRECT_FLAGS);
2421+
24202422
if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
24212423
return -EINVAL;
24222424

0 commit comments

Comments
 (0)