From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f180.google.com (mail-qt1-f180.google.com [209.85.160.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EBD2C30FC21 for ; Sat, 1 Aug 2026 22:29:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785623368; cv=none; b=cMm3WmPSlsHuXAgRZy3hntFDiOdXaHhD+5XSyB9J0mRr2z/ZEEUMAwSJLjuudIU83ISczkcK1OVcn21yzH3get5xsPuZRoSIF8KdxRW0Ev3PUDAdv3qkQEpaIqea7yiZLB97DHjn/0LBqWOzb9ufBPC4JeOYxMTv734a3P4C6JU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785623368; c=relaxed/simple; bh=0UFkvjNv0bQt5HrSYPmeTIaP939CNyqwdnq4nzsrZPI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sYZ73veYGkc9+kBUdvua3FBV1/QcZrbEA6nJuPlPCPoGTeSQOFYch0lZdP/jewOpIJ+NNeil2Vejmzz+3eHXfQKB/XKHqv4SecMKP/kE/wRiIStOo9Q7NFFHv0it30boYemC3DAmNj/I66U1nz2Xt+cNg01CXktAvMKVSAO8L3o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JNLaW7lz; arc=none smtp.client-ip=209.85.160.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JNLaW7lz" Received: by mail-qt1-f180.google.com with SMTP id d75a77b69052e-51a868b6962so21578821cf.2 for ; Sat, 01 Aug 2026 15:29:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785623366; x=1786228166; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=TOfcCbWV+8AiSnf47MNeISZ41ODzpJCZxK60zqEsNHY=; b=JNLaW7lzExiAsoN2R9FX+sdU+NTXrjjZfGdDrdWalQXszj8DbgXe4tvf1SdIitj4gV 34dz08WGZsv/SGH8QtCAPkWtP02OVXybs2K6iBb3MT/M4uKk5a+Ha6KMD/BlZqYPIvaq 1hSZsdb+/B7RBiws4L9Xd4lieNXTFTYTN3SLC/xtjbWgZg9vLpNjEHLenJ2y95XqMfnY mkJ0Gzkk1JOz3xBwe0O36hXUjT6amgdBzIGMUDiAkcqjezl49t4+/QNFcU1/MiRxsa+v JAQ3s8ZOq7C8VyJXIc4ZKPM6BwI0KOAYwr1w8qKcRs8l508MgcvZzH801kPohqJ3JSoM nDdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785623366; x=1786228166; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=TOfcCbWV+8AiSnf47MNeISZ41ODzpJCZxK60zqEsNHY=; b=IzMsjdEuQnX+x/U6I7HBZe3Dt++s2V8c6V5wQ4FG2xKdTkTXXR1qT8Exnn4cMmbazn DgCiOMn3qAHmKheF5mu52n7ggkQjbCXhlPr/9CWZPTzNZyYp5VpEdD2K/qTk007NCa3B 6XpD1sI3yMsDqQlXQw/Sy19ioET2PgbvH+dZo8rdtRMO/bXADJpd62t6LhoteVtBnPET citqEmGfsIR7CX/QRBjdB72xwhg6O+SGtwbI4moWXKkwXrSa2NV1gDO57rToG9xqLzRO dgl8iTejU64pVp95S/wCC1HsvgwqNUz8XH/MeiAoQ0PMBsvKLtA+AwAbFj5MEEJXWrpn tmqA== X-Gm-Message-State: AOJu0YzzuUd0HxP2aq6OOysHktHN8Am7VLTFggMPTqW32CTcXEvCcbc4 NywKFi+UsdBDWR6JBK9VBs052fv6BL0Yh8gy09EiHEmFs00ZGeJjxY3s X-Gm-Gg: AR+sD128RbvJCkO8qVyA5gCcNilEaUlDYhCxkO+jQHGVFjXJtCUsITq+Oq+UeuQb0gm 8m/KqPLJ2Zc4VJ2kVACeLxrUGyTVngEzzopE8L6szmyZuKldnZNZA6ogXaCrCIHAGpN3Y8N2Jhs x89smQ0LKrtR2v51ui0HEHOC82UEscCsyxshULwDfYkhFGyqrVWEX4G22ghQNtfpzp5spNfGnD0 SLVb8FWxtjqSxSPUgUgOHgO3DhIwa9eYwL+uX7Mtg1njYyRR5F33Wrht16zHrs44IXBogpd+mHy 7aA42QLNDQN8M+g7CSkhAx5r+q/19xjX8GE5dyAoj0hUb2YmrwHY9Y/yHduHkkY2hSqmuD2LLab hnEOKKzohNDTHxtcyZb3QlwgoQAhhCFeEXmO7HAWwtoGqtVnGJpPAQBONfV0EnzAyJVEiVrMgVR IKOUJEPAd9ueEa26raEU6oGdKKmgmDIShL7RjKBbBmGMUxm3peRW+H7TYl6Z2X0eGhBA/sTWQ14 2nA8rHsJuJL1Ge3VKUvpIjM9fzCSpPQ7wILnY02EnH6oEfJggCc3NJz0W0MnXhwKxXjjr7b50YR +/ZJn2N5Ul6+cDI= X-Received: by 2002:a05:622a:34a:b0:50d:3e1e:7998 with SMTP id d75a77b69052e-52b567b2834mr97453361cf.37.1785623366012; Sat, 01 Aug 2026 15:29:26 -0700 (PDT) Received: from ip-172-31-15-253.ec2.internal (ec2-32-195-55-166.compute-1.amazonaws.com. [32.195.55.166]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-52b4eb956c3sm33403831cf.22.2026.08.01.15.29.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 01 Aug 2026 15:29:25 -0700 (PDT) From: Deep Shah To: netdev@vger.kernel.org, Richard Cochran , "David S . Miller" , Jakub Kicinski , Paolo Abeni , Eric Dumazet , Andrew Lunn Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Shuah Khan , Vadim Fedorenko , Simon Horman , Deep Shah Subject: [PATCH net-next v3 1/2] ptp: reject frequency adjustments that overflow scaled_ppm_to_ppb() Date: Sat, 1 Aug 2026 22:29:22 +0000 Message-ID: <20260801222923.39017-2-deepshah146@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260801222923.39017-1-deepshah146@gmail.com> References: <20260801222923.39017-1-deepshah146@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit ptp_clock_adjtime() validates an ADJ_FREQUENCY request by converting the requested scaled ppm to ppb and comparing it against ops->max_adj: long ppb = scaled_ppm_to_ppb(tx->freq); if (ppb > ops->max_adj || ppb < -ops->max_adj) return -ERANGE; scaled_ppm_to_ppb() computes (1 + ppm) * 125 >> 13 in s64. For a sufficiently large tx->freq the multiplication overflows s64 and wraps, so the resulting ppb can fall back within [-max_adj, max_adj] and pass the check. The unclamped tx->freq is then handed to ->adjfine(), where drivers scale it again (e.g. scaled_ppm * 762939453125 in ptp_idt82p33) and program a bogus frequency word. For example tx->freq = 147573952589676412 makes (1 + ppm) * 125 equal 2^64 + 9, which wraps to ppb == 0 and is accepted. The caller already has write access to the PHC, so this hardens the max_adj sanity check rather than crossing a privilege boundary, and well-behaved user space (e.g. ptp4l) never requests such values. It is a follow-up to commit 475b92f93216 ("ptp: improve max_adj check against unreasonable values"), which handled the analogous s32 narrowing but not this multiplication overflow. Detect the overflow with check_*_overflow() and reject the request in ptp_clock_adjtime() instead of acting on the wrapped value. Signed-off-by: Deep Shah Reviewed-by: Vadim Fedorenko --- drivers/ptp/ptp_clock.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c index d6f54ccaf93b..4111342d64f0 100644 --- a/drivers/ptp/ptp_clock.c +++ b/drivers/ptp/ptp_clock.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -159,7 +160,18 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx) delta = ktime_to_ns(kt); err = ops->adjtime(ops, delta); } else if (tx->modes & ADJ_FREQUENCY) { - long ppb = scaled_ppm_to_ppb(tx->freq); + long ppb; + s64 tmp; + + /* + * scaled_ppm_to_ppb() multiplies (1 + freq) by 125 in s64; + * reject a ->freq large enough to overflow that, which would + * otherwise wrap the result back into the max_adj range. + */ + if (check_add_overflow((s64)tx->freq, (s64)1, &tmp) || + check_mul_overflow(tmp, (s64)125, &tmp)) + return -ERANGE; + ppb = scaled_ppm_to_ppb(tx->freq); if (ppb > ops->max_adj || ppb < -ops->max_adj) return -ERANGE; err = ops->adjfine(ops, tx->freq); -- 2.43.0