Skip to contentSkip to conversation

net: ipv4: Fix uninitialized pointer warning in fnhe_remove_oldest

PATCH In review Purva Yeshi 3 participants 5 messages Apr 17, 2025 – Apr 18, 2025

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

PATCH 1/1 net: ipv4: Fix uninitialized pointer warning in fnhe_remove_oldest

Fix Smatch-detected issue:
net/ipv4/route.c:605 fnhe_remove_oldest() error:
uninitialized symbol 'oldest_p'.

Initialize oldest_p to NULL to avoid uninitialized pointer warning in fnhe_remove_oldest.

Check that oldest_p is not NULL after the loop to ensure no dereferencing of uninitialized pointers.

---
 net/ipv4/route.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/ipv4/route.c +7 -4
index 753704f75b2c..2e5159127cb9 100644
@@ -587,7 +587,7 @@ static void fnhe_flush_routes(struct fib_nh_exception *fnhe)
 
 static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash)
 {
-	struct fib_nh_exception __rcu **fnhe_p, **oldest_p;
+	struct fib_nh_exception __rcu **fnhe_p, **oldest_p = NULL;
 	struct fib_nh_exception *fnhe, *oldest = NULL;
 
 	for (fnhe_p = &hash->chain; ; fnhe_p = &fnhe->fnhe_next) {
@@ -601,9 +601,12 @@ static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash)
 			oldest_p = fnhe_p;
 		}
 	}
-	fnhe_flush_routes(oldest);
-	*oldest_p = oldest->fnhe_next;
-	kfree_rcu(oldest, rcu);
+
+	if (oldest_p) {  /* Ensure to have valid oldest_p element */
+		fnhe_flush_routes(oldest);
+		*oldest_p = oldest->fnhe_next;
+		kfree_rcu(oldest, rcu);
+	}
 }
 
 static u32 fnhe_hashfun(__be32 daddr)
Signature · 1 line
2.34.1
#1 lore raw

From: Purva Yeshi <[email protected]>
Date: Thu, 17 Apr 2025 15:11:26 +0530

Quoted 6 lines

Fix Smatch-detected issue:
net/ipv4/route.c:605 fnhe_remove_oldest() error:
uninitialized symbol 'oldest_p'.

Initialize oldest_p to NULL to avoid uninitialized pointer warning in fnhe_remove_oldest.

How does it remain uninitialised ?

update_or_create_fnhe() ensures the bucket is not empty before calling fnhe_remove_oldest().

Quoted 39 lines

Check that oldest_p is not NULL after the loop to ensure no dereferencing of uninitialized pointers.

---
 net/ipv4/route.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/ipv4/route.c +7 -4
index 753704f75b2c..2e5159127cb9 100644
@@ -587,7 +587,7 @@ static void fnhe_flush_routes(struct fib_nh_exception *fnhe)
 
 static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash)
 {
-	struct fib_nh_exception __rcu **fnhe_p, **oldest_p;
+	struct fib_nh_exception __rcu **fnhe_p, **oldest_p = NULL;
 	struct fib_nh_exception *fnhe, *oldest = NULL;
 
 	for (fnhe_p = &hash->chain; ; fnhe_p = &fnhe->fnhe_next) {
@@ -601,9 +601,12 @@ static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash)
 			oldest_p = fnhe_p;
 		}
 	}
-	fnhe_flush_routes(oldest);
-	*oldest_p = oldest->fnhe_next;
-	kfree_rcu(oldest, rcu);
+
+	if (oldest_p) {  /* Ensure to have valid oldest_p element */
+		fnhe_flush_routes(oldest);
+		*oldest_p = oldest->fnhe_next;
+		kfree_rcu(oldest, rcu);
+	}
 }
 
 static u32 fnhe_hashfun(__be32 daddr)
maintainer
#2 lore raw
Quoted 6 lines · Kuniyuki Iwashima

From: Purva Yeshi <[email protected]>
Date: Thu, 17 Apr 2025 15:11:26 +0530

