Skip to contentSkip to conversation

i2c: Fix end of loop test in i2c_atr_find_mapping_by_addr()

PATCH Applied Dan Carpenter 5 participants 7 messages Apr 23, 2025 – May 2, 2025

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

PATCH 1/1 i2c: Fix end of loop test in i2c_atr_find_mapping_by_addr()

When the list_for_each_entry_reverse() exits without hitting a break then the list cursor points to invalid memory. So this check for if (c2a->fixed) is checking bogus memory. Fix it by using a "found" variable to track if we found what we were looking for or not.

Fixes c3f55241882b ("i2c: Support dynamic address translation")
---
 drivers/i2c/i2c-atr.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/i2c/i2c-atr.c +7 -3
index d5aa6738370c..1aeaecacc26c 100644
@@ -240,6 +240,7 @@ i2c_atr_find_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 	struct i2c_atr *atr = chan->atr;
 	struct i2c_atr_alias_pair *c2a;
 	struct list_head *alias_pairs;
+	bool found = false;
 	u16 alias;
 	int ret;
 
@@ -258,11 +259,14 @@ i2c_atr_find_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 		if (unlikely(list_empty(alias_pairs)))
 			return NULL;
 
-		list_for_each_entry_reverse(c2a, alias_pairs, node)
-			if (!c2a->fixed)
+		list_for_each_entry_reverse(c2a, alias_pairs, node) {
+			if (!c2a->fixed) {
+				found = true;
 				break;
+			}
+		}
 
-		if (c2a->fixed)
+		if (!found)
 			return NULL;
 
 		atr->ops->detach_addr(atr, chan->chan_id, c2a->addr);
Signature · 1 line
2.47.2
#1 lore raw

Hello Dan,

Quoted 4 lines · On Wednesday, 23 April 2025 10:21:18 CEST Dan Carpenter wrote:

When the list_for_each_entry_reverse() exits without hitting a break then the list cursor points to invalid memory. So this check for if (c2a->fixed) is checking bogus memory. Fix it by using a "found" variable to track if we found what we were looking for or not.

IIUC the for loop ending condition in list_for_each_entry_reverse() is "!list_entry_is_head(pos, head, member);", so even if the loop runs to completion, the pointer should still be valid right?

Thanks,

Signature · 3 lines
Romain Gantois, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#2 lore raw
Quoted 12 lines · Romain Gantois

Hello Dan,

Quoted 4 lines · On Wednesday, 23 April 2025 10:21:18 CEST Dan Carpenter wrote:

When the list_for_each_entry_reverse() exits without hitting a break then the list cursor points to invalid memory. So this check for if (c2a->fixed) is checking bogus memory. Fix it by using a "found" variable to track if we found what we were looking for or not.

IIUC the for loop ending condition in list_for_each_entry_reverse() is "!list_entry_is_head(pos, head, member);", so even if the loop runs to completion, the pointer should still be valid right?

head is &chan->alias_pairs. pos is an offset off the head. In this case, the offset is zero. So it's &chan->alias_pairs minus zero.

So we exit the list with c2a = (void *)&chan->alias_pairs.

If you look how struct i2c_atr_chan is declareted the next struct member after alias_pairs is:

	struct i2c_atr_alias_pool *alias_pool;

So if (c2a->fixed) is poking around in the alias_pool pointer. It's not out of bounds but it's not valid either.

regards,
dan carpenter

maintainer Reviewed-by
#3 lore raw

Hi,

Quoted 19 lines · Dan Carpenter
Quoted 12 lines · Romain Gantois

Hello Dan,

Quoted 4 lines · On Wednesday, 23 April 2025 10:21:18 CEST Dan Carpenter wrote:

When the list_for_each_entry_reverse() exits without hitting a break then the list cursor points to invalid memory. So this check for if (c2a->fixed) is checking bogus memory. Fix it by using a "found" variable to track if we found what we were looking for or not.

IIUC the for loop ending condition in list_for_each_entry_reverse() is "!list_entry_is_head(pos, head, member);", so even if the loop runs to completion, the pointer should still be valid right?

head is &chan->alias_pairs. pos is an offset off the head. In this case, the offset is zero. So it's &chan->alias_pairs minus zero.

So we exit the list with c2a = (void *)&chan->alias_pairs.

Quoted 7 lines

If you look how struct i2c_atr_chan is declareted the next struct member after alias_pairs is:

	struct i2c_atr_alias_pool *alias_pool;

