Skip to contentSkip to conversation

apparmor: Replace deprecated strcpy with memcpy in gen_symlink_name

PATCH RESEND In review Thorsten Blum 2 participants 5 messages Nov 26, 2025 – Feb 4

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

PATCH RESEND 1/1 apparmor: Replace deprecated strcpy with memcpy in gen_symlink_name

strcpy() is deprecated; use memcpy() instead. Unlike strcpy(), memcpy() does not copy the NUL terminator from the source string, which would be overwritten anyway on every iteration when using strcpy(). snprintf() then ensures that 'char *s' is NUL-terminated.

Replace the hard-coded path length to remove the magic number 6, and add a comment explaining the extra 11 bytes.

---
 security/apparmor/apparmorfs.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
security/apparmor/apparmorfs.c +8 -4
index 391a586d0557..4b2752200ce2 100644
@@ -1602,16 +1602,20 @@ static char *gen_symlink_name(int depth, const char *dirname, const char *fname)
 {
 	char *buffer, *s;
 	int error;
-	int size = depth * 6 + strlen(dirname) + strlen(fname) + 11;
+	const char *path = "../../";
+	size_t path_len = strlen(path);
+	int size;
 
+	/* Extra 11 bytes: "raw_data" (9) + two slashes "//" (2) */
+	size = depth * path_len + strlen(dirname) + strlen(fname) + 11;
 	s = buffer = kmalloc(size, GFP_KERNEL);
 	if (!buffer)
 		return ERR_PTR(-ENOMEM);
 
 	for (; depth > 0; depth--) {
-		strcpy(s, "../../");
-		s += 6;
-		size -= 6;
+		memcpy(s, path, path_len);
+		s += path_len;
+		size -= path_len;
 	}
 
 	error = snprintf(s, size, "raw_data/%s/%s", dirname, fname);
Signature · 1 line
2.51.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Thorsten Blum

strcpy() is deprecated; use memcpy() instead. Unlike strcpy(), memcpy() does not copy the NUL terminator from the source string, which would be overwritten anyway on every iteration when using strcpy(). snprintf() then ensures that 'char *s' is NUL-terminated.

Replace the hard-coded path length to remove the magic number 6, and add a comment explaining the extra 11 bytes.

hey Thorsten,

sorry I have actually pulled these in, and tested them. I didn't send out the acks yet because I have another patch that I was waiting on a proper
signed-off-by: on.

I am going to have to pull that one so I can push. I'll add acks now but the push isn't going to happen for a few hours.

Quoted 32 lines
  security/apparmor/apparmorfs.c | 12 ++++++++----
  1 file changed, 8 insertions(+), 4 deletions(-)
security/apparmor/apparmorfs.c +8 -4
index 391a586d0557..4b2752200ce2 100644
@@ -1602,16 +1602,20 @@ static char *gen_symlink_name(int depth, const char *dirname, const char *fname)
  {
  	char *buffer, *s;
  	int error;
-	int size = depth * 6 + strlen(dirname) + strlen(fname) + 11;
+	const char *path = "../../";
+	size_t path_len = strlen(path);
+	int size;
 
+	/* Extra 11 bytes: "raw_data" (9) + two slashes "//" (2) */
+	size = depth * path_len + strlen(dirname) + strlen(fname) + 11;
  	s = buffer = kmalloc(size, GFP_KERNEL);
  	if (!buffer)
  		return ERR_PTR(-ENOMEM);
 
  	for (; depth > 0; depth--) {
-		strcpy(s, "../../");
-		s += 6;
-		size -= 6;
+		memcpy(s, path, path_len);
+		s += path_len;
+		size -= path_len;
  	}
 
  	error = snprintf(s, size, "raw_data/%s/%s", dirname, fname);
#2 lore raw
Quoted 10 lines · John Johansen

hey Thorsten,

sorry I have actually pulled these in, and tested them. I didn't send out the acks yet because I have another patch that I was waiting on a proper
signed-off-by: on.

I am going to have to pull that one so I can push. I'll add acks now but the push isn't going to happen for a few hours.

Ah sorry for resending then, didn't know you looked at them already.

Thanks,
Thorsten

#3 lore raw

Hi John,

Quoted 7 lines · Thorsten Blum
Quoted 6 lines · John Johansen

hey Thorsten,

sorry I have actually pulled these in, and tested them. I didn't send out the acks yet because I have another patch that I was waiting on a proper
signed-off-by: on.

Quoted 6 lines
Quoted 4 lines

I am going to have to pull that one so I can push. I'll add acks now but the push isn't going to happen for a few hours.

Ah sorry for resending then, didn't know you looked at them already.

Did you ever push the commits? I can't find them anywhere.

Thanks,
Thorsten

maintainer
#4 lore raw
Quoted 10 lines · Thorsten Blum

Hi John,

Quoted 7 lines · Thorsten Blum
Quoted 6 lines · John Johansen

hey Thorsten,

sorry I have actually pulled these in, and tested them. I didn't send out the acks yet because I have another patch that I was waiting on a proper
signed-off-by: on.

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

I am going to have to pull that one so I can push. I'll add acks now but the push isn't going to happen for a few hours.

Ah sorry for resending then, didn't know you looked at them already.

Did you ever push the commits? I can't find them anywhere.

sorry I am having "fun" email issue atm

it should be in apparmor-next. I have been staging that through gitlab

https://gitlab.com/apparmor/apparmor-kernel
and
https://gitlab.com/jjohansen/apparmor-kernel

trying to get better CI integration with the userspace side. It was stuck again and I had to manually kick it last week.