Skip to contentSkip to conversation

PCI: dwc: fix find_next_bit() usage

PATCH Applied Niklas Cassel 5 participants 5 messages Sep 4, 2019 – Oct 15, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: dwc: fix find_next_bit() usage

find_next_bit() takes a parameter of size long, and performs arithmetic that assumes that the argument is of size long.

Therefore we cannot pass a u32, since this will cause find_next_bit() to read outside the stack buffer and will produce the following print:

BUG: KASAN: stack-out-of-bounds in find_next_bit+0x38/0xb0
Fixes 1b497e6493c4 ("PCI: dwc: Fix uninitialized variable in dw_handle_msi_irq()")
---
 drivers/pci/controller/dwc/pcie-designware-host.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/pci/controller/dwc/pcie-designware-host.c +6 -5
index d3156446ff27..45f21640c977 100644
@@ -78,7 +78,8 @@ static struct msi_domain_info dw_pcie_msi_domain_info = {
 irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
 {
 	int i, pos, irq;
-	u32 val, num_ctrls;
+	unsigned long val;
+	u32 status, num_ctrls;
 	irqreturn_t ret = IRQ_NONE;
 
 	num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
@@ -86,14 +87,14 @@ irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
 	for (i = 0; i < num_ctrls; i++) {
 		dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS +
 					(i * MSI_REG_CTRL_BLOCK_SIZE),
-				    4, &val);
-		if (!val)
+				    4, &status);
+		if (!status)
 			continue;
 
 		ret = IRQ_HANDLED;
+		val = status;
 		pos = 0;
-		while ((pos = find_next_bit((unsigned long *) &val,
-					    MAX_MSI_IRQS_PER_CTRL,
+		while ((pos = find_next_bit(&val, MAX_MSI_IRQS_PER_CTRL,
 					    pos)) != MAX_MSI_IRQS_PER_CTRL) {
 			irq = irq_find_mapping(pp->irq_domain,
 					       (i * MAX_MSI_IRQS_PER_CTRL) +
Signature · 1 line
2.21.0
Acked-by on v1 1/1
#1 lore raw
Quoted 48 lines · Niklas Cassel

find_next_bit() takes a parameter of size long, and performs arithmetic that assumes that the argument is of size long.

Therefore we cannot pass a u32, since this will cause find_next_bit() to read outside the stack buffer and will produce the following print:

BUG: KASAN: stack-out-of-bounds in find_next_bit+0x38/0xb0
Fixes 1b497e6493c4 ("PCI: dwc: Fix uninitialized variable in dw_handle_msi_irq()")
---
 drivers/pci/controller/dwc/pcie-designware-host.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/pci/controller/dwc/pcie-designware-host.c +6 -5
index d3156446ff27..45f21640c977 100644
@@ -78,7 +78,8 @@ static struct msi_domain_info dw_pcie_msi_domain_info = {
 irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
 {
 	int i, pos, irq;
-	u32 val, num_ctrls;
+	unsigned long val;
+	u32 status, num_ctrls;
 	irqreturn_t ret = IRQ_NONE;
 
 	num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
@@ -86,14 +87,14 @@ irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
 	for (i = 0; i < num_ctrls; i++) {
 		dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS +
 					(i * MSI_REG_CTRL_BLOCK_SIZE),
-				    4, &val);
-		if (!val)
+				    4, &status);
+		if (!status)
 			continue;
 
 		ret = IRQ_HANDLED;
+		val = status;
 		pos = 0;
-		while ((pos = find_next_bit((unsigned long *) &val,
-					    MAX_MSI_IRQS_PER_CTRL,
+		while ((pos = find_next_bit(&val, MAX_MSI_IRQS_PER_CTRL,
 					    pos)) != MAX_MSI_IRQS_PER_CTRL) {
 			irq = irq_find_mapping(pp->irq_domain,
 					       (i * MAX_MSI_IRQS_PER_CTRL) +
Signature · 1 line
2.21.0

Hi Niklas!

The patch looks nice! Thanks!

Reviewed-by on v1 1/1
#2 lore raw
In reply to Niklas Cassel
Quoted 10 lines · Niklas Cassel

find_next_bit() takes a parameter of size long, and performs arithmetic that assumes that the argument is of size long.

Therefore we cannot pass a u32, since this will cause find_next_bit() to read outside the stack buffer and will produce the following print:

BUG: KASAN: stack-out-of-bounds in find_next_bit+0x38/0xb0
Fixes 1b497e6493c4 ("PCI: dwc: Fix uninitialized variable in dw_handle_msi_irq()")

---

Quoted 39 lines
 drivers/pci/controller/dwc/pcie-designware-host.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/pci/controller/dwc/pcie-designware-host.c +6 -5
index d3156446ff27..45f21640c977 100644
@@ -78,7 +78,8 @@ static struct msi_domain_info dw_pcie_msi_domain_info = {
 irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
 {
 	int i, pos, irq;
-	u32 val, num_ctrls;
+	unsigned long val;
+	u32 status, num_ctrls;
 	irqreturn_t ret = IRQ_NONE;
 
 	num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
@@ -86,14 +87,14 @@ irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
 	for (i = 0; i < num_ctrls; i++) {
 		dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS +
 					(i * MSI_REG_CTRL_BLOCK_SIZE),
-				    4, &val);
-		if (!val)
+				    4, &status);
+		if (!status)
 			continue;
 
 		ret = IRQ_HANDLED;
+		val = status;
 		pos = 0;
-		while ((pos = find_next_bit((unsigned long *) &val,
-					    MAX_MSI_IRQS_PER_CTRL,
+		while ((pos = find_next_bit(&val, MAX_MSI_IRQS_PER_CTRL,
 					    pos)) != MAX_MSI_IRQS_PER_CTRL) {
 			irq = irq_find_mapping(pp->irq_domain,
 					       (i * MAX_MSI_IRQS_PER_CTRL) +
Signature · 1 line
2.21.0
Tested-by on v1 1/1
#3 lore raw
In reply to Niklas Cassel
Quoted 10 lines · Niklas Cassel

find_next_bit() takes a parameter of size long, and performs arithmetic that assumes that the argument is of size long.

Therefore we cannot pass a u32, since this will cause find_next_bit() to read outside the stack buffer and will produce the following print:

BUG: KASAN: stack-out-of-bounds in find_next_bit+0x38/0xb0
Fixes 1b497e6493c4 ("PCI: dwc: Fix uninitialized variable in dw_handle_msi_irq()")
Quoted 40 lines
---
 drivers/pci/controller/dwc/pcie-designware-host.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/pci/controller/dwc/pcie-designware-host.c +6 -6
index d3156446ff27..45f21640c977 100644
@@ -78,7 +78,8 @@ static struct msi_domain_info dw_pcie_msi_domain_info = {
 irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
 {
        int i, pos, irq;
-       u32 val, num_ctrls;
+       unsigned long val;
+       u32 status, num_ctrls;
        irqreturn_t ret = IRQ_NONE;
 
        num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
@@ -86,14 +87,14 @@ irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
        for (i = 0; i < num_ctrls; i++) {
                dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS +
                                        (i * MSI_REG_CTRL_BLOCK_SIZE),
-                                   4, &val);
-               if (!val)
+                                   4, &status);
+               if (!status)
                        continue;
 
                ret = IRQ_HANDLED;
+               val = status;
                pos = 0;
-               while ((pos = find_next_bit((unsigned long *) &val,
-                                           MAX_MSI_IRQS_PER_CTRL,
+               while ((pos = find_next_bit(&val, MAX_MSI_IRQS_PER_CTRL,
                                            pos)) != MAX_MSI_IRQS_PER_CTRL) {
                        irq = irq_find_mapping(pp->irq_domain,
                                               (i * MAX_MSI_IRQS_PER_CTRL) +
--

2.21.0

#4 lore raw
In reply to Niklas Cassel
Quoted 5 lines · Niklas Cassel

find_next_bit() takes a parameter of size long, and performs arithmetic that assumes that the argument is of size long.

Therefore we cannot pass a u32, since this will cause find_next_bit() to read outside the stack buffer and will produce the following print:

Quoted 7 lines
BUG: KASAN: stack-out-of-bounds in find_next_bit+0x38/0xb0
Fixes 1b497e6493c4 ("PCI: dwc: Fix uninitialized variable in dw_handle_msi_irq()")
---
 drivers/pci/controller/dwc/pcie-designware-host.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Applied to pci/dwc, thanks.

Lorenzo

Quoted 36 lines
drivers/pci/controller/dwc/pcie-designware-host.c +6 -5
index d3156446ff27..45f21640c977 100644
@@ -78,7 +78,8 @@ static struct msi_domain_info dw_pcie_msi_domain_info = {
 irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
 {
 	int i, pos, irq;
-	u32 val, num_ctrls;
+	unsigned long val;
+	u32 status, num_ctrls;
 	irqreturn_t ret = IRQ_NONE;
 
 	num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
@@ -86,14 +87,14 @@ irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
 	for (i = 0; i < num_ctrls; i++) {
 		dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS +
 					(i * MSI_REG_CTRL_BLOCK_SIZE),
-				    4, &val);
-		if (!val)
+				    4, &status);
+		if (!status)
 			continue;
 
 		ret = IRQ_HANDLED;
+		val = status;
 		pos = 0;
-		while ((pos = find_next_bit((unsigned long *) &val,
-					    MAX_MSI_IRQS_PER_CTRL,
+		while ((pos = find_next_bit(&val, MAX_MSI_IRQS_PER_CTRL,
 					    pos)) != MAX_MSI_IRQS_PER_CTRL) {
 			irq = irq_find_mapping(pp->irq_domain,
 					       (i * MAX_MSI_IRQS_PER_CTRL) +
Signature · 1 line
2.21.0