Skip to contentSkip to conversation

net: qualcomm: rmnet: fix TX aggregation and teardown bugs

PATCH Superseded Antonio Quartulli 1 participant 8 messages Nov 26

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover net: qualcomm: rmnet: fix TX aggregation and teardown bugs

Bugs in rmnet TX aggregation and bridge teardown, found while reviewing [1]. Each commit message includes a reproducer.

All were reproduced in a VM (KASAN, kmemleak, DEBUG_OBJECTS) before the fix and are gone after it; patch 2 needs a debug udelay() to widen the race window, patch 3 a 32-bit kernel.

Although this patchset is targeting net, the described reproducers should help maintainers understandsing if patches should rather be demoted to net-next material...

Regards,

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

Antonio Quartulli (6):
net: qualcomm: rmnet: recheck the aggregate after flushing it
net: qualcomm: rmnet: always arm the flush timer for a new aggregate
net: qualcomm: rmnet: fix TX aggregation timeout overflow on 32-bit
net: qualcomm: rmnet: stop TX aggregation after detaching the port
net: qualcomm: rmnet: account linearization failures on the real
device
net: qualcomm: rmnet: unlink the bridge from the vnd it is attached to

 .../net/ethernet/qualcomm/rmnet/rmnet_config.c    |  7 ++++---
 .../net/ethernet/qualcomm/rmnet/rmnet_map_data.c  | 15 ++++++---------
 2 files changed, 10 insertions(+), 12 deletions(-)
Signature · 1 line
2.56.1
#1 lore raw

PATCH 1/6 net: qualcomm: rmnet: recheck the aggregate after flushing it

When an skb does not fit in the pending aggregate, rmnet_map_tx_aggregate() flushes it with agg_lock dropped, then jumps back past the skbagg_head check. rmnet vnds are lltx and can share a port, so another CPU may start an aggregate meanwhile: it is then overwritten and leaked along with the packets chained to it, or overtaken by a skb sent without aggregation.

Reproducer, with UDP sent through both vnds from several CPUs:

ip link add v0 mtu 9000 type veth peer name v1 mtu 9000 ip link set v0 up; ip link set v1 up
ip link add link v0 name rmnet1 type rmnet mux_id 1 ip link add link v0 name rmnet2 type rmnet mux_id 2

  ethtool -C rmnet1 tx-aggr-max-frames 64 tx-aggr-max-bytes 4096 \
          tx-aggr-time-usecs 2000

The tx_bytes of rmnet1 and rmnet2 soon exceed those of v0, and kmemleak reports skbs allocated by skb_copy_expand() in rmnet_map_tx_aggregate().

Move the label before the skbagg_head check.

Fixes 64b5d1f8f2d1 ("net: qualcomm: rmnet: add tx packets aggregation")
---
 drivers/net/ethernet/qualcomm/rmnet/rmnet_map_data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/qualcomm/rmnet/rmnet_map_data.c +1 -1
