mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] platform: Replace deprecated strcpy in platform_device_alloc
@ 2025-10-31 12:18 Thorsten Blum
  2025-10-31 12:24 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 3+ messages in thread
From: Thorsten Blum @ 2025-10-31 12:18 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich
  Cc: linux-hardening, Thorsten Blum, linux-kernel

First, use struct_size(), which provides additional compile-time checks
for structures with flexible array members (e.g., __must_be_array()), to
calculate the number of bytes to allocate for a new 'platform_object'.

Then, since we know the length of 'name' and that it is guaranteed to be
NUL-terminated, replace the deprecated strcpy() with a simple memcpy().

Link: https://github.com/KSPP/linux/issues/88
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 drivers/base/platform.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index 09450349cf32..55ec4fb023e2 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -13,6 +13,7 @@
 #include <linux/platform_device.h>
 #include <linux/of_device.h>
 #include <linux/of_irq.h>
+#include <linux/overflow.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
@@ -577,10 +578,11 @@ static void platform_device_release(struct device *dev)
 struct platform_device *platform_device_alloc(const char *name, int id)
 {
 	struct platform_object *pa;
+	size_t name_len = strlen(name);
 
-	pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
+	pa = kzalloc(struct_size(pa, name, name_len + 1), GFP_KERNEL);
 	if (pa) {
-		strcpy(pa->name, name);
+		memcpy(pa->name, name, name_len + 1);
 		pa->pdev.name = pa->name;
 		pa->pdev.id = id;
 		device_initialize(&pa->pdev.dev);
-- 
2.51.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] platform: Replace deprecated strcpy in platform_device_alloc
  2025-10-31 12:18 [PATCH] platform: Replace deprecated strcpy in platform_device_alloc Thorsten Blum
@ 2025-10-31 12:24 ` Greg Kroah-Hartman
  2025-10-31 12:50   ` Thorsten Blum
  0 siblings, 1 reply; 3+ messages in thread
From: Greg Kroah-Hartman @ 2025-10-31 12:24 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Rafael J. Wysocki, Danilo Krummrich, linux-hardening, linux-kernel

On Fri, Oct 31, 2025 at 01:18:58PM +0100, Thorsten Blum wrote:
> First, use struct_size(), which provides additional compile-time checks
> for structures with flexible array members (e.g., __must_be_array()), to
> calculate the number of bytes to allocate for a new 'platform_object'.
> 
> Then, since we know the length of 'name' and that it is guaranteed to be
> NUL-terminated, replace the deprecated strcpy() with a simple memcpy().

This makes no sense.  You are saying we know the length, and we know it
is NULL terminated, so let's be complex and do a strlen() and memcpy()
instead of a normal strcpy().

Please no.  This is not ok at all.

greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] platform: Replace deprecated strcpy in platform_device_alloc
  2025-10-31 12:24 ` Greg Kroah-Hartman
@ 2025-10-31 12:50   ` Thorsten Blum
  0 siblings, 0 replies; 3+ messages in thread
From: Thorsten Blum @ 2025-10-31 12:50 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Rafael J. Wysocki, Danilo Krummrich, linux-hardening, linux-kernel

On 31. Oct 2025, at 13:24, Greg Kroah-Hartman wrote:
> On Fri, Oct 31, 2025 at 01:18:58PM +0100, Thorsten Blum wrote:
>> First, use struct_size(), which provides additional compile-time checks
>> for structures with flexible array members (e.g., __must_be_array()), to
>> calculate the number of bytes to allocate for a new 'platform_object'.
>> 
>> Then, since we know the length of 'name' and that it is guaranteed to be
>> NUL-terminated, replace the deprecated strcpy() with a simple memcpy().
> 
> This makes no sense.  You are saying we know the length, and we know it
> is NULL terminated, so let's be complex and do a strlen() and memcpy()
> instead of a normal strcpy().

The current version already calculates strlen(name), and then strcpy()
does another strlen() internally, which we can safely skip by using
memcpy() directly.

The current code is correct, but by removing the deprecated strcpy(), we
also remove a redundant call to strlen() without any functional changes.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-10-31 12:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-10-31 12:18 [PATCH] platform: Replace deprecated strcpy in platform_device_alloc Thorsten Blum
2025-10-31 12:24 ` Greg Kroah-Hartman
2025-10-31 12:50   ` Thorsten Blum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®