Skip to content

Commit ca83156

Browse files
hyperenjuSasha Levin
authored andcommitted
bpf: Fix stack-out-of-bounds write in devmap
[ Upstream commit b7bf516 ] get_upper_ifindexes() iterates over all upper devices and writes their indices into an array without checking bounds. Also the callers assume that the max number of upper devices is MAX_NEST_DEV and allocate excluded_devices[1+MAX_NEST_DEV] on the stack, but that assumption is not correct and the number of upper devices could be larger than MAX_NEST_DEV (e.g., many macvlans), causing a stack-out-of-bounds write. Add a max parameter to get_upper_ifindexes() to avoid the issue. When there are too many upper devices, return -EOVERFLOW and abort the redirect. To reproduce, create more than MAX_NEST_DEV(8) macvlans on a device with an XDP program attached using BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS. Then send a packet to the device to trigger the XDP redirect path. Reported-by: syzbot+10cc7f13760b31bd2e61@syzkaller.appspotmail.com Closes: https://lore.kernel.org/all/698c4ce3.050a0220.340abe.000b.GAE@google.com/T/ Fixes: aeea1b8 ("bpf, devmap: Exclude XDP broadcast to master device") Reviewed-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: Kohei Enju <kohei@enjuk.jp> Link: https://lore.kernel.org/r/20260225053506.4738-1-kohei@enjuk.jp Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 66959ed commit ca83156

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

kernel/bpf/devmap.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -588,18 +588,22 @@ static inline bool is_ifindex_excluded(int *excluded, int num_excluded, int ifin
588588
}
589589

590590
/* Get ifindex of each upper device. 'indexes' must be able to hold at
591-
* least MAX_NEST_DEV elements.
592-
* Returns the number of ifindexes added.
591+
* least 'max' elements.
592+
* Returns the number of ifindexes added, or -EOVERFLOW if there are too
593+
* many upper devices.
593594
*/
594-
static int get_upper_ifindexes(struct net_device *dev, int *indexes)
595+
static int get_upper_ifindexes(struct net_device *dev, int *indexes, int max)
595596
{
596597
struct net_device *upper;
597598
struct list_head *iter;
598599
int n = 0;
599600

600601
netdev_for_each_upper_dev_rcu(dev, upper, iter) {
602+
if (n >= max)
603+
return -EOVERFLOW;
601604
indexes[n++] = upper->ifindex;
602605
}
606+
603607
return n;
604608
}
605609

@@ -615,7 +619,11 @@ int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
615619
int err;
616620

617621
if (exclude_ingress) {
618-
num_excluded = get_upper_ifindexes(dev_rx, excluded_devices);
622+
num_excluded = get_upper_ifindexes(dev_rx, excluded_devices,
623+
ARRAY_SIZE(excluded_devices) - 1);
624+
if (num_excluded < 0)
625+
return num_excluded;
626+
619627
excluded_devices[num_excluded++] = dev_rx->ifindex;
620628
}
621629

@@ -733,7 +741,11 @@ int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
733741
int err;
734742

735743
if (exclude_ingress) {
736-
num_excluded = get_upper_ifindexes(dev, excluded_devices);
744+
num_excluded = get_upper_ifindexes(dev, excluded_devices,
745+
ARRAY_SIZE(excluded_devices) - 1);
746+
if (num_excluded < 0)
747+
return num_excluded;
748+
737749
excluded_devices[num_excluded++] = dev->ifindex;
738750
}
739751

0 commit comments

Comments
 (0)