So if (c2a->fixed) is poking around in the alias_pool pointer. It's not out of bounds but it's not valid either.

Maybe it's just me, but I had hard time following that explanation. So here's mine:

The list head (i2c_atr_chan.alias_pairs) is not a full entry, it's just a struct list_head. When the for loop runs to completion, c2a doesn't point to a struct i2c_atr_alias_pair, so you can't access c2a->fixed.

For the patch:

Tomi

Reviewed-by
#4 lore raw
Quoted 29 lines · On Thursday, 24 April 2025 08:32:22 CEST Tomi Valkeinen wrote:

Hi,

Quoted 18 lines · Dan Carpenter
Quoted 11 lines · Romain Gantois

Hello Dan,

Quoted 4 lines · On Wednesday, 23 April 2025 10:21:18 CEST Dan Carpenter wrote:

When the list_for_each_entry_reverse() exits without hitting a break then the list cursor points to invalid memory. So this check for if (c2a->fixed) is checking bogus memory. Fix it by using a "found" variable to track if we found what we were looking for or not.

IIUC the for loop ending condition in list_for_each_entry_reverse() is "!list_entry_is_head(pos, head, member);", so even if the loop runs to completion, the pointer should still be valid right?

head is &chan->alias_pairs. pos is an offset off the head. In this case, the offset is zero. So it's &chan->alias_pairs minus zero.

So we exit the list with c2a = (void *)&chan->alias_pairs.

Quoted 7 lines

If you look how struct i2c_atr_chan is declareted the next struct member

after alias_pairs is:

	struct i2c_atr_alias_pool *alias_pool;

So if (c2a->fixed) is poking around in the alias_pool pointer. It's not out of bounds but it's not valid either.

Quoted 6 lines

Maybe it's just me, but I had hard time following that explanation. So here's mine:

The list head (i2c_atr_chan.alias_pairs) is not a full entry, it's just a struct list_head. When the for loop runs to completion, c2a doesn't point to a struct i2c_atr_alias_pair, so you can't access c2a->fixed.

Ah I see, in that case thanks for the fix Dan!

reviewer Reviewed-by
#5 lore raw
Quoted 33 lines · On Thu, 24 Apr 2025 09:10:43 +0200 Romain Gantois <[email protected]>…
Quoted 32 lines · On Thursday, 24 April 2025 08:32:22 CEST Tomi Valkeinen wrote:

Hi,

Quoted 18 lines · Dan Carpenter
Quoted 11 lines · Romain Gantois

Hello Dan,

Quoted 4 lines · On Wednesday, 23 April 2025 10:21:18 CEST Dan Carpenter wrote:

When the list_for_each_entry_reverse() exits without hitting a break then the list cursor points to invalid memory. So this check for if (c2a->fixed) is checking bogus memory. Fix it by using a "found" variable to track if we found what we were looking for or not.

IIUC the for loop ending condition in list_for_each_entry_reverse() is "!list_entry_is_head(pos, head, member);", so even if the loop runs to completion, the pointer should still be valid right?

head is &chan->alias_pairs. pos is an offset off the head. In this case, the offset is zero. So it's &chan->alias_pairs minus zero.

So we exit the list with c2a = (void *)&chan->alias_pairs.

Quoted 7 lines

If you look how struct i2c_atr_chan is declareted the next struct member

after alias_pairs is:

	struct i2c_atr_alias_pool *alias_pool;

So if (c2a->fixed) is poking around in the alias_pool pointer. It's not out of bounds but it's not valid either.

Maybe it's just me, but I had hard time following that explanation. So here's mine:

Quoted 7 lines
Quoted 3 lines

The list head (i2c_atr_chan.alias_pairs) is not a full entry, it's just a struct list_head. When the for loop runs to completion, c2a doesn't point to a struct i2c_atr_alias_pair, so you can't access c2a->fixed.

Ah I see, in that case thanks for the fix Dan!

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Applied
#6 lore raw
In reply to Dan Carpenter
Quoted 7 lines · Dan Carpenter

When the list_for_each_entry_reverse() exits without hitting a break then the list cursor points to invalid memory. So this check for if (c2a->fixed) is checking bogus memory. Fix it by using a "found" variable to track if we found what we were looking for or not.

Fixes c3f55241882b ("i2c: Support dynamic address translation")

Applied to for-next with Tomi's description added, thanks!