Skip to contentSkip to conversation

Revert "arm64/sysreg: refactor deprecated strncpy"

PATCH In review Mostafa Saleh 2 participants 4 messages Aug 31, 2023 – Sep 1, 2023

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

PATCH 1/1 Revert "arm64/sysreg: refactor deprecated strncpy"

This reverts commit d232606773a0b09ec7f1ffc25f63abe801d011fd.

Using strscpy is not correct in this context and the commit assumption is not right "strncpy is deprecated for use on NUL-terminated destination strings".

strncpy is used here to copy parts of the string(cmdline) separated by spaces into the buffer and not a NULL terminated string.

This breaks the arm options "kvm-arm.mode=protected, arm64.nobti ..."

---
 arch/arm64/kernel/idreg-override.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/kernel/idreg-override.c +3 -3
index aee12c75b738..2fe2491b692c 100644
@@ -262,9 +262,9 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
 		if (!len)
 			return;
 
-		len = strscpy(buf, cmdline, ARRAY_SIZE(buf));
-		if (len == -E2BIG)
-			len = ARRAY_SIZE(buf) - 1;
+		len = min(len, ARRAY_SIZE(buf) - 1);
+		strncpy(buf, cmdline, len);
+		buf[len] = 0;
 
 		if (strcmp(buf, "--") == 0)
 			return;
Signature · 1 line
2.42.0.rc2.253.gd59a3bf2b4-goog
maintainer
#1 lore raw

Hi Mostafa,

Quoted 24 lines · Mostafa Saleh

This reverts commit d232606773a0b09ec7f1ffc25f63abe801d011fd.

Using strscpy is not correct in this context and the commit assumption is not right "strncpy is deprecated for use on NUL-terminated destination strings".

strncpy is used here to copy parts of the string(cmdline) separated by spaces into the buffer and not a NULL terminated string.

This breaks the arm options "kvm-arm.mode=protected, arm64.nobti ..."

---
 arch/arm64/kernel/idreg-override.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/kernel/idreg-override.c +0 -0
index aee12c75b738..2fe2491b692c 100644
@@ -262,9 +262,9 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
 		if (!len)
 			return;

arch/arm64/kernel/idreg-override.c static __init void __parse_cmdline(const char *cmdline, bool parse_aliases) · line 270

Quoted 10 lines
arch/arm64/kernel/idreg-override.c +3 -3
 
-		len = strscpy(buf, cmdline, ARRAY_SIZE(buf));
-		if (len == -E2BIG)
-			len = ARRAY_SIZE(buf) - 1;
+		len = min(len, ARRAY_SIZE(buf) - 1);
+		strncpy(buf, cmdline, len);
+		buf[len] = 0;
 
 		if (strcmp(buf, "--") == 0)
 			return;

Instead of completely reverting the patch, maybe something like the hack below (completely untested), so that we can still get rid of another instance of strncpy(), and yet bring back some sanity in the logic?

Thanks,

	M.
arch/arm64/kernel/idreg-override.c +2 -3
index aee12c75b738..be5c778a3f14 100644
@@ -262,9 +262,8 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
 		if (!len)
 			return;
 
-		len = strscpy(buf, cmdline, ARRAY_SIZE(buf));
-		if (len == -E2BIG)
-			len = ARRAY_SIZE(buf) - 1;
+		len = min(len, ARRAY_SIZE(buf) - 1);
+		strscpy(buf, cmdline, len);
 
 		if (strcmp(buf, "--") == 0)
 			return;
Signature · 1 line
Without deviation from the norm, progress is not possible.
author Tested-by
#2 lore raw

Hi Marc,

Quoted 36 lines · Marc Zyngier

Hi Mostafa,

Quoted 32 lines · Mostafa Saleh

This reverts commit d232606773a0b09ec7f1ffc25f63abe801d011fd.

Using strscpy is not correct in this context and the commit assumption is not right "strncpy is deprecated for use on NUL-terminated destination strings".

strncpy is used here to copy parts of the string(cmdline) separated by spaces into the buffer and not a NULL terminated string.

This breaks the arm options "kvm-arm.mode=protected, arm64.nobti ..."

---
 arch/arm64/kernel/idreg-override.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/kernel/idreg-override.c +3 -3
index aee12c75b738..2fe2491b692c 100644
@@ -262,9 +262,9 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
 		if (!len)
 			return;
 
-		len = strscpy(buf, cmdline, ARRAY_SIZE(buf));
-		if (len == -E2BIG)
-			len = ARRAY_SIZE(buf) - 1;
+		len = min(len, ARRAY_SIZE(buf) - 1);
+		strncpy(buf, cmdline, len);
+		buf[len] = 0;
 
