Skip to contentSkip to conversation

pds_core: Allocate pdsc_viftype_defaults copy with ARRAY_SIZE()

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

Subsystems (3)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 pds_core: Allocate pdsc_viftype_defaults copy with ARRAY_SIZE()

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 is allocating a copy of pdsc_viftype_defaults, which is an array of struct pdsc_viftype. To correctly return "struct pdsc_viftype *" in the future, adjust the allocation to allocating ARRAY_SIZE-many entries. The resulting allocation size is the same.

---
Cc: Shannon Nelson <[email protected]>
Cc: Brett Creeley <[email protected]>
Cc: Andrew Lunn <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: <[email protected]>

---
 drivers/net/ethernet/amd/pds_core/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/ethernet/amd/pds_core/core.c +2 -1
index 1eb0d92786f7..451b005d48d0 100644
@@ -415,7 +415,8 @@ static int pdsc_viftypes_init(struct pdsc *pdsc)
 {
 	enum pds_core_vif_types vt;
 
-	pdsc->viftype_status = kzalloc(sizeof(pdsc_viftype_defaults),
+	pdsc->viftype_status = kcalloc(ARRAY_SIZE(pdsc_viftype_defaults),
+				       sizeof(*pdsc->viftype_status),
 				       GFP_KERNEL);
 	if (!pdsc->viftype_status)
 		return -ENOMEM;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 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.)

Quoted 6 lines

This is allocating a copy of pdsc_viftype_defaults, which is an array of struct pdsc_viftype. To correctly return "struct pdsc_viftype *" in the future, adjust the allocation to allocating ARRAY_SIZE-many entries. The resulting allocation size is the same.

Thanks,
sln

Quoted 30 lines

---
Cc: Shannon Nelson <[email protected]>
Cc: Brett Creeley <[email protected]>
Cc: Andrew Lunn <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: <[email protected]>

---
  drivers/net/ethernet/amd/pds_core/core.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/ethernet/amd/pds_core/core.c +2 -2
index 1eb0d92786f7..451b005d48d0 100644
@@ -415,7 +415,8 @@ static int pdsc_viftypes_init(struct pdsc *pdsc)
  {
         enum pds_core_vif_types vt;
 
-       pdsc->viftype_status = kzalloc(sizeof(pdsc_viftype_defaults),
+       pdsc->viftype_status = kcalloc(ARRAY_SIZE(pdsc_viftype_defaults),
+                                      sizeof(*pdsc->viftype_status),
                                        GFP_KERNEL);
         if (!pdsc->viftype_status)
                 return -ENOMEM;
--

2.34.1

#2 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 11 lines · On Fri, 25 Apr 2025 23:07:13 -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 is allocating a copy of pdsc_viftype_defaults, which is an array of struct pdsc_viftype. To correctly return "struct pdsc_viftype *" in the future, adjust the allocation to allocating ARRAY_SIZE-many entries. The resulting allocation size is the same.

[...]

Here is the summary with links:
- pds_core: Allocate pdsc_viftype_defaults copy with ARRAY_SIZE()

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

You are awesome, thank you!

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