index 39d6d084e73f..73b04fe386df 100644
@@ -600,11 +600,11 @@ unsigned int rmnet_map_tx_aggregate(struct sk_buff *skb, struct rmnet_port *port
 	memcpy(&last, &port->agg_last, sizeof(struct timespec64));
 	ktime_get_real_ts64(&port->agg_last);
 
+new_packet:
 	if (!port->skbagg_head) {
 		/* Check to see if we should agg first. If the traffic is very
 		 * sparse, don't aggregate.
 		 */
-new_packet:
 		diff = timespec64_sub(port->agg_last, last);
 		size = port->egress_agg_params.bytes - skb->len;
 
Signature · 1 line
2.56.1
#2 lore raw

PATCH 2/6 net: qualcomm: rmnet: always arm the flush timer for a new aggregate

In reply to Antonio Quartulli

On flush, rmnet_map_tx_aggregate() resets agg_state and drops agg_lock before cancelling the flush timer. A CPU starting a new aggregate in that window finds the old timer active, so it neither arms it nor sets agg_state. Once the old timer is cancelled, the new aggregate waits for the next packet, and rmnet_map_tx_aggregate_exit() leaks it because agg_state is not -EINPROGRESS.

To make reproducing this bug easier, a udelay(2000) was added after agg_lock is dropped in the flush paths. Then, with two vnds on one port, tx-aggr-max-frames 2 and tx-aggr-time-usecs 100000, one CPU sends 3 packets on the first vnd (bypass, new aggregate, flush) and another sends 1 packet on the second vnd 0.5 ms later: that packet stays queued past the timeout.

Cancel the timer under agg_lock, so a timer armed for a newer aggregate cannot be cancelled, and arm it whenever agg_state is not -EINPROGRESS; hrtimer_start() copes with a timer that is still enqueued or running. If the cancel races with a running callback, the flush work it queued finds either no aggregate or the new one, which it flushes early.

Fixes 64b5d1f8f2d1 ("net: qualcomm: rmnet: add tx packets aggregation")
---
 drivers/net/ethernet/qualcomm/rmnet/rmnet_map_data.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/ethernet/qualcomm/rmnet/rmnet_map_data.c +3 -3
index 73b04fe386df..fdfc65c7c5e5 100644
@@ -635,8 +635,8 @@ unsigned int rmnet_map_tx_aggregate(struct sk_buff *skb, struct rmnet_port *port
 	if (skb->len > size) {
 		agg_skb = port->skbagg_head;
 		reset_aggr_params(port);
+		hrtimer_try_to_cancel(&port->hrtimer);
 		spin_unlock_bh(&port->agg_lock);
-		hrtimer_cancel(&port->hrtimer);
 		rmnet_send_skb(port, agg_skb);
 		spin_lock_bh(&port->agg_lock);
 		goto new_packet;
@@ -658,14 +658,14 @@ unsigned int rmnet_map_tx_aggregate(struct sk_buff *skb, struct rmnet_port *port
 	    port->skbagg_head->len == port->egress_agg_params.bytes) {
 		agg_skb = port->skbagg_head;
 		reset_aggr_params(port);
+		hrtimer_try_to_cancel(&port->hrtimer);
 		spin_unlock_bh(&port->agg_lock);
-		hrtimer_cancel(&port->hrtimer);
 		rmnet_send_skb(port, agg_skb);
 		return len;
 	}
 
 schedule:
-	if (!hrtimer_active(&port->hrtimer) && port->agg_state != -EINPROGRESS) {
+	if (port->agg_state != -EINPROGRESS) {
 		port->agg_state = -EINPROGRESS;
 		hrtimer_start(&port->hrtimer,
 			      ns_to_ktime(port->egress_agg_params.time_nsec),
Signature · 1 line
2.56.1
#3 lore raw

PATCH 3/6 net: qualcomm: rmnet: fix TX aggregation timeout overflow on 32-bit

In reply to Antonio Quartulli

On 32-bit, time * NSEC_PER_USEC is computed in 32 bits before being stored in the u64 time_nsec, so a tx-aggr-time-usecs above 4294967
wraps: 5000000 is stored as 705032704 ns and read back as 705032.

Reproducer:

ethtool -C rmnet0 tx-aggr-max-frames 64 tx-aggr-time-usecs 5000000 ethtool -c rmnet0

Do the multiplication in 64 bits.

Fixes 64b5d1f8f2d1 ("net: qualcomm: rmnet: add tx packets aggregation")
---
 drivers/net/ethernet/qualcomm/rmnet/rmnet_map_data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/qualcomm/rmnet/rmnet_map_data.c +1 -1
index fdfc65c7c5e5..b156e864535f 100644
@@ -689,7 +689,7 @@ void rmnet_map_update_ul_agg_config(struct rmnet_port *port, u32 size,
 	spin_lock_bh(&port->agg_lock);
 	port->egress_agg_params.bytes = size;
 	WRITE_ONCE(port->egress_agg_params.count, count);
-	port->egress_agg_params.time_nsec = time * NSEC_PER_USEC;
+	port->egress_agg_params.time_nsec = (u64)time * NSEC_PER_USEC;
 	spin_unlock_bh(&port->agg_lock);
 }
 
Signature · 1 line
2.56.1
#4 lore raw

PATCH 4/6 net: qualcomm: rmnet: stop TX aggregation after detaching the port

In reply to Antonio Quartulli

rmnet_unregister_real_device() tears down the aggregation state before unregistering the rx handler. When the last vnd on a port is deleted, that vnd is still registered and the egress path still finds the port through rx_handler_data, so a transmit can start an aggregate and arm the timer. The port is then freed: the timer fires on freed memory and the aggregate leaks.

Reproducer:

ip link add link v0 name rmnet1 type rmnet mux_id 1

  ethtool -C rmnet1 tx-aggr-max-frames 64 tx-aggr-max-bytes 4096 \
          tx-aggr-time-usecs 1000000

then delete rmnet1 while flooding UDP through it from all CPUs. DEBUG_OBJECTS_TIMERS reports "ODEBUG: free active object type: hrtimer", KASAN a use-after-free once the timer fires.

Unregister the rx handler first: after its RCU grace period no transmit can find the port.

Fixes 64b5d1f8f2d1 ("net: qualcomm: rmnet: add tx packets aggregation")
---
 drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c +2 -2
index 61b04c6c0390..2ff4b9519139 100644
@@ -40,10 +40,10 @@ static int rmnet_unregister_real_device(struct net_device *real_dev)
 	if (port->nr_rmnet_devs)
 		return -EINVAL;
 
-	rmnet_map_tx_aggregate_exit(port);
-
 	netdev_rx_handler_unregister(real_dev);
 
+	rmnet_map_tx_aggregate_exit(port);
+
 	kfree(port);
 
 	netdev_dbg(real_dev, "Removed from rmnet\n");
Signature · 1 line
2.56.1
#5 lore raw

PATCH 5/6 net: qualcomm: rmnet: account linearization failures on the real device

In reply to Antonio Quartulli

When the real device lacks NETIF_F_FRAGLIST and linearizing an aggregate fails, rmnet_send_skb() counts the drop on port->rmnet_dev. That is the vnd created last on the port, never updated when it is deleted, so it may point to a freed net_device.

The aggregate head is sized for tx-aggr-max-bytes, so linearizing only allocates when that limit grows while an aggregate is pending. Reproducer:

ip link add v0 mtu 16000 type veth peer name v1 mtu 16000 ethtool -K v0 tx-scatter-gather-fraglist off
ip link add link v0 name rmnet1 type rmnet mux_id 1 ip link add link v0 name rmnet2 type rmnet mux_id 2 ethtool -C rmnet1 tx-aggr-max-frames 64 tx-aggr-time-usecs 2000 ip link del rmnet2

then flood UDP through rmnet1 with failslab on the senders (task-filter, probability 10) while switching tx-aggr-max-bytes between 512 and 12288. KASAN reports a use-after-free in rmnet_send_skb().

Count the drop on the real device, where it happens: the aggregate may hold packets from any vnd.

Fixes 64b5d1f8f2d1 ("net: qualcomm: rmnet: add tx packets aggregation")
---
 drivers/net/ethernet/qualcomm/rmnet/rmnet_map_data.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/net/ethernet/qualcomm/rmnet/rmnet_map_data.c +1 -4
index b156e864535f..3de093198474 100644
@@ -543,10 +543,7 @@ static void rmnet_send_skb(struct rmnet_port *port, struct sk_buff *skb)
 {
 	if (skb_needs_linearize(skb, port->dev->features)) {
 		if (unlikely(__skb_linearize(skb))) {
-			struct rmnet_priv *priv;
-
-			priv = netdev_priv(port->rmnet_dev);
-			this_cpu_inc(priv->pcpu_stats->stats.tx_drops);
+			dev_core_stats_tx_dropped_inc(port->dev);
 			dev_kfree_skb_any(skb);
 			return;
 		}
Signature · 1 line
2.56.1
#6 lore raw

PATCH 6/6 net: qualcomm: rmnet: unlink the bridge from the vnd it is attached to

In reply to Antonio Quartulli

When the real device of a bridged port is unregistered or moved to another netns, rmnet_unregister_bridge() unlinks the bridge device from the real port's rmnet_dev: the vnd created last, never updated when it is deleted. If the newest vnd was deleted before attaching the bridge to the remaining one, the unlink reads and writes the freed net_device, the actual bridge link is left in place and unregistering the vnd hangs on its references.

Reproducer:

ip link add v0 type veth peer name v1
ip link add v2 type veth peer name v3
ip link set v0 up; ip link set v2 up
ip link add link v0 name rmnet1 type rmnet mux_id 1 ip link add link v0 name rmnet2 type rmnet mux_id 2 ip link del rmnet2
ip link set v2 master rmnet1
ip link del v0

Take the vnd from the bridge device's port, which records the one it is attached to.

Fixes d939b6d30bea ("net: rmnet: fix bridge mode bugs")
---
 drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c +2 -1
index 2ff4b9519139..96129101e477 100644
@@ -96,9 +96,9 @@ static void rmnet_unregister_bridge(struct rmnet_port *port)
 	if (port->rmnet_mode != RMNET_EPMODE_BRIDGE)
 		return;
 
-	rmnet_dev = port->rmnet_dev;
 	if (!port->nr_rmnet_devs) {
 		/* bridge device */
+		rmnet_dev = port->rmnet_dev;
 		real_dev = port->bridge_ep;
 		bridge_dev = port->dev;
 
@@ -108,6 +108,7 @@ static void rmnet_unregister_bridge(struct rmnet_port *port)
 	} else {
 		/* real device */
 		bridge_dev = port->bridge_ep;
+		rmnet_dev = rmnet_get_port_rtnl(bridge_dev)->rmnet_dev;
 
 		port->bridge_ep = NULL;
 		port->rmnet_mode = RMNET_EPMODE_VND;
Signature · 1 line
2.56.1
#7 lore raw
In reply to Antonio Quartulli

Hi,

I just realized that patches 4, 5 and 6 duplicate fixes already in net-next, which I missed (they are not in net yet):

4/6 == 0af0e210b5bd ("net: qualcomm: rmnet: fix TX aggregation
teardown race")
5/6, 6/6 are covered by 4f89bcfa1e16 ("net: qualcomm: rmnet: avoid
stale VND access on TX drop")

Therefore they can be dropped.

Patches 1-3 fix bugs still present in net-next, but 1 and 2 conflict with 7bcccc40cdb5 ("net: qualcomm: rmnet: bound TX aggregates on byte-limit update").

Since those related fixes went to net-next, I will just resend 1, 2 and 3 rebased on net-next.

Regards,

pw-bot: cr

Signature · 5 lines
Antonio Quartulli

CEO and Co-Founder
Mandelbit Srl
https://www.mandelbit.com