Message ID | 430a1271-a45c-4f5a-90c7-a62703ac7cf4@ancud.ru |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp614170vqs; Thu, 9 Nov 2023 10:09:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IGmG429KBIqbfM8nXR9PP9IB7XnYQr7BGj1icY2r8uz7yPs8uch2PCiKdW6/0CwT6xYO7nN X-Received: by 2002:a17:902:9a93:b0:1cc:6b5a:a2f1 with SMTP id w19-20020a1709029a9300b001cc6b5aa2f1mr5441010plp.8.1699553371952; Thu, 09 Nov 2023 10:09:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699553371; cv=none; d=google.com; s=arc-20160816; b=wC9HvT+MvvAuoTqAofbiuG7oITSfmhQ9bNJGWkDOJ5QgIqqnhTlErw6j0cXmdfG/4I DdWjngsPTP1q18/DEYH57up2s4+4pSmqDQKc9qTNLsk19+5zxhnAUdU5lXgl+eipo9/I p0wbu9CmyBke0vzAAPejYodA1Mp0wkk6CGygJXqJ+INstiYnP6MjgkJw3nNL5fdAQDkg eNQnsmODUNNZh2s63CtsuTlvDGG22noG0QiLZosYTt9nju7eh6yv0R01GOKYNjehWZOE bmRrykcdijOOQBs/FhG2zaI/kv+tdbReT3UDMAPpuusbiLvi0ANHprE2X8exAljSVnqr xlMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language:cc:to :subject:from:user-agent:mime-version:date:message-id; bh=tlpHyCsRyYO95Ym79pM1rQv6P3csItrNZjgrfqlGx8E=; fh=lwgto5dPFEWoYNQTiWX9fbaxzWBM6YuPo0/1akwU8Vc=; b=SSQ7vdhPsuAcEuQC/EjIuJPXN+bNCYtLeiuEHRPHZmO2oERQwrtGgJMoXBocfJKuwp pF6MNZyD4Sna6cJjK2OFF2qpiMRdTqKCJdzpn3q5EtTgNlE5t+98mrbhCKv+5GH9AlHn YN8/L7iRoAJsjaEaTWA0Nk0C7J/KXJM2iOqaxTHQdk4u6XvIS24Q/0YqSxdDsisRlP84 yNigxEH9tEy55sgc/0SdYWcPhEPRLImKaosCFhOs5VeJaFauFhoBYTV8MILSIYYMD6Rk Utjl5Lq1lIEpYSbr5PkBC8MerqZL5Ex4DsVLJmM5lWMjlW5M6U3DcNYNVmPbMr9clDDD J1Mw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id m4-20020a170902db0400b001c9fe071f2csi5817205plx.105.2023.11.09.10.09.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 10:09:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id DBD0183C38C7; Thu, 9 Nov 2023 10:09:29 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344928AbjKISJO (ORCPT <rfc822;lhua1029@gmail.com> + 31 others); Thu, 9 Nov 2023 13:09:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231478AbjKISJM (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 9 Nov 2023 13:09:12 -0500 Received: from relay164.nicmail.ru (relay164.nicmail.ru [91.189.117.8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7DC0CE; Thu, 9 Nov 2023 10:09:07 -0800 (PST) Received: from [10.28.138.151] (port=11776 helo=[192.168.95.111]) by relay.hosting.mail.nic.ru with esmtp (Exim 5.55) (envelope-from <kiryushin@ancud.ru>) id 1r19Sf-00020H-DZ; Thu, 09 Nov 2023 21:09:01 +0300 Received: from [87.245.155.195] (account kiryushin@ancud.ru HELO [192.168.95.111]) by incarp1103.mail.hosting.nic.ru (Exim 5.55) with id 1r19Sf-001rie-0S; Thu, 09 Nov 2023 21:09:01 +0300 Message-ID: <430a1271-a45c-4f5a-90c7-a62703ac7cf4@ancud.ru> Date: Thu, 9 Nov 2023 21:08:59 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Nikita Kiryushin <kiryushin@ancud.ru> Subject: [PATCH] ACPI: LPIT: fix u32 multiplication overflow To: "Rafael J. Wysocki" <rafael@kernel.org> Cc: Len Brown <lenb@kernel.org>, Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>, linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, lvc-project@linuxtesting.org Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-MS-Exchange-Organization-SCL: -1 Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 09 Nov 2023 10:09:29 -0800 (PST) X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782110876519534510 X-GMAIL-MSGID: 1782110876519534510 |
Series |
ACPI: LPIT: fix u32 multiplication overflow
|
|
Commit Message
Nikita Kiryushin
Nov. 9, 2023, 6:08 p.m. UTC
In lpit_update_residency there is a possibility of overflow
in multiplication, if tsc_khz is large enough (> UINT_MAX/1000).
Change multiplication to mul_u32_u32.
Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes: eeb2d80d502a ("ACPI / LPIT: Add Low Power Idle Table (LPIT) support")
Signed-off-by: Nikita Kiryushin <kiryushin@ancud.ru>
---
drivers/acpi/acpi_lpit.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Thu, Nov 9, 2023 at 7:09 PM Nikita Kiryushin <kiryushin@ancud.ru> wrote: > > In lpit_update_residency there is a possibility of overflow > in multiplication, if tsc_khz is large enough (> UINT_MAX/1000). That would be a TSC ticking at hundreds of millions of kHz if I'm not mistaken. Why is it really a concern? > Change multiplication to mul_u32_u32. So why is this better? > Found by Linux Verification Center (linuxtesting.org) with SVACE. > > Fixes: eeb2d80d502a ("ACPI / LPIT: Add Low Power Idle Table (LPIT) support") > Signed-off-by: Nikita Kiryushin <kiryushin@ancud.ru> > --- > drivers/acpi/acpi_lpit.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/acpi/acpi_lpit.c b/drivers/acpi/acpi_lpit.c > index c5598b6d5db8..794962c5c88e 100644 > --- a/drivers/acpi/acpi_lpit.c > +++ b/drivers/acpi/acpi_lpit.c > @@ -105,7 +105,7 @@ static void lpit_update_residency(struct > lpit_residency_info *info, > return; > info->frequency = lpit_native->counter_frequency ? > - lpit_native->counter_frequency : tsc_khz * 1000; > + lpit_native->counter_frequency : mul_u32_u32(tsc_khz, 1000U); > if (!info->frequency) > info->frequency = 1; > -- 2.34.1 >
On Tue, Nov 21, 2023 at 8:56 PM Rafael J. Wysocki <rafael@kernel.org> wrote: > > On Thu, Nov 9, 2023 at 7:09 PM Nikita Kiryushin <kiryushin@ancud.ru> wrote: > > > > In lpit_update_residency there is a possibility of overflow > > in multiplication, if tsc_khz is large enough (> UINT_MAX/1000). > > That would be a TSC ticking at hundreds of millions of kHz if I'm not > mistaken. That should be "hundreds of thousands of kHz", so I was mistaken. But anyway: > Why is it really a concern? > > > Change multiplication to mul_u32_u32. > > So why is this better? > > > Found by Linux Verification Center (linuxtesting.org) with SVACE. > > > > Fixes: eeb2d80d502a ("ACPI / LPIT: Add Low Power Idle Table (LPIT) support") > > Signed-off-by: Nikita Kiryushin <kiryushin@ancud.ru> > > --- > > drivers/acpi/acpi_lpit.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/drivers/acpi/acpi_lpit.c b/drivers/acpi/acpi_lpit.c > > index c5598b6d5db8..794962c5c88e 100644 > > --- a/drivers/acpi/acpi_lpit.c > > +++ b/drivers/acpi/acpi_lpit.c > > @@ -105,7 +105,7 @@ static void lpit_update_residency(struct > > lpit_residency_info *info, > > return; > > info->frequency = lpit_native->counter_frequency ? > > - lpit_native->counter_frequency : tsc_khz * 1000; > > + lpit_native->counter_frequency : mul_u32_u32(tsc_khz, 1000U); > > if (!info->frequency) > > info->frequency = 1; > > -- 2.34.1 > >
My reasoning was around something like: 1) tsc_khz is declared as unsigned int tsc_khz; 2) tsc_khz * 1000 would overflow, if the result is larger, than an unsigned int could hold; 3) given tsc_khz * 1000 > UINT_MAX is bad, tsc_khz > UINT_MAX / 1000 is bad; 4) if UINT_MAX is 4294967295, than tsc_khz > 4294967.295 is bad, for example 4294968 would lead to overflow; 5) 4294968 kHz is 4294.968 MHz, which seems realistically high to me. For me, tsc: Refined TSC clocksource calibration: 3393.624 MHz (seems like, it is derived from the same value, pr_info("Refined TSC clocksource calibration: %lu.%03lu MHz\n", (unsigned long)tsc_khz / 1000, (unsigned long)tsc_khz % 1000); ) Not sure about the math above, but it seemed reasonable enough to me to switch to overflow-resilient arithmetic here. On 11/21/23 23:14, Rafael J. Wysocki wrote: > On Tue, Nov 21, 2023 at 8:56 PM Rafael J. Wysocki <rafael@kernel.org> wrote: > That should be "hundreds of thousands of kHz", so I was mistaken. > > But anyway: > >> Why is it really a concern? >>
On Wed, Nov 22, 2023 at 8:41 PM Nikita Kiryushin <kiryushin@ancud.ru> wrote: > > My reasoning was around something like: > > 1) tsc_khz is declared as unsigned int tsc_khz; > > 2) tsc_khz * 1000 would overflow, if the result is larger, than an > unsigned int could hold; > > 3) given tsc_khz * 1000 > UINT_MAX is bad, tsc_khz > UINT_MAX / 1000 is bad; > > 4) if UINT_MAX is 4294967295, than tsc_khz > 4294967.295 is bad, for > example 4294968 would lead to overflow; > > 5) 4294968 kHz is 4294.968 MHz, which seems realistically high to me. > > For me, tsc: Refined TSC clocksource calibration: 3393.624 MHz > > (seems like, it is derived from the same value, > > pr_info("Refined TSC clocksource calibration: %lu.%03lu MHz\n", > (unsigned long)tsc_khz / 1000, > (unsigned long)tsc_khz % 1000); > > ) OK, fair enough. > Not sure about the math above, but it seemed reasonable enough to me to > switch to overflow-resilient arithmetic here.
diff --git a/drivers/acpi/acpi_lpit.c b/drivers/acpi/acpi_lpit.c index c5598b6d5db8..794962c5c88e 100644 --- a/drivers/acpi/acpi_lpit.c +++ b/drivers/acpi/acpi_lpit.c @@ -105,7 +105,7 @@ static void lpit_update_residency(struct lpit_residency_info *info, return; info->frequency = lpit_native->counter_frequency ? - lpit_native->counter_frequency : tsc_khz * 1000; + lpit_native->counter_frequency : mul_u32_u32(tsc_khz, 1000U); if (!info->frequency) info->frequency = 1; -- 2.34.1