Skip to contentSkip to conversation

PCI: endpoint: Fix NULL pointer dereference for ->get_features()

PATCH Superseded Shradha Todi 2 participants 3 messages Dec 18, 2020 – Dec 24, 2020 1 message not in the archive

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20201218154704epcas5p1446559adbd4a5b267a4c940b5f744970@epcas5p1.samsung.com

#1 lore raw

PATCH v2 1/1 PCI: endpoint: Fix NULL pointer dereference for ->get_features()

get_features ops of pci_epc_ops may return NULL, causing NULL pointer dereference in pci_epf_test_bind function. Let us add a check for pci_epc_feature pointer in pci_epf_test_bind before we access it to avoid any such NULL pointer dereference and return -ENOTSUPP in case pci_epc_feature is not found.

---
v2:
rebase on v1
v1: https://lore.kernel.org/patchwork/patch/1208269/

 drivers/pci/endpoint/functions/pci-epf-test.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/pci/endpoint/functions/pci-epf-test.c +8 -5
index 66723d5..f1842e6 100644
@@ -835,13 +835,16 @@ static int pci_epf_test_bind(struct pci_epf *epf)
 		return -EINVAL;
 
 	epc_features = pci_epc_get_features(epc, epf->func_no);
-	if (epc_features) {
-		linkup_notifier = epc_features->linkup_notifier;
-		core_init_notifier = epc_features->core_init_notifier;
-		test_reg_bar = pci_epc_get_first_free_bar(epc_features);
-		pci_epf_configure_bar(epf, epc_features);
+	if (!epc_features) {
+		dev_err(&epf->dev, "epc_features not implemented\n");
+		return -EOPNOTSUPP;
 	}
 
+	linkup_notifier = epc_features->linkup_notifier;
+	core_init_notifier = epc_features->core_init_notifier;
+	test_reg_bar = pci_epc_get_first_free_bar(epc_features);
+	pci_epf_configure_bar(epf, epc_features);
+
 	epf_test->test_reg_bar = test_reg_bar;
 	epf_test->epc_features = epc_features;
 
Signature · 1 line
2.7.4
#2 lore raw
Quoted 5 lines · Shradha Todi

get_features ops of pci_epc_ops may return NULL, causing NULL pointer dereference in pci_epf_test_bind function. Let us add a check for pci_epc_feature pointer in pci_epf_test_bind before we access it to avoid any such NULL pointer dereference and return -ENOTSUPP in case pci_epc_feature is not found.

Can you add a Fixes: tag to identify where this broke to help people decide where to backport the fix?

Quoted 40 lines

---
v2:
rebase on v1
v1: https://lore.kernel.org/patchwork/patch/1208269/

 drivers/pci/endpoint/functions/pci-epf-test.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/pci/endpoint/functions/pci-epf-test.c +8 -5
index 66723d5..f1842e6 100644
@@ -835,13 +835,16 @@ static int pci_epf_test_bind(struct pci_epf *epf)
 		return -EINVAL;
 
 	epc_features = pci_epc_get_features(epc, epf->func_no);
-	if (epc_features) {
-		linkup_notifier = epc_features->linkup_notifier;
-		core_init_notifier = epc_features->core_init_notifier;
-		test_reg_bar = pci_epc_get_first_free_bar(epc_features);
-		pci_epf_configure_bar(epf, epc_features);
+	if (!epc_features) {
+		dev_err(&epf->dev, "epc_features not implemented\n");
+		return -EOPNOTSUPP;
 	}
 
+	linkup_notifier = epc_features->linkup_notifier;
+	core_init_notifier = epc_features->core_init_notifier;
+	test_reg_bar = pci_epc_get_first_free_bar(epc_features);
+	pci_epf_configure_bar(epf, epc_features);
+
 	epf_test->test_reg_bar = test_reg_bar;
 	epf_test->epc_features = epc_features;
 
Signature · 1 line
2.7.4
#3 lore raw
Quoted 4 lines

From: Bjorn Helgaas <[email protected]>
Subject: Re: [PATCH v2] PCI: endpoint: Fix NULL pointer dereference for -

Quoted 1 line

get_features()

Quoted 8 lines
Quoted 5 lines · Shradha Todi

get_features ops of pci_epc_ops may return NULL, causing NULL pointer dereference in pci_epf_test_bind function. Let us add a check for pci_epc_feature pointer in pci_epf_test_bind before we access it to avoid any such NULL pointer dereference and return -ENOTSUPP in case pci_epc_feature is not found.

Can you add a Fixes: tag to identify where this broke to help people

decide

Quoted 2 lines

where to backport the fix?

Thanks for the review. Sure we will add the Fixes tag and respin this patch.

Quoted 45 lines
Quoted 9 lines

---
v2:
rebase on v1
v1:
https://protect2.fireeye.com/v1/url?k=6193fa90-3e08c3dd-619271df-0cc47
aa8f5ba-a79111ee2c1d1660&q=1&e=a9ee1ef7-2713-4099-8d06-

738532d78b26&u=

Quoted 35 lines

https%3A%2F%2Flore.kernel.org%2Fpatchwork%2Fpatch%2F1208269%2F

 drivers/pci/endpoint/functions/pci-epf-test.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/pci/endpoint/functions/pci-epf-test.c +8 -6
index 66723d5..f1842e6 100644
@@ -835,13 +835,16 @@ static int pci_epf_test_bind(struct pci_epf *epf)
 		return -EINVAL;
 
 	epc_features = pci_epc_get_features(epc, epf->func_no);
-	if (epc_features) {
-		linkup_notifier = epc_features->linkup_notifier;
-		core_init_notifier = epc_features->core_init_notifier;
-		test_reg_bar = pci_epc_get_first_free_bar(epc_features);
-		pci_epf_configure_bar(epf, epc_features);
+	if (!epc_features) {
+		dev_err(&epf->dev, "epc_features not implemented\n");
+		return -EOPNOTSUPP;
 	}
 
+	linkup_notifier = epc_features->linkup_notifier;
+	core_init_notifier = epc_features->core_init_notifier;
+	test_reg_bar = pci_epc_get_first_free_bar(epc_features);
+	pci_epf_configure_bar(epf, epc_features);
+
 	epf_test->test_reg_bar = test_reg_bar;
 	epf_test->epc_features = epc_features;
 
--

2.7.4