Skip to contentSkip to conversation

PCI: Fix unmasked value in pci_generic_config_write32()

PATCH In review Mohamad Raizudeen 2 participants 5 messages Jul 27 – Sep 16 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: Fix unmasked value in pci_generic_config_write32()

When writing less than 4 bytes, pci_generic_config_write32() uses a read-modify-write sequence to protect adjacent bytes. The code comment states the goal is to "merge in the <size> bits we intend to write".

However the code does not mask the incoming 'val' before shifting it. Since the function 'pci_generic_config_write32()' accepts a 'u32', a caller could pass a value with extra bits set outside the intended write size, those extra bits will shift into the adjacent bytes and corrupt them.

Fix this my masking 'val' before the shift so only the target bytes are written. This makes the code do what the comment says and matches the read function, pci_generic_config_read32().

Fixes 1f94a94f67e10 ("PCI: Add generic config accessors")
---
 drivers/pci/access.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/access.c +1 -1
index b123da16b63b..47b38d01694c 100644
@@ -176,7 +176,7 @@ int pci_generic_config_write32(struct pci_bus *bus, unsigned int devfn,
 
 	mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
 	tmp = readl(addr) & mask;
-	tmp |= val << ((where & 0x3) * 8);
+	tmp |= (val & ((1 << (size * 8)) - 1)) << ((where & 0x3) * 8);
 	writel(tmp, addr);
 
 	return PCIBIOS_SUCCESSFUL;
Signature · 1 line
2.53.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

When writing less than 4 bytes, pci_generic_config_write32() uses a read-modify-write sequence to protect adjacent bytes. The code comment states the goal is to "merge in the <size> bits we intend to write".

However the code does not mask the incoming 'val' before shifting it. Since the function 'pci_generic_config_write32()' accepts a 'u32', a caller could pass a value with extra bits set outside the intended write size, those extra bits will shift into the adjacent bytes and corrupt them.

Quoted 6 lines

Fix this my masking 'val' before the shift so only the target bytes are written. This makes the code do what the comment says and matches the read function, pci_generic_config_read32().

Fixes 1f94a94f67e10 ("PCI: Add generic config accessors")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Mohamad Raizudeen
Quoted 35 lines · Mohamad Raizudeen

When writing less than 4 bytes, pci_generic_config_write32() uses a read-modify-write sequence to protect adjacent bytes. The code comment states the goal is to "merge in the <size> bits we intend to write".

However the code does not mask the incoming 'val' before shifting it. Since the function 'pci_generic_config_write32()' accepts a 'u32', a caller could pass a value with extra bits set outside the intended write size, those extra bits will shift into the adjacent bytes and corrupt them.

Fix this my masking 'val' before the shift so only the target bytes are written. This makes the code do what the comment says and matches the read function, pci_generic_config_read32().

Fixes 1f94a94f67e10 ("PCI: Add generic config accessors")
---
 drivers/pci/access.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/access.c +1 -1
index b123da16b63b..47b38d01694c 100644
@@ -176,7 +176,7 @@ int pci_generic_config_write32(struct pci_bus *bus, unsigned int devfn,
 
 	mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
 	tmp = readl(addr) & mask;
-	tmp |= val << ((where & 0x3) * 8);
+	tmp |= (val & ((1 << (size * 8)) - 1)) << ((where & 0x3) * 8);
 	writel(tmp, addr);
 
 	return PCIBIOS_SUCCESSFUL;
Signature · 1 line
2.53.0

Hi Bjorn,

Just following up on this patch. Please let me know if you have any feedback or if further changes are needed.

Thanks & regards,
Mohamad Raizudeen

#3 lore raw
In reply to Mohamad Raizudeen
Quoted 35 lines · Mohamad Raizudeen

When writing less than 4 bytes, pci_generic_config_write32() uses a read-modify-write sequence to protect adjacent bytes. The code comment states the goal is to "merge in the <size> bits we intend to write".

However the code does not mask the incoming 'val' before shifting it. Since the function 'pci_generic_config_write32()' accepts a 'u32', a caller could pass a value with extra bits set outside the intended write size, those extra bits will shift into the adjacent bytes and corrupt them.

Fix this my masking 'val' before the shift so only the target bytes are written. This makes the code do what the comment says and matches the read function, pci_generic_config_read32().

Fixes 1f94a94f67e10 ("PCI: Add generic config accessors")
---
 drivers/pci/access.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/access.c +1 -1
index b123da16b63b..47b38d01694c 100644
@@ -176,7 +176,7 @@ int pci_generic_config_write32(struct pci_bus *bus, unsigned int devfn,
 
 	mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
 	tmp = readl(addr) & mask;
-	tmp |= val << ((where & 0x3) * 8);
+	tmp |= (val & ((1 << (size * 8)) - 1)) << ((where & 0x3) * 8);
 	writel(tmp, addr);
 
 	return PCIBIOS_SUCCESSFUL;
Signature · 1 line
2.53.0

Hi Bjorn,
Gentle ping on this patch. Is there any further changes needed?

Thanks
Mohamad Raizudeen

#4 lore raw
In reply to Mohamad Raizudeen
Quoted 35 lines · Mohamad Raizudeen

When writing less than 4 bytes, pci_generic_config_write32() uses a read-modify-write sequence to protect adjacent bytes. The code comment states the goal is to "merge in the <size> bits we intend to write".

However the code does not mask the incoming 'val' before shifting it. Since the function 'pci_generic_config_write32()' accepts a 'u32', a caller could pass a value with extra bits set outside the intended write size, those extra bits will shift into the adjacent bytes and corrupt them.

Fix this my masking 'val' before the shift so only the target bytes are written. This makes the code do what the comment says and matches the read function, pci_generic_config_read32().

Fixes 1f94a94f67e10 ("PCI: Add generic config accessors")
---
 drivers/pci/access.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/access.c +1 -1
index b123da16b63b..47b38d01694c 100644
@@ -176,7 +176,7 @@ int pci_generic_config_write32(struct pci_bus *bus, unsigned int devfn,
 
 	mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
 	tmp = readl(addr) & mask;
-	tmp |= val << ((where & 0x3) * 8);
+	tmp |= (val & ((1 << (size * 8)) - 1)) << ((where & 0x3) * 8);
 	writel(tmp, addr);
 
 	return PCIBIOS_SUCCESSFUL;
Signature · 1 line
2.53.0

Hi Bjorn,

Just following up on this patch. Please let me know if you have any feedback or if further changes are needed.

Thanks,
Mohamad Raizudeen