bpf: devmap: fix stack-out-of-bounds write in get_upper_ifindexes()
Subsystems (4)
PATCH v2 1/1 bpf: devmap: fix stack-out-of-bounds write in get_upper_ifindexes()
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.
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.
---
Changes:
v2:
- fix formatting, accounting that max-line-length is 100
v1: https://lore.kernel.org/bpf/[email protected]/
--- kernel/bpf/devmap.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
kernel/bpf/devmap.c +8 -4
@@ -588,16 +588,18 @@ static inline bool is_ifindex_excluded(int *excluded, int num_excluded, int ifin }/* Get ifindex of each upper device. 'indexes' must be able to hold at -* least MAX_NEST_DEV elements. +* least 'max' elements. * Returns the number of ifindexes added. */ -static int get_upper_ifindexes (struct net_device *dev,int *indexes) +static int get_upper_ifindexes (struct net_device *dev,int *indexes,int max) {struct net_device *upper;struct list_head *iter;int n =0 ;netdev_for_each_upper_dev_rcu (dev, upper, iter) { +if (n >= max) +break ; indexes[n++] = upper->ifindex; }return n;
@@ -615,7 +617,8 @@ int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,int err;if (exclude_ingress) { - num_excluded =get_upper_ifindexes (dev_rx, excluded_devices); + num_excluded =get_upper_ifindexes (dev_rx, excluded_devices, +ARRAY_SIZE (excluded_devices) -1 ); excluded_devices[num_excluded++] = dev_rx->ifindex; }
@@ -733,7 +736,8 @@ int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,int err;if (exclude_ingress) { - num_excluded =get_upper_ifindexes (dev, excluded_devices); + num_excluded =get_upper_ifindexes (dev, excluded_devices, +ARRAY_SIZE (excluded_devices) -1 ); excluded_devices[num_excluded++] = dev->ifindex; }
Signature · 1 line
2.51.0
Quoted 40 lines · Kohei Enju
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_DEVand 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 thanMAX_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.To reproduce, create more than
MAX_NEST_DEV(8)macvlans on a device with an XDP program attached usingBPF_F_BROADCAST|BPF_F_EXCLUDE_INGRESS. Then send a packet to the device to trigger the XDP redirect path.Reported-by [email protected]Fixes aeea1b86f936 ("bpf, devmap: Exclude XDP broadcast to master device")Signed-off-by Kohei Enju <[email protected]>---
Changes:
v2:
- fix formatting, accounting that max-line-length is 100
v1: https://lore.kernel.org/bpf/[email protected]/--- kernel/bpf/devmap.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)kernel/bpf/devmap.c +1
-1@@ -588,16 +588,18 @@ static inline bool is_ifindex_excluded(int *excluded, int num_excluded, int ifin }/* Get ifindex of each upper device. 'indexes' must be able to hold at -* least MAX_NEST_DEV elements. +* least 'max' elements. * Returns the number of ifindexes added. */
kernel/bpf/devmap.c static inline bool is_ifindex_excluded(int *excluded, int num_excluded, int ifin · line 602
Quoted 10 lines
kernel/bpf/devmap.c +3
-1-static int get_upper_ifindexes (struct net_device *dev,int *indexes) +static int get_upper_ifindexes (struct net_device *dev,int *indexes,int max) {struct net_device *upper;struct list_head *iter;int n =0 ;netdev_for_each_upper_dev_rcu (dev, upper, iter) { +if (n >= max) +break ;
Hmm, how about returning an error here, and aborting the redirect entirely? This silent truncation to an arbitrary number seems a bit surprising, and very hard to debug. With an error, there will at least be an error tracepoint to catch the failure.
-Toke
Quoted 59 lines · Toke Høiland-Jørgensen
Quoted 41 lines · Kohei Enju
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_DEVand 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 thanMAX_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.To reproduce, create more than
MAX_NEST_DEV(8)macvlans on a device with an XDP program attached usingBPF_F_BROADCAST|BPF_F_EXCLUDE_INGRESS. Then send a packet to the device to trigger the XDP redirect path.Reported-by [email protected]Fixes aeea1b86f936 ("bpf, devmap: Exclude XDP broadcast to master device")Signed-off-by Kohei Enju <[email protected]>---
Changes:
v2:
- fix formatting, accounting that max-line-length is 100
v1: https://lore.kernel.org/bpf/[email protected]/--- kernel/bpf/devmap.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)kernel/bpf/devmap.c +1
-2@@ -588,16 +588,18 @@ static inline bool is_ifindex_excluded(int *excluded, int num_excluded, int ifin }/* Get ifindex of each upper device. 'indexes' must be able to hold at -* least MAX_NEST_DEV elements. +* least 'max' elements. * Returns the number of ifindexes added. */ -static int get_upper_ifindexes (struct net_device *dev,int *indexes)kernel/bpf/devmap.c static inline bool is_ifindex_excluded(int *excluded, int num_excluded, int ifin · line 603
Quoted 10 lines
kernel/bpf/devmap.c +3
-0+static int get_upper_ifindexes (struct net_device *dev,int *indexes,int max) {struct net_device *upper;struct list_head *iter;int n =0 ;netdev_for_each_upper_dev_rcu (dev, upper, iter) { +if (n >= max) +break ;Hmm, how about returning an error here, and aborting the redirect entirely? This silent truncation to an arbitrary number seems a bit surprising, and very hard to debug. With an error, there will at least be an error tracepoint to catch the failure.
Quoted 1 line
-Toke
Hi, Toke.
Thanks for the review.
Indeed I agree that silent truncation is surprising and hard to debug. I'll send a v3 that returns -EOVERFLOW and aborts the redirect when there are too many upper devices.