Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp590947rdb; Thu, 30 Nov 2023 12:41:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IFpyyarmcJ8mQrrB4SAMi4M3PgHtoBNRIYGAqA5ucWUze1JBHXcXiIqnyckSbKIikPvcg/i X-Received: by 2002:a05:6358:c8f:b0:16e:2762:cdca with SMTP id o15-20020a0563580c8f00b0016e2762cdcamr20361069rwj.3.1701376878569; Thu, 30 Nov 2023 12:41:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701376878; cv=none; d=google.com; s=arc-20160816; b=wgkU5BII+NrgsrmONReJlOBUesf1//wK2s0hkT/cdp4zgebaBSwiNvxCP+BSCX5K6g iVPdOlGSa1NaAl6TrybsNFXKeE11OQPRkdJnfDIiNuytNmIKBTlwD1Yqm92Bs6LnY5vS DDd2K7/lgX425TGKhoOlxsWp5vzS3wuSKaiJybeNkDZAl6QEVAHvuIxDm5PjfXVt8PWO gM3lxMYynRhH1O1wzsaMQSLRLQMDRYOk0YuT66DV6Adr6a/sa2Zm8AQSjgj+bb2VkR4h 1TR0Vl90wj9FjlURPE0kd+5DdstP1FcUlOJMtLud7AsHDpkn+jcv/xUf3xgzeb/FSYpI biGA== 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=HToncW+LE9zv/92mmvcSPtZdBZoVOlwPwlFSkGOjsOE=; fh=XBjQESVthQsG0hcOFlZ5QzzhO/L3KwY5gysE4xk+PWE=; b=TWy6mnfBmbT1MAhrj681ABcDXxaoDmnFPo2nKqK8IthDjRQFqzYde0xW2teI6xEuqH JYNCEI8LwXx7N2kCSDxSnJZJ9GMtJPFxeWxbwqtgHsSgp5rfZdGbd10ydCYv1VhkuCc5 RGKhq/dIv2pNipL/iYMQW/lqY4GrXrlce0Qq0FRTBQ7KWXk1ESnQoiJmSw8F2CaVBlmQ LaBL1Y4w6jq+QWnowF11X10JGHlV9sOMWUblJhl6EVLb567UIFLMU2LxYc6N6Rp8JNsA ytQ221ObD88ascSQiFMhTUTdeJmzIwWVm3hTCk5C3ksXuaBPyynP8iHZD6l13DMypBZ2 cs4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=KqArQt8j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id i19-20020a63d453000000b0059beadab759si2046601pgj.652.2023.11.30.12.41.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 12:41:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=KqArQt8j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id A1E7680967A6; Thu, 30 Nov 2023 12:41:15 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229685AbjK3Uk7 (ORCPT + 99 others); Thu, 30 Nov 2023 15:40:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229591AbjK3Uk5 (ORCPT ); Thu, 30 Nov 2023 15:40:57 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C69D41703 for ; Thu, 30 Nov 2023 12:41:03 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-2856254bd74so1240982a91.2 for ; Thu, 30 Nov 2023 12:41:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1701376863; x=1701981663; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=HToncW+LE9zv/92mmvcSPtZdBZoVOlwPwlFSkGOjsOE=; b=KqArQt8j49LztbJuDs5bQg1WD9YUnXMeVk0KOYid8RZIg6X6p4jcsEchBBTG3QAesG z9y4vA9ABXoM+z9dKcvvUanSKAmaYBffW6Draer049LSacHsXbv/fPim+AeoAaiFYMYI XSd44EzQlb1ImNNqD+MLm0i78ErsbWDZiSL+0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701376863; x=1701981663; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=HToncW+LE9zv/92mmvcSPtZdBZoVOlwPwlFSkGOjsOE=; b=VgAksyJETVwbndOwNL2gum2xisfRkRSlvtMy1/u8CCHKtGhUIxNhaIHf2qKr5OI9HL TmBLwsebcA4XV51HVfEHK1AmntJVKHpk9+S790ZDGNJLXUoQDJmaCn+PFaBaVOBvaMUF jlbQ0U4haxqhchmIXWe3TRX5upml7h2Q6VyM+yyMY6kZGp5b5t+fFSktKzNdrbmxzETY bMlFRmimSORcm5Yctnottd8J8FZwql+1tZbACtBIzXV9LhxnYbdU3DARPfRcZb9ZR8vk nFzprpNwN1io8GAPdtupEIeIHxuPUnJF+BPwJsdm94qHh640csEiqrPgJFejmJLOwnkv ZbNw== X-Gm-Message-State: AOJu0YxlhQjnf0sPzcI46XZ1Bd9Bl5e9lm9MQIe6nbb84C57xy2vCa9a jtpXNNyE+YxZHwkM33M/av6qxg== X-Received: by 2002:a17:90a:357:b0:280:11e2:12d6 with SMTP id 23-20020a17090a035700b0028011e212d6mr23564604pjf.45.1701376862994; Thu, 30 Nov 2023 12:41:02 -0800 (PST) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id y1-20020a17090ad70100b002802d9d4e96sm3643102pju.54.2023.11.30.12.41.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 12:41:02 -0800 (PST) From: Kees Cook To: "Martin K. Petersen" Cc: Kees Cook , "James E.J. Bottomley" , Steffen Maier , Benjamin Block , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Azeem Shaikh , linux-s390@vger.kernel.org, linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v2] scsi: zfcp: Replace strlcpy() with strscpy() Date: Thu, 30 Nov 2023 12:41:00 -0800 Message-Id: <20231130204056.it.978-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2619; i=keescook@chromium.org; h=from:subject:message-id; bh=4T81lpDgaAOBeJi0R1mIeRgW4gBp3WUx30Gv6j9ztlg=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlaPNc3DGEDg6n6R2jxBMkNqrOja1osTULCasaX UZxQBioA02JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZWjzXAAKCRCJcvTf3G3A Jt5HD/9yXU4FXoT7iWh3EJhfuA3Y8HjktI8X29ZdPW0aN9g75IBIIy3FgqByl/7RW4rsCf5IHqJ 1aRTj37ijdQRxNQujB1di6Z5d4ENubNma2Mxr5wzCo8z/hpZT2jIPljlx3QkPgzrXLDYh2wMkqQ g69yTCJNXDdqug8Fva4Kd43e0vKoS/hYP7ZwpD5KLe8TjFTpvsO5mtx/6BmfOPjnT3hwE+j4yPt eaqmeLip+SqCsS2/8GAACN4LnGN80Nf5qBo2IxfcyqVb1GqVV7RJUUXH3O2XYzztawyWNwa9fbP lZ29Kv/CohmuVG0IJ16NnPQpampEdGS+3pruhhAN5bLYDDXoMKLyOKCzPvr+yi+KkvNQ8b2cnLy hHr55lbbbJzexMLk3qSUwrPXoZz3tXqJfKuU+QjiAbepT4atZ2iNNuJlpU+yEKy0joztLRe7siI AigbbIEwoO0HFTZ5XxhuSnjD3l0KUvs4a/MzQX6bPW/hlSDlgWgWPwASipbPWg4FsABC8skhV0d ytiReTa8uhR3GObcq6FbtR4lPUFRnrQAGEO5n+cFP7GQoCiDIXzCMbLC5llgbhie36UiI2pAyaD 4FYFS6Gdgpz9+oTBiO9wC+OZczjqPLM6wDULZbIrqimHDLDi4wzoTfXXWj+IgukvhJRQYRIfY1A 12tH4Xb UQs6sKKg== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.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 (fry.vger.email [0.0.0.0]); Thu, 30 Nov 2023 12:41:15 -0800 (PST) strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated[1]. Additionally, it returns the size of the source string, not the resulting size of the destination string. In an effort to remove strlcpy() completely[2], replace strlcpy() here with strscpy(). Overflow should be impossible here, but actually check for buffer sizes being identical with BUILD_BUG_ON(), and include a run-time check as well. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy [1] Link: https://github.com/KSPP/linux/issues/89 [2] Cc: "Martin K. Petersen" Cc: "James E.J. Bottomley" Cc: Steffen Maier Cc: Benjamin Block Cc: Heiko Carstens Cc: Vasily Gorbik Cc: Alexander Gordeev Cc: Christian Borntraeger Cc: Sven Schnelle Cc: Azeem Shaikh Cc: linux-s390@vger.kernel.org Cc: linux-scsi@vger.kernel.org Signed-off-by: Kees Cook --- v2: - add BUILD_BUG_ON (bblock) - CC SCSI maintainers (bblock) v1: https://lore.kernel.org/all/20231116191435.work.581-kees@kernel.org/ --- drivers/s390/scsi/zfcp_fc.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/s390/scsi/zfcp_fc.c b/drivers/s390/scsi/zfcp_fc.c index 4f0d0e55f0d4..d6516ab00437 100644 --- a/drivers/s390/scsi/zfcp_fc.c +++ b/drivers/s390/scsi/zfcp_fc.c @@ -900,8 +900,19 @@ static void zfcp_fc_rspn(struct zfcp_adapter *adapter, zfcp_fc_ct_ns_init(&rspn_req->ct_hdr, FC_NS_RSPN_ID, FC_SYMBOLIC_NAME_SIZE); hton24(rspn_req->rspn.fr_fid.fp_fid, fc_host_port_id(shost)); - len = strlcpy(rspn_req->rspn.fr_name, fc_host_symbolic_name(shost), - FC_SYMBOLIC_NAME_SIZE); + + BUILD_BUG_ON(sizeof(rspn_req->name) != + sizeof(fc_host_symbolic_name(shost))); + BUILD_BUG_ON(sizeof(rspn_req->name) != + type_max(typeof(rspn_req->rspn.fr_name_len)) + 1); + len = strscpy(rspn_req->name, fc_host_symbolic_name(shost), + sizeof(rspn_req->name)); + /* + * It should be impossible for this to truncate (see BUILD_BUG_ON() + * above), but be robust anyway. + */ + if (WARN_ON(len < 0)) + len = sizeof(rspn_req->name) - 1; rspn_req->rspn.fr_name_len = len; sg_init_one(&fc_req->sg_req, rspn_req, sizeof(*rspn_req)); -- 2.34.1