Skip to contentSkip to conversation

arm64/idreg: use strscpy() is more robust and safer

PATCH In review [email protected] 3 participants 3 messages Sep 29, 2022 – Oct 5, 2022 arm64

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64/idreg: use strscpy() is more robust and safer

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL terminated strings.

---
 arch/arm64/kernel/idreg-override.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kernel/idreg-override.c +1 -1
index 95133765ed29..61bbec7ef62e 100644
@@ -246,7 +246,7 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
                        return;
 
                len = min(len, ARRAY_SIZE(buf) - 1);
-               strncpy(buf, cmdline, len);
+               strscpy(buf, cmdline, len);
                buf[len] = 0;
 
                if (strcmp(buf, "--") == 0)
Signature · 1 line
2.15.2
maintainer
#1 lore raw
Quoted 17 lines · On Thu, Sep 29, 2022 at 07:29:06AM +0000, [email protected] wrote:

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL terminated strings.

---
 arch/arm64/kernel/idreg-override.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kernel/idreg-override.c +0 -0
index 95133765ed29..61bbec7ef62e 100644

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

Quoted 6 lines
arch/arm64/kernel/idreg-override.c +1 -1
@@ -246,7 +246,7 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
                        return;
 
                len = min(len, ARRAY_SIZE(buf) - 1);
-               strncpy(buf, cmdline, len);
+               strscpy(buf, cmdline, len);
                buf[len] = 0;

Aren't we terminating the buffer explicitly here anyway?

Will

#2 lore raw

From: Will Deacon

Quoted 7 lines

Sent: 04 October 2022 12:48

Quoted 4 lines · On Thu, Sep 29, 2022 at 07:29:06AM +0000, [email protected] wrote:

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL terminated strings.

In this case the input string isn't NUL terminated....

Quoted 15 lines
Quoted 15 lines
---
 arch/arm64/kernel/idreg-override.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kernel/idreg-override.c +0 -0
index 95133765ed29..61bbec7ef62e 100644
@@ -246,7 +246,7 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
                        return;
 
Quoted 6 lines

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

Quoted 4 lines
arch/arm64/kernel/idreg-override.c +1 -1
                len = min(len, ARRAY_SIZE(buf) - 1);
-               strncpy(buf, cmdline, len);
+               strscpy(buf, cmdline, len);
                buf[len] = 0;

Aren't we terminating the buffer explicitly here anyway?

I doubt the code was tested.
It is always wrong regardless of the initial value of 'len'.

I think using strscpy() will delete the last character and always add two '\0'.

On the face of it, that could probably be a memcpy().

But with the checks you don't need any of the length checks that memcpy() might be gaining.

OTOH, if the code used parameqn() a few lines lower the entire copy could be removed.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)