drivers/edac/edac_mc: Remove all strcpy() uses
PATCH v3 1/1 drivers/edac/edac_mc: Remove all strcpy() uses
strcpy() performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. The safe replacement is strscpy().
This is a previous step in the path to remove the strcpy() function entirely from the kernel.
---
This is a task of the KSPP [1]
[1] https://github.com/KSPP/linux/issues/88
Changelog v1 -> v2
- Use the strscpy() instead of scnprintf() to add labels and follow a code pattern more similar to the current one (advance "p" and decrement "left") (Robert Richter).
Changelog v2 -> v3
- Rename the "left" variable to "len" (Robert Richter).
- Use strlen(p) instead of strlen(OTHER_LABEL) to decrement "len" and increment "p" as otherwise "left" could underflow and p overflow (Robert Richter).
Previous versions:
v1
https://lore.kernel.org/linux-hardening/[email protected]/
v2
https://lore.kernel.org/linux-hardening/[email protected]/
drivers/edac/edac_mc.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/edac/edac_mc.c +9 -7
@@ -1032,6 +1032,7 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type,int i, n_labels =0 ;struct edac_raw_error_desc *e = &mci->error_desc;bool any_memory = true; +size_t len;edac_dbg (3 ,"MC%d\n" , mci->mc_idx);
@@ -1086,6 +1087,7 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type,*/ p = e->label; *p ='\0' ; + len =sizeof (e->label);mci_for_each_dimm (mci, dimm) {if (top_layer >=0 && top_layer != dimm->location[0 ])
@@ -1113,11 +1115,11 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type, p = e->label; *p ='\0' ; }else { -if (p != e->label) { -strcpy (p,OTHER_LABEL ); - p +=strlen (OTHER_LABEL ); - } -strcpy (p, dimm->label); +const char *text = (p != e->label) ?OTHER_LABEL : + dimm->label; + +strscpy (p, text, len); + len -=strlen (p); p +=strlen (p); }
@@ -1140,9 +1142,9 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type, }if (any_memory) -strcpy (e->label,"any memory" ); +strscpy (e->label,"any memory" ,sizeof (e->label));else if (!*e->label) -strcpy (e->label,"unknown memory" ); +strscpy (e->label,"unknown memory" ,sizeof (e->label));edac_inc_csrow (e, row, chan);
--
2.25.1
Quoted 3 lines · Len Baker
strcpy()performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. The safe replacement isstrscpy().
Probably better to change the commit subject to something like what is generally used by the subsystem.
Maybe:
EDAC/mc: Convert strcpy to strscpy
or
EDAC/mc: Prefer strscpy over strcpy
and also:
drivers/edac/edac_mc.c
Quoted 1 line
drivers/edac/edac_mc.c +0
-0
[]
Quoted 5 lines
drivers/edac/edac_mc.c +0
-1@@ -1113,11 +1115,11 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type, p = e->label; *p = '\0'; } else { -if (p != e->label) {
drivers/edac/edac_mc.c void edac_mc_handle_error(const enum hw_event_mc_err_type type, · line 1123
Quoted 10 lines
drivers/edac/edac_mc.c +5
-4-strcpy (p,OTHER_LABEL ); - p +=strlen (OTHER_LABEL ); - } -strcpy (p, dimm->label); +const char *text = (p != e->label) ?OTHER_LABEL : + dimm->label; + +strscpy (p, text, len); + len -=strlen (p); p += strlen(p);
Perhaps this should use scnprintf rather than strscpy Something like:
n += scnprintf(buf + n, len - n, "%s", p == e->label ? dim->label : OTHER_LABEL);
Hi,
Quoted 5 lines · Joe Perches
Quoted 3 lines · Len Baker
strcpy()performs no bounds checking on the destination buffer. This could result in linear overflows beyond the end of the buffer, leading to all kinds of misbehaviors. The safe replacement isstrscpy().
Quoted 7 lines
Probably better to change the commit subject to something like what is generally used by the subsystem.
Maybe:
EDAC/mc: Convert strcpy to strscpy
or
EDAC/mc: Prefer strscpy over strcpy
Ok, no problem. I like the second one.
Quoted 22 lines
and also:
drivers/edac/edac_mc.c
Quoted 1 line
drivers/edac/edac_mc.c +0
-0[]
Quoted 5 lines
drivers/edac/edac_mc.c +0
-1@@ -1113,11 +1115,11 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type, p = e->label; *p = '\0'; } else { -if (p != e->label) {drivers/edac/edac_mc.c void edac_mc_handle_error(const enum hw_event_mc_err_type type, · line 1123
Quoted 10 lines
drivers/edac/edac_mc.c +5
-4-strcpy (p,OTHER_LABEL ); - p +=strlen (OTHER_LABEL ); - } -strcpy (p, dimm->label); +const char *text = (p != e->label) ?OTHER_LABEL : + dimm->label; + +strscpy (p, text, len); + len -=strlen (p); p += strlen(p);Perhaps this should use scnprintf rather than strscpy Something like:
Quoted 3 lines
n += scnprintf(buf + n, len - n, "%s", p == e->label ? dim->label : OTHER_LABEL);
In the first version [1] the scnprintf was used but Robert Richter don't see any benefit compared with the current implementation.
[1] https://lore.kernel.org/linux-hardening/[email protected]/
Regards,
Len
Quoted 9 lines · Len Baker
Quoted 5 lines
Perhaps this should use scnprintf rather than strscpy Something like:
n += scnprintf(buf + n, len - n, "%s", p == e->label ? dim->label : OTHER_LABEL);In the first version [1] the scnprintf was used but Robert Richter don't see any benefit compared with the current implementation.
[1] https://lore.kernel.org/linux-hardening/[email protected]/
Reason is that there is the assumption that p must always point at the end of the string and its trailing zero byte. I am not opposed using the string function's return code instead of strlen() to get the length. But why using formated output if strscpy() can be used?
-Robert
Quoted 10 lines · Robert Richter
Quoted 8 lines · Len Baker
Quoted 5 lines
Perhaps this should use scnprintf rather than strscpy Something like:
n += scnprintf(buf + n, len - n, "%s", p == e->label ? dim->label : OTHER_LABEL);In the first version [1] the scnprintf was used but Robert Richter don't see any benefit compared with the current implementation.
Quoted 6 lines
Quoted 1 line
[1] https://lore.kernel.org/linux-hardening/[email protected]/
Reason is that there is the assumption that p must always point at the end of the string and its trailing zero byte. I am not opposed using the string function's return code instead of
strlen()to get the length. But why using formated output ifstrscpy()can be used?
strscpy and scnprintf have different return values and it's simpler and much more common to use scnprintf for appended strings that are limited to a specific buffer length.
From: Joe Perches
Quoted 15 lines
Sent: 09 August 2021 18:19
Quoted 12 lines · Robert Richter
Quoted 9 lines · Len Baker
Quoted 5 lines
Perhaps this should use scnprintf rather than strscpy Something like:
n += scnprintf(buf + n, len - n, "%s", p == e->label ? dim->label : OTHER_LABEL);In the first version [1] the scnprintf was used but Robert Richter don't see any benefit compared with the current implementation.
[1] https://lore.kernel.org/linux-hardening/[email protected]/
Quoted 8 lines
Quoted 4 lines
Reason is that there is the assumption that p must always point at the end of the string and its trailing zero byte. I am not opposed using the string function's return code instead of
strlen()to get the length. But why using formated output ifstrscpy()can be used?strscpy and scnprintf have different return values and it's simpler and much more common to use scnprintf for appended strings that are limited to a specific buffer length.
scnprintf() will be a lot slower, but has a much better return value than most of the strxxxcpy() functions.
The only slight problem is that you can't differentiate overflow from a max-length output.
Trouble is fixing that adds 'yet another set of functions'. Clearly we need the yellow with purple stripe ones :-) Probably:
offset = xxx(buf, len, offset, ......)
where offset == len on truncation.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Quoted 3 lines · Joe Perches
strscpy and scnprintf have different return values and it's simpler and much more common to use scnprintf for appended strings that are limited to a specific buffer length.
Calculating the bytes written from the return value is a oneliner.
-Robert
Quoted 7 lines · Robert Richter
Quoted 3 lines · Joe Perches
strscpy and scnprintf have different return values and it's simpler and much more common to use scnprintf for appended strings that are limited to a specific buffer length.
Calculating the bytes written from the return value is a oneliner.
Not really.
You still have to test for strscpy's possible return of -E2BIG.
Quoted 11 lines · Joe Perches
Quoted 7 lines · Robert Richter
Quoted 3 lines · Joe Perches
strscpy and scnprintf have different return values and it's simpler and much more common to use scnprintf for appended strings that are limited to a specific buffer length.
Calculating the bytes written from the return value is a oneliner.
Not really.
You still have to test for strscpy's possible return of-E2BIG.
I thought of:
num = strscpy(p, OTHER_LABEL, len); num = num < 0 ? len : num; len -= num; p += num;
Clearly, this does not look nice, esp. if this is repeated in the code. That's why I prefer the strlen(p) implementation:
strscpy(p, OTHER_LABEL, len); len -= strlen(p); p += strlen(p);
-Robert
Quoted 4 lines · Len Baker
drivers/edac/edac_mc.c +0
-0@@ -1113,11 +1115,11 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type, p = e->label; *p ='\0' ; }else {
drivers/edac/edac_mc.c void edac_mc_handle_error(const enum hw_event_mc_err_type type, · line 1122
Quoted 10 lines
drivers/edac/edac_mc.c +5
-5-if (p != e->label) { -strcpy (p,OTHER_LABEL ); - p +=strlen (OTHER_LABEL ); - } -strcpy (p, dimm->label); +const char *text = (p != e->label) ?OTHER_LABEL : + dimm->label; + +strscpy (p, text, len); + len -=strlen (p);
The logic looks broken and dimm labels are not properly copied (the code should add an " or " separator between labels).
-Robert
Quoted 3 lines
drivers/edac/edac_mc.c +0
-0p +=strlen (p); }
Hi,
Quoted 11 lines · Robert Richter
Quoted 9 lines · Len Baker
drivers/edac/edac_mc.c +0
-5@@ -1113,11 +1115,11 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type, p = e->label; *p ='\0' ; }else { -if (p != e->label) { -strcpy (p,OTHER_LABEL ); - p +=strlen (OTHER_LABEL ); - } -strcpy (p, dimm->label);
Quoted 8 lines
drivers/edac/edac_mc.c void edac_mc_handle_error(const enum hw_event_mc_err_type type, · line 1122
Quoted 5 lines
drivers/edac/edac_mc.c +5
-0+const char *text = (p != e->label) ?OTHER_LABEL : + dimm->label; + +strscpy (p, text, len); + len -=strlen (p);The logic looks broken and dimm labels are not properly copied (the code should add an " or " separator between labels).
Apologies. My bad.
Regards,
Len
Quoted 4 lines
Quoted 4 lines
drivers/edac/edac_mc.c +0
-0p +=strlen (p); }