linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] drivers/edac/edac_mc: Remove all strcpy() uses
@ 2021-08-01 14:35 Len Baker
  2021-08-02  9:19 ` Robert Richter
  0 siblings, 1 reply; 3+ messages in thread
From: Len Baker @ 2021-08-01 14:35 UTC (permalink / raw)
  To: Kees Cook, Borislav Petkov, Mauro Carvalho Chehab, Tony Luck,
	James Morse, Robert Richter
  Cc: Len Baker, 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().

This is a previous step in the path to remove the strcpy() function
entirely from the kernel.

Signed-off-by: Len Baker <len.baker@gmx.com>
---
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).

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

diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c
index f6d462d0be2d..13b50be22ba6 100644
--- a/drivers/edac/edac_mc.c
+++ b/drivers/edac/edac_mc.c
@@ -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 left;

 	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';
+	left = sizeof(e->label);

 	mci_for_each_dimm(mci, dimm) {
 		if (top_layer >= 0 && top_layer != dimm->location[0])
@@ -1114,10 +1116,12 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type,
 			*p = '\0';
 		} else {
 			if (p != e->label) {
-				strcpy(p, OTHER_LABEL);
+				strscpy(p, OTHER_LABEL, left);
+				left -= strlen(OTHER_LABEL);
 				p += strlen(OTHER_LABEL);
 			}
-			strcpy(p, dimm->label);
+			strscpy(p, dimm->label, left);
+			left -= strlen(p);
 			p += strlen(p);
 		}

@@ -1140,9 +1144,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 related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] drivers/edac/edac_mc: Remove all strcpy() uses
  2021-08-01 14:35 [PATCH v2] drivers/edac/edac_mc: Remove all strcpy() uses Len Baker
@ 2021-08-02  9:19 ` Robert Richter
  2021-08-02  9:52   ` Robert Richter
  0 siblings, 1 reply; 3+ messages in thread
From: Robert Richter @ 2021-08-02  9:19 UTC (permalink / raw)
  To: Len Baker
  Cc: Kees Cook, Borislav Petkov, Mauro Carvalho Chehab, Tony Luck,
	James Morse, linux-hardening, linux-edac, linux-kernel

On 01.08.21 16:35:58, Len Baker wrote:

> @@ -1114,10 +1116,12 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type,
>  			*p = '\0';
>  		} else {
>  			if (p != e->label) {
> -				strcpy(p, OTHER_LABEL);
> +				strscpy(p, OTHER_LABEL, left);
> +				left -= strlen(OTHER_LABEL);
>  				p += strlen(OTHER_LABEL);

Those both must be strlen(p) now as otherwise 'left' could underflow
(and p overflow).

-Robert

>  			}
> -			strcpy(p, dimm->label);
> +			strscpy(p, dimm->label, left);
> +			left -= strlen(p);
>  			p += strlen(p);
>  		}
> 

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

* Re: [PATCH v2] drivers/edac/edac_mc: Remove all strcpy() uses
  2021-08-02  9:19 ` Robert Richter
@ 2021-08-02  9:52   ` Robert Richter
  0 siblings, 0 replies; 3+ messages in thread
From: Robert Richter @ 2021-08-02  9:52 UTC (permalink / raw)
  To: Len Baker
  Cc: Kees Cook, Borislav Petkov, Mauro Carvalho Chehab, Tony Luck,
	James Morse, linux-hardening, linux-edac, linux-kernel

On 02.08.21 11:19:33, Robert Richter wrote:
> On 01.08.21 16:35:58, Len Baker wrote:
> 
> > @@ -1114,10 +1116,12 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type,
> >  			*p = '\0';
> >  		} else {
> >  			if (p != e->label) {
> > -				strcpy(p, OTHER_LABEL);
> > +				strscpy(p, OTHER_LABEL, left);
> > +				left -= strlen(OTHER_LABEL);

Another note: Other parts of the code use 'len' instead of 'left', so
if you could change that too?

Thanks,

-Robert

> >  				p += strlen(OTHER_LABEL);
> 
> Those both must be strlen(p) now as otherwise 'left' could underflow
> (and p overflow).
> 
> -Robert
> 
> >  			}
> > -			strcpy(p, dimm->label);
> > +			strscpy(p, dimm->label, left);
> > +			left -= strlen(p);
> >  			p += strlen(p);
> >  		}
> > 

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

end of thread, other threads:[~2021-08-02  9:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-01 14:35 [PATCH v2] drivers/edac/edac_mc: Remove all strcpy() uses Len Baker
2021-08-02  9:19 ` Robert Richter
2021-08-02  9:52   ` Robert Richter

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