Skip to contentSkip to conversation

9p/xen: Fix end of loop tests for list_for_each_entry

PATCH In review Harshvardhan Jha 3 participants 9 messages Jul 25, 2021 – Jul 27, 2021

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

PATCH 1/1 9p/xen: Fix end of loop tests for list_for_each_entry

The list_for_each_entry() iterator, "priv" in this code, can never be NULL so the warning would never be printed.

---
From static analysis. Not tested.

---
 net/9p/trans_xen.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/9p/trans_xen.c +2 -2
index f4fea28e05da..3ec1a51a6944 100644
@@ -138,7 +138,7 @@ static bool p9_xen_write_todo(struct xen_9pfs_dataring *ring, RING_IDX size)
 
 static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
 {
-	struct xen_9pfs_front_priv *priv = NULL;
+	struct xen_9pfs_front_priv *priv;
 	RING_IDX cons, prod, masked_cons, masked_prod;
 	unsigned long flags;
 	u32 size = p9_req->tc.size;
@@ -151,7 +151,7 @@ static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
 			break;
 	}
 	read_unlock(&xen_9pfs_lock);
-	if (!priv || priv->client != client)
+	if (list_entry_is_head(priv, &xen_9pfs_devs, list))
 		return -EINVAL;
 
 	num = p9_req->tc.tag % priv->num_rings;
Signature · 1 line
2.32.0
#1 lore raw

Harshvardhan Jha wrote on Sun, Jul 25, 2021 at 11:21:03PM +0530:

Quoted 2 lines

The list_for_each_entry() iterator, "priv" in this code, can never be NULL so the warning would never be printed.

hm? priv won't be NULL but priv->client won't be client, so it will return -EINVAL alright in practice?

This does fix an invalid read after the list head, so there's a real bug, but the commit message needs fixing.

Quoted 4 lines

---
From static analysis. Not tested.

+Stefano in To - I also can't test xen right now :/
This looks functional to me but if you have a bit of time to spare just a mount test can't hurt.

Quoted 26 lines
---
 net/9p/trans_xen.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/9p/trans_xen.c +2 -2
index f4fea28e05da..3ec1a51a6944 100644
@@ -138,7 +138,7 @@ static bool p9_xen_write_todo(struct xen_9pfs_dataring *ring, RING_IDX size)
 
 static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
 {
-	struct xen_9pfs_front_priv *priv = NULL;
+	struct xen_9pfs_front_priv *priv;
 	RING_IDX cons, prod, masked_cons, masked_prod;
 	unsigned long flags;
 	u32 size = p9_req->tc.size;
@@ -151,7 +151,7 @@ static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
 			break;
 	}
 	read_unlock(&xen_9pfs_lock);
-	if (!priv || priv->client != client)
+	if (list_entry_is_head(priv, &xen_9pfs_devs, list))
 		return -EINVAL;
 
 	num = p9_req->tc.tag % priv->num_rings;
Signature · 1 line
Dominique
Reviewed-by Tested-by
#2 lore raw
Quoted 9 lines · On Mon, 26 Jul 2021, [email protected] wrote:

Harshvardhan Jha wrote on Sun, Jul 25, 2021 at 11:21:03PM +0530:

Quoted 2 lines

The list_for_each_entry() iterator, "priv" in this code, can never be NULL so the warning would never be printed.

hm? priv won't be NULL but priv->client won't be client, so it will return -EINVAL alright in practice?

This does fix an invalid read after the list head, so there's a real bug, but the commit message needs fixing.

Agreed

Quoted 7 lines
Quoted 3 lines

---
From static analysis. Not tested.

+Stefano in To - I also can't test xen right now :/
This looks functional to me but if you have a bit of time to spare just a mount test can't hurt.

Yes, I did test it successfully. Aside from the commit messaged to be reworded:

Quoted 26 lines
Quoted 26 lines
---
 net/9p/trans_xen.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/9p/trans_xen.c +2 -2
index f4fea28e05da..3ec1a51a6944 100644
@@ -138,7 +138,7 @@ static bool p9_xen_write_todo(struct xen_9pfs_dataring *ring, RING_IDX size)
 
 static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
 {
-	struct xen_9pfs_front_priv *priv = NULL;
+	struct xen_9pfs_front_priv *priv;
 	RING_IDX cons, prod, masked_cons, masked_prod;
 	unsigned long flags;
 	u32 size = p9_req->tc.size;
@@ -151,7 +151,7 @@ static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
 			break;
 	}
 	read_unlock(&xen_9pfs_lock);
