From patchwork Tue Feb 14 13:28:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 56918 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2977042wrn; Tue, 14 Feb 2023 05:39:56 -0800 (PST) X-Google-Smtp-Source: AK7set++K9NS8sQug3EH79uKgg985bpWVATu5EA2z6v8YlQtkaoIN3jDZBUhYtsFyYt/mJldM/qS X-Received: by 2002:a17:906:7c07:b0:8af:54d2:2088 with SMTP id t7-20020a1709067c0700b008af54d22088mr3312197ejo.37.1676381996362; Tue, 14 Feb 2023 05:39:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676381996; cv=none; d=google.com; s=arc-20160816; b=BKi31aVxMr0DET6CDcLnhWTGFMLbgStl0b2YcrZtgM6wK7M+LOzhXvOhjIVIZHZ+W7 03/zA1pQd8NQq8Tp27ud0DDT7mZGlQ+3TgThRuW2DMdTjm6Meb3egyfnE7j1i3OXl5Ni 5OWYRQON9/d6yfioABWYc/8yCwYU7eCVf3JpBDoUD1ImdIGoIS0bgZZBpvqH0/Y4jFs9 WoSROTAbnCeYlLNmyzRWYfl6jsuwtiKdixBKzA+w755n2T+0DEoOGTc4mtb/tl0XY8Rb 2O4Wpv9FDZIUtefdAgXjfs4M8dvGX/eHJ4C0ANVokW3z4OTGPknDk8C5Muvz7ZeSXPS2 6VcQ== 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=gpDh1DmX7Bl/x6os/XXhXCw0xQlJJIAGNpSab3G9Uqg=; b=J1p4rD1k2YKPc0oTDFPeAtYog+2hhazCiObtuj6SNnKAt1vsy0JVbRkDvg8Qb4+vlL kdTCAX8QjFgI9KmZKKHACX//pYl0CgkNbOaYTrMWSJ3D/RUUr36Blg/K+xljHMJVCIGU 0VkDgtoXLPgwzeJ5dFdNIy/2+XFh+d1OBabMZ4xg+a0j8jxkiwAViR3CpRO9PAsXwZFt AJRkmiPz07zOS5hvV6q59ss+RWsTguXkmy9kBSTcqnXRSfUCO0Wxd3JCyTgu6ctJoo9P PKhWfLJWUEq6vROrnszfn/PkmtrED+ZrUsFAPsj/oMcEqlIOce5XUHUjBQZGfhThLwgB a8Ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=epaypN3m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h6-20020a170906530600b008b13572ce10si879889ejo.302.2023.02.14.05.39.32; Tue, 14 Feb 2023 05:39:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=epaypN3m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232686AbjBNN2v (ORCPT + 99 others); Tue, 14 Feb 2023 08:28:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232295AbjBNN2t (ORCPT ); Tue, 14 Feb 2023 08:28:49 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B47C5244B9; Tue, 14 Feb 2023 05:28:46 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5488B61638; Tue, 14 Feb 2023 13:28:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BAE3FC433EF; Tue, 14 Feb 2023 13:28:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1676381325; bh=R7CqfsQVNTbRXsOCtxZQme9R3utr37VbIF0emcmEE9I=; h=From:To:Cc:Subject:Date:From; b=epaypN3m7eOIycqKKcNT8P5Ud8OucaQ9BqvzyUGBO6GviRpQ3tls/AXx6pCMi5SR/ QmsHNFOkTiwd4c2EB6hobIaB/TubYi71m8m+WX2v5TV08E++kW4GrtCLKdgsoWBeac gtiykMHNlPrnTsPRYTsi9mzp+Nu6eHfUuqiGfZeMUlqRMjlSMdqI2A3aXbhKf3nzE3 SSEbjzIjdLlGkBSW3kSbQ4USrhEQhoSa44P96zs4nV1pDohnQ7VsjZ7i3MVcsASlJa o57r1w2y0ESqROdBzMfSflmQSRo5hGqPDjFiCIjsEL6wSOwRUM+V9Paclse/3L+jnR Zs/wou8z5nspg== From: Arnd Bergmann To: Brian King , "James E.J. Bottomley" , "Martin K. Petersen" , Nathan Chancellor , Nick Desaulniers , James Bottomley Cc: Damien Le Moal , Arnd Bergmann , Kees Cook , Niklas Cassel , John Garry , linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH] [v2] scsi: ipr: work around fortify-string warning Date: Tue, 14 Feb 2023 14:28:08 +0100 Message-Id: <20230214132831.2118392-1-arnd@kernel.org> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757813927882038069?= X-GMAIL-MSGID: =?utf-8?q?1757813927882038069?= From: Arnd Bergmann The ipr_log_vpd_compact() function triggers a fortified memcpy() warning about a potential string overflow with all versions of clang: In file included from drivers/scsi/ipr.c:43: In file included from include/linux/string.h:254: include/linux/fortify-string.h:520:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning] __write_overflow_field(p_size_field, size); ^ include/linux/fortify-string.h:520:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning] 2 errors generated. I don't see anything actually wrong with the function, but this is the only instance I can reproduce of the fortification going wrong in the kernel at the moment, so the easiest solution may be to rewrite the function into something that does not trigger the warning. Instead of having a combined buffer for vendor/device/serial strings, use three separate local variables and just truncate the whitespace individually. Fixes: 8cf093e275d0 ("[SCSI] ipr: Improved dual adapter errors") Cc: Kees Cook Signed-off-by: Arnd Bergmann Reviewed-by: Damien Le Moal Reviewed-by: Kees Cook Acked-by: Brian King --- I did not try to bisect which commit introduced this behavior into the fortified memcpy(), the Fixes: commit is the one that introduced the ipr_log_vpd_compact() function but this predates the fortified string helpers. v2 changes: - fix off-by-one error --- drivers/scsi/ipr.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/drivers/scsi/ipr.c b/drivers/scsi/ipr.c index 198d3f20d682..f1f61705ccd5 100644 --- a/drivers/scsi/ipr.c +++ b/drivers/scsi/ipr.c @@ -1516,23 +1516,22 @@ static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd) } /** - * strip_and_pad_whitespace - Strip and pad trailing whitespace. - * @i: index into buffer - * @buf: string to modify + * strip_whitespace - Strip and pad trailing whitespace. + * @i: size of buffer + * @buf: string to modify * - * This function will strip all trailing whitespace, pad the end - * of the string with a single space, and NULL terminate the string. + * This function will strip all trailing whitespace and + * NUL terminate the string. * - * Return value: - * new length of string **/ -static int strip_and_pad_whitespace(int i, char *buf) +static void strip_whitespace(int i, char *buf) { + if (i < 1) + return; + i--; while (i && buf[i] == ' ') i--; - buf[i+1] = ' '; - buf[i+2] = '\0'; - return i + 2; + buf[i+1] = '\0'; } /** @@ -1547,19 +1546,21 @@ static int strip_and_pad_whitespace(int i, char *buf) static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb, struct ipr_vpd *vpd) { - char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3]; - int i = 0; + char vendor_id[IPR_VENDOR_ID_LEN + 1]; + char product_id[IPR_PROD_ID_LEN + 1]; + char sn[IPR_SERIAL_NUM_LEN + 1]; - memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN); - i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer); + memcpy(vendor_id, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN); + strip_whitespace(IPR_VENDOR_ID_LEN, vendor_id); - memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN); - i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer); + memcpy(product_id, vpd->vpids.product_id, IPR_PROD_ID_LEN); + strip_whitespace(IPR_PROD_ID_LEN, product_id); - memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN); - buffer[IPR_SERIAL_NUM_LEN + i] = '\0'; + memcpy(sn, vpd->sn, IPR_SERIAL_NUM_LEN); + strip_whitespace(IPR_SERIAL_NUM_LEN, sn); - ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer); + ipr_hcam_err(hostrcb, "%s VPID/SN: %s %s %s\n", prefix, + vendor_id, product_id, sn); } /**