Skip to contentSkip to conversation

perf/x86/intel/uncore: fix integer overflow on shift of a u32 integer

PATCH In review Colin King 2 participants 3 messages Oct 2, 2019

Subsystems (3)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 perf/x86/intel/uncore: fix integer overflow on shift of a u32 integer

From: Colin Ian King <[email protected]>

Shifting the u32 integer result of (pci_dword & SNR_IMC_MMIO_BASE_MASK) will end up with an overflow when pci_dword greater than 0x1ff. Fix this by casting pci_dword to a resource_size_t before masking and shifting it.

Addresses-Coverity: ("Unintentional integer overflow")

Fixes ee49532b38dd ("perf/x86/intel/uncore: Add IMC uncore support for Snow Ridge")
---
 arch/x86/events/intel/uncore_snbep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/uncore_snbep.c +1 -1
index b10a5ec79e48..ed69df1340d9 100644
@@ -4415,7 +4415,7 @@ static void snr_uncore_mmio_init_box(struct intel_uncore_box *box)
 		return;
 
 	pci_read_config_dword(pdev, SNR_IMC_MMIO_BASE_OFFSET, &pci_dword);
-	addr = (pci_dword & SNR_IMC_MMIO_BASE_MASK) << 23;
+	addr = ((resource_size_t)pci_dword & SNR_IMC_MMIO_BASE_MASK) << 23;
 
 	pci_read_config_dword(pdev, SNR_IMC_MMIO_MEM0_OFFSET, &pci_dword);
 	addr |= (pci_dword & SNR_IMC_MMIO_MEM0_MASK) << 12;
Signature · 1 line
2.20.1
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Colin King

From: Colin Ian King <[email protected]>

Shifting the u32 integer result of (pci_dword & SNR_IMC_MMIO_BASE_MASK) will end up with an overflow when pci_dword greater than 0x1ff. Fix this by casting pci_dword to a resource_size_t before masking and shifting it.

Addresses-Coverity: ("Unintentional integer overflow")

I don't see that tag in Documentation/process/submitting-patches.rst ;)

IIUC this is unintented truncation of the upper bits due to missing type promotion before the shift, rather than overflow (i.e. the value wrapping across addition/subtraction), so I think the wording is slightly misleading.

Does coverity call that integer overflow?

It might be better to say:

| [PATCH] perf/x86/intel/uncore: don't truncate upper bits of address
|
| Shifting the u32 integer result of (pci_dword & SNR_IMC_MMIO_BASE_MASK)
| by 23 will throw away the upper 23 bits of the potentially 64-bit
| address. Fix this by casting pci_dword to a resource_size_t before
| masking and shifting it.
|
| Found by coverity ("Unintentional integer overflow").

Otherwise, the patch looks fine to me:

Thanks,
Mark.

Quoted 22 lines
Fixes ee49532b38dd ("perf/x86/intel/uncore: Add IMC uncore support for Snow Ridge")
---
 arch/x86/events/intel/uncore_snbep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/uncore_snbep.c +1 -1
index b10a5ec79e48..ed69df1340d9 100644
@@ -4415,7 +4415,7 @@ static void snr_uncore_mmio_init_box(struct intel_uncore_box *box)
 		return;
 
 	pci_read_config_dword(pdev, SNR_IMC_MMIO_BASE_OFFSET, &pci_dword);
-	addr = (pci_dword & SNR_IMC_MMIO_BASE_MASK) << 23;
+	addr = ((resource_size_t)pci_dword & SNR_IMC_MMIO_BASE_MASK) << 23;
 
 	pci_read_config_dword(pdev, SNR_IMC_MMIO_MEM0_OFFSET, &pci_dword);
 	addr |= (pci_dword & SNR_IMC_MMIO_MEM0_MASK) << 12;
Signature · 1 line
2.20.1
#2 lore raw
Quoted 9 lines · Mark Rutland
Quoted 7 lines · Colin King

From: Colin Ian King <[email protected]>

Shifting the u32 integer result of (pci_dword & SNR_IMC_MMIO_BASE_MASK) will end up with an overflow when pci_dword greater than 0x1ff. Fix this by casting pci_dword to a resource_size_t before masking and shifting it.

Addresses-Coverity: ("Unintentional integer overflow")

Quoted 8 lines

I don't see that tag in Documentation/process/submitting-patches.rst ;)

IIUC this is unintented truncation of the upper bits due to missing type promotion before the shift, rather than overflow (i.e. the value wrapping across addition/subtraction), so I think the wording is slightly misleading.

Does coverity call that integer overflow?

Coverity calls it "Unintentional integer overflow". I suspect Coverity first spots the overflow before the truncation occurs and so flags that up as the root cause.

Quoted 41 lines

It might be better to say:

| [PATCH] perf/x86/intel/uncore: don't truncate upper bits of address
|
| Shifting the u32 integer result of (pci_dword & SNR_IMC_MMIO_BASE_MASK)
| by 23 will throw away the upper 23 bits of the potentially 64-bit
| address. Fix this by casting pci_dword to a resource_size_t before
| masking and shifting it.
|
| Found by coverity ("Unintentional integer overflow").

Otherwise, the patch looks fine to me:

Thanks,
Mark.

Quoted 22 lines
Fixes ee49532b38dd ("perf/x86/intel/uncore: Add IMC uncore support for Snow Ridge")
---
 arch/x86/events/intel/uncore_snbep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/uncore_snbep.c +1 -1
index b10a5ec79e48..ed69df1340d9 100644
@@ -4415,7 +4415,7 @@ static void snr_uncore_mmio_init_box(struct intel_uncore_box *box)
 		return;
 
 	pci_read_config_dword(pdev, SNR_IMC_MMIO_BASE_OFFSET, &pci_dword);
-	addr = (pci_dword & SNR_IMC_MMIO_BASE_MASK) << 23;
+	addr = ((resource_size_t)pci_dword & SNR_IMC_MMIO_BASE_MASK) << 23;
 
 	pci_read_config_dword(pdev, SNR_IMC_MMIO_MEM0_OFFSET, &pci_dword);
 	addr |= (pci_dword & SNR_IMC_MMIO_MEM0_MASK) << 12;
Signature · 1 line
2.20.1