Skip to contentSkip to conversation

rose: fix dangling neighbour pointers in rose_rt_device_down()

PATCH Superseded Kohei Enju 2 participants 5 messages Jun 25, 2025 – Jun 28, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kohei Enju

author
#0 lore raw

PATCH 1/1 rose: fix dangling neighbour pointers in rose_rt_device_down()

There are two bugs in rose_rt_device_down() that can lead to use-after-free:

1. The loop bound t->count is modified within the loop, which can cause the loop to terminate early and miss some entries.

2. When removing an entry from the neighbour array, the subsequent entries are moved up to fill the gap, but the loop index i is still incremented, causing the next entry to be skipped.

For example, if a node has three neighbours (A, B, A) and A is being removed:
- 1st iteration (i=0): A is removed, array becomes (B, A, A), count=2
- 2nd iteration (i=1): We now check A instead of B, skipping B entirely
- 3rd iteration (i=2): Loop terminates early due to count=2

This leaves the second A in the array with count=2, but the rose_neigh structure has been freed. Accessing code assumes that the first count entries are valid pointers, causing a use-after-free when it accesses the dangling pointer.

Fix both issues by iterating over the array in reverse order with a fixed loop bound. This ensures that all entries are examined and that the removal of an entry doesn't affect the iteration of subsequent entries.

---
 net/rose/rose_route.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
net/rose/rose_route.c +4 -12
index 2dd6bd3a3011..a488fd8c4710 100644
@@ -479,7 +479,7 @@ void rose_rt_device_down(struct net_device *dev)
 {
 	struct rose_neigh *s, *rose_neigh;
 	struct rose_node  *t, *rose_node;
-	int i;
+	int i, j;
 
 	spin_lock_bh(&rose_node_list_lock);
 	spin_lock_bh(&rose_neigh_list_lock);
@@ -497,22 +497,14 @@ void rose_rt_device_down(struct net_device *dev)
 			t         = rose_node;
 			rose_node = rose_node->next;
 
-			for (i = 0; i < t->count; i++) {
+			for (i = t->count - 1; i >= 0; i--) {
 				if (t->neighbour[i] != s)
 					continue;
 
 				t->count--;
 
-				switch (i) {
-				case 0:
-					t->neighbour[0] = t->neighbour[1];
-					fallthrough;
-				case 1:
-					t->neighbour[1] = t->neighbour[2];
-					break;
-				case 2:
-					break;
-				}
+				for (j = i; j < t->count; j++)
+					t->neighbour[j] = t->neighbour[j + 1];
 			}
 
 			if (t->count <= 0)
Signature · 1 line
2.48.1
author
#1 lore raw

The example ([Senario2] below) in the commit message was incorrect. Correctly, UAF will happen in the [Senario1] below.

Let me clarify those senarios.

When the entries to be removed (A) are consecutive, the second A is not checked, leading to UAF.
[Senario1]

    (A, A, B) with count=3
    i=0: 
         (A, A, B) -> (A, B) with count=2
          ^ checked
    i=1: 
         (A, B) -> (A, B) with count=2
             ^ checked (B, not A!)
    i=2: (doesn't occur because i < count is false)
    ===> A remains with count=2 although A was freed, so UAF will happen.

When the entries to be removed (A) are not consecutive, all A entries are removed luckily.
[Senario2]

    (A, B, A) with count=3
    i=0: 
         (A, B, A) -> (B, A) with count=2
          ^ checked
    i=1: 
         (B, A) -> (B) with count=1
             ^ checked (A, not B)
    i=2: (doesn't occur because i < count is false)
    ===> No A remains. No UAF in this case.

Although, even in the senario2, the fundamental issue remains because B is never checked.
The fix addresses issues by preventing unintended skips.

Please let me know if I'm overlooking something or my understanding is incorrect.
Thanks!

author
#2 lore raw
In reply to Kohei Enju
Quoted 18 lines

Message-ID: <[email protected]> (raw)

There are two bugs in rose_rt_device_down() that can lead to use-after-free:

1. The loop bound t->count is modified within the loop, which can cause the loop to terminate early and miss some entries.

2. When removing an entry from the neighbour array, the subsequent entries are moved up to fill the gap, but the loop index i is still incremented, causing the next entry to be skipped.

For example, if a node has three neighbours (A, B, A) and A is being removed:
- 1st iteration (i=0): A is removed, array becomes (B, A, A), count=2
- 2nd iteration (i=1): We now check A instead of B, skipping B entirely
- 3rd iteration (i=2): Loop terminates early due to count=2

Quoted 4 lines

This leaves the second A in the array with count=2, but the rose_neigh structure has been freed. Accessing code assumes that the first count entries are valid pointers, causing a use-after-free when it accesses the dangling pointer.

(Resending because I forgot to cite the patch, please ignore the former reply from me. Sorry for messing up.)

The example ([Senario2] below) in the commit message was incorrect. Correctly, UAF will happen in the [Senario1] below.

Let me clarify those senarios.

When the entries to be removed (A) are consecutive, the second A is not checked, leading to UAF.
[Senario1]

    (A, A, B) with count=3
    i=0: 
         (A, A, B) -> (A, B) with count=2
          ^ checked
    i=1: 
         (A, B) -> (A, B) with count=2
             ^ checked (B, not A!)
    i=2: (doesn't occur because i < count is false)
    ===> A remains with count=2 although A was freed, so UAF will happen.

When the entries to be removed (A) are not consecutive, all A entries are removed luckily.
[Senario2]

    (A, B, A) with count=3
    i=0: 
         (A, B, A) -> (B, A) with count=2
          ^ checked
    i=1: 
         (B, A) -> (B) with count=1
             ^ checked (A, not B)
    i=2: (doesn't occur because i < count is false)
    ===> No A remains. No UAF in this case.

Although, even in the senario2, the fundamental issue remains because B is never checked.
The fix addresses issues by preventing unintended skips.

Please let me know if I'm overlooking something or my understanding is incorrect.
Thanks!

Quoted 54 lines

Fix both issues by iterating over the array in reverse order with a fixed loop bound. This ensures that all entries are examined and that the removal of an entry doesn't affect the iteration of subsequent entries.

---
 net/rose/rose_route.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
net/rose/rose_route.c +4 -12
index 2dd6bd3a3011..a488fd8c4710 100644
@@ -479,7 +479,7 @@ void rose_rt_device_down(struct net_device *dev)
 {
 	struct rose_neigh *s, *rose_neigh;
 	struct rose_node  *t, *rose_node;
-	int i;
+	int i, j;
 
 	spin_lock_bh(&rose_node_list_lock);
 	spin_lock_bh(&rose_neigh_list_lock);
@@ -497,22 +497,14 @@ void rose_rt_device_down(struct net_device *dev)
 			t         = rose_node;
 			rose_node = rose_node->next;
 
-			for (i = 0; i < t->count; i++) {
+			for (i = t->count - 1; i >= 0; i--) {
 				if (t->neighbour[i] != s)
 					continue;
 
 				t->count--;
 
-				switch (i) {
-				case 0:
-					t->neighbour[0] = t->neighbour[1];
-					fallthrough;
-				case 1:
-					t->neighbour[1] = t->neighbour[2];
-					break;
-				case 2:
-					break;
-				}
+				for (j = i; j < t->count; j++)
+					t->neighbour[j] = t->neighbour[j + 1];
 			}
 
 			if (t->count <= 0)
Signature · 1 line
2.48.1
maintainer
#3 lore raw
Quoted 18 lines · Kohei Enju
Quoted 18 lines

Message-ID: <[email protected]> (raw)

There are two bugs in rose_rt_device_down() that can lead to use-after-free:

1. The loop bound t->count is modified within the loop, which can cause the loop to terminate early and miss some entries.

2. When removing an entry from the neighbour array, the subsequent entries are moved up to fill the gap, but the loop index i is still incremented, causing the next entry to be skipped.

For example, if a node has three neighbours (A, B, A) and A is being removed:
- 1st iteration (i=0): A is removed, array becomes (B, A, A), count=2
- 2nd iteration (i=1): We now check A instead of B, skipping B entirely
- 3rd iteration (i=2): Loop terminates early due to count=2

Quoted 7 lines
Quoted 4 lines

This leaves the second A in the array with count=2, but the rose_neigh structure has been freed. Accessing code assumes that the first count entries are valid pointers, causing a use-after-free when it accesses the dangling pointer.

(Resending because I forgot to cite the patch, please ignore the former reply from me. Sorry for messing up.)

This resend was not needed.

Quoted 2 lines

The example ([Senario2] below) in the commit message was incorrect.

Please send an updated version of the patch including the correct description in the commit message.

[...]

Quoted 11 lines
Quoted 11 lines
net/rose/rose_route.c +1 -1
@@ -497,22 +497,14 @@ void rose_rt_device_down(struct net_device *dev)
 			t         = rose_node;
 			rose_node = rose_node->next;
 
-			for (i = 0; i < t->count; i++) {
+			for (i = t->count - 1; i >= 0; i--) {
 				if (t->neighbour[i] != s)
 					continue;
 
 				t->count--;
 
Quoted 12 lines

net/rose/rose_route.c void rose_rt_device_down(struct net_device *dev) · line 507

Quoted 12 lines
net/rose/rose_route.c +2 -10
-				switch (i) {
-				case 0:
-					t->neighbour[0] = t->neighbour[1];
-					fallthrough;
-				case 1:
-					t->neighbour[1] = t->neighbour[2];
-					break;
-				case 2:
-					break;
-				}
+				for (j = i; j < t->count; j++)
+					t->neighbour[j] = t->neighbour[j + 1];

You can possibly use memmove() here instead of adding another loop.

/P

author
#4 lore raw
Quoted 27 lines

Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

Quoted 23 lines · Kohei Enju
Quoted 22 lines

Message-ID: <[email protected]> (raw)

There are two bugs in rose_rt_device_down() that can lead to use-after-free:

1. The loop bound t->count is modified within the loop, which can cause the loop to terminate early and miss some entries.

2. When removing an entry from the neighbour array, the subsequent entries are moved up to fill the gap, but the loop index i is still incremented, causing the next entry to be skipped.

For example, if a node has three neighbours (A, B, A) and A is being removed:
- 1st iteration (i=0): A is removed, array becomes (B, A, A), count=2
- 2nd iteration (i=1): We now check A instead of B, skipping B entirely
- 3rd iteration (i=2): Loop terminates early due to count=2

This leaves the second A in the array with count=2, but the rose_neigh structure has been freed. Accessing code assumes that the first count entries are valid pointers, causing a use-after-free when it accesses the dangling pointer.

Quoted 4 lines
Quoted 2 lines

(Resending because I forgot to cite the patch, please ignore the former reply from me. Sorry for messing up.)

This resend was not needed.

Acknowledged.

Quoted 4 lines
Quoted 1 line

The example ([Senario2] below) in the commit message was incorrect.

Please send an updated version of the patch including the correct description in the commit message.

Sure, I'll update and send as V2.

Quoted 12 lines

[...]

Quoted 11 lines
Quoted 11 lines
net/rose/rose_route.c +1 -1
@@ -497,22 +497,14 @@ void rose_rt_device_down(struct net_device *dev)
 			t         = rose_node;
 			rose_node = rose_node->next;
 
-			for (i = 0; i < t->count; i++) {
+			for (i = t->count - 1; i >= 0; i--) {
 				if (t->neighbour[i] != s)
 					continue;
 
 				t->count--;
 
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
net/rose/rose_route.c +0 -4
-				switch (i) {
-				case 0:
-					t->neighbour[0] = t->neighbour[1];
-					fallthrough;
Quoted 8 lines

net/rose/rose_route.c void rose_rt_device_down(struct net_device *dev) · line 507

Quoted 8 lines
net/rose/rose_route.c +2 -6
-				case 1:
-					t->neighbour[1] = t->neighbour[2];
-					break;
-				case 2:
-					break;
-				}
+				for (j = i; j < t->count; j++)
+					t->neighbour[j] = t->neighbour[j + 1];

You can possibly use memmove() here instead of adding another loop.

That sounds like a good suggestion. Thank you for reviewing!

Quoted 1 line

/P

Regards,
Kohei