Re: [PATCH v2] EDAC/thunderx: Fix some potential buffer overflow in thunderx_ocx_com_threaded_isr()

From: Dan Carpenter
Date: Tue Oct 24 2023 - 01:35:47 EST


On Sat, Oct 21, 2023 at 07:13:51PM +0200, Christophe JAILLET wrote:
> @@ -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);
>
> for (lane = 0; lane < OCX_RX_LANES; lane++)
> if (ctx->reg_com_int & BIT(lane)) {
> - snprintf(other, OCX_OTHER_SIZE,
> - "\n\tOCX_LNE_INT[%02d]: %016llx OCX_LNE_STAT11[%02d]: %016llx",
> - lane, ctx->reg_lane_int[lane],
> - lane, ctx->reg_lane_stat11[lane]);
> -
> - strncat(msg, other, OCX_MESSAGE_SIZE);
> -
> decode_register(other, OCX_OTHER_SIZE,
> ocx_lane_errors,
> ctx->reg_lane_int[lane]);
> - strncat(msg, other, OCX_MESSAGE_SIZE);
> +
> + remaining -= scnprintf(msg + (OCX_MESSAGE_SIZE - remaining),
> + remaining,

Instead of doing "remaining -=" the canonincal way is "off +=". Then
the snprintf() becomes:

off += scnprintf(msg + off, OCX_MESSAGE_SIZE - off, ""\n\tOCX_...

Your way works but it makes my head hurt.

regards,
dan carpenter