Received: from mail.simonwunderlich.de (mail.simonwunderlich.de [23.88.38.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD87E3FE35F for ; Wed, 3 Jun 2026 07:25:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=23.88.38.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780471542; cv=none; b=G8YhQ7ILuzE0pgbwgXWvnoo+FQYl7+IEGB4X7m8Kux5S5KEVpndWBYPq1jP8m9z58ddV4ZCq+zdFoDwldppu8NCEbUOgf1VDGjZC5l2ABezg9XpyVSReapAZ2Y2yu6Q0uObY1YzkNSY7RzjtrHXVnxhwt3iIxSkCFWfvTnCivoE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780471542; c=relaxed/simple; bh=IiK6gMCtcJO/q0UgK2F0JiUVSYWXWNO7JBXFUhEimOk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s09zDRJh1dv3G1EXUbluOXTwvTPUr2Zmh/Di6VK8hlUr53sJRD4yb2Jdu9VLRutxDRBMAfYpg+hR9HxopW83tez20HV3lW3AlHPjcLaBg89R+DOJ8anvrcLB+1oyJV7S2agOcJLRBgH+jusLXgUFX/J/6UsQXRlxbj0iy76+Veg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=simonwunderlich.de; spf=pass smtp.mailfrom=simonwunderlich.de; dkim=pass (2048-bit key) header.d=simonwunderlich.de header.i=@simonwunderlich.de header.b=hQITO+zl; arc=none smtp.client-ip=23.88.38.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=simonwunderlich.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=simonwunderlich.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=simonwunderlich.de header.i=@simonwunderlich.de header.b="hQITO+zl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=simonwunderlich.de; s=09092022; t=1780471532; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BuVoWFd4W/HCKuFvrohKZQ0oO4kQS41DcVZ6puYOLXs=; b=hQITO+zlB026UQ0gfElumiFC5rm2dl/jMZi5uk18hoBfwA2Y61SeKZCC8uSZ1JOu/LgJ91 zd6UhU2/MJHa4ajwFjjbAMmvdJjPXE/qIKd4Hzut4FgI0OYZRZXBPoJzqyMsPhhTxY0PIl ZQG+SXXkpN0wI0io5HwcDxFswVnpfFxfJDEX5dJUGJldZXF3DfTD2UyDVwsricX24To0ij LwskrR3rPblKC9qfdErPUubo36Ok21ZoZV87dahZzLiOF7HdZ2Pv7t8jV1lMNbuEuKlTqV fOnwd2+RquUKMXcmRZa31OsLLXe1JI7BONQmK3kQdetykvh4WhDbe2/ZKugbPw== From: Simon Wunderlich To: netdev@vger.kernel.org Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , b.a.t.m.a.n@lists.open-mesh.org, Sven Eckelmann , stable@kernel.org, Simon Wunderlich Subject: [PATCH net-next 05/15] batman-adv: tp_meter: avoid divide-by-zero for dec_cwnd Date: Wed, 3 Jun 2026 09:25:16 +0200 Message-ID: <20260603072527.174487-6-sw@simonwunderlich.de> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260603072527.174487-1-sw@simonwunderlich.de> References: <20260603072527.174487-1-sw@simonwunderlich.de> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sven Eckelmann 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 Signed-off-by: Simon Wunderlich --- 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 9ecbc6023cfc9..1655f181c9293 100644 --- a/net/batman-adv/tp_meter.c +++ b/net/batman-adv/tp_meter.c @@ -154,9 +154,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