linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] powercap: intel_rapl: fix UBSAN shift-out-of-bounds issue
@ 2022-09-20  6:08 chao.qin
  2022-09-21 18:17 ` Rafael J. Wysocki
  0 siblings, 1 reply; 2+ messages in thread
From: chao.qin @ 2022-09-20  6:08 UTC (permalink / raw)
  To: rafael, linux-pm, linux-kernel; +Cc: paul.mei, lili.li, chao.qin

From: Chao Qin <chao.qin@intel.com>

When value < time_unit, the parameter of ilog2() will be zero and
the return value is -1. u64(-1) is too large for shift exponent
and then will trigger shift-out-of-bounds:

shift exponent 18446744073709551615 is too large for 32-bit type 'int'
Call Trace:
 rapl_compute_time_window_core
 rapl_write_data_raw
 set_time_window
 store_constraint_time_window_us

Signed-off-by: Chao Qin <chao.qin@intel.com>
Acked-by: Zhang Rui <rui.zhang@intel.com>
---
 drivers/powercap/intel_rapl_common.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/powercap/intel_rapl_common.c b/drivers/powercap/intel_rapl_common.c
index 21d624f9f5fb..bf91248630c3 100644
--- a/drivers/powercap/intel_rapl_common.c
+++ b/drivers/powercap/intel_rapl_common.c
@@ -994,6 +994,9 @@ static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
 		y = value & 0x1f;
 		value = (1 << y) * (4 + f) * rp->time_unit / 4;
 	} else {
+		if (value < rp->time_unit)
+			return 0;
+
 		do_div(value, rp->time_unit);
 		y = ilog2(value);
 		f = div64_u64(4 * (value - (1 << y)), 1 << y);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] powercap: intel_rapl: fix UBSAN shift-out-of-bounds issue
  2022-09-20  6:08 [PATCH] powercap: intel_rapl: fix UBSAN shift-out-of-bounds issue chao.qin
@ 2022-09-21 18:17 ` Rafael J. Wysocki
  0 siblings, 0 replies; 2+ messages in thread
From: Rafael J. Wysocki @ 2022-09-21 18:17 UTC (permalink / raw)
  To: chao.qin
  Cc: Rafael J. Wysocki, Linux PM, Linux Kernel Mailing List, paul.mei,
	lili.li

On Tue, Sep 20, 2022 at 8:19 AM <chao.qin@intel.com> wrote:
>
> From: Chao Qin <chao.qin@intel.com>
>
> When value < time_unit, the parameter of ilog2() will be zero and
> the return value is -1. u64(-1) is too large for shift exponent
> and then will trigger shift-out-of-bounds:
>
> shift exponent 18446744073709551615 is too large for 32-bit type 'int'
> Call Trace:
>  rapl_compute_time_window_core
>  rapl_write_data_raw
>  set_time_window
>  store_constraint_time_window_us
>
> Signed-off-by: Chao Qin <chao.qin@intel.com>
> Acked-by: Zhang Rui <rui.zhang@intel.com>
> ---
>  drivers/powercap/intel_rapl_common.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/drivers/powercap/intel_rapl_common.c b/drivers/powercap/intel_rapl_common.c
> index 21d624f9f5fb..bf91248630c3 100644
> --- a/drivers/powercap/intel_rapl_common.c
> +++ b/drivers/powercap/intel_rapl_common.c
> @@ -994,6 +994,9 @@ static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
>                 y = value & 0x1f;
>                 value = (1 << y) * (4 + f) * rp->time_unit / 4;
>         } else {
> +               if (value < rp->time_unit)
> +                       return 0;
> +
>                 do_div(value, rp->time_unit);
>                 y = ilog2(value);
>                 f = div64_u64(4 * (value - (1 << y)), 1 << y);
> --

Applied as 6.1 material, thanks!

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-09-21 18:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-20  6:08 [PATCH] powercap: intel_rapl: fix UBSAN shift-out-of-bounds issue chao.qin
2022-09-21 18:17 ` Rafael J. Wysocki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).