-	if (!priv || priv->client != client)
+	if (list_entry_is_head(priv, &xen_9pfs_devs, list))
 		return -EINVAL;
 
 	num = p9_req->tc.tag % priv->num_rings;
#3 lore raw

Re: [External] : Re: [PATCH] 9p/xen: Fix end of loop tests for list_for_each_entry

Quoted 18 lines · Stefano Stabellini
Quoted 9 lines · On Mon, 26 Jul 2021, [email protected] wrote:

Harshvardhan Jha wrote on Sun, Jul 25, 2021 at 11:21:03PM +0530:

Quoted 2 lines

The list_for_each_entry() iterator, "priv" in this code, can never be NULL so the warning would never be printed.

hm? priv won't be NULL but priv->client won't be client, so it will return -EINVAL alright in practice?

This does fix an invalid read after the list head, so there's a real bug, but the commit message needs fixing.

Agreed

Quoted 4 lines
Quoted 3 lines

---
From static analysis. Not tested.

Quoted 6 lines
Quoted 3 lines

+Stefano in To - I also can't test xen right now :/
This looks functional to me but if you have a bit of time to spare just a mount test can't hurt.

Yes, I did test it successfully. Aside from the commit messaged to be reworded:

How's this? ===========================BEGIN======================================== 9p/xen: Fix end of loop tests for list_for_each_entry

This patch addresses the following problems:
- priv can never be NULL, so this part of the check is useless
- if the loop ran through the whole list, priv->client is invalid and
it is more appropriate and sufficient to check for the end of list_for_each_entry loop condition.

---
From static analysis. Not tested. ===========================END==========================================

Quoted 31 lines
Quoted 26 lines
Quoted 26 lines
---
  net/9p/trans_xen.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
net/9p/trans_xen.c +2 -2
index f4fea28e05da..3ec1a51a6944 100644
@@ -138,7 +138,7 @@ static bool p9_xen_write_todo(struct xen_9pfs_dataring *ring, RING_IDX size)
 
  static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
  {
-	struct xen_9pfs_front_priv *priv = NULL;
+	struct xen_9pfs_front_priv *priv;
  	RING_IDX cons, prod, masked_cons, masked_prod;
  	unsigned long flags;
  	u32 size = p9_req->tc.size;
@@ -151,7 +151,7 @@ static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
  			break;
  	}
  	read_unlock(&xen_9pfs_lock);
-	if (!priv || priv->client != client)
+	if (list_entry_is_head(priv, &xen_9pfs_devs, list))
  		return -EINVAL;
 
  	num = p9_req->tc.tag % priv->num_rings;
#4 lore raw

Re: [External] : Re: [PATCH] 9p/xen: Fix end of loop tests for list_for_each_entry

Quoted 29 lines · Harshvardhan Jha
Quoted 18 lines · Stefano Stabellini
Quoted 9 lines · On Mon, 26 Jul 2021, [email protected] wrote:

Harshvardhan Jha wrote on Sun, Jul 25, 2021 at 11:21:03PM +0530:

Quoted 2 lines

The list_for_each_entry() iterator, "priv" in this code, can never be NULL so the warning would never be printed.

hm? priv won't be NULL but priv->client won't be client, so it will return -EINVAL alright in practice?

This does fix an invalid read after the list head, so there's a real bug, but the commit message needs fixing.

Agreed

Quoted 4 lines
Quoted 3 lines

---
From static analysis. Not tested.

Quoted 6 lines
Quoted 3 lines

+Stefano in To - I also can't test xen right now :/
This looks functional to me but if you have a bit of time to spare just a mount test can't hurt.

Yes, I did test it successfully. Aside from the commit messaged to be reworded:

How's this? ===========================BEGIN======================================== 9p/xen: Fix end of loop tests for list_for_each_entry

Quoted 7 lines

This patch addresses the following problems:
- priv can never be NULL, so this part of the check is useless
- if the loop ran through the whole list, priv->client is invalid and
it is more appropriate and sufficient to check for the end of list_for_each_entry loop condition.

That's fine

Quoted 36 lines

---
From static analysis. Not tested. ===========================END==========================================

Quoted 32 lines
Quoted 27 lines
Quoted 27 lines
---
  net/9p/trans_xen.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
