[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <202310241629.0A4206316F@keescook>
Date: Tue, 24 Oct 2023 16:39:36 -0700
From: Kees Cook <keescook@...omium.org>
To: Christophe JAILLET <christophe.jaillet@...adoo.fr>
Cc: Robert Richter <rric@...nel.org>, Borislav Petkov <bp@...en8.de>,
Tony Luck <tony.luck@...el.com>,
James Morse <james.morse@....com>,
Mauro Carvalho Chehab <mchehab@...nel.org>,
Sergey Temerkhanov <s.temerkhanov@...il.com>,
linux-hardening@...r.kernel.org, linux-kernel@...r.kernel.org,
kernel-janitors@...r.kernel.org, Borislav Petkov <bp@...e.de>,
linux-edac@...r.kernel.org
Subject: Re: [PATCH v2] EDAC/thunderx: Fix some potential buffer overflow in
thunderx_ocx_com_threaded_isr()
On Sat, Oct 21, 2023 at 07:13:51PM +0200, Christophe JAILLET wrote:
> strncat() usage in thunderx_ocx_com_threaded_isr() is wrong.
> The size given to strncat() is the maximum number of bytes that can be
> written, excluding the trailing NULL.
>
> Here, the size of the 'msg' buffer is used (i.e. OCX_MESSAGE_SIZE), not
> the space that is remaining.
> The space for the ending NULL is also not taken into account.
>
> in order to fix it:
> - call decode_register() before the snprintf() calls
> - use scnprintf() instead of snprintf() and compute, in the 'remaining'
> variable, the space that is still available in the 'msg' buffer
> - add a %s at the end of the format strings and append directly the
> result of decode_register() stored in 'other'
> - write directly at the right position in the 'msg' buffer when
> appending some data in the for loop.
>
> Doing so, all usages of strncat() are removed.
>
> Fixes: 41003396f932 ("EDAC, thunderx: Add Cavium ThunderX EDAC driver")
> Signed-off-by: Christophe JAILLET <christophe.jaillet@...adoo.fr>
> ---
> This is cross-compile tested only.
> Review with care.
>
> v2: remove some other erroneous usage of strncat()
> ---
> drivers/edac/thunderx_edac.c | 44 ++++++++++++++++--------------------
> 1 file changed, 20 insertions(+), 24 deletions(-)
>
> diff --git a/drivers/edac/thunderx_edac.c b/drivers/edac/thunderx_edac.c
> index b9c5772da959..62e1e120178b 100644
> --- a/drivers/edac/thunderx_edac.c
> +++ b/drivers/edac/thunderx_edac.c
> @@ -1111,6 +1111,7 @@ static irqreturn_t thunderx_ocx_com_threaded_isr(int irq, void *irq_id)
>
> unsigned long tail;
> struct ocx_com_err_ctx *ctx;
> + size_t remaining;
> int lane;
> char *msg;
> char *other;
> @@ -1127,27 +1128,26 @@ static irqreturn_t thunderx_ocx_com_threaded_isr(int irq, void *irq_id)
> ARRAY_SIZE(ocx->com_err_ctx));
> ctx = &ocx->com_err_ctx[tail];
>
> - snprintf(msg, OCX_MESSAGE_SIZE, "%s: OCX_COM_INT: %016llx",
> - ocx->edac_dev->ctl_name, ctx->reg_com_int);
> -
> decode_register(other, OCX_OTHER_SIZE,
> ocx_com_errors, ctx->reg_com_int);
>
> - strncat(msg, other, OCX_MESSAGE_SIZE);
> + remaining = OCX_MESSAGE_SIZE;
> + remaining -= scnprintf(msg, remaining, "%s: OCX_COM_INT: %016llx%s",
> + ocx->edac_dev->ctl_name, ctx->reg_com_int,
> + other);
As the replacements get longer, I would encourage you to use seq_buf
instead -- it does all the length math internally. For example:
seq_buf s;
msg = kmalloc(OCX_MESSAGE_SIZE, GFP_KERNEL);
seq_buf_init(&s, msg, OCX_MESSAGE_SIZE);
seq_buf_printf(&s, "%s: OCX_COM_INT: %016llx%s",
ocx->edac_dev->ctl_name,
ctx->reg_com_int, other);
...
for (lane = 0; lane < OCX_RX_LANES; lane++)
if (ctx->reg_com_int & BIT(lane)) {
...
seq_buf_printf(&s, "\n\tOCX_LNE_INT[%02d]: %016llx OCX_LNE_STAT11[%02d]: %016llx%s",
lane, ctx->reg_lane_int[lane],
lane, ctx->reg_lane_stat11[lane],
other);
}
...
seq_buf_terminate(&s);
etc...
Though I think the seq_buf API could use some improvement: it should
always maintainer a %NUL-terminated string so the final
seq_buf_terminate() isn't needed...
--
Kees Cook
Powered by blists - more mailing lists