Skip to contentSkip to conversation

tipc: Fix end of loop tests for list_for_each_entry()

PATCH Applied Dan Carpenter 3 participants 3 messages Feb 22, 2022 – Feb 24, 2022

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

PATCH 1/1 tipc: Fix end of loop tests for list_for_each_entry()

These tests are supposed to check if the loop exited via a break or not. However the tests are wrong because if we did not exit via a break then "p" is not a valid pointer. In that case, it's the equivalent of "if (*(u32 *)sr == *last_key) {". That's going to work most of the time, but there is a potential for those to be equal.

Fixes 1593123a6a49 ("tipc: add name table dump to new netlink api")
Fixes 1a1a143daf84 ("tipc: add publication dump to new netlink api")
---
 net/tipc/name_table.c | 2 +-
 net/tipc/socket.c     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
net/tipc/name_table.c +1 -1
index 01396dd1c899..1d8ba233d047 100644
@@ -967,7 +967,7 @@ static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
 		list_for_each_entry(p, &sr->all_publ, all_publ)
 			if (p->key == *last_key)
 				break;
-		if (p->key != *last_key)
+		if (list_entry_is_head(p, &sr->all_publ, all_publ))
 			return -EPIPE;
 	} else {
 		p = list_first_entry(&sr->all_publ,
net/tipc/socket.c +1 -1
index 3e63c83e641c..7545321c3440 100644
@@ -3749,7 +3749,7 @@ static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
 			if (p->key == *last_publ)
 				break;
 		}
-		if (p->key != *last_publ) {
+		if (list_entry_is_head(p, &tsk->publications, binding_sock)) {
 			/* We never set seq or call nl_dump_check_consistent()
 			 * this means that setting prev_seq here will cause the
 			 * consistence check to fail in the netlink callback
Signature · 1 line
2.20.1
#1 lore raw
This patch was applied to netdev/net.git (master) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net.git (master)
by David S. Miller <[email protected]>:

Quoted 11 lines · On Tue, 22 Feb 2022 16:43:12 +0300 you wrote:

These tests are supposed to check if the loop exited via a break or not. However the tests are wrong because if we did not exit via a break then "p" is not a valid pointer. In that case, it's the equivalent of "if (*(u32 *)sr == *last_key) {". That's going to work most of the time, but there is a potential for those to be equal.

Fixes 1593123a6a49 ("tipc: add name table dump to new netlink api")
Fixes 1a1a143daf84 ("tipc: add publication dump to new netlink api")

[...]

Here is the summary with links:
- [net] tipc: Fix end of loop tests for list_for_each_entry()

    https://git.kernel.org/netdev/net/c/a1f8fec4dac8

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Dan Carpenter
Quoted 32 lines · Dan Carpenter

These tests are supposed to check if the loop exited via a break or not. However the tests are wrong because if we did not exit via a break then "p" is not a valid pointer. In that case, it's the equivalent of "if (*(u32 *)sr == *last_key) {". That's going to work most of the time, but there is a potential for those to be equal.

Fixes 1593123a6a49 ("tipc: add name table dump to new netlink api")
Fixes 1a1a143daf84 ("tipc: add publication dump to new netlink api")
---
  net/tipc/name_table.c | 2 +-
  net/tipc/socket.c     | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
net/tipc/name_table.c +1 -1
index 01396dd1c899..1d8ba233d047 100644
@@ -967,7 +967,7 @@ static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
  		list_for_each_entry(p, &sr->all_publ, all_publ)
  			if (p->key == *last_key)
  				break;
-		if (p->key != *last_key)
+		if (list_entry_is_head(p, &sr->all_publ, all_publ))
  			return -EPIPE;
  	} else {
  		p = list_first_entry(&sr->all_publ,
net/tipc/socket.c +0 -0
index 3e63c83e641c..7545321c3440 100644

net/tipc/socket.c static int __tipc_nl_list_sk_publ(struct sk_buff *skb, · line 3755

Quoted 8 lines
net/tipc/socket.c +1 -1
@@ -3749,7 +3749,7 @@ static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
  			if (p->key == *last_publ)
  				break;
  		}
-		if (p->key != *last_publ) {
+		if (list_entry_is_head(p, &tsk->publications, binding_sock)) {
  			/* We never set seq or call nl_dump_check_consistent()
  			 * this means that setting prev_seq here will cause the
  			 * consistence check to fail in the netlink callback