Re: [PATCH v3] drivers/edac/edac_mc: Remove all strcpy() uses
From: Len Baker <hidden>
Date: 2021-08-08 11:26:52
Also in:
linux-hardening, lkml
Hi, On Sat, Aug 07, 2021 at 10:09:35AM -0700, Joe Perches wrote:
On Sat, 2021-08-07 at 17:59 +0200, Len Baker wrote:quoted
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().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.
and also:quoted
diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c[]quoted
@@ -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);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/20210725162954.9861-1-len.baker@gmx.com/ (local) Regards, Len