diff mbox series

x86/PCI: Fix Broadcom CNB20LE unintended sign extension (redux)

Message ID 20181025135231.27817-1-colin.king@canonical.com (mailing list archive)
State New, archived
Delegated to: Bjorn Helgaas
Headers show
Series x86/PCI: Fix Broadcom CNB20LE unintended sign extension (redux) | expand

Commit Message

Colin King Oct. 25, 2018, 1:52 p.m. UTC
From: Colin Ian King <colin.king@canonical.com>

In the expression "word1 << 16", word1 starts as u16, but is promoted to
a signed int, then sign-extended to resource_size_t, which is probably
not what was intended.  Cast to resource_size_t to avoid the sign
extension.

This fixes an identical issue as fixed by commit 0b2d70764bb3
("x86/PCI: Fix Broadcom CNB20LE unintended sign extension") back in 2014.

Detected by CoverityScan, CID#138749, 138750 ("Unintended sign extension")

Fixes: 3f6ea84a3035 ("PCI: read memory ranges out of Broadcom CNB20LE host bridge")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 arch/x86/pci/broadcom_bus.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Bjorn Helgaas Nov. 9, 2018, 12:02 a.m. UTC | #1
On Thu, Oct 25, 2018 at 02:52:31PM +0100, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> In the expression "word1 << 16", word1 starts as u16, but is promoted to
> a signed int, then sign-extended to resource_size_t, which is probably
> not what was intended.  Cast to resource_size_t to avoid the sign
> extension.
> 
> This fixes an identical issue as fixed by commit 0b2d70764bb3
> ("x86/PCI: Fix Broadcom CNB20LE unintended sign extension") back in 2014.
> 
> Detected by CoverityScan, CID#138749, 138750 ("Unintended sign extension")
> 
> Fixes: 3f6ea84a3035 ("PCI: read memory ranges out of Broadcom CNB20LE host bridge")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>

How lame that I fixed one but not both with 0b2d70764bb3, sorry about
that!

Applied to pci/enumeration for v4.21, thanks!

> ---
>  arch/x86/pci/broadcom_bus.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/pci/broadcom_bus.c b/arch/x86/pci/broadcom_bus.c
> index 526536c81ddc..d09c401a300d 100644
> --- a/arch/x86/pci/broadcom_bus.c
> +++ b/arch/x86/pci/broadcom_bus.c
> @@ -50,8 +50,8 @@ static void __init cnb20le_res(u8 bus, u8 slot, u8 func)
>  	word1 = read_pci_config_16(bus, slot, func, 0xc0);
>  	word2 = read_pci_config_16(bus, slot, func, 0xc2);
>  	if (word1 != word2) {
> -		res.start = (word1 << 16) | 0x0000;
> -		res.end   = (word2 << 16) | 0xffff;
> +		res.start = ((resource_size_t) word1 << 16) | 0x0000;
> +		res.end   = ((resource_size_t) word2 << 16) | 0xffff;
>  		res.flags = IORESOURCE_MEM;
>  		update_res(info, res.start, res.end, res.flags, 0);
>  	}
> -- 
> 2.19.1
>
diff mbox series

Patch

diff --git a/arch/x86/pci/broadcom_bus.c b/arch/x86/pci/broadcom_bus.c
index 526536c81ddc..d09c401a300d 100644
--- a/arch/x86/pci/broadcom_bus.c
+++ b/arch/x86/pci/broadcom_bus.c
@@ -50,8 +50,8 @@  static void __init cnb20le_res(u8 bus, u8 slot, u8 func)
 	word1 = read_pci_config_16(bus, slot, func, 0xc0);
 	word2 = read_pci_config_16(bus, slot, func, 0xc2);
 	if (word1 != word2) {
-		res.start = (word1 << 16) | 0x0000;
-		res.end   = (word2 << 16) | 0xffff;
+		res.start = ((resource_size_t) word1 << 16) | 0x0000;
+		res.end   = ((resource_size_t) word2 << 16) | 0xffff;
 		res.flags = IORESOURCE_MEM;
 		update_res(info, res.start, res.end, res.flags, 0);
 	}