Skip to contentSkip to conversation

ipv4: fib: Fix fib_info_hash_alloc() allocation type

PATCH Applied Kees Cook 5 participants 7 messages Apr 26, 2025 – Apr 29, 2025

Subsystems (3)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 ipv4: fib: Fix fib_info_hash_alloc() allocation type

In preparation for making the kmalloc family of allocators type aware, we need to make sure that the returned type from the allocation matches the type of the variable being assigned. (Before, the allocator would always return "void *", which can be implicitly cast to any pointer type.)

This was allocating many sizeof(struct hlist_head *) when it actually wanted sizeof(struct hlist_head). Luckily these are the same size. Adjust the allocation type to match the assignment.

---
Cc: "David S. Miller" <[email protected]>
Cc: David Ahern <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: Simon Horman <[email protected]>
Cc: <[email protected]>

---
 net/ipv4/fib_semantics.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/fib_semantics.c +1 -1
index f68bb9e34c34..37d12b0bc6be 100644
@@ -365,7 +365,7 @@ static struct hlist_head *fib_info_laddrhash_bucket(const struct net *net,
 static struct hlist_head *fib_info_hash_alloc(unsigned int hash_bits)
 {
 	/* The second half is used for prefsrc */
-	return kvcalloc((1 << hash_bits) * 2, sizeof(struct hlist_head *),
+	return kvcalloc((1 << hash_bits) * 2, sizeof(struct hlist_head),
 			GFP_KERNEL);
 }
 
Signature · 1 line
2.34.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Kees Cook

In preparation for making the kmalloc family of allocators type aware, we need to make sure that the returned type from the allocation matches the type of the variable being assigned. (Before, the allocator would always return "void *", which can be implicitly cast to any pointer type.)

This was allocating many sizeof(struct hlist_head *) when it actually wanted sizeof(struct hlist_head). Luckily these are the same size. Adjust the allocation type to match the assignment.

Thanks, your analysis matches my understanding of the code.

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Kees Cook
Quoted 27 lines · Kees Cook

In preparation for making the kmalloc family of allocators type aware, we need to make sure that the returned type from the allocation matches the type of the variable being assigned. (Before, the allocator would always return "void *", which can be implicitly cast to any pointer type.)

This was allocating many sizeof(struct hlist_head *) when it actually wanted sizeof(struct hlist_head). Luckily these are the same size. Adjust the allocation type to match the assignment.

---
Cc: "David S. Miller" <[email protected]>
Cc: David Ahern <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: Simon Horman <[email protected]>
Cc: <[email protected]>

---
 net/ipv4/fib_semantics.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/fib_semantics.c +0 -0
index f68bb9e34c34..37d12b0bc6be 100644

net/ipv4/fib_semantics.c static struct hlist_head *fib_info_laddrhash_bucket(const struct net *net, · line 371

Quoted 8 lines
net/ipv4/fib_semantics.c +1 -1
@@ -365,7 +365,7 @@ static struct hlist_head *fib_info_laddrhash_bucket(const struct net *net,
 static struct hlist_head *fib_info_hash_alloc(unsigned int hash_bits)
 {
 	/* The second half is used for prefsrc */
-	return kvcalloc((1 << hash_bits) * 2, sizeof(struct hlist_head *),
+	return kvcalloc((1 << hash_bits) * 2, sizeof(struct hlist_head),
 			GFP_KERNEL);
 }
 
Fixes fa336adc100e ("ipv4: fib: Allocate fib_info_hash[] and

fib_info_laddrhash[] by kvcalloc().)

Reviewed-by
#3 lore raw

Thanks for CC me, David.

From: David Ahern <[email protected]>
Date: Mon, 28 Apr 2025 16:50:53 -0600

Quoted 39 lines · Kees Cook
Quoted 27 lines

In preparation for making the kmalloc family of allocators type aware, we need to make sure that the returned type from the allocation matches the type of the variable being assigned. (Before, the allocator would always return "void *", which can be implicitly cast to any pointer type.)

This was allocating many sizeof(struct hlist_head *) when it actually wanted sizeof(struct hlist_head). Luckily these are the same size. Adjust the allocation type to match the assignment.

---
Cc: "David S. Miller" <[email protected]>
Cc: David Ahern <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: Simon Horman <[email protected]>
Cc: <[email protected]>

---
 net/ipv4/fib_semantics.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/fib_semantics.c +0 -0
index f68bb9e34c34..37d12b0bc6be 100644

net/ipv4/fib_semantics.c static struct hlist_head *fib_info_laddrhash_bucket(const struct net *net, · line 371

Quoted 8 lines
net/ipv4/fib_semantics.c +1 -1
@@ -365,7 +365,7 @@ static struct hlist_head *fib_info_laddrhash_bucket(const struct net *net,
 static struct hlist_head *fib_info_hash_alloc(unsigned int hash_bits)
 {
 	/* The second half is used for prefsrc */
-	return kvcalloc((1 << hash_bits) * 2, sizeof(struct hlist_head *),
+	return kvcalloc((1 << hash_bits) * 2, sizeof(struct hlist_head),
 			GFP_KERNEL);
 }
 
Quoted 2 lines
Fixes fa336adc100e ("ipv4: fib: Allocate fib_info_hash[] and

fib_info_laddrhash[] by kvcalloc().)

I agree this should target net.git as the last statement will be false with LOCKDEP.

Thanks!

author
#4 lore raw
Quoted 43 lines · Kuniyuki Iwashima

Thanks for CC me, David.

From: David Ahern <[email protected]>
Date: Mon, 28 Apr 2025 16:50:53 -0600

Quoted 39 lines · Kees Cook
Quoted 27 lines

In preparation for making the kmalloc family of allocators type aware, we need to make sure that the returned type from the allocation matches the type of the variable being assigned. (Before, the allocator would always return "void *", which can be implicitly cast to any pointer type.)

This was allocating many sizeof(struct hlist_head *) when it actually wanted sizeof(struct hlist_head). Luckily these are the same size. Adjust the allocation type to match the assignment.

---
Cc: "David S. Miller" <[email protected]>
Cc: David Ahern <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: Simon Horman <[email protected]>
Cc: <[email protected]>

---
 net/ipv4/fib_semantics.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/fib_semantics.c +0 -0
index f68bb9e34c34..37d12b0bc6be 100644

net/ipv4/fib_semantics.c static struct hlist_head *fib_info_laddrhash_bucket(const struct net *net, · line 371

Quoted 8 lines
net/ipv4/fib_semantics.c +1 -1
@@ -365,7 +365,7 @@ static struct hlist_head *fib_info_laddrhash_bucket(const struct net *net,
 static struct hlist_head *fib_info_hash_alloc(unsigned int hash_bits)
 {
 	/* The second half is used for prefsrc */
-	return kvcalloc((1 << hash_bits) * 2, sizeof(struct hlist_head *),
+	return kvcalloc((1 << hash_bits) * 2, sizeof(struct hlist_head),
 			GFP_KERNEL);
 }
 
Quoted 5 lines
Quoted 2 lines
Fixes fa336adc100e ("ipv4: fib: Allocate fib_info_hash[] and

fib_info_laddrhash[] by kvcalloc().)

I agree this should target net.git as the last statement will be false with LOCKDEP.

Which will be false with lockdep? Unless I'm missing it, I think hlist_head is always pointer sized:

struct hlist_head {
	struct hlist_node *first;
};
Quoted 2 lines

Thanks!

Signature · 1 line
Kees Cook
#5 lore raw

From: Kees Cook <[email protected]>
Date: Mon, 28 Apr 2025 20:52:59 -0700

Quoted 44 lines · Kuniyuki Iwashima
Quoted 43 lines

Thanks for CC me, David.

From: David Ahern <[email protected]>
Date: Mon, 28 Apr 2025 16:50:53 -0600

Quoted 39 lines · Kees Cook
Quoted 27 lines

In preparation for making the kmalloc family of allocators type aware, we need to make sure that the returned type from the allocation matches the type of the variable being assigned. (Before, the allocator would always return "void *", which can be implicitly cast to any pointer type.)

This was allocating many sizeof(struct hlist_head *) when it actually wanted sizeof(struct hlist_head). Luckily these are the same size. Adjust the allocation type to match the assignment.

---
Cc: "David S. Miller" <[email protected]>
Cc: David Ahern <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: Simon Horman <[email protected]>
Cc: <[email protected]>

---
 net/ipv4/fib_semantics.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/fib_semantics.c +0 -0
index f68bb9e34c34..37d12b0bc6be 100644

net/ipv4/fib_semantics.c static struct hlist_head *fib_info_laddrhash_bucket(const struct net *net, · line 371

Quoted 8 lines
net/ipv4/fib_semantics.c +1 -1
@@ -365,7 +365,7 @@ static struct hlist_head *fib_info_laddrhash_bucket(const struct net *net,
 static struct hlist_head *fib_info_hash_alloc(unsigned int hash_bits)
 {
 	/* The second half is used for prefsrc */
-	return kvcalloc((1 << hash_bits) * 2, sizeof(struct hlist_head *),
+	return kvcalloc((1 << hash_bits) * 2, sizeof(struct hlist_head),
 			GFP_KERNEL);
 }
 
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Fixes fa336adc100e ("ipv4: fib: Allocate fib_info_hash[] and

fib_info_laddrhash[] by kvcalloc().)

I agree this should target net.git as the last statement will be false with LOCKDEP.

Which will be false with lockdep? Unless I'm missing it, I think hlist_head is always pointer sized:

Oh sorry, now I'm not sure why I mentioned lockdep... maybe confused with other code :/

Quoted 4 lines
struct hlist_head {
	struct hlist_node *first;
};
#6 lore raw
In reply to Kees Cook
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Fri, 25 Apr 2025 23:05:30 -0700 you wrote:

In preparation for making the kmalloc family of allocators type aware, we need to make sure that the returned type from the allocation matches the type of the variable being assigned. (Before, the allocator would always return "void *", which can be implicitly cast to any pointer type.)

This was allocating many sizeof(struct hlist_head *) when it actually wanted sizeof(struct hlist_head). Luckily these are the same size. Adjust the allocation type to match the assignment.

[...]

Here is the summary with links:
- ipv4: fib: Fix fib_info_hash_alloc() allocation type

    https://git.kernel.org/netdev/net-next/c/fca6170f5a03

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html