From patchwork Sat Oct 21 17:13:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 156480 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp400364vqx; Sat, 21 Oct 2023 10:14:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGqQalX0D+SivipquHGDLxTP3yDGAm+P+C3Gc8isRZ5zBFMFhjkWH6HCXk3p/Qt3/QoUd7L X-Received: by 2002:aa7:981a:0:b0:6b4:6b34:8cef with SMTP id e26-20020aa7981a000000b006b46b348cefmr4301305pfl.34.1697908460771; Sat, 21 Oct 2023 10:14:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697908460; cv=none; d=google.com; s=arc-20160816; b=BYam+QKvW0A3GWdiarZWYpxwyfTmzxpxkfyB/d+qk5ZIqVGSbe1/09JLa0lGtjwjYB O2aIcaxI5UKcad8UO9jn/PeoXPJk+hhJ9qF2YymHsZnM2QhJYynG41uDwaz4ypKm9MU5 38SBTP9XY6nx5UkBUfp9tQL9I0Fh59YlWEn61G2kuA00ExWghJbY4BgDGYS/XKW9FxDg NRHAOD6XG0X+L7JZU/xf1/z42NRg5yi8QIGVgLlxt4wx+HFIfuiEyHfbHA13O64F2KFy uk1JIHh4BNRt48q0ORj/GK971ErfIOJchPJlL8omOu1e90BUsRVGZnPr25r9K1m1C+Ux +6Ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=pK3MzADyg7f3DHS0ePJyTiMBz9RdsBY2+tKOv52dh6g=; fh=wKmN5Beh/Q3sLHmr+sGMVEVQZMCnjvtyUwmtct5chQw=; b=aMZWhCMTwazuWKwdtNHUS778YAXXEiNpQExZYn/fivD8ZfmzOMEcdsO2g0vLC3O7Bu Ptl1ba/UQetiYjl/rBkCFxTeuuAh5z7jEQgUoZw+7fPfC8eMZpp0QY7a5Dqvy5l7HHCA Y3rr+o5Z0M6kUbcBfuLIZynMmYoNM9qACJ1lfzJdbbn9xYCaCGLv1ZyvNVgCIbpNfEST jJAnH6edh6BwPxnWX0XAxLDULXs9E1d0dOXv/ctlMwNrf7iCFXSrZv15emeGsw03RcUM p4EvkObnucePWa/FD/o9IBz/4SfOkXHUR4amsCML3qPa/CRWwlOkydI8Slb/LW7MrvJH U4FQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b="gz/EL4aN"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id d17-20020a63f251000000b00578a28df3e2si3945885pgk.816.2023.10.21.10.14.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Oct 2023 10:14:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b="gz/EL4aN"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 6BF428046BCA; Sat, 21 Oct 2023 10:14:18 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231701AbjJUROE (ORCPT + 26 others); Sat, 21 Oct 2023 13:14:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231356AbjJUROD (ORCPT ); Sat, 21 Oct 2023 13:14:03 -0400 Received: from smtp.smtpout.orange.fr (smtp-28.smtpout.orange.fr [80.12.242.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0A4A135 for ; Sat, 21 Oct 2023 10:13:57 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id uFXtq9F17aUVkuFXtqtgWE; Sat, 21 Oct 2023 19:13:56 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1697908436; bh=pK3MzADyg7f3DHS0ePJyTiMBz9RdsBY2+tKOv52dh6g=; h=From:To:Cc:Subject:Date; b=gz/EL4aNvcCjwQ5+Uze1yQqxPlVj4WXVbvhNdIbGIBtw/+WMMHNn6imM8F2cjeYbP a+JiD10A6i//W4w7ZAixd/Zd89WrW+3qqV71LeCtLV6e77k4+6CmS+feIysr9cd+4V KKh3frWd3e7P6EZIGr7POvRZeaqyavFxafNjjnXuFZWn34Uv970dzHWnwC+E+5ba8d Ce6idAeg+S+CzMCxAJqJQVqER6Ar2FJcqjJFGnI8pVJ3T5hcQC6sTYCl4z49/T2ZJq 9jv9F5Q0f7bbqPFGsxYcmXALLhN8+IOkMSvAtw8iGRg1iaTal7hdoDzkflsfwvj3Rf GxVnVS2qwRcFg== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sat, 21 Oct 2023 19:13:56 +0200 X-ME-IP: 86.243.2.178 From: Christophe JAILLET To: keescook@chromium.org, Robert Richter , Borislav Petkov , Tony Luck , James Morse , Mauro Carvalho Chehab , Sergey Temerkhanov Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , Borislav Petkov , linux-edac@vger.kernel.org Subject: [PATCH v2] EDAC/thunderx: Fix some potential buffer overflow in thunderx_ocx_com_threaded_isr() Date: Sat, 21 Oct 2023 19:13:51 +0200 Message-Id: <91ec35cd8e2e86fa3d24c2e8ea6970e0437cdfd2.1697908406.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Sat, 21 Oct 2023 10:14:18 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780385610209508726 X-GMAIL-MSGID: 1780386062311084859 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 --- 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); 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, + "\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); } if (ctx->reg_com_int & OCX_COM_INT_CE) @@ -1209,15 +1209,13 @@ static irqreturn_t thunderx_ocx_lnk_threaded_isr(int irq, void *irq_id) ctx = &ocx->link_err_ctx[tail]; - snprintf(msg, OCX_MESSAGE_SIZE, - "%s: OCX_COM_LINK_INT[%d]: %016llx", - ocx->edac_dev->ctl_name, - ctx->link, ctx->reg_com_link_int); - decode_register(other, OCX_OTHER_SIZE, ocx_com_link_errors, ctx->reg_com_link_int); - strncat(msg, other, OCX_MESSAGE_SIZE); + snprintf(msg, OCX_MESSAGE_SIZE, + "%s: OCX_COM_LINK_INT[%d]: %016llx%s", + ocx->edac_dev->ctl_name, + ctx->link, ctx->reg_com_link_int, other); if (ctx->reg_com_link_int & OCX_COM_LINK_INT_UE) edac_device_handle_ue(ocx->edac_dev, 0, 0, msg); @@ -1889,14 +1887,12 @@ static irqreturn_t thunderx_l2c_threaded_isr(int irq, void *irq_id) while (CIRC_CNT(l2c->ring_head, l2c->ring_tail, ARRAY_SIZE(l2c->err_ctx))) { - snprintf(msg, L2C_MESSAGE_SIZE, - "%s: %s: %016llx, %s: %016llx", - l2c->edac_dev->ctl_name, reg_int_name, ctx->reg_int, - ctx->reg_ext_name, ctx->reg_ext); - decode_register(other, L2C_OTHER_SIZE, l2_errors, ctx->reg_int); - strncat(msg, other, L2C_MESSAGE_SIZE); + snprintf(msg, L2C_MESSAGE_SIZE, + "%s: %s: %016llx, %s: %016llx%s", + l2c->edac_dev->ctl_name, reg_int_name, ctx->reg_int, + ctx->reg_ext_name, ctx->reg_ext, other); if (ctx->reg_int & mask_ue) edac_device_handle_ue(l2c->edac_dev, 0, 0, msg);