* [PATCH] PCI: pci-bridge-emul: fix array overruns, improve safety
@ 2021-02-02 17:07 Russell King
2021-02-03 10:25 ` Pali Rohár
2021-02-17 23:30 ` Bjorn Helgaas
0 siblings, 2 replies; 3+ messages in thread
From: Russell King @ 2021-02-02 17:07 UTC (permalink / raw)
To: Bjorn Helgaas; +Cc: Pali Roh__r, Thomas Petazzoni, Lorenzo Pieralisi, linux-pci
We allow up to PCI_EXP_SLTSTA2 registers to be accessed, but the
PCIe behaviour (pcie_cap_regs_behavior) array only covers up to
PCI_EXP_RTSTA. Expand this array to avoid walking off the end of it.
Do the same for pci_regs_behavior for consistency, and add a
BUILD_BUG_ON() to also check the bridge->conf structure size.
Fixes: 23a5fba4d941 ("PCI: Introduce PCI bridge emulated config space common logic")
Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
---
drivers/pci/pci-bridge-emul.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/drivers/pci/pci-bridge-emul.c b/drivers/pci/pci-bridge-emul.c
index 139869d50eb2..fdaf86a888b7 100644
--- a/drivers/pci/pci-bridge-emul.c
+++ b/drivers/pci/pci-bridge-emul.c
@@ -21,8 +21,9 @@
#include "pci-bridge-emul.h"
#define PCI_BRIDGE_CONF_END PCI_STD_HEADER_SIZEOF
+#define PCI_CAP_PCIE_SIZEOF (PCI_EXP_SLTSTA2 + 2)
#define PCI_CAP_PCIE_START PCI_BRIDGE_CONF_END
-#define PCI_CAP_PCIE_END (PCI_CAP_PCIE_START + PCI_EXP_SLTSTA2 + 2)
+#define PCI_CAP_PCIE_END (PCI_CAP_PCIE_START + PCI_CAP_PCIE_SIZEOF)
/**
* struct pci_bridge_reg_behavior - register bits behaviors
@@ -46,7 +47,8 @@ struct pci_bridge_reg_behavior {
u32 w1c;
};
-static const struct pci_bridge_reg_behavior pci_regs_behavior[] = {
+static const
+struct pci_bridge_reg_behavior pci_regs_behavior[PCI_STD_HEADER_SIZEOF / 4] = {
[PCI_VENDOR_ID / 4] = { .ro = ~0 },
[PCI_COMMAND / 4] = {
.rw = (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
@@ -164,7 +166,8 @@ static const struct pci_bridge_reg_behavior pci_regs_behavior[] = {
},
};
-static const struct pci_bridge_reg_behavior pcie_cap_regs_behavior[] = {
+static const
+struct pci_bridge_reg_behavior pcie_cap_regs_behavior[PCI_CAP_PCIE_SIZEOF / 4] = {
[PCI_CAP_LIST_ID / 4] = {
/*
* Capability ID, Next Capability Pointer and
@@ -260,6 +263,8 @@ static const struct pci_bridge_reg_behavior pcie_cap_regs_behavior[] = {
int pci_bridge_emul_init(struct pci_bridge_emul *bridge,
unsigned int flags)
{
+ BUILD_BUG_ON(sizeof(bridge->conf) != PCI_BRIDGE_CONF_END);
+
bridge->conf.class_revision |= cpu_to_le32(PCI_CLASS_BRIDGE_PCI << 16);
bridge->conf.header_type = PCI_HEADER_TYPE_BRIDGE;
bridge->conf.cache_line_size = 0x10;
--
2.20.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] PCI: pci-bridge-emul: fix array overruns, improve safety
2021-02-02 17:07 [PATCH] PCI: pci-bridge-emul: fix array overruns, improve safety Russell King
@ 2021-02-03 10:25 ` Pali Rohár
2021-02-17 23:30 ` Bjorn Helgaas
1 sibling, 0 replies; 3+ messages in thread
From: Pali Rohár @ 2021-02-03 10:25 UTC (permalink / raw)
To: Russell King
Cc: Bjorn Helgaas, Thomas Petazzoni, Lorenzo Pieralisi, linux-pci
On Tuesday 02 February 2021 17:07:46 Russell King wrote:
> We allow up to PCI_EXP_SLTSTA2 registers to be accessed, but the
> PCIe behaviour (pcie_cap_regs_behavior) array only covers up to
> PCI_EXP_RTSTA. Expand this array to avoid walking off the end of it.
>
> Do the same for pci_regs_behavior for consistency, and add a
> BUILD_BUG_ON() to also check the bridge->conf structure size.
>
> Fixes: 23a5fba4d941 ("PCI: Introduce PCI bridge emulated config space common logic")
> Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
Looks good!
Reviewed-by: Pali Rohár <pali@kernel.org>
Just to note that I'm planning to send a patch which adds missing
register definitions for pcie_cap_regs_behavior[].
> ---
> drivers/pci/pci-bridge-emul.c | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/pci/pci-bridge-emul.c b/drivers/pci/pci-bridge-emul.c
> index 139869d50eb2..fdaf86a888b7 100644
> --- a/drivers/pci/pci-bridge-emul.c
> +++ b/drivers/pci/pci-bridge-emul.c
> @@ -21,8 +21,9 @@
> #include "pci-bridge-emul.h"
>
> #define PCI_BRIDGE_CONF_END PCI_STD_HEADER_SIZEOF
> +#define PCI_CAP_PCIE_SIZEOF (PCI_EXP_SLTSTA2 + 2)
> #define PCI_CAP_PCIE_START PCI_BRIDGE_CONF_END
> -#define PCI_CAP_PCIE_END (PCI_CAP_PCIE_START + PCI_EXP_SLTSTA2 + 2)
> +#define PCI_CAP_PCIE_END (PCI_CAP_PCIE_START + PCI_CAP_PCIE_SIZEOF)
>
> /**
> * struct pci_bridge_reg_behavior - register bits behaviors
> @@ -46,7 +47,8 @@ struct pci_bridge_reg_behavior {
> u32 w1c;
> };
>
> -static const struct pci_bridge_reg_behavior pci_regs_behavior[] = {
> +static const
> +struct pci_bridge_reg_behavior pci_regs_behavior[PCI_STD_HEADER_SIZEOF / 4] = {
> [PCI_VENDOR_ID / 4] = { .ro = ~0 },
> [PCI_COMMAND / 4] = {
> .rw = (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
> @@ -164,7 +166,8 @@ static const struct pci_bridge_reg_behavior pci_regs_behavior[] = {
> },
> };
>
> -static const struct pci_bridge_reg_behavior pcie_cap_regs_behavior[] = {
> +static const
> +struct pci_bridge_reg_behavior pcie_cap_regs_behavior[PCI_CAP_PCIE_SIZEOF / 4] = {
> [PCI_CAP_LIST_ID / 4] = {
> /*
> * Capability ID, Next Capability Pointer and
> @@ -260,6 +263,8 @@ static const struct pci_bridge_reg_behavior pcie_cap_regs_behavior[] = {
> int pci_bridge_emul_init(struct pci_bridge_emul *bridge,
> unsigned int flags)
> {
> + BUILD_BUG_ON(sizeof(bridge->conf) != PCI_BRIDGE_CONF_END);
> +
> bridge->conf.class_revision |= cpu_to_le32(PCI_CLASS_BRIDGE_PCI << 16);
> bridge->conf.header_type = PCI_HEADER_TYPE_BRIDGE;
> bridge->conf.cache_line_size = 0x10;
> --
> 2.20.1
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] PCI: pci-bridge-emul: fix array overruns, improve safety
2021-02-02 17:07 [PATCH] PCI: pci-bridge-emul: fix array overruns, improve safety Russell King
2021-02-03 10:25 ` Pali Rohár
@ 2021-02-17 23:30 ` Bjorn Helgaas
1 sibling, 0 replies; 3+ messages in thread
From: Bjorn Helgaas @ 2021-02-17 23:30 UTC (permalink / raw)
To: Russell King
Cc: Bjorn Helgaas, Pali Roh__r, Thomas Petazzoni, Lorenzo Pieralisi,
linux-pci
On Tue, Feb 02, 2021 at 05:07:46PM +0000, Russell King wrote:
> We allow up to PCI_EXP_SLTSTA2 registers to be accessed, but the
> PCIe behaviour (pcie_cap_regs_behavior) array only covers up to
> PCI_EXP_RTSTA. Expand this array to avoid walking off the end of it.
>
> Do the same for pci_regs_behavior for consistency, and add a
> BUILD_BUG_ON() to also check the bridge->conf structure size.
>
> Fixes: 23a5fba4d941 ("PCI: Introduce PCI bridge emulated config space common logic")
> Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
Applied with Pali's Reviewed-by to pci/enumeration for v5.12, thanks!
> ---
> drivers/pci/pci-bridge-emul.c | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/pci/pci-bridge-emul.c b/drivers/pci/pci-bridge-emul.c
> index 139869d50eb2..fdaf86a888b7 100644
> --- a/drivers/pci/pci-bridge-emul.c
> +++ b/drivers/pci/pci-bridge-emul.c
> @@ -21,8 +21,9 @@
> #include "pci-bridge-emul.h"
>
> #define PCI_BRIDGE_CONF_END PCI_STD_HEADER_SIZEOF
> +#define PCI_CAP_PCIE_SIZEOF (PCI_EXP_SLTSTA2 + 2)
> #define PCI_CAP_PCIE_START PCI_BRIDGE_CONF_END
> -#define PCI_CAP_PCIE_END (PCI_CAP_PCIE_START + PCI_EXP_SLTSTA2 + 2)
> +#define PCI_CAP_PCIE_END (PCI_CAP_PCIE_START + PCI_CAP_PCIE_SIZEOF)
>
> /**
> * struct pci_bridge_reg_behavior - register bits behaviors
> @@ -46,7 +47,8 @@ struct pci_bridge_reg_behavior {
> u32 w1c;
> };
>
> -static const struct pci_bridge_reg_behavior pci_regs_behavior[] = {
> +static const
> +struct pci_bridge_reg_behavior pci_regs_behavior[PCI_STD_HEADER_SIZEOF / 4] = {
> [PCI_VENDOR_ID / 4] = { .ro = ~0 },
> [PCI_COMMAND / 4] = {
> .rw = (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
> @@ -164,7 +166,8 @@ static const struct pci_bridge_reg_behavior pci_regs_behavior[] = {
> },
> };
>
> -static const struct pci_bridge_reg_behavior pcie_cap_regs_behavior[] = {
> +static const
> +struct pci_bridge_reg_behavior pcie_cap_regs_behavior[PCI_CAP_PCIE_SIZEOF / 4] = {
> [PCI_CAP_LIST_ID / 4] = {
> /*
> * Capability ID, Next Capability Pointer and
> @@ -260,6 +263,8 @@ static const struct pci_bridge_reg_behavior pcie_cap_regs_behavior[] = {
> int pci_bridge_emul_init(struct pci_bridge_emul *bridge,
> unsigned int flags)
> {
> + BUILD_BUG_ON(sizeof(bridge->conf) != PCI_BRIDGE_CONF_END);
> +
> bridge->conf.class_revision |= cpu_to_le32(PCI_CLASS_BRIDGE_PCI << 16);
> bridge->conf.header_type = PCI_HEADER_TYPE_BRIDGE;
> bridge->conf.cache_line_size = 0x10;
> --
> 2.20.1
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-02-17 23:31 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-02 17:07 [PATCH] PCI: pci-bridge-emul: fix array overruns, improve safety Russell King
2021-02-03 10:25 ` Pali Rohár
2021-02-17 23:30 ` Bjorn Helgaas
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.