Skip to contentSkip to conversation

qed: fix uninit pointer read in qed_mcp_nvm_info_populate()

PATCH Superseded Gianfranco Trad 2 participants 5 messages Dec 11, 2024 – Dec 15, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qed: fix uninit pointer read in qed_mcp_nvm_info_populate()

Coverity reports an uninit pointer read in qed_mcp_nvm_info_populate(). If qed_mcp_bist_nvm_get_num_images() returns -EOPNOTSUPP, this leads to jump to label out with nvm_info.image_att being uninit while assigning it to p_hwfn->nvm_info.image_att.
Add check on rc against -EOPNOTSUPP to avoid such uninit pointer read.

---
Note:
- Fixes: tag should be "7a0ea70da56e net/qed: allow old cards not supporting "num_images" to work" ?

 drivers/net/ethernet/qlogic/qed/qed_mcp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/qlogic/qed/qed_mcp.c +1 -1
index b45efc272fdb..127943b39f61 100644
@@ -3387,7 +3387,7 @@ int qed_mcp_nvm_info_populate(struct qed_hwfn *p_hwfn)
 	}
 out:
 	/* Update hwfn's nvm_info */
-	if (nvm_info.num_images) {
+	if (nvm_info.num_images && rc != -EOPNOTSUPP) {
 		p_hwfn->nvm_info.num_images = nvm_info.num_images;
 		kfree(p_hwfn->nvm_info.image_att);
 		p_hwfn->nvm_info.image_att = nvm_info.image_att;
Signature · 1 line
2.43.0
reviewer
#1 lore raw
Quoted 20 lines · Gianfranco Trad

Coverity reports an uninit pointer read in qed_mcp_nvm_info_populate(). If qed_mcp_bist_nvm_get_num_images() returns -EOPNOTSUPP, this leads to jump to label out with nvm_info.image_att being uninit while assigning it to p_hwfn->nvm_info.image_att.
Add check on rc against -EOPNOTSUPP to avoid such uninit pointer read.

---
Note:
- Fixes: tag should be "7a0ea70da56e net/qed: allow old cards not supporting "num_images" to work" ?

 drivers/net/ethernet/qlogic/qed/qed_mcp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/qlogic/qed/qed_mcp.c +0 -0
index b45efc272fdb..127943b39f61 100644

drivers/net/ethernet/qlogic/qed/qed_mcp.c int qed_mcp_nvm_info_populate(struct qed_hwfn *p_hwfn) · line 3393

Quoted 8 lines
drivers/net/ethernet/qlogic/qed/qed_mcp.c +1 -1
@@ -3387,7 +3387,7 @@ int qed_mcp_nvm_info_populate(struct qed_hwfn *p_hwfn)
 	}
 out:
 	/* Update hwfn's nvm_info */
-	if (nvm_info.num_images) {
+	if (nvm_info.num_images && rc != -EOPNOTSUPP) {
 		p_hwfn->nvm_info.num_images = nvm_info.num_images;
 		kfree(p_hwfn->nvm_info.image_att);
 		p_hwfn->nvm_info.image_att = nvm_info.image_att;

Are you sure that nvm_info.num_images can be non-zero if rc == -EOPNOTSUPP?

The cited commit state:

Commit 43645ce03e00 ("qed: Populate nvm image attribute shadow.") added support for populating flash image attributes, notably "num_images". However, some cards were not able to return this information. In such cases, the driver would return EINVAL, causing the driver to exit.

Add check to return EOPNOTSUPP instead of EINVAL when the card is not able to return these information. The caller function already handles EOPNOTSUPP without error.

So I would expect that nvm_info.num_images is 0.

If not, perhaps an alternate fix is to make that so, either by setting it in qed_mcp_bist_nvm_get_num_images, or where the return value of qed_mcp_bist_nvm_get_num_images is checked (just before the goto out).

And, in any case I think some testing is in order.

#2 lore raw
Quoted 16 lines · Simon Horman
Quoted 15 lines · Gianfranco Trad

Coverity reports an uninit pointer read in qed_mcp_nvm_info_populate(). If qed_mcp_bist_nvm_get_num_images() returns -EOPNOTSUPP, this leads to jump to label out with nvm_info.image_att being uninit while assigning it to p_hwfn->nvm_info.image_att.
Add check on rc against -EOPNOTSUPP to avoid such uninit pointer read.

---
Note:
- Fixes: tag should be "7a0ea70da56e net/qed: allow old cards not supporting "num_images" to work" ?

  drivers/net/ethernet/qlogic/qed/qed_mcp.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

drivers/net/ethernet/qlogic/qed/qed_mcp.c int qed_mcp_nvm_info_populate(struct qed_hwfn *p_hwfn) · line 3393

Quoted 13 lines
drivers/net/ethernet/qlogic/qed/qed_mcp.c +1 -1
index b45efc272fdb..127943b39f61 100644
@@ -3387,7 +3387,7 @@ int qed_mcp_nvm_info_populate(struct qed_hwfn *p_hwfn)
  	}
  out:
  	/* Update hwfn's nvm_info */
-	if (nvm_info.num_images) {
+	if (nvm_info.num_images && rc != -EOPNOTSUPP) {
  		p_hwfn->nvm_info.num_images = nvm_info.num_images;
  		kfree(p_hwfn->nvm_info.image_att);
  		p_hwfn->nvm_info.image_att = nvm_info.image_att;

Hi Simon,

Quoted 2 lines

Are you sure that nvm_info.num_images can be non-zero if rc == -EOPNOTSUPP?

In the coverity report, the static analyzer is able to take the true branch on nvm_info.num_images. I didn't physically reproduce this logical state as I don't possess the matching hardware.

Quoted 12 lines

The cited commit state:

Commit 43645ce03e00 ("qed: Populate nvm image attribute shadow.") added support for populating flash image attributes, notably "num_images". However, some cards were not able to return this information. In such cases, the driver would return EINVAL, causing the driver to exit.

Add check to return EOPNOTSUPP instead of EINVAL when the card is not able to return these information. The caller function already handles EOPNOTSUPP without error.

Quoted 6 lines

So I would expect that nvm_info.num_images is 0.

If not, perhaps an alternate fix is to make that so, either by setting it in qed_mcp_bist_nvm_get_num_images, or where the return value of qed_mcp_bist_nvm_get_num_images is checked (just before the goto out).

Makes sense, so something like this I suppose:

drivers/net/ethernet/qlogic/qed/qed_mcp.c +3 -1
@@ -3301,8 +3301,10 @@ int qed_mcp_bist_nvm_get_num_images(struct qed_hwfn *p_hwfn,
  	if (rc)
  		return rc;
 
-	if (((rsp & FW_MSG_CODE_MASK) == FW_MSG_CODE_UNSUPPORTED))
+	if (((rsp & FW_MSG_CODE_MASK) == FW_MSG_CODE_UNSUPPORTED)) {
+		*num_images = 0;
  		rc = -EOPNOTSUPP;
+	}
 

Or the second option you stated.

Quoted 1 line

And, in any case I think some testing is in order.

I strongly agree. Let me know if I can help more with this.

Thanks for your time,
--Gian

reviewer
#3 lore raw
Quoted 74 lines · Gianfranco Trad
Quoted 15 lines · Simon Horman
Quoted 14 lines · Gianfranco Trad

Coverity reports an uninit pointer read in qed_mcp_nvm_info_populate(). If qed_mcp_bist_nvm_get_num_images() returns -EOPNOTSUPP, this leads to jump to label out with nvm_info.image_att being uninit while assigning it to p_hwfn->nvm_info.image_att.
Add check on rc against -EOPNOTSUPP to avoid such uninit pointer read.

---
Note:
- Fixes: tag should be "7a0ea70da56e net/qed: allow old cards not supporting "num_images" to work" ?

  drivers/net/ethernet/qlogic/qed/qed_mcp.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

drivers/net/ethernet/qlogic/qed/qed_mcp.c int qed_mcp_nvm_info_populate(struct qed_hwfn *p_hwfn) · line 3393

Quoted 13 lines
drivers/net/ethernet/qlogic/qed/qed_mcp.c +1 -1
index b45efc272fdb..127943b39f61 100644
@@ -3387,7 +3387,7 @@ int qed_mcp_nvm_info_populate(struct qed_hwfn *p_hwfn)
  	}
  out:
  	/* Update hwfn's nvm_info */
-	if (nvm_info.num_images) {
+	if (nvm_info.num_images && rc != -EOPNOTSUPP) {
  		p_hwfn->nvm_info.num_images = nvm_info.num_images;
  		kfree(p_hwfn->nvm_info.image_att);
  		p_hwfn->nvm_info.image_att = nvm_info.image_att;

Hi Simon,

Quoted 2 lines

Are you sure that nvm_info.num_images can be non-zero if rc == -EOPNOTSUPP?

In the coverity report, the static analyzer is able to take the true branch on nvm_info.num_images. I didn't physically reproduce this logical state as I don't possess the matching hardware.

Quoted 12 lines

The cited commit state:

Commit 43645ce03e00 ("qed: Populate nvm image attribute shadow.") added support for populating flash image attributes, notably "num_images". However, some cards were not able to return this information. In such cases, the driver would return EINVAL, causing the driver to exit.

Add check to return EOPNOTSUPP instead of EINVAL when the card is not able to return these information. The caller function already handles EOPNOTSUPP without error.

Quoted 6 lines

So I would expect that nvm_info.num_images is 0.

If not, perhaps an alternate fix is to make that so, either by setting it in qed_mcp_bist_nvm_get_num_images, or where the return value of qed_mcp_bist_nvm_get_num_images is checked (just before the goto out).

Makes sense, so something like this I suppose:

drivers/net/ethernet/qlogic/qed/qed_mcp.c +3 -1
@@ -3301,8 +3301,10 @@ int qed_mcp_bist_nvm_get_num_images(struct qed_hwfn
 *p_hwfn,
  	if (rc)
  		return rc;
 
-	if (((rsp & FW_MSG_CODE_MASK) == FW_MSG_CODE_UNSUPPORTED))
+	if (((rsp & FW_MSG_CODE_MASK) == FW_MSG_CODE_UNSUPPORTED)) {
+		*num_images = 0;
  		rc = -EOPNOTSUPP;
+	}
 
Quoted 1 line

Or the second option you stated.

Yes, that is what I was thinking.
But as it is a side effect, and thus hidden slightly, on reflection perhaps the second option is better. IDK.

Quoted 7 lines
Quoted 1 line

And, in any case I think some testing is in order.

I strongly agree. Let me know if I can help more with this.

Thanks for your time,
--Gian

#4 lore raw
Quoted 75 lines · Simon Horman
Quoted 74 lines · Gianfranco Trad
Quoted 15 lines · Simon Horman
Quoted 14 lines · Gianfranco Trad

Coverity reports an uninit pointer read in qed_mcp_nvm_info_populate(). If qed_mcp_bist_nvm_get_num_images() returns -EOPNOTSUPP, this leads to jump to label out with nvm_info.image_att being uninit while assigning it to p_hwfn->nvm_info.image_att.
Add check on rc against -EOPNOTSUPP to avoid such uninit pointer read.

---
Note:
- Fixes: tag should be "7a0ea70da56e net/qed: allow old cards not supporting "num_images" to work" ?

   drivers/net/ethernet/qlogic/qed/qed_mcp.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

drivers/net/ethernet/qlogic/qed/qed_mcp.c int qed_mcp_nvm_info_populate(struct qed_hwfn *p_hwfn) · line 3393

Quoted 13 lines
drivers/net/ethernet/qlogic/qed/qed_mcp.c +1 -1
index b45efc272fdb..127943b39f61 100644
@@ -3387,7 +3387,7 @@ int qed_mcp_nvm_info_populate(struct qed_hwfn *p_hwfn)
   	}
   out:
   	/* Update hwfn's nvm_info */
-	if (nvm_info.num_images) {
+	if (nvm_info.num_images && rc != -EOPNOTSUPP) {
   		p_hwfn->nvm_info.num_images = nvm_info.num_images;
   		kfree(p_hwfn->nvm_info.image_att);
   		p_hwfn->nvm_info.image_att = nvm_info.image_att;

Hi Simon,

Quoted 2 lines

Are you sure that nvm_info.num_images can be non-zero if rc == -EOPNOTSUPP?

In the coverity report, the static analyzer is able to take the true branch on nvm_info.num_images. I didn't physically reproduce this logical state as I don't possess the matching hardware.

Quoted 12 lines

The cited commit state:

Commit 43645ce03e00 ("qed: Populate nvm image attribute shadow.") added support for populating flash image attributes, notably "num_images". However, some cards were not able to return this information. In such cases, the driver would return EINVAL, causing the driver to exit.

Add check to return EOPNOTSUPP instead of EINVAL when the card is not able to return these information. The caller function already handles EOPNOTSUPP without error.

Quoted 6 lines

So I would expect that nvm_info.num_images is 0.

If not, perhaps an alternate fix is to make that so, either by setting it in qed_mcp_bist_nvm_get_num_images, or where the return value of qed_mcp_bist_nvm_get_num_images is checked (just before the goto out).

Makes sense, so something like this I suppose:

drivers/net/ethernet/qlogic/qed/qed_mcp.c +3 -1
@@ -3301,8 +3301,10 @@ int qed_mcp_bist_nvm_get_num_images(struct qed_hwfn
 *p_hwfn,
   	if (rc)
   		return rc;
 
-	if (((rsp & FW_MSG_CODE_MASK) == FW_MSG_CODE_UNSUPPORTED))
+	if (((rsp & FW_MSG_CODE_MASK) == FW_MSG_CODE_UNSUPPORTED)) {
+		*num_images = 0;
   		rc = -EOPNOTSUPP;
+	}
 
Quoted 6 lines
Quoted 1 line

Or the second option you stated.

Yes, that is what I was thinking.
But as it is a side effect, and thus hidden slightly, on reflection perhaps the second option is better. IDK.

Got it. Will send a v2 accordingly.

Thanks for your time,
--Gian

Quoted 7 lines
Quoted 7 lines
Quoted 1 line

And, in any case I think some testing is in order.

I strongly agree. Let me know if I can help more with this.

Thanks for your time,
--Gian