Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Sven Eckelmann <sven@narfation.org>
To: stable@vger.kernel.org
Cc: Sven Eckelmann <sven@narfation.org>
Subject: [PATCH 5.10 05/23] batman-adv: tp_meter: avoid divide-by-zero for dec_cwnd
Date: Fri, 26 Jun 2026 18:09:34 +0200	[thread overview]
Message-ID: <20260626160952.123713-6-sven@narfation.org> (raw)
In-Reply-To: <20260626160952.123713-1-sven@narfation.org>

commit 33ccd52f3cc9ed46ce395199f89aa3234dc83314 upstream.

The cwnd is always MSS <= cwnd <= 0x20000000. But the calculation in
batadv_tp_update_cwnd() assumes unsigned 32 bit arithmetics.

    ((mss * 8) ** 2) / (cwnd * 8)

In case cwnd is actually 0x20000000, it will be shifted by 3 bit to the
left end up at 0x100000000 or U32_MAX + 1. It will therefore wrap around
and be 0 - resulting in:

    ((mss * 8) ** 2) / 0

This is of course invalid and cannot be calculated. The calculation should
must be simplified to avoid this overflow:

   (mss ** 2) * 8 / cwnd

It will keep the precision enhancement from the scaling (by 8) but avoid
the overflow in the divisor.

In theory, there could still be an overflow in the dividend. It is at the
moment fixed to BATADV_TP_PLEN in batadv_tp_recv_ack() - so it is not an
imminent problem. But allowing it to use the whole u32 bit range, would
mean that it can still use up to 67 bits. To keep this calculation safe for
32 bit arithmetic, mss must never use more than floor((32 - 3) / 2) bits -
or in other words: must never be larger than 16383.

Cc: stable@kernel.org
Fixes: 33a3bb4a3345 ("batman-adv: throughput meter implementation")
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 net/batman-adv/tp_meter.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/net/batman-adv/tp_meter.c b/net/batman-adv/tp_meter.c
index 10953096996ab..118cb5dd285c2 100644
--- a/net/batman-adv/tp_meter.c
+++ b/net/batman-adv/tp_meter.c
@@ -153,9 +153,12 @@ static void batadv_tp_update_cwnd(struct batadv_tp_vars *tp_vars, u32 mss)
 		return;
 	}
 
+	/* prevent overflow in (mss * mss) << 3 */
+	mss = min_t(u32, mss, (1U << 14) - 1);
+
 	/* increment CWND at least of 1 (section 3.1 of RFC5681) */
 	tp_vars->dec_cwnd += max_t(u32, 1U << 3,
-				   ((mss * mss) << 6) / (tp_vars->cwnd << 3));
+				   ((mss * mss) << 3) / tp_vars->cwnd);
 	if (tp_vars->dec_cwnd < (mss << 3)) {
 		spin_unlock_bh(&tp_vars->cwnd_lock);
 		return;
-- 
2.47.3


  parent reply	other threads:[~2026-06-26 16:10 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-26 16:09 [PATCH 5.10 00/23] batman-adv: 7.2 merge window fixes backports Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 01/23] batman-adv: tp_meter: keep unacked list in ascending ordered Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 02/23] batman-adv: tp_meter: initialize dup_acks explicitly Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 03/23] batman-adv: tp_meter: initialize dec_cwnd explicitly Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 04/23] batman-adv: tp_meter: avoid window underflow Sven Eckelmann
2026-06-26 16:09 ` Sven Eckelmann [this message]
2026-06-26 16:09 ` [PATCH 5.10 06/23] batman-adv: tp_meter: fix fast recovery precondition Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 07/23] batman-adv: tp_meter: handle seqno wrap-around for fast recovery detection Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 08/23] batman-adv: tp_meter: add only finished tp_vars to lists Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 09/23] batman-adv: bla: annotate lasttime access with READ/WRITE_ONCE Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 10/23] batman-adv: prevent ELP transmission interval underflow Sven Eckelmann
2026-06-27 11:26   ` Sasha Levin
2026-06-27 12:05     ` Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 11/23] batman-adv: tp_meter: initialize last_recv_time during init Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 12/23] batman-adv: frag: ensure fragment is writable before modifying TTL Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 13/23] batman-adv: frag: avoid underflow of TTL Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 14/23] batman-adv: v: prevent OGM aggregation on disabled hardif Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 15/23] batman-adv: tp_meter: restrict number of unacked list entries Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 16/23] batman-adv: tp_meter: annotate last_recv_time access with READ/WRITE_ONCE Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 17/23] batman-adv: tp_meter: prevent parallel modifications of last_recv Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 18/23] batman-adv: tp_meter: handle overlapping packets Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 19/23] batman-adv: tt: don't merge change entries with different VIDs Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 20/23] batman-adv: tt: track roam count per VID Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 21/23] batman-adv: dat: prevent false sharing between VLANs Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 22/23] batman-adv: tvlv: enforce 2-byte alignment Sven Eckelmann
2026-06-26 16:09 ` [PATCH 5.10 23/23] batman-adv: tvlv: avoid race of cifsnotfound handler state Sven Eckelmann
2026-06-28  3:33 ` [PATCH 5.10 00/23] batman-adv: 7.2 merge window fixes backports Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260626160952.123713-6-sven@narfation.org \
    --to=sven@narfation.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox