[PATCH] of: replace deprecated strcpy() with strscpy()

Miguel García posted 1 patch 2 months, 1 week ago
drivers/of/overlay.c | 5 +++--
drivers/of/pdt.c     | 4 ++--
2 files changed, 5 insertions(+), 4 deletions(-)
[PATCH] of: replace deprecated strcpy() with strscpy()
Posted by Miguel García 2 months, 1 week ago
strcpy() is deprecated for NUL-terminated strings. Replace the uses in
overlay.c and pdt.c with strscpy(), using the known buffer sizes:
- new_prop->value (length in new_prop->length)
- local arrays in pdt.c

Signed-off-by: Miguel García <miguelgarciaroman8@gmail.com>
---
 drivers/of/overlay.c | 5 +++--
 drivers/of/pdt.c     | 4 ++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/of/overlay.c b/drivers/of/overlay.c
index 1af6f52d0708..b11e1d045c9a 100644
--- a/drivers/of/overlay.c
+++ b/drivers/of/overlay.c
@@ -258,8 +258,9 @@ static struct property *dup_and_fixup_symbol_prop(
 	if (!new_prop->name || !new_prop->value)
 		goto err_free_new_prop;
 
-	strcpy(new_prop->value, target_path);
-	strcpy(new_prop->value + target_path_len, path_tail);
+	strscpy(new_prop->value, target_path, new_prop->length);
+	strscpy(new_prop->value + target_path_len, path_tail,
+	new_prop->length - target_path_len);
 
 	of_property_set_flag(new_prop, OF_DYNAMIC);
 
diff --git a/drivers/of/pdt.c b/drivers/of/pdt.c
index cb0cb374b21f..1337ac4e8e80 100644
--- a/drivers/of/pdt.c
+++ b/drivers/of/pdt.c
@@ -51,7 +51,7 @@ static char * __init of_pdt_build_full_name(struct device_node *dp)
 	if (!of_pdt_prom_ops->pkg2path(dp->phandle, path, sizeof(path), &len)) {
 		name = kbasename(path);
 		buf = prom_early_alloc(strlen(name) + 1);
-		strcpy(buf, name);
+		strscpy(buf, name, sizeof(buf));
 		return buf;
 	}
 
@@ -84,7 +84,7 @@ static struct property * __init of_pdt_build_one_prop(phandle node, char *prev,
 
 	p->name = (char *) (p + 1);
 	if (special_name) {
-		strcpy(p->name, special_name);
+		strscpy(p->name, special_name, sizeof(p->name));
 		p->length = special_len;
 		p->value = prom_early_alloc(special_len);
 		memcpy(p->value, special_val, special_len);
-- 
2.34.1

Re: [PATCH] of: replace deprecated strcpy() with strscpy()
Posted by Krzysztof Kozlowski 2 months, 1 week ago
On Thu, Jul 24, 2025 at 09:33:41AM +0200, Miguel García wrote:
> @@ -84,7 +84,7 @@ static struct property * __init of_pdt_build_one_prop(phandle node, char *prev,
>  
>  	p->name = (char *) (p + 1);
>  	if (special_name) {
> -		strcpy(p->name, special_name);
> +		strscpy(p->name, special_name, sizeof(p->name));

This was not ever tested and I don't think you understand how C pointers
work.

Please document here how exactly did you test this code path?

Best regards,
Krzysztof