Quoted 7 lines

arch/arm64/kernel/idreg-override.c static __init void __parse_cmdline(const char *cmdline, bool parse_aliases) · line 269

Quoted 2 lines
arch/arm64/kernel/idreg-override.c +0 -0
 		if (strcmp(buf, "--") == 0)
 			return;

Instead of completely reverting the patch, maybe something like the hack below (completely untested), so that we can still get rid of another instance of strncpy(), and yet bring back some sanity in the logic?

I was thinking of something similar, but I see we limit the len anyway by the buffer size - 1 and force the NUL at the end so it should be safe, unless the goal is to get rid of strncpy all the way, in this case we can do it as you proposed.

There is also a V3 of the original patch which uses memcpy instead.
https://lore.kernel.org/all/[email protected]/

Quoted 24 lines

Thanks,

	M.
arch/arm64/kernel/idreg-override.c +2 -3
index aee12c75b738..be5c778a3f14 100644
@@ -262,9 +262,8 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
 		if (!len)
 			return;
 
-		len = strscpy(buf, cmdline, ARRAY_SIZE(buf));
-		if (len == -E2BIG)
-			len = ARRAY_SIZE(buf) - 1;
+		len = min(len, ARRAY_SIZE(buf) - 1);
+		strscpy(buf, cmdline, len);
 
 		if (strcmp(buf, "--") == 0)
 			return;
Signature · 1 line
-- 
Quoted 3 lines

Without deviation from the norm, progress is not possible.

Thanks,
Mostafa

maintainer
#3 lore raw
Quoted 47 lines · Mostafa Saleh

Hi Marc,

Quoted 36 lines · Marc Zyngier

Hi Mostafa,

Quoted 32 lines · Mostafa Saleh

This reverts commit d232606773a0b09ec7f1ffc25f63abe801d011fd.

Using strscpy is not correct in this context and the commit assumption is not right "strncpy is deprecated for use on NUL-terminated destination strings".

strncpy is used here to copy parts of the string(cmdline) separated by spaces into the buffer and not a NULL terminated string.

This breaks the arm options "kvm-arm.mode=protected, arm64.nobti ..."

---
 arch/arm64/kernel/idreg-override.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/kernel/idreg-override.c +3 -3
index aee12c75b738..2fe2491b692c 100644
@@ -262,9 +262,9 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
 		if (!len)
 			return;
 
-		len = strscpy(buf, cmdline, ARRAY_SIZE(buf));
-		if (len == -E2BIG)
-			len = ARRAY_SIZE(buf) - 1;
+		len = min(len, ARRAY_SIZE(buf) - 1);
+		strncpy(buf, cmdline, len);
+		buf[len] = 0;
 
Quoted 7 lines

arch/arm64/kernel/idreg-override.c static __init void __parse_cmdline(const char *cmdline, bool parse_aliases) · line 269

Quoted 2 lines
arch/arm64/kernel/idreg-override.c +0 -0
 		if (strcmp(buf, "--") == 0)
 			return;

Instead of completely reverting the patch, maybe something like the hack below (completely untested), so that we can still get rid of another instance of strncpy(), and yet bring back some sanity in the logic?

Quoted 7 lines

I was thinking of something similar, but I see we limit the len anyway by the buffer size - 1 and force the NUL at the end so it should be safe, unless the goal is to get rid of strncpy all the way, in this case we can do it as you proposed.

There is also a V3 of the original patch which uses memcpy instead.
https://lore.kernel.org/all/[email protected]/

Ah, that one would actually get my vote, because it does exactly what I actually intended this piece of code to do.

Justin, do you mind rebasing your v3 on top of to restore the currently broken functionality?

Quoted 19 lines
Quoted 19 lines

Thanks,

	M.
arch/arm64/kernel/idreg-override.c +2 -3
index aee12c75b738..be5c778a3f14 100644
@@ -262,9 +262,8 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
 		if (!len)
 			return;
 
-		len = strscpy(buf, cmdline, ARRAY_SIZE(buf));
-		if (len == -E2BIG)
-			len = ARRAY_SIZE(buf) - 1;
+		len = min(len, ARRAY_SIZE(buf) - 1);
+		strscpy(buf, cmdline, len);
 
Quoted 8 lines
Quoted 6 lines
arch/arm64/kernel/idreg-override.c +0 -0
 		if (strcmp(buf, "--") == 0)
 			return;
Signature · 1 line
-- 

Cheers Mostafa.

You could also just pick it, and repost it as a v2 (I really don't mind). It should give Will and Catalin a choice of implementations (and an opportunity for some additional bike-shedding ;-)).

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.