From: Joe Perches <joe@perches.com>
To: Len Baker <len.baker@gmx.com>, Borislav Petkov <bp@alien8.de>,
Mauro Carvalho Chehab <mchehab@kernel.org>,
Tony Luck <tony.luck@intel.com>,
James Morse <james.morse@arm.com>,
Robert Richter <rric@kernel.org>
Cc: 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 v5] EDAC/mc: Prefer strscpy or scnprintf over strcpy
Date: Sun, 29 Aug 2021 09:38:37 -0700 [thread overview]
Message-ID: <7b6dc45194f28db52740c2a604550f6879dafe36.camel@perches.com> (raw)
In-Reply-To: <20210829161547.6069-1-len.baker@gmx.com>
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 ";
next prev parent reply other threads:[~2021-08-29 16:38 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-29 16:15 [PATCH v5] EDAC/mc: Prefer strscpy or scnprintf over strcpy Len Baker
2021-08-29 16:38 ` Joe Perches [this message]
2021-09-03 14:21 ` 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=7b6dc45194f28db52740c2a604550f6879dafe36.camel@perches.com \
--to=joe@perches.com \
--cc=David.Laight@ACULAB.COM \
--cc=bp@alien8.de \
--cc=james.morse@arm.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).