Skip to contentSkip to conversation

misc: alcor_pci: fix null-ptr-deref when there is no PCI bridge

PATCH Superseded Tong Zhang 3 participants 15 messages Apr 26, 2021 – May 19, 2021 Char and misc drivers

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tong Zhang

author
#0 lore raw

PATCH 1/1 misc: alcor_pci: fix null-ptr-deref when there is no PCI bridge

the PCI bridge might be NULL, so we'd better check before use it

[    1.246492] BUG: kernel NULL pointer dereference, address: 00000000000000c0
[    1.248731] RIP: 0010:pci_read_config_byte+0x5/0x40
[    1.253998] Call Trace:
[    1.254131]  ? alcor_pci_find_cap_offset.isra.0+0x3a/0x100 [alcor_pci]
[    1.254476]  alcor_pci_probe+0x169/0x2d5 [alcor_pci]
---
 drivers/misc/cardreader/alcor_pci.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/misc/cardreader/alcor_pci.c +3 -0
index cd402c89189e..1c33453fd5c7 100644
@@ -102,6 +102,9 @@ static int alcor_pci_find_cap_offset(struct alcor_pci_priv *priv,
 	u8 val8;
 	u32 val32;
 
+	if (!pci)
+		return 0;
+
 	where = ALCOR_CAP_START_OFFSET;
 	pci_read_config_byte(pci, where, &val8);
 	if (!val8)
Signature · 1 line
2.25.1
#1 lore raw
Quoted 30 lines · Tong Zhang

the PCI bridge might be NULL, so we'd better check before use it

[    1.246492] BUG: kernel NULL pointer dereference, address: 00000000000000c0
[    1.248731] RIP: 0010:pci_read_config_byte+0x5/0x40
[    1.253998] Call Trace:
[    1.254131]  ? alcor_pci_find_cap_offset.isra.0+0x3a/0x100 [alcor_pci]
[    1.254476]  alcor_pci_probe+0x169/0x2d5 [alcor_pci]
---
 drivers/misc/cardreader/alcor_pci.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/misc/cardreader/alcor_pci.c +3 -0
index cd402c89189e..1c33453fd5c7 100644
@@ -102,6 +102,9 @@ static int alcor_pci_find_cap_offset(struct alcor_pci_priv *priv,
 	u8 val8;
 	u32 val32;
 
+	if (!pci)
+		return 0;
+
 	where = ALCOR_CAP_START_OFFSET;
 	pci_read_config_byte(pci, where, &val8);
 	if (!val8)
Signature · 1 line
2.25.1

I do not understand, how can pci ever be NULL? There is only 1 way this function can be called, and it's through the alcor_pci_probe() call, which should have always set up the parent and pci pointers that get passed to this function.

How can that not happen? If it can happen, then something earlier than this should be fixed instead of papering over the root problem here.

How did you duplicate the crash you list above?

thanks,

greg k-h

author
#2 lore raw
Quoted 23 lines · Greg Kroah-Hartman
Quoted 21 lines · Tong Zhang

the PCI bridge might be NULL, so we'd better check before use it

[    1.246492] BUG: kernel NULL pointer dereference, address: 00000000000000c0
[    1.248731] RIP: 0010:pci_read_config_byte+0x5/0x40
[    1.253998] Call Trace:
[    1.254131]  ? alcor_pci_find_cap_offset.isra.0+0x3a/0x100 [alcor_pci]
[    1.254476]  alcor_pci_probe+0x169/0x2d5 [alcor_pci]
---
 drivers/misc/cardreader/alcor_pci.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/misc/cardreader/alcor_pci.c +0 -0
index cd402c89189e..1c33453fd5c7 100644
@@ -102,6 +102,9 @@ static int alcor_pci_find_cap_offset(struct alcor_pci_priv *priv,
      u8 val8;
      u32 val32;
 
Quoted 11 lines
Quoted 9 lines
drivers/misc/cardreader/alcor_pci.c +3 -1
+     if (!pci)
+             return 0;
+
      where = ALCOR_CAP_START_OFFSET;
      pci_read_config_byte(pci, where, &val8);
      if (!val8)
--

2.25.1

I do not understand, how can pci ever be NULL? There is only 1 way this

Hi Greg,
I think the problem is with

    priv->parent_pdev = pdev->bus->self
where bus->self can be NULL. when bus->self is NULL, calling
    alcor_pci_find_cap_offset(priv, priv->parent_pdev) is equivalent

to alcor_pci_find_cap_offset(priv, NULL)

- Tong

Quoted 12 lines

function can be called, and it's through the alcor_pci_probe() call, which should have always set up the parent and pci pointers that get passed to this function.

How can that not happen? If it can happen, then something earlier than this should be fixed instead of papering over the root problem here.

How did you duplicate the crash you list above?

thanks,

greg k-h

#3 lore raw
Quoted 37 lines · Tong Zhang
Quoted 23 lines · Greg Kroah-Hartman
Quoted 21 lines · Tong Zhang

the PCI bridge might be NULL, so we'd better check before use it

[    1.246492] BUG: kernel NULL pointer dereference, address: 00000000000000c0
[    1.248731] RIP: 0010:pci_read_config_byte+0x5/0x40
[    1.253998] Call Trace:
[    1.254131]  ? alcor_pci_find_cap_offset.isra.0+0x3a/0x100 [alcor_pci]
[    1.254476]  alcor_pci_probe+0x169/0x2d5 [alcor_pci]
---
 drivers/misc/cardreader/alcor_pci.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/misc/cardreader/alcor_pci.c +0 -0
index cd402c89189e..1c33453fd5c7 100644
@@ -102,6 +102,9 @@ static int alcor_pci_find_cap_offset(struct alcor_pci_priv *priv,
      u8 val8;
      u32 val32;
 
Quoted 11 lines
Quoted 9 lines
drivers/misc/cardreader/alcor_pci.c +3 -1
+     if (!pci)
+             return 0;
+
      where = ALCOR_CAP_START_OFFSET;
      pci_read_config_byte(pci, where, &val8);
      if (!val8)
--

2.25.1

I do not understand, how can pci ever be NULL? There is only 1 way this

Quoted 4 lines

Hi Greg,
I think the problem is with

    priv->parent_pdev = pdev->bus->self

where bus->self can be NULL. when bus->self is NULL, calling

How can bus->self be NULL?

Did you see this on a real system? How did you duplicate the error listed here?

thanks,

greg k-h

author
#4 lore raw
Quoted 8 lines · Greg Kroah-Hartman
Quoted 6 lines · Tong Zhang
Quoted 4 lines · Greg Kroah-Hartman
Quoted 1 line · Tong Zhang

the PCI bridge might be NULL, so we'd better check before use it

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

I do not understand, how can pci ever be NULL? There is only 1 way this

Hi Greg,
I think the problem is with

    priv->parent_pdev = pdev->bus->self

where bus->self can be NULL. when bus->self is NULL, calling

How can bus->self be NULL?

Hi Greg,
Please correct me if I am wrong,
when bus->self is not NULL, it means there is a bridge, However, a device can be directly attached to the port on the root complex. In this case, the bus->self is NULL.

Quoted 2 lines

Did you see this on a real system? How did you duplicate the error listed here?

I did this in QEMU. If QEMU is considered not real, then I haven't seen an alcor controller configured in this way in a real system. That being said, this kind of configuration is still legit IMHO. Best,
- Tong

Quoted 3 lines

thanks,

greg k-h

#5 lore raw
Quoted 17 lines · Tong Zhang
Quoted 15 lines · Greg Kroah-Hartman
Quoted 12 lines · Tong Zhang
Quoted 5 lines · Greg Kroah-Hartman
Quoted 1 line · Tong Zhang

the PCI bridge might be NULL, so we'd better check before use it

I do not understand, how can pci ever be NULL? There is only 1 way this

Hi Greg,
I think the problem is with

    priv->parent_pdev = pdev->bus->self

where bus->self can be NULL. when bus->self is NULL, calling

Quoted 7 lines
Quoted 1 line

How can bus->self be NULL?

Hi Greg,
Please correct me if I am wrong,
when bus->self is not NULL, it means there is a bridge, However, a device can be directly attached to the port on the root complex. In this case, the bus->self is NULL.

Does that ever happen with a device on the root like that?

Quoted 5 lines
Quoted 2 lines

Did you see this on a real system? How did you duplicate the error listed here?

I did this in QEMU. If QEMU is considered not real, then I haven't seen an alcor controller configured in this way in a real system. That being said, this kind of configuration is still legit IMHO.

Ah, ok, that makes more sense, this is a virtual system.

I suggest, again, steping back up and just not calling this function if you are on the root, as it does not make any sense to do so for a device that is not there.

thanks,

greg k-h

author
#6 lore raw

PATCH v2 1/1 misc: alcor_pci: fix null-ptr-deref when there is no PCI bridge

Device might be attached to root complex directly. In this case, bus->self(bridge) will be NULL, so we'd better check before use it

[    1.246492] BUG: kernel NULL pointer dereference, address: 00000000000000c0
[    1.248731] RIP: 0010:pci_read_config_byte+0x5/0x40
[    1.253998] Call Trace:
[    1.254131]  ? alcor_pci_find_cap_offset.isra.0+0x3a/0x100 [alcor_pci]
[    1.254476]  alcor_pci_probe+0x169/0x2d5 [alcor_pci]

---
v2: check before calling alcor_pci_find_cap_offset()

 drivers/misc/cardreader/alcor_pci.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/misc/cardreader/alcor_pci.c +3 -0
index cd402c89189e..175c6b06f7aa 100644
@@ -139,6 +139,9 @@ static void alcor_pci_init_check_aspm(struct alcor_pci_priv *priv)
 	u32 val32;
 
 	priv->pdev_cap_off    = alcor_pci_find_cap_offset(priv, priv->pdev);
+
+	if (!priv->parent_pdev)
+		return;
 	priv->parent_cap_off = alcor_pci_find_cap_offset(priv,
 							 priv->parent_pdev);
 
Signature · 1 line
2.25.1
#7 lore raw
Quoted 22 lines · Tong Zhang

Device might be attached to root complex directly. In this case, bus->self(bridge) will be NULL, so we'd better check before use it

[    1.246492] BUG: kernel NULL pointer dereference, address: 00000000000000c0
[    1.248731] RIP: 0010:pci_read_config_byte+0x5/0x40
[    1.253998] Call Trace:
[    1.254131]  ? alcor_pci_find_cap_offset.isra.0+0x3a/0x100 [alcor_pci]
[    1.254476]  alcor_pci_probe+0x169/0x2d5 [alcor_pci]

---
v2: check before calling alcor_pci_find_cap_offset()

 drivers/misc/cardreader/alcor_pci.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/misc/cardreader/alcor_pci.c +0 -0
index cd402c89189e..175c6b06f7aa 100644

drivers/misc/cardreader/alcor_pci.c static void alcor_pci_init_check_aspm(struct alcor_pci_priv *priv) · line 144

Quoted 6 lines
drivers/misc/cardreader/alcor_pci.c +3 -0
@@ -139,6 +139,9 @@ static void alcor_pci_init_check_aspm(struct alcor_pci_priv *priv)
 	u32 val32;
 
 	priv->pdev_cap_off    = alcor_pci_find_cap_offset(priv, priv->pdev);
+
+	if (!priv->parent_pdev)
+		return;

That feels wrong, you just prevented all of the remaining logic in this call to not be set up. Did you test this and did the driver and device still work properly if it hits this check?

thanks,

greg k-h

author
#8 lore raw
Quoted 35 lines · Greg Kroah-Hartman
Quoted 22 lines · Tong Zhang

Device might be attached to root complex directly. In this case, bus->self(bridge) will be NULL, so we'd better check before use it

[    1.246492] BUG: kernel NULL pointer dereference, address: 00000000000000c0
[    1.248731] RIP: 0010:pci_read_config_byte+0x5/0x40
[    1.253998] Call Trace:
[    1.254131]  ? alcor_pci_find_cap_offset.isra.0+0x3a/0x100 [alcor_pci]
[    1.254476]  alcor_pci_probe+0x169/0x2d5 [alcor_pci]

---
v2: check before calling alcor_pci_find_cap_offset()

 drivers/misc/cardreader/alcor_pci.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/misc/cardreader/alcor_pci.c +0 -0
index cd402c89189e..175c6b06f7aa 100644

drivers/misc/cardreader/alcor_pci.c static void alcor_pci_init_check_aspm(struct alcor_pci_priv *priv) · line 144

Quoted 6 lines
drivers/misc/cardreader/alcor_pci.c +3 -0
@@ -139,6 +139,9 @@ static void alcor_pci_init_check_aspm(struct alcor_pci_priv *priv)
      u32 val32;
 
      priv->pdev_cap_off    = alcor_pci_find_cap_offset(priv, priv->pdev);
+
+     if (!priv->parent_pdev)
+             return;

That feels wrong, you just prevented all of the remaining logic in this call to not be set up. Did you test this and did the driver and device still work properly if it hits this check?

Quoted 3 lines

thanks,

greg k-h

Sorry, probably I misunderstood your previous email. Please correct me if I am wrong.
What I did here is to disable ASPM completely if it is attached to the root complex, which is OK since ASPM is optional and we cannot really do ASPM on the root complex.
Also, alcor_pci_init_check_aspm() is responsible for checking the device and its parent(bridge) aspm capability offset.
This function will set priv->parent_cap_off and priv->pdev_cap_off. Those two capability offset will be used in alcor_pci_aspm_ctrl() to determine whether the PCI link+device supports aspm or not. In our case the pdev_cap_off remains 0 when alcor_pci_aspm_ctrl() is called and it simply returns.
So I think it can still work.
- Tong

#9 lore raw
Quoted 41 lines · Tong Zhang
Quoted 35 lines · Greg Kroah-Hartman
Quoted 22 lines · Tong Zhang

Device might be attached to root complex directly. In this case, bus->self(bridge) will be NULL, so we'd better check before use it

[    1.246492] BUG: kernel NULL pointer dereference, address: 00000000000000c0
[    1.248731] RIP: 0010:pci_read_config_byte+0x5/0x40
[    1.253998] Call Trace:
[    1.254131]  ? alcor_pci_find_cap_offset.isra.0+0x3a/0x100 [alcor_pci]
[    1.254476]  alcor_pci_probe+0x169/0x2d5 [alcor_pci]

---
v2: check before calling alcor_pci_find_cap_offset()

 drivers/misc/cardreader/alcor_pci.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/misc/cardreader/alcor_pci.c +0 -0
index cd402c89189e..175c6b06f7aa 100644

drivers/misc/cardreader/alcor_pci.c static void alcor_pci_init_check_aspm(struct alcor_pci_priv *priv) · line 144

Quoted 6 lines
drivers/misc/cardreader/alcor_pci.c +3 -0
@@ -139,6 +139,9 @@ static void alcor_pci_init_check_aspm(struct alcor_pci_priv *priv)
      u32 val32;
 
      priv->pdev_cap_off    = alcor_pci_find_cap_offset(priv, priv->pdev);
+
+     if (!priv->parent_pdev)
+             return;

That feels wrong, you just prevented all of the remaining logic in this call to not be set up. Did you test this and did the driver and device still work properly if it hits this check?

Quoted 3 lines

thanks,

greg k-h

Quoted 13 lines

Sorry, probably I misunderstood your previous email. Please correct me if I am wrong.
What I did here is to disable ASPM completely if it is attached to the root complex, which is OK since ASPM is optional and we cannot really do ASPM on the root complex.
Also, alcor_pci_init_check_aspm() is responsible for checking the device and its parent(bridge) aspm capability offset.
This function will set priv->parent_cap_off and priv->pdev_cap_off. Those two capability offset will be used in alcor_pci_aspm_ctrl() to determine whether the PCI link+device supports aspm or not. In our case the pdev_cap_off remains 0 when alcor_pci_aspm_ctrl() is called and it simply returns.
So I think it can still work.

Ok, that makes more sense.

Can you document that better and add a comment here, and properly handle the whitespace and resubmit?

thanks,

greg k-h

author
#10 lore raw

PATCH v3 1/1 misc: alcor_pci: fix null-ptr-deref when there is no PCI bridge

There is an issue with the ASPM(optional) capability checking function. A device might be attached to root complex directly, in this case, bus->self(bridge) will be NULL, thus priv->parent_pdev is NULL. Since alcor_pci_init_check_aspm(priv->parent_pdev) checks the PCI link's ASPM capability and populate parent_cap_off, which will be used later by alcor_pci_aspm_ctrl() to dynamically turn on/off device, what we can do here is to avoid checking the capability if we are on the root complex. This will make pdev_cap_off 0 and alcor_pci_aspm_ctrl() will simply return when bring called, effectively disable ASPM for the device.

[    1.246492] BUG: kernel NULL pointer dereference, address: 00000000000000c0
[    1.248731] RIP: 0010:pci_read_config_byte+0x5/0x40
[    1.253998] Call Trace:
[    1.254131]  ? alcor_pci_find_cap_offset.isra.0+0x3a/0x100 [alcor_pci]
[    1.254476]  alcor_pci_probe+0x169/0x2d5 [alcor_pci]

---
v2: check before calling alcor_pci_find_cap_offset()
v3: Add comment. Enable the dev_dbg() output when priv->parent_pdev is NULL.

 drivers/misc/cardreader/alcor_pci.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/misc/cardreader/alcor_pci.c +7 -1
index cd402c89189e..0a62307f7ffb 100644
@@ -139,7 +139,13 @@ static void alcor_pci_init_check_aspm(struct alcor_pci_priv *priv)
 	u32 val32;
 
 	priv->pdev_cap_off    = alcor_pci_find_cap_offset(priv, priv->pdev);
-	priv->parent_cap_off = alcor_pci_find_cap_offset(priv,
+	/*
+	 * A device might be attached to root complex directly and
+	 * priv->parent_pdev will be NULL. In this case we don't check its
+	 * capability and disable ASPM completely.
+	 */
+	if (!priv->parent_pdev)
+		priv->parent_cap_off = alcor_pci_find_cap_offset(priv,
 							 priv->parent_pdev);
 
 	if ((priv->pdev_cap_off == 0) || (priv->parent_cap_off == 0)) {
Signature · 1 line
2.25.1
#11 lore raw

Hello Tong Zhang,

This is a semi-automatic email about new static checker warnings.

The patch 3ce3e45cc333: "misc: alcor_pci: fix null-ptr-deref when there is no PCI bridge" from May 13, 2021, leads to the following Smatch complaint:

    drivers/misc/cardreader/alcor_pci.c:149 alcor_pci_init_check_aspm()
    error: we previously assumed 'priv->parent_pdev' could be null (see line 147)

drivers/misc/cardreader/alcor_pci.c
142 /*

   143           * A device might be attached to root complex directly and
   144           * priv->parent_pdev will be NULL. In this case we don't check its
   145           * capability and disable ASPM completely.
   146           */
   147		if (!priv->parent_pdev)
                    ^^^^^^^^^^^^^^^^^^

148 priv->parent_cap_off = alcor_pci_find_cap_offset(priv,

   149								 priv->parent_pdev);
                                                                 ^^^^^^^^^^^^^^^^^^

It will just crash inside the function call. Is the if statement reversed?

   150	
   151		if ((priv->pdev_cap_off == 0) || (priv->parent_cap_off == 0)) {

regards,
dan carpenter

author
#12 lore raw
Quoted 24 lines · Dan Carpenter

Hello Tong Zhang,

This is a semi-automatic email about new static checker warnings.

The patch 3ce3e45cc333: "misc: alcor_pci: fix null-ptr-deref when there is no PCI bridge" from May 13, 2021, leads to the following Smatch complaint:

    drivers/misc/cardreader/alcor_pci.c:149 alcor_pci_init_check_aspm()
    error: we previously assumed 'priv->parent_pdev' could be null (see line 147)

drivers/misc/cardreader/alcor_pci.c
142 /*

   143           * A device might be attached to root complex directly and
   144           * priv->parent_pdev will be NULL. In this case we don't check its
   145           * capability and disable ASPM completely.
   146           */
   147          if (!priv->parent_pdev)
                    ^^^^^^^^^^^^^^^^^^
   148                  priv->parent_cap_off = alcor_pci_find_cap_offset(priv,
   149                                                           priv->parent_pdev);
                                                                 ^^^^^^^^^^^^^^^^^^
Quoted 8 lines

It will just crash inside the function call. Is the if statement reversed?

   150
   151          if ((priv->pdev_cap_off == 0) || (priv->parent_cap_off == 0)) {

regards,
dan carpenter

Thanks Dan.
I already corrected this in v4
https://lkml.org/lkml/2021/5/18/1040
Please check if the issue persists.
- Tong

author
#13 lore raw
In reply to Greg Kroah-Hartman
Quoted 7 lines · Greg Kroah-Hartman

Can you document that better and add a comment here, and properly handle the whitespace and resubmit?

thanks,

greg k-h

Thanks Greg, revised as suggested and sent as v3.
- Tong

author
#14 lore raw
In reply to Greg Kroah-Hartman
Quoted 5 lines · Greg Kroah-Hartman

I suggest, again, steping back up and just not calling this function if you are on the root, as it does not make any sense to do so for a device that is not there.

Agreed.
I did a revision based on your suggestion and sent it as v2. Thanks,
- Tong

Quoted 3 lines

thanks,

greg k-h