linux-edac.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@alien8.de>
To: Robert Richter <rric@kernel.org>
Cc: Len Baker <len.baker@gmx.com>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Tony Luck <tony.luck@intel.com>,
	James Morse <james.morse@arm.com>, Joe Perches <joe@perches.com>,
	David Laight <David.Laight@aculab.com>,
	Kees Cook <keescook@chromium.org>,
	linux-hardening@vger.kernel.org, linux-edac@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] EDAC/mc: Prefer strscpy or scnprintf over strcpy, sprintf and snprintf
Date: Wed, 15 Sep 2021 13:58:25 +0200	[thread overview]
Message-ID: <YUHf4cEEKFjQznJS@zn.tnic> (raw)
In-Reply-To: <YT8S3poKyd5Nr5cK@rric.localdomain>

On Mon, Sep 13, 2021 at 10:59:10AM +0200, Robert Richter wrote:
> From 01a3c62a533e71984dfff7189e247b3e848f1449 Mon Sep 17 00:00:00 2001
> From: Len Baker <len.baker@gmx.com>
> Date: Fri, 3 Sep 2021 17:05:39 +0200
> Subject: [PATCH] EDAC/mc: Prefer strscpy or scnprintf over strcpy, sprintf
>  and snprintf
> 
> 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][2]
> 
> 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.
> 
> While at it, fix all other broken string generation code that wrongly
> interprets snprintf()'s return code or just uses sprintf(), implement
> that using scnprintf() here too. Drop breaks in loops around
> scnprintf() as it is safe now to loop. Moreover, the check is
> needless: For the case when the buffer is exhausted, len never gets
> zero because scnprintf() takes the full buffer length as input
> parameter, but excludes the trailing '\0' in its return code and thus,
> 1 is the minimum len.
> 
> [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy
> [2] https://github.com/KSPP/linux/issues/88
> 
>  [ rric: Replace snprintf() with scnprintf(), rework sprintf() user,
>    drop breaks in loops around scnprintf(), introduce 'end' pointer to
>    reduce pointer arithmetic, use prefix pattern for e->location,
>    adjust subject and description ]
> 
> Co-developed-by: Joe Perches <joe@perches.com>
> Signed-off-by: Joe Perches <joe@perches.com>
> Signed-off-by: Len Baker <len.baker@gmx.com>
> Signed-off-by: Robert Richter <rrichter@amd.com>
> ---
>  drivers/edac/edac_mc.c | 42 ++++++++++++++++++------------------------
>  1 file changed, 18 insertions(+), 24 deletions(-)

Applied, thanks.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

  reply	other threads:[~2021-09-15 11:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-03 15:05 [PATCH v6] EDAC/mc: Prefer strscpy or scnprintf over strcpy Len Baker
2021-09-03 17:03 ` Joe Perches
2021-09-04 11:23   ` Len Baker
2021-09-04 11:36     ` Borislav Petkov
2021-09-04 13:22       ` Len Baker
2021-09-13  8:59 ` [PATCH] EDAC/mc: Prefer strscpy or scnprintf over strcpy, sprintf and snprintf Robert Richter
2021-09-15 11:58   ` Borislav Petkov [this message]
2021-09-18  9:17   ` Len Baker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YUHf4cEEKFjQznJS@zn.tnic \
    --to=bp@alien8.de \
    --cc=David.Laight@aculab.com \
    --cc=james.morse@arm.com \
    --cc=joe@perches.com \
    --cc=keescook@chromium.org \
    --cc=len.baker@gmx.com \
    --cc=linux-edac@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=rric@kernel.org \
    --cc=tony.luck@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).