net/9p/trans_xen.c +1 -1
index f4fea28e05da..3ec1a51a6944 100644
@@ -138,7 +138,7 @@ static bool p9_xen_write_todo(struct
 xen_9pfs_dataring *ring, RING_IDX size)
    static int p9_xen_request(struct p9_client *client, struct p9_req_t
 *p9_req)
  {
-	struct xen_9pfs_front_priv *priv = NULL;
+	struct xen_9pfs_front_priv *priv;
  	RING_IDX cons, prod, masked_cons, masked_prod;
  	unsigned long flags;
  	u32 size = p9_req->tc.size;
net/9p/trans_xen.c +1 -1
@@ -151,7 +151,7 @@ static int p9_xen_request(struct p9_client *client,
 struct p9_req_t *p9_req)
  			break;
  	}
  	read_unlock(&xen_9pfs_lock);
-	if (!priv || priv->client != client)
+	if (list_entry_is_head(priv, &xen_9pfs_devs, list))
  		return -EINVAL;
    	num = p9_req->tc.tag % priv->num_rings;
#5 lore raw

Re: [External] : Re: [PATCH] 9p/xen: Fix end of loop tests for list_for_each_entry

Stefano Stabellini wrote on Mon, Jul 26, 2021 at 04:54:29PM -0700:

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

Yes, I did test it successfully. Aside from the commit messaged to be reworded:

How's this? ===========================BEGIN======================================== 9p/xen: Fix end of loop tests for list_for_each_entry

Quoted 7 lines
Quoted 7 lines

This patch addresses the following problems:
- priv can never be NULL, so this part of the check is useless
- if the loop ran through the whole list, priv->client is invalid and
it is more appropriate and sufficient to check for the end of list_for_each_entry loop condition.

Will take the patch with this text as commit message later tonight

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Thanks for the test!

Signature · 1 line
Dominique
#6 lore raw

Re: [External] : Re: [PATCH] 9p/xen: Fix end of loop tests for list_for_each_entry

Quoted 13 lines · On 27/07/21 5:29 am, [email protected] wrote:

Stefano Stabellini wrote on Mon, Jul 26, 2021 at 04:54:29PM -0700:

Quoted 12 lines
Quoted 12 lines
Quoted 2 lines

Yes, I did test it successfully. Aside from the commit messaged to be reworded:

How's this? ===========================BEGIN======================================== 9p/xen: Fix end of loop tests for list_for_each_entry

This patch addresses the following problems:
- priv can never be NULL, so this part of the check is useless
- if the loop ran through the whole list, priv->client is invalid and
it is more appropriate and sufficient to check for the end of list_for_each_entry loop condition.

Quoted 3 lines
Quoted 1 line
Quoted 1 line

Will take the patch with this text as commit message later tonight

If you want I can resend the patch with this commit message

Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Thanks for the test!

#7 lore raw

PATCH 1/1 9p/xen: Fix end of loop tests for list_for_each_entry

In reply to Stefano Stabellini

This patch addresses the following problems:
- priv can never be NULL, so this part of the check is useless
- if the loop ran through the whole list, priv->client is invalid and
it is more appropriate and sufficient to check for the end of list_for_each_entry loop condition.

---
 net/9p/trans_xen.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/9p/trans_xen.c +2 -2
index f4fea28e05da..3ec1a51a6944 100644
@@ -138,7 +138,7 @@ static bool p9_xen_write_todo(struct xen_9pfs_dataring *ring, RING_IDX size)
 
 static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
 {
-	struct xen_9pfs_front_priv *priv = NULL;
+	struct xen_9pfs_front_priv *priv;
 	RING_IDX cons, prod, masked_cons, masked_prod;
 	unsigned long flags;
 	u32 size = p9_req->tc.size;
@@ -151,7 +151,7 @@ static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
 			break;
 	}
 	read_unlock(&xen_9pfs_lock);
-	if (!priv || priv->client != client)
+	if (list_entry_is_head(priv, &xen_9pfs_devs, list))
 		return -EINVAL;
 
 	num = p9_req->tc.tag % priv->num_rings;
Signature · 1 line
2.32.0
#8 lore raw

Harshvardhan Jha wrote on Tue, Jul 27, 2021 at 05:37:10AM +0530:

Quoted 7 lines

This patch addresses the following problems:
- priv can never be NULL, so this part of the check is useless
- if the loop ran through the whole list, priv->client is invalid and
it is more appropriate and sufficient to check for the end of list_for_each_entry loop condition.

Alright, taken and pushed to linux-next.
I'll send it to Linus next week-ish

FWIW, this isn't a merge so messing with the commit message is fine and you didn't really need to resend (either is fine), but if you do for next time please tag in the subject it's a v2 (e.g. [PATCH v2]), optionally with a changelog below the three dashes that won't be included in the final commit message (not really useful here as we discussed the change just before, but for bigger subsystems it can help)

Signature · 1 line
Dominique