Skip to contentSkip to conversation

net: Catch invalid index in XPS mapping

PATCH Superseded Nick Child 3 participants 7 messages Mar 17, 2023 – Mar 21, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Nick Child

author
#0 lore raw

PATCH 1/2 net: Catch invalid index in XPS mapping

When setting the XPS value of a TX queue, add a conditional to ensure that the index of the queue is less than the number of allocated TX queues.

Previously, this scenario went uncaught. In the best case, it resulted in unnecessary allocations. In the worst case, it resulted in out-of-bounds memory references through calls to netdev_get_tx_queue( dev, index).

Fixes 537c00de1c9b ("net: Add functions netif_reset_xps_queue and netif_set_xps_queue")

---
This is a result of my own foolish mistake of giving an invalid index to __netif_set_xps_queue [1]. While the function adds the queue to the cpu's XPS queue map, the queue is never used due to a conditional in __get_xps_queue_idx. But there is a risk of random memory reading and writing that should be prevented.

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

 net/core/dev.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/dev.c +3 -0
index c7853192563d..cd3878043846 100644
@@ -2535,6 +2535,9 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
 	struct xps_map *map, *new_map;
 	unsigned int nr_ids;
 
+	if (index >= dev->num_tx_queues)
+		return -EINVAL;
+
 	if (dev->num_tc) {
 		/* Do not allow XPS on subordinate device directly */
 		num_tc = dev->num_tc;
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH 2/2 netdev: Enforce index cap in netdev_get_tx_queue

When requesting a TX queue at a given index, prevent out-of-bounds referencing by ensuring that the index is within the allocated number of queues.

If there is an out-of-bounds reference then inform the user and return a reference to the first tx queue instead.

Fixes e8a0464cc950 ("netdev: Allocate multiple queues for TX.")
---
 include/linux/netdevice.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/linux/netdevice.h +7 -0
index 23b0d7eaaadd..fe88b1a7393d 100644
@@ -2482,6 +2482,13 @@ static inline
 struct netdev_queue *netdev_get_tx_queue(const struct net_device *dev,
 					 unsigned int index)
 {
+	if (unlikely(index >= dev->num_tx_queues)) {
+		net_warn_ratelimited("%s selects TX queue %d, but number of TX queues is %d\n",
+				     dev->name, index,
+				     dev->num_tx_queues);
+		return &dev->_tx[0];
+	}
+
 	return &dev->_tx[index];
 }
 
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH net 2/2] netdev: Enforce index cap in netdev_get_tx_queue

Quoted 18 lines · Nick Child

When requesting a TX queue at a given index, prevent out-of-bounds referencing by ensuring that the index is within the allocated number of queues.

If there is an out-of-bounds reference then inform the user and return a reference to the first tx queue instead.

Fixes e8a0464cc950 ("netdev: Allocate multiple queues for TX.")
---
 include/linux/netdevice.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/linux/netdevice.h +0 -0
index 23b0d7eaaadd..fe88b1a7393d 100644

include/linux/netdevice.h static inline · line 2489

Quoted 8 lines
include/linux/netdevice.h +5 -0
@@ -2482,6 +2482,13 @@ static inline
 struct netdev_queue *netdev_get_tx_queue(const struct net_device *dev,
 					 unsigned int index)
 {
+	if (unlikely(index >= dev->num_tx_queues)) {
+		net_warn_ratelimited("%s selects TX queue %d, but number of TX queues is %d\n",
+				     dev->name, index,
+				     dev->num_tx_queues);
+		return &dev->_tx[0];

Why return first queue here instead of NULL, wouldn't that confuse the caller instead of return proper (NULL) value?

Piotr.

Quoted 8 lines
include/linux/netdevice.h +2 -0
+	}
+
 	return &dev->_tx[index];
 }
 
Signature · 1 line
2.31.1
author
#3 lore raw

Re: [PATCH net 2/2] netdev: Enforce index cap in netdev_get_tx_queue

Quoted 14 lines · Piotr Raczynski
Quoted 13 lines · Nick Child

When requesting a TX queue at a given index, prevent out-of-bounds referencing by ensuring that the index is within the allocated number of queues.

If there is an out-of-bounds reference then inform the user and return a reference to the first tx queue instead.

Fixes e8a0464cc950 ("netdev: Allocate multiple queues for TX.")
---
  include/linux/netdevice.h | 7 +++++++
  1 file changed, 7 insertions(+)
Quoted 17 lines

include/linux/netdevice.h static inline · line 2489

Quoted 13 lines
include/linux/netdevice.h +5 -0
index 23b0d7eaaadd..fe88b1a7393d 100644
@@ -2482,6 +2482,13 @@ static inline
  struct netdev_queue *netdev_get_tx_queue(const struct net_device *dev,
  					 unsigned int index)
  {
+	if (unlikely(index >= dev->num_tx_queues)) {
+		net_warn_ratelimited("%s selects TX queue %d, but number of TX queues is %d\n",
+				     dev->name, index,
+				     dev->num_tx_queues);
+		return &dev->_tx[0];

Why return first queue here instead of NULL, wouldn't that confuse the caller instead of return proper (NULL) value?

Thanks for reviewing Piotr.
netdev_get_tx_queue has over 300 callers, most of these calls use the returned queue immediately without any checking on the returned value. I don't expect all of these callers to go and add conditionals to handle this case either.

So I opted for the warning message and a valid return value. That being said, I am open to more opinions.

Quoted 9 lines

Piotr.

Quoted 8 lines
include/linux/netdevice.h +2 -0
+	}
+
  	return &dev->_tx[index];
  }
 
Signature · 1 line
2.31.1
maintainer
#4 lore raw

Re: [PATCH net 2/2] netdev: Enforce index cap in netdev_get_tx_queue

In reply to Nick Child
Quoted 5 lines · On Fri, 17 Mar 2023 13:19:41 -0500 Nick Child wrote:
include/linux/netdevice.h +0 -0
index 23b0d7eaaadd..fe88b1a7393d 100644

include/linux/netdevice.h static inline · line 2491

Quoted 10 lines
include/linux/netdevice.h +7 -0
@@ -2482,6 +2482,13 @@ static inline
 struct netdev_queue *netdev_get_tx_queue(const struct net_device *dev,
 					 unsigned int index)
 {
+	if (unlikely(index >= dev->num_tx_queues)) {
+		net_warn_ratelimited("%s selects TX queue %d, but number of TX queues is %d\n",
+				     dev->name, index,
+				     dev->num_tx_queues);
+		return &dev->_tx[0];
+	}
+

Should we maybe do DEBUG_NET_WARN_ON_ONCE() instead? It will likely run multiple times per each Tx packet, so I wonder if we really want to add a branch for what's effectively defensive programming...

Reviewed-by on v1 1/2
#5 lore raw
In reply to Nick Child
Quoted 40 lines · Nick Child

When setting the XPS value of a TX queue, add a conditional to ensure that the index of the queue is less than the number of allocated TX queues.

Previously, this scenario went uncaught. In the best case, it resulted in unnecessary allocations. In the worst case, it resulted in out-of-bounds memory references through calls to netdev_get_tx_queue( dev, index).

Fixes 537c00de1c9b ("net: Add functions netif_reset_xps_queue and netif_set_xps_queue")

---
This is a result of my own foolish mistake of giving an invalid index to __netif_set_xps_queue [1]. While the function adds the queue to the cpu's XPS queue map, the queue is never used due to a conditional in __get_xps_queue_idx. But there is a risk of random memory reading and writing that should be prevented.

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

 net/core/dev.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/dev.c +3 -0
index c7853192563d..cd3878043846 100644
@@ -2535,6 +2535,9 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
 	struct xps_map *map, *new_map;
 	unsigned int nr_ids;
 
+	if (index >= dev->num_tx_queues)
+		return -EINVAL;
+
 	if (dev->num_tc) {
 		/* Do not allow XPS on subordinate device directly */
 		num_tc = dev->num_tc;
Signature · 1 line
2.31.1

Reasonable check added, thanks.

maintainer
#6 lore raw
In reply to Nick Child

net/core/dev.c int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask, · line 2539

Quoted 2 lines · On Fri, 17 Mar 2023 13:19:40 -0500 Nick Child wrote:
net/core/dev.c +2 -0
+	if (index >= dev->num_tx_queues)
+		return -EINVAL;

WARN_ON_ONCE()? On a quick grep virtio does not check return value for example. Others may assume this never fails and not print any warning, and users will have "fun time" figuring out why their machine fell of the network / where is the probe error coming from..

Also seems like net-next material? Why do we consider this a fix? It's defensive / debug check, ain't no bug to assume callers are sane..