Skip to contentSkip to conversation

libbpf: Replace strcpy() with memcpy() in bpf_object__new()

PATCH In review Suchit Karunakaran 4 participants 10 messages Jul 17, 2025 – Jul 18, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: Replace strcpy() with memcpy() in bpf_object__new()

Replace the unsafe strcpy() call with memcpy() when copying the path into the bpf_object structure. Since the memory is pre-allocated to exactly strlen(path) + 1 bytes and the length is already known, memcpy() is safer than strcpy().

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
index 52e353368f58..279f226dd965 100644
@@ -1495,7 +1495,7 @@ static struct bpf_object *bpf_object__new(const char *path,
 		return ERR_PTR(-ENOMEM);
 	}
 
-	strcpy(obj->path, path);
+	memcpy(obj->path, path, strlen(path) + 1);
 	if (obj_name) {
 		libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name));
 	} else {
Signature · 1 line
2.50.1
reviewer
#1 lore raw
Quoted 4 lines · Suchit Karunakaran

Replace the unsafe strcpy() call with memcpy() when copying the path into the bpf_object structure. Since the memory is pre-allocated to exactly strlen(path) + 1 bytes and the length is already known, memcpy() is safer than strcpy().

I don't understand in this particular context why strcpy() is less safer than memcpy(). Both of them will achieve the exactly same goal.

Quoted 19 lines
---
  tools/lib/bpf/libbpf.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
index 52e353368f58..279f226dd965 100644
@@ -1495,7 +1495,7 @@ static struct bpf_object *bpf_object__new(const char *path,
  		return ERR_PTR(-ENOMEM);
  	}
 
-	strcpy(obj->path, path);
+	memcpy(obj->path, path, strlen(path) + 1);
  	if (obj_name) {
  		libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name));
  	} else {
author
#2 lore raw
Quoted 4 lines · Yonghong Song

On 7/17/25 4:59 AM, Suchit Karunakaran wrote:

Quoted 9 lines
Quoted 4 lines

Replace the unsafe strcpy() call with memcpy() when copying the path into the bpf_object structure. Since the memory is pre-allocated to exactly strlen(path) + 1 bytes and the length is already known, memcpy() is safer than strcpy().

I don't understand in this particular context why strcpy() is less safer than memcpy(). Both of them will achieve the exactly same goal.

Sorry, I meant that strcpy() is generally considered unsafe because it doesn't perform bounds checking. Its use is deprecated and discouraged, as noted in Documentation/process/deprecated.rst. I made this change with that in mind, although I'm not entirely certain whether it's actually unsafe in this specific context.

#3 lore raw
Quoted 15 lines · Suchit K
Quoted 4 lines · Yonghong Song

On 7/17/25 4:59 AM, Suchit Karunakaran wrote:

Quoted 9 lines
Quoted 4 lines

Replace the unsafe strcpy() call with memcpy() when copying the path into the bpf_object structure. Since the memory is pre-allocated to exactly strlen(path) + 1 bytes and the length is already known, memcpy() is safer than strcpy().

I don't understand in this particular context why strcpy() is less safer than memcpy(). Both of them will achieve the exactly same goal.

Quoted 6 lines

Sorry, I meant that strcpy() is generally considered unsafe because it doesn't perform bounds checking. Its use is deprecated and discouraged, as noted in Documentation/process/deprecated.rst. I made this change with that in mind, although I'm not entirely certain whether it's actually unsafe in this specific context.

Your change also did not do any bounds checking at all, so how is this now safer?

confused,

greg k-h

author
#4 lore raw
Quoted 6 lines

Your change also did not do any bounds checking at all, so how is this now safer?

confused,

greg k-h

I assumed bounds checking wasn't necessary here because obj is allocated at the start of the function with enough space (sizeof(struct bpf_object) + strlen(path) + 1). My main motivation for the change was the deprecation of strcpy(). However, thinking about it now, I'm not entirely sure memcpy is even needed in this context. I'd really appreciate any feedback or clarification on the best approach here.

#5 lore raw
In reply to Suchit Karunakaran
Quoted 16 lines · Suchit Karunakaran

Replace the unsafe strcpy() call with memcpy() when copying the path into the bpf_object structure. Since the memory is pre-allocated to exactly strlen(path) + 1 bytes and the length is already known, memcpy() is safer than strcpy().

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index 52e353368f58..279f226dd965 100644

tools/lib/bpf/libbpf.c static struct bpf_object *bpf_object__new(const char *path, · line 1498

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -1495,7 +1495,7 @@ static struct bpf_object *bpf_object__new(const char *path,
                return ERR_PTR(-ENOMEM);
        }
 
-       strcpy(obj->path, path);
+       memcpy(obj->path, path, strlen(path) + 1);

This is user-space libbpf code, where the API contract mandates that the path argument is a well-formed zero-terminated C string. Plus, if you look at the few lines above, we allocate just enough space to fit the entire contents of the string without truncation.

In other words, there is nothing to fix or improve here.

pw-bot: cr

Quoted 6 lines
tools/lib/bpf/libbpf.c +0 -1
        if (obj_name) {
                libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name));
        } else {
--

2.50.1

author
#6 lore raw
Quoted 31 lines · Andrii Nakryiko
Quoted 16 lines · Suchit Karunakaran

Replace the unsafe strcpy() call with memcpy() when copying the path into the bpf_object structure. Since the memory is pre-allocated to exactly strlen(path) + 1 bytes and the length is already known, memcpy() is safer than strcpy().

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index 52e353368f58..279f226dd965 100644

tools/lib/bpf/libbpf.c static struct bpf_object *bpf_object__new(const char *path, · line 1498

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -1495,7 +1495,7 @@ static struct bpf_object *bpf_object__new(const char *path,
                return ERR_PTR(-ENOMEM);
        }
 
-       strcpy(obj->path, path);
+       memcpy(obj->path, path, strlen(path) + 1);

This is user-space libbpf code, where the API contract mandates that the path argument is a well-formed zero-terminated C string. Plus, if you look at the few lines above, we allocate just enough space to fit the entire contents of the string without truncation.

Quoted 4 lines

In other words, there is nothing to fix or improve here.

pw-bot: cr

That makes sense, strcpy() is indeed safe here. Thanks for the clarification.

author
#7 lore raw
In reply to Andrii Nakryiko
Quoted 7 lines

This is user-space libbpf code, where the API contract mandates that the path argument is a well-formed zero-terminated C string. Plus, if you look at the few lines above, we allocate just enough space to fit the entire contents of the string without truncation.

In other words, there is nothing to fix or improve here.

Even though it’s safe in this context, would it still be a good idea to replace strcpy() with something like memcpy() since it's deprecated? I’m still a beginner in kernel development and trying to find my way around, so I’d appreciate any guidance.

#8 lore raw
Quoted 11 lines · Suchit K
Quoted 8 lines

This is user-space libbpf code, where the API contract mandates that the path argument is a well-formed zero-terminated C string. Plus, if you look at the few lines above, we allocate just enough space to fit the entire contents of the string without truncation.

In other words, there is nothing to fix or improve here.

Even though it’s safe in this context, would it still be a good idea to replace strcpy() with something like memcpy() since it's

no, there is no need. And keep in mind that this is libbpf library source code, which is developed as part of kernel repo, but isn't running inside the kernel itself

Quoted 2 lines

deprecated? I’m still a beginner in kernel development and trying to find my way around, so I’d appreciate any guidance.

author
#9 lore raw
Quoted 11 lines · Andrii Nakryiko
Quoted 9 lines · Suchit K
Quoted 8 lines

This is user-space libbpf code, where the API contract mandates that the path argument is a well-formed zero-terminated C string. Plus, if you look at the few lines above, we allocate just enough space to fit the entire contents of the string without truncation.

In other words, there is nothing to fix or improve here.

Quoted 7 lines
Quoted 2 lines

Even though it’s safe in this context, would it still be a good idea to replace strcpy() with something like memcpy() since it's

no, there is no need. And keep in mind that this is libbpf library source code, which is developed as part of kernel repo, but isn't running inside the kernel itself

Yup got it. Thank you so much for the clarification.