linux-edac.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v5] EDAC/mc: Prefer strscpy or scnprintf over strcpy
@ 2021-08-29 16:15 Len Baker
  2021-08-29 16:38 ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Len Baker @ 2021-08-29 16:15 UTC (permalink / raw)
  To: Borislav Petkov, Mauro Carvalho Chehab, Tony Luck, James Morse,
	Robert Richter
  Cc: Len Baker, Joe Perches, David Laight, Kees Cook, linux-hardening,
	linux-edac, linux-kernel

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() [1].

However, to simplify and clarify the code, to concatenate labels use
the scnprintf() function. This way it is not necessary to check the
return value of strscpy (-E2BIG if the parameter count is 0 or the src
was truncated) since the scnprintf returns always the number of chars
written into the buffer. This function returns always a nul-terminated
string even if it needs to be truncated.

The main reason behind this patch is to remove all the strcpy() uses
from the kernel with the purpose to clean up the proliferation of
str*cpy() functions. Later on, the next step will be remove all the
strcpy implementations [2].

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy
[2] https://github.com/KSPP/linux/issues/88

Signed-off-by: Len Baker <len.baker@gmx.com>
---
Hi,

Next try. I hope this version will be accepted. Anyway, thanks for
the reviews and patience ;)

Thanks,
Len

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).

Changelog v3 -> v4
- Change the commit subject (Joe Perches).
- Fix broken logic (Robert Richter).
- Rebase against v5.14-rc5.

Changelog v4 -> v5
- Change the commit subject.
- Clarify why the change is being made by adding more info to the
  commit message (Borislav Petkov).
- Use scnprintf instead of strscpy (Joe Perches).
- Rebase against v5.14-rc7.

Previous versions:

v1
https://lore.kernel.org/linux-hardening/20210725162954.9861-1-len.baker@gmx.com/

v2
https://lore.kernel.org/linux-hardening/20210801143558.12674-1-len.baker@gmx.com/

v3
https://lore.kernel.org/linux-hardening/20210807155957.10069-1-len.baker@gmx.com/

v4
https://lore.kernel.org/linux-hardening/20210814075527.5999-1-len.baker@gmx.com/

 drivers/edac/edac_mc.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c
index f6d462d0be2d..4fe1286ad7a2 100644
--- a/drivers/edac/edac_mc.c
+++ b/drivers/edac/edac_mc.c
@@ -1032,6 +1032,8 @@ 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;
+	int n;

 	edac_dbg(3, "MC%d\n", mci->mc_idx);

@@ -1086,6 +1088,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,12 +1116,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);
-			p += strlen(p);
+			const char *or = (p != e->label) ? OTHER_LABEL : "";
+
+			n = scnprintf(p, len, "%s%s", or, dimm->label);
+			len -= n;
+			p += n;
 		}

 		/*
@@ -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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v5] EDAC/mc: Prefer strscpy or scnprintf over strcpy
  2021-08-29 16:15 [PATCH v5] EDAC/mc: Prefer strscpy or scnprintf over strcpy Len Baker
@ 2021-08-29 16:38 ` Joe Perches
  2021-09-03 14:21   ` Len Baker
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Perches @ 2021-08-29 16:38 UTC (permalink / raw)
  To: Len Baker, Borislav Petkov, Mauro Carvalho Chehab, Tony Luck,
	James Morse, Robert Richter
  Cc: David Laight, Kees Cook, linux-hardening, linux-edac, linux-kernel

On Sun, 2021-08-29 at 18:15 +0200, Len Baker wrote:
> 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() [1].
> 
> However, to simplify and clarify the code, to concatenate labels use
> the scnprintf() function. This way it is not necessary to check the
> return value of strscpy (-E2BIG if the parameter count is 0 or the src
> was truncated) since the scnprintf returns always the number of chars
> written into the buffer. This function returns always a nul-terminated
> string even if it needs to be truncated.
[]
> diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c
[]
> @@ -1113,12 +1116,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);
> -			p += strlen(p);
> +			const char *or = (p != e->label) ? OTHER_LABEL : "";
> +
> +			n = scnprintf(p, len, "%s%s", or, dimm->label);
> +			len -= n;
> +			p += n;

A very common and intelligible mechanism for this is:

	const char *prefix = "";
	int n = 0;
	...
			n += scnprintf(e->label + n, sizeof(e->label) - n,
				       "%s%s", prefix, dimm->label);
			prefix = " or ";



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v5] EDAC/mc: Prefer strscpy or scnprintf over strcpy
  2021-08-29 16:38 ` Joe Perches
@ 2021-09-03 14:21   ` Len Baker
  0 siblings, 0 replies; 3+ messages in thread
From: Len Baker @ 2021-09-03 14:21 UTC (permalink / raw)
  To: Joe Perches
  Cc: Len Baker, Borislav Petkov, Mauro Carvalho Chehab, Tony Luck,
	James Morse, Robert Richter, David Laight, Kees Cook,
	linux-hardening, linux-edac, linux-kernel

Hi,

On Sun, Aug 29, 2021 at 09:38:37AM -0700, Joe Perches wrote:
> On Sun, 2021-08-29 at 18:15 +0200, Len Baker wrote:
> > 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() [1].
> >
> > However, to simplify and clarify the code, to concatenate labels use
> > the scnprintf() function. This way it is not necessary to check the
> > return value of strscpy (-E2BIG if the parameter count is 0 or the src
> > was truncated) since the scnprintf returns always the number of chars
> > written into the buffer. This function returns always a nul-terminated
> > string even if it needs to be truncated.
> []
> > diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c
> []
> > @@ -1113,12 +1116,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);
> > -			p += strlen(p);
> > +			const char *or = (p != e->label) ? OTHER_LABEL : "";
> > +
> > +			n = scnprintf(p, len, "%s%s", or, dimm->label);
> > +			len -= n;
> > +			p += n;
>
> A very common and intelligible mechanism for this is:
>
> 	const char *prefix = "";
> 	int n = 0;
> 	...
> 			n += scnprintf(e->label + n, sizeof(e->label) - n,
> 				       "%s%s", prefix, dimm->label);
> 			prefix = " or ";
>
Ok, I will send a new version using this common mechanism. Thanks for the
advise.

Regards,
Len

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-09-03 14:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-29 16:15 [PATCH v5] EDAC/mc: Prefer strscpy or scnprintf over strcpy Len Baker
2021-08-29 16:38 ` Joe Perches
2021-09-03 14:21   ` Len Baker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).