Quoted 4 lines

Fix Smatch-detected issue:
net/ipv4/route.c:605 fnhe_remove_oldest() error:
uninitialized symbol 'oldest_p'.

Quoted 8 lines
Quoted 2 lines

Initialize oldest_p to NULL to avoid uninitialized pointer warning in fnhe_remove_oldest.

How does it remain uninitialised ?

update_or_create_fnhe() ensures the bucket is not empty before calling fnhe_remove_oldest().

agreed. Not the simplest logic, but I do not see how oldest_p can be unset after the loop.

#3 lore raw
Quoted 10 lines · David Ahern
Quoted 9 lines · Kuniyuki Iwashima

From: Purva Yeshi <[email protected]>
Date: Thu, 17 Apr 2025 15:11:26 +0530

Quoted 6 lines

Fix Smatch-detected issue:
net/ipv4/route.c:605 fnhe_remove_oldest() error:
uninitialized symbol 'oldest_p'.

Initialize oldest_p to NULL to avoid uninitialized pointer warning in fnhe_remove_oldest.

Quoted 8 lines
Quoted 5 lines

How does it remain uninitialised ?

update_or_create_fnhe() ensures the bucket is not empty before calling fnhe_remove_oldest().

agreed. Not the simplest logic, but I do not see how oldest_p can be unset after the loop.

Hi David,

The loop always sets oldest_p when the list has at least one entry, which the caller guarantees. Smatch doesn't catch that context, so it flags a false positive.

Best regards,
Purva

#4 lore raw
In reply to Kuniyuki Iwashima
Quoted 6 lines · Kuniyuki Iwashima

From: Purva Yeshi <[email protected]>
Date: Thu, 17 Apr 2025 15:11:26 +0530

Quoted 4 lines

Fix Smatch-detected issue:
net/ipv4/route.c:605 fnhe_remove_oldest() error:
uninitialized symbol 'oldest_p'.

Quoted 7 lines
Quoted 2 lines

Initialize oldest_p to NULL to avoid uninitialized pointer warning in fnhe_remove_oldest.

How does it remain uninitialised ?

update_or_create_fnhe() ensures the bucket is not empty before calling fnhe_remove_oldest().

Hi Kuniyuki,

Thanks for the feedback.

Smatch reports this because oldest_p is conditionally assigned inside the loop, and it doesn't reason about the guarantees made by update_or_create_fnhe() (i.e., that the list is non-empty). So it flags oldest_p as potentially uninitialized when dereferenced.

I understand your point, logically, the code is safe, and the warning is a false positive.

Quoted 41 lines
Quoted 41 lines

Check that oldest_p is not NULL after the loop to ensure no dereferencing of uninitialized pointers.

---
  net/ipv4/route.c | 11 +++++++----
  1 file changed, 7 insertions(+), 4 deletions(-)
net/ipv4/route.c +7 -4
index 753704f75b2c..2e5159127cb9 100644
@@ -587,7 +587,7 @@ static void fnhe_flush_routes(struct fib_nh_exception *fnhe)
 
  static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash)
  {
-	struct fib_nh_exception __rcu **fnhe_p, **oldest_p;
+	struct fib_nh_exception __rcu **fnhe_p, **oldest_p = NULL;
  	struct fib_nh_exception *fnhe, *oldest = NULL;
 
  	for (fnhe_p = &hash->chain; ; fnhe_p = &fnhe->fnhe_next) {
@@ -601,9 +601,12 @@ static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash)
  			oldest_p = fnhe_p;
  		}
  	}
-	fnhe_flush_routes(oldest);
-	*oldest_p = oldest->fnhe_next;
-	kfree_rcu(oldest, rcu);
+
+	if (oldest_p) {  /* Ensure to have valid oldest_p element */
+		fnhe_flush_routes(oldest);
+		*oldest_p = oldest->fnhe_next;
+		kfree_rcu(oldest, rcu);
+	}
  }
 
  static u32 fnhe_hashfun(__be32 daddr)