[PATCH net-next] net: rfkill: Replace strcpy with memcpy to improve rfkill_alloc

From: Thorsten Blum

Date: Mon Dec 15 2025 - 07:21:01 EST


strcpy() is deprecated [1] and uses an additional strlen() internally;
use memcpy() directly since we already know the length of 'name' and
that it is guaranteed to be NUL-terminated.

Use struct_size(), which provides additional compile-time checks for
structures with flexible array members (e.g., __must_be_array()), to
determine the allocation size for a new 'struct rfkill'.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy [1]
Signed-off-by: Thorsten Blum <thorsten.blum@xxxxxxxxx>
---
net/rfkill/core.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/net/rfkill/core.c b/net/rfkill/core.c
index 7d3e82e4c2fc..db1260acb182 100644
--- a/net/rfkill/core.c
+++ b/net/rfkill/core.c
@@ -986,6 +986,7 @@ struct rfkill * __must_check rfkill_alloc(const char *name,
{
struct rfkill *rfkill;
struct device *dev;
+ size_t name_sz;

if (WARN_ON(!ops))
return NULL;
@@ -999,14 +1000,15 @@ struct rfkill * __must_check rfkill_alloc(const char *name,
if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES))
return NULL;

- rfkill = kzalloc(sizeof(*rfkill) + strlen(name) + 1, GFP_KERNEL);
+ name_sz = strlen(name) + 1;
+ rfkill = kzalloc(struct_size(rfkill, name, name_sz), GFP_KERNEL);
if (!rfkill)
return NULL;

spin_lock_init(&rfkill->lock);
INIT_LIST_HEAD(&rfkill->node);
rfkill->type = type;
- strcpy(rfkill->name, name);
+ memcpy(rfkill->name, name, name_sz);
rfkill->ops = ops;
rfkill->data = ops_data;

--
Thorsten Blum <thorsten.blum@xxxxxxxxx>
GPG: 1D60 735E 8AEF 3BE4 73B6 9D84 7336 78FD 8DFE EAD4