Skip to contentSkip to conversation

driver core: Replace strcpy() with memcpy()

PATCH In review [email protected] 2 participants 5 messages Jun 6 – Jun 10 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 driver core: Replace strcpy() with memcpy()

From: David Laight <[email protected]>

The length of the string is calculated in order to allocate the correct sized memory block, use the same length to copy in the string.

---
This is one of a group of patches that remove potentially unbounded strcpy() calls.

They are mostly replaced by strscpy() or, when strlen() has just been called, with memcpy() (usually including the '\0').

Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.

The changes were made by getting the compiler to detect the calls and then fixing the code by hand.

Note that all the changes are only compile tested.

Some Makefiles were changed to allow files to contain strcpy(). As well as 'difficult to fix' files, this included 'show' functions as they really need to use sysfs_emit() or seq_printf().

All the patches are being sent individually to avoid very long cc lists. Apologies for the terse commit messages and likely unexpected tags. (There are about 100 patches in total.)

 drivers/base/platform.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/base/platform.c +3 -2
index 75b4698d0e58..63fdfffe1a8c 100644
@@ -617,10 +617,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 len = strlen(name);
 
-	pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
+	pa = kzalloc(sizeof(*pa) + len + 1, GFP_KERNEL);
 	if (pa) {
-		strcpy(pa->name, name);
+		memcpy(pa->name, name, len);
 		pa->pdev.name = pa->name;
 		pa->pdev.id = id;
 		device_initialize(&pa->pdev.dev);
Signature · 1 line
2.39.5
maintainer
#1 lore raw

drivers/base/platform.c static void platform_device_release(struct device *dev) · line 620

Quoted 9 lines · david.laight.linux
drivers/base/platform.c +1 -0
index 75b4698d0e58..63fdfffe1a8c 100644
@@ -617,10 +617,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 len = strlen(name);

This could be strlen(name) + 1 right away, which would also avoid the memcpy() to implicitly rely on kzalloc() zeroing the memory, where strcpy() was self-contained before.

drivers/base/platform.c static void platform_device_release(struct device *dev) · line 624

Quoted 6 lines
drivers/base/platform.c +2 -2
-	pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
+	pa = kzalloc(sizeof(*pa) + len + 1, GFP_KERNEL);
 	if (pa) {
-		strcpy(pa->name, name);
+		memcpy(pa->name, name, len);

But in general, what's the improvement? strlen() still operates on a potentially unbounded string?

It removes the redundant strlen() calculation that is implied by the current code, though this may be eliminated by CSE optimization.

#2 lore raw
Quoted 14 lines · On Tue, 09 Jun 2026 16:08:55 +0200 "Danilo Krummrich" <[email protected]> wrote:

drivers/base/platform.c static void platform_device_release(struct device *dev) · line 620

Quoted 9 lines · david.laight.linux
drivers/base/platform.c +1 -0
index 75b4698d0e58..63fdfffe1a8c 100644
@@ -617,10 +617,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 len = strlen(name);  

This could be strlen(name) + 1 right away, which would also avoid the memcpy() to implicitly rely on kzalloc() zeroing the memory, where strcpy() was self-contained before.

I tried not to do that 'optimisation'.
Here the kzalloc() is almost certainly needed for other reasons. There is also the issue that, in principle (but probably not in practise), the input string might change between the strlen() and the copy. So it is probably best practise to not rely on the '\0' still being there when the copy is done.

Quoted 11 lines

drivers/base/platform.c static void platform_device_release(struct device *dev) · line 624

Quoted 7 lines
drivers/base/platform.c +2 -2
-	pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
+	pa = kzalloc(sizeof(*pa) + len + 1, GFP_KERNEL);
 	if (pa) {
-		strcpy(pa->name, name);
+		memcpy(pa->name, name, len);  

But in general, what's the improvement? strlen() still operates on a potentially unbounded string?

Quoted 2 lines

It removes the redundant strlen() calculation that is implied by the current code, though this may be eliminated by CSE optimization.

CSE won't eliminate it.
It probably wouldn't eliminate a second strlen(name) either.

I'm not trying to remove strlen(), just strcpy().

But not just changing them all the strscpy().

David

maintainer
#3 lore raw
Quoted 18 lines · David Laight
Quoted 14 lines · On Tue, 09 Jun 2026 16:08:55 +0200 "Danilo Krummrich" <[email protected]> wrote:

drivers/base/platform.c static void platform_device_release(struct device *dev) · line 620

Quoted 9 lines · david.laight.linux
drivers/base/platform.c +1 -0
index 75b4698d0e58..63fdfffe1a8c 100644
@@ -617,10 +617,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 len = strlen(name);  

This could be strlen(name) + 1 right away, which would also avoid the memcpy() to implicitly rely on kzalloc() zeroing the memory, where strcpy() was self-contained before.

Quoted 2 lines

I tried not to do that 'optimisation'.
Here the kzalloc() is almost certainly needed for other reasons.

This is true, it is needed for other reasons anyway. My point is that currently it is self-contained and it doesn't matter if the preconditions ever change, so I'd rather we have strlen(name) + 1 right away.

Quoted 2 lines

There is also the issue that, in principle (but probably not in practise), the input string might change between the strlen() and the copy.

This is not a concern for this function, but let's assume it was, then your proposed patch wouldn't prevent anything bad either.

For instance, if the string would change to be shorter than what strlen() measured, the memcpy() becomes invalid either way.

Also, in that case the strcpy() would be more robust and vice versa with a longer one. But again, this is not a realistic scenario anyway.

Quoted 14 lines

So it is probably best practise to not rely on the '\0' still being there when the copy is done.

Quoted 11 lines

drivers/base/platform.c static void platform_device_release(struct device *dev) · line 624

Quoted 7 lines
drivers/base/platform.c +2 -2
-	pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
+	pa = kzalloc(sizeof(*pa) + len + 1, GFP_KERNEL);
 	if (pa) {
-		strcpy(pa->name, name);
+		memcpy(pa->name, name, len);  

But in general, what's the improvement? strlen() still operates on a potentially unbounded string?

Quoted 4 lines
Quoted 2 lines

It removes the redundant strlen() calculation that is implied by the current code, though this may be eliminated by CSE optimization.

CSE won't eliminate it.

Ok, but in the context of platform_device_alloc() not a huge benefit either.

Is there any other reason for this change? Am I missing something?

#4 lore raw
Quoted 26 lines · On Wed, 10 Jun 2026 00:49:10 +0200 "Danilo Krummrich" <[email protected]> wrote:
Quoted 18 lines · David Laight
Quoted 14 lines · On Tue, 09 Jun 2026 16:08:55 +0200 "Danilo Krummrich" <[email protected]> wrote:

drivers/base/platform.c static void platform_device_release(struct device *dev) · line 620

Quoted 9 lines · david.laight.linux
drivers/base/platform.c +1 -0
index 75b4698d0e58..63fdfffe1a8c 100644
@@ -617,10 +617,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 len = strlen(name);    

This could be strlen(name) + 1 right away, which would also avoid the memcpy() to implicitly rely on kzalloc() zeroing the memory, where strcpy() was self-contained before.

Quoted 2 lines

I tried not to do that 'optimisation'.
Here the kzalloc() is almost certainly needed for other reasons.

This is true, it is needed for other reasons anyway. My point is that currently it is self-contained and it doesn't matter if the preconditions ever change, so I'd rather we have strlen(name) + 1 right away.

Quoted 8 lines
Quoted 2 lines

There is also the issue that, in principle (but probably not in practise), the input string might change between the strlen() and the copy.

This is not a concern for this function, but let's assume it was, then your proposed patch wouldn't prevent anything bad either.

For instance, if the string would change to be shorter than what strlen() measured, the memcpy() becomes invalid either way.

Becoming shorter doesn't matter.
The problem is if is becomes longer (in some fixed length buffer). Then the copied string isn't terminated.
This does happen for updates of current->comm so can be a real problem.

Quoted 18 lines

Also, in that case the strcpy() would be more robust and vice versa with a longer one. But again, this is not a realistic scenario anyway.

Quoted 14 lines

So it is probably best practise to not rely on the '\0' still being there when the copy is done.

Quoted 11 lines

drivers/base/platform.c static void platform_device_release(struct device *dev) · line 624

Quoted 7 lines
drivers/base/platform.c +2 -2
-	pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
+	pa = kzalloc(sizeof(*pa) + len + 1, GFP_KERNEL);
 	if (pa) {
-		strcpy(pa->name, name);
+		memcpy(pa->name, name, len);    

But in general, what's the improvement? strlen() still operates on a potentially unbounded string?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

It removes the redundant strlen() calculation that is implied by the current code, though this may be eliminated by CSE optimization.

CSE won't eliminate it.

Ok, but in the context of platform_device_alloc() not a huge benefit either.

Is there any other reason for this change? Am I missing something?

I'm sure there is an overall plan to remove all calls to strcpy() and strcat() because of their potential to overrun the destination buffer.

-- David