cpufreq: intel_pstate: fix possible integer overflow
Subsystems (2)
PATCH 1/1 cpufreq: intel_pstate: fix possible integer overflow
The expression 'pstate << 8' is evaluated using 32-bit arithmetic while 'val' expects an expression of type u64.
--- drivers/cpufreq/intel_pstate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpufreq/intel_pstate.c +1 -1
@@ -395,7 +395,7 @@ static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)trace_cpu_frequency (pstate *100000 , cpu->cpu); cpu->pstate.current_pstate = pstate; - val = pstate <<8 ; + val = (u64 )pstate <<8 ;if (limits.no_turbo) val |= (u64 )1 <<32 ;
Signature · 1 line
1.8.4
Quoted 4 lines · Geyslan G. Bem
The expression 'pstate << 8' is evaluated using 32-bit arithmetic while 'val' expects an expression of type u64.
Signed-off-by Geyslan G. Bem <[email protected]>
Quoted 18 lines
--- drivers/cpufreq/intel_pstate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/cpufreq/intel_pstate.c +1
-1@@ -395,7 +395,7 @@ static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)trace_cpu_frequency (pstate *100000 , cpu->cpu); cpu->pstate.current_pstate = pstate; - val = pstate <<8 ; + val = (u64 )pstate <<8 ;if (limits.no_turbo) val |= (u64 )1 <<32 ;
Quoted 6 lines · Dirk Brandewie
Quoted 4 lines · Geyslan G. Bem
The expression 'pstate << 8' is evaluated using 32-bit arithmetic while 'val' expects an expression of type u64.
Signed-off-by Geyslan G. Bem <[email protected]>Acked-by Dirk Brandewie <[email protected]>
Actually, isn't (pstate << 8) guaranteed not to overflow?
Quoted 19 lines
Quoted 18 lines
--- drivers/cpufreq/intel_pstate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/cpufreq/intel_pstate.c +1
-1@@ -395,7 +395,7 @@ static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)trace_cpu_frequency (pstate *100000 , cpu->cpu); cpu->pstate.current_pstate = pstate; - val = pstate <<8 ; + val = (u64 )pstate <<8 ;if (limits.no_turbo) val |= (u64 )1 <<32 ;
Signature · 2 lines
I speak only for myself. Rafael J. Wysocki, Intel Open Source Technology Center.
Quoted 10 lines · Rafael J. Wysocki
Quoted 6 lines · Dirk Brandewie
Quoted 4 lines · Geyslan G. Bem
The expression 'pstate << 8' is evaluated using 32-bit arithmetic while 'val' expects an expression of type u64.
Signed-off-by Geyslan G. Bem <[email protected]>Acked-by Dirk Brandewie <[email protected]>Actually, isn't (pstate << 8) guaranteed not to overflow?
Yes, I was assuming this was caught by a static checking tool. I didn't see a downside to giving the compilier complete information.
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
--- drivers/cpufreq/intel_pstate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/cpufreq/intel_pstate.c +1
-1@@ -395,7 +395,7 @@ static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)trace_cpu_frequency (pstate *100000 , cpu->cpu); cpu->pstate.current_pstate = pstate; - val = pstate <<8 ; + val = (u64 )pstate <<8 ;if (limits.no_turbo) val |= (u64 )1 <<32 ;
Quoted 6 lines · Dirk Brandewie
Quoted 5 lines · Rafael J. Wysocki
Quoted 4 lines · Dirk Brandewie
Quoted 3 lines · Geyslan G. Bem
The expression 'pstate << 8' is evaluated using 32-bit arithmetic while 'val' expects an expression of type u64.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
Signed-off-by Geyslan G. Bem <[email protected]>Acked-by Dirk Brandewie <[email protected]>Actually, isn't (pstate << 8) guaranteed not to overflow?
Yes, I was assuming this was caught by a static checking tool.
What was caught by the tool was the fact that 1UL << 32 might overflow on 32-bit, so using BIT(32) wasn't correct.
Quoted 1 line
I didn't see a downside to giving the compilier complete information.
Well, in that case the function's argument should be u64 rather than int.
Either you know that it won't overflow, in which case the explicit type casting doesn't change anything, or you are not sure, in which case it's better to use u64 as the original type anyway in my opinion.
Thanks!
Signature · 2 lines
I speak only for myself. Rafael J. Wysocki, Intel Open Source Technology Center.
A message that is not in the archive (sent off-list or not yet received): CANOOhLRCC360exKKePsJGxYqx3ULZf1Ac0y16hsnPQNP69OHwA@mail.gmail.com
2013/10/21 Dirk Brandewie <[email protected]>:
Quoted 12 lines · Rafael J. Wysocki
Quoted 9 lines · Dirk Brandewie
Quoted 7 lines · Rafael J. Wysocki
Quoted 6 lines · Dirk Brandewie
Quoted 5 lines · Geyslan G. Bem
Quoted 4 lines
The expression 'pstate << 8' is evaluated using 32-bit arithmetic while
'val' expects an expression of type u64.
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
Signed-off-by Geyslan G. Bem <[email protected]>Acked-by Dirk Brandewie <[email protected]>Actually, isn't (pstate << 8) guaranteed not to overflow?
Yes, I was assuming this was caught by a static checking tool.
Yes, it was caught by Coverity, and I didn't debug the possibles pstate's.
Quoted 2 lines
Quoted 2 lines
What was caught by the tool was the fact that 1UL << 32 might overflow on 32-bit, so using BIT(32) wasn't correct.
This is the entire output:
CID 1108110 (#1 of 1): Unintentional integer overflow (OVERFLOW_BEFORE_WIDEN)overflow_before_widen: Potentially overflowing expression "pstate << 8" with type "int" (32 bits, signed) is evaluated using 32-bit arithmetic before being used in a context which expects an expression of type "u64" (64 bits, unsigned). To avoid overflow, cast the left operand to "u64" before performing the left shift.
Quoted 11 lines
Quoted 8 lines
Quoted 2 lines
I didn't see a downside to giving the compilier complete information.
Well, in that case the function's argument should be u64 rather than int.
Either you know that it won't overflow, in which case the explicit type casting doesn't change anything, or you are not sure, in which case it's better to use u64 as the original type anyway in my opinion.
pstate << 8 can't overflow we can drop this
I realized that are five calls to intel_pstate_set_pstate()
/drivers/cpufreq/intel_pstate.c:410
/drivers/cpufreq/intel_pstate.c:417
/drivers/cpufreq/intel_pstate.c:432
/drivers/cpufreq/intel_pstate.c:514
/drivers/cpufreq/intel_pstate.c:566
I really don't know if the values that pstate assumes could cause overflow. And I really don't know if these values may change in the future. So I have assumed that the most careful is to cast, making the code error proof.
But you know the code, thus know what is better. ;)
Cheers.
Quoted 7 lines
Quoted 7 lines
Thanks!
--
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.