Message ID | 20231116191435.work.581-kees@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9910:0:b0:403:3b70:6f57 with SMTP id i16csp56875vqn; Thu, 16 Nov 2023 11:15:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IEVbLH1l7YQK4uYKTrpuGnzaPlgFSEUHotNNKXvqNNHX961M3hdq7YeiODC4slTTdPq0GEQ X-Received: by 2002:a17:903:244d:b0:1c9:e4f2:a39d with SMTP id l13-20020a170903244d00b001c9e4f2a39dmr3481316pls.49.1700162110406; Thu, 16 Nov 2023 11:15:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700162110; cv=none; d=google.com; s=arc-20160816; b=soBlgxfbQ3IgXcHRlczQbg502+KCo+xEPbQdWo9DzVPL038XuEfpf1UopMxMYlnmCW qQ6txnDz04VcXzZB709bThNpgqMtfcB3ufG3y7eD7EDcdy1fBCyJFk3b6cY76HVSdFGd qYrFOoVK/7iY8k6Prei6yQQ47WmkPeK7ETohZYnsREldyBxF7IThTVUERsHTW78kTWgT 2JanWMKqQgXnsiaDjwUug5VoWro3kgdboN7n2nNxcGXbuChkbPrcP0VvK5NJS8tP7UWk ZjShlOEWCotgO7Z+puuI1sTBUgj9qf/tNkFFCIrZ+UNXuoaVnia74YIHD1qJMkTxUaOm khaQ== 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=xXMlqUa9U+ONyBzylSfeWWc20gFnP/0YsPE87Yq0BOQ=; fh=09bV8Kx0luTQLh0AZEroAqHY6J5w1BPXLlg0Mgc2Wgc=; b=a06yvJITXjgDsLdDbQrHtU0fjfoj2CI0XXmqGvDdjKma5UuS2RaloVe2VGsLpdMGny vs9MBoDk17zWchpq5gwnECvRLHWN/ISHRJjWQBf9+/u5RqZN5zEPVAGzeRgLAsByy0HC hRoMWt8oEbC88ldVD9B+ohRQ7yTXuBLEIFS7fUph/Ouy54qcB/NA+dPiI0vIMzJl556Y RxDvY8hscU808wB2nVSRNE3V5jtgQV6N4E/Levix2ltSUCKfRipUMfyml4r1yY7oRFFY M/iQTvAJKBSAvNNouYoPnZLOFOFDJZ8CkVUfLCU7ekxTB4CqewY5PWj5/z7pSk9r2eFC 84xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PRSE2M1O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id 12-20020a170902c24c00b001cdf0388455si45679plg.111.2023.11.16.11.14.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Nov 2023 11:15:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PRSE2M1O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 3E14D81DD7F9; Thu, 16 Nov 2023 11:14:45 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345470AbjKPTOn (ORCPT <rfc822;jaysivo@gmail.com> + 30 others); Thu, 16 Nov 2023 14:14:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231314AbjKPTOl (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 16 Nov 2023 14:14:41 -0500 Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC238D55 for <linux-kernel@vger.kernel.org>; Thu, 16 Nov 2023 11:14:37 -0800 (PST) Received: by mail-oi1-x22d.google.com with SMTP id 5614622812f47-3b2ec5ee2e4so721543b6e.3 for <linux-kernel@vger.kernel.org>; Thu, 16 Nov 2023 11:14:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1700162077; x=1700766877; 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=xXMlqUa9U+ONyBzylSfeWWc20gFnP/0YsPE87Yq0BOQ=; b=PRSE2M1ORVm7fO5vG8Ol92WXrkNFp1x4Oy4Ag4nrtHgrOIrnbZ0jg6YTpNCajdevk5 97ne8VGKG1rug+OceA7nx3KYfmKYTxue2zaKWIrCE87GBPG4NjQwhhubH3IbCIKga0QH s479EnroSES6n+gmAttCGt3lzQsoWatPNMQns= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700162077; x=1700766877; 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=xXMlqUa9U+ONyBzylSfeWWc20gFnP/0YsPE87Yq0BOQ=; b=ijfvyXjNjKM0TMnMnao0KmVPLqH1sSKdpDbMnpf9gURaQiT/7+dVAzcD3WVYQCT3/x YRxc+U7aGeEjsvuBUvoTkv0NLP2dox4Q97LjScsAPoL6ENETHPJrcuzx8flz77Jfjb+D SJuZ97FUo0D+rm65Q+VDSUdDQmND66e+ND3BkYkTjVxPFwDIjAOY92lCCepDi3uCM4CS 0eaGGepTocF2dcJq4Nf4qRQErv7+cRvphz/AWo833RGOhTE0UneWklNEk/+G7iKCuUxP Kjgm5I+aMViSez2UiDXmglXovG0Agcm2bSUA5vPG1fnHHLun0xMX5lcsXGhOkb7K+9vr Re4Q== X-Gm-Message-State: AOJu0YxdGX33l3MBeN3VKsEK7QcvQON1tnKVUcr2jSbLdzZ1kcss5Ccn 0ey/RIZMK7eKH1tLFuBLOSCBlw== X-Received: by 2002:a05:6808:13cc:b0:3b2:e6a4:e177 with SMTP id d12-20020a05680813cc00b003b2e6a4e177mr24284312oiw.51.1700162077185; Thu, 16 Nov 2023 11:14:37 -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 y17-20020a63e251000000b005ab7b055573sm47469pgj.79.2023.11.16.11.14.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Nov 2023 11:14:36 -0800 (PST) From: Kees Cook <keescook@chromium.org> To: Steffen Maier <maier@linux.ibm.com> Cc: Kees Cook <keescook@chromium.org>, Benjamin Block <bblock@linux.ibm.com>, Heiko Carstens <hca@linux.ibm.com>, Vasily Gorbik <gor@linux.ibm.com>, Alexander Gordeev <agordeev@linux.ibm.com>, Christian Borntraeger <borntraeger@linux.ibm.com>, Sven Schnelle <svens@linux.ibm.com>, Azeem Shaikh <azeemshaikh38@gmail.com>, linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH] scsi: zfcp: Replace strlcpy() with strscpy() Date: Thu, 16 Nov 2023 11:14:35 -0800 Message-Id: <20231116191435.work.581-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2125; i=keescook@chromium.org; h=from:subject:message-id; bh=n/5QOMTS/PQ87jfirRVAutDJEB7F5Jn3WLO02CU/Zxk=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlVmobOo7x61Ke/67MhKfWOLSNoGNoSj987Uk83 vjr19oRi1OJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZVZqGwAKCRCJcvTf3G3A JnY/D/wJJH1WvSnfcOFsHJ7ya6qtSFf2j4ns7EUFNJSsRvSJTm16bUOEbYBISKfoDSA0nJl9QdC Kq1Kdoo/POkGyIJaVT1ksnnKoo1CaomOCqIXxC0BvUjoUzKGVaRQx3UxVRx1tznTM1srcjzdbDo EL7KzXzBmD2fRaI9tdtxujifs4qF5H8lX7+LlgKiGWD97An4mlPxktKW5vdO0N4n+lNN9pYsC5Y sOr9Lfo9CqR9l0PdxAK+GG7f0orxLMKxpCr/CbjYyR6epEgG430To5caSUabjbcpYum0Eckl4pp AJvL8FPevJbV8mBBh9CGwzuPyUhotxw/mmeq1mMxWqDcCH9WMOwiqOW9exZGun+BT2oba/EjWWM 7doqbdFNhFoCMTx3oBuCpBgzo+grrxYBi8w4gfnqP4ax7K70EnGtmYWbTPkyFWs2L3IEeC2g25j jJmf7RRM9gVipZQG4MEW8qn1RYQR5vxvbDUs8sgKP3QEw6TBzKQ8AdY18O49jUDrsFrYUp8isAn j5DXCRqc8rxHuxrH0RdeEa3/uMOtPNEJUGEOkKQ3/5SZekJBQ0oIUtX5o47qCSUHAzRq6CmNhA+ xUaww+US21aOvGxxpOSr72MJoDcCSgyOWR2Y8Tr75noNYgECiTwUxEgA0Gj+rPi9UcOOB4Fer5V PlWWlQh bp1J2Obw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=unavailable 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 16 Nov 2023 11:14:45 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782749184981032579 X-GMAIL-MSGID: 1782749184981032579 |
Series |
scsi: zfcp: Replace strlcpy() with strscpy()
|
|
Commit Message
Kees Cook
Nov. 16, 2023, 7:14 p.m. UTC
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().
Be explicitly robust in the face of truncation, which should be an
impossible state.
Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy [1]
Link: https://github.com/KSPP/linux/issues/89 [2]
Cc: Steffen Maier <maier@linux.ibm.com>
Cc: Benjamin Block <bblock@linux.ibm.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Azeem Shaikh <azeemshaikh38@gmail.com>
Cc: linux-s390@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
drivers/s390/scsi/zfcp_fc.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
Comments
On Thu, Nov 16, 2023 at 11:14:35AM -0800, Kees Cook wrote: Hi Kees, > diff --git a/drivers/s390/scsi/zfcp_fc.c b/drivers/s390/scsi/zfcp_fc.c > index 4f0d0e55f0d4..1a29f10767fc 100644 > --- a/drivers/s390/scsi/zfcp_fc.c > +++ b/drivers/s390/scsi/zfcp_fc.c > @@ -900,8 +900,15 @@ 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); > + len = strscpy(rspn_req->name, fc_host_symbolic_name(shost), > + sizeof(rspn_req->name)); Could you please explain why do you copy to rspn_req->name instead of rspn_req->rspn.fr_name? > + /* > + * It should be impossible for this to truncate, as > + * sizeof(rspn_req->name) is equal to max size of > + * fc_host_symbolic_name(shost), but check anyway. > + */ > + if (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)); Thanks!
On Fri, Nov 17, 2023 at 07:19:48PM +0100, Alexander Gordeev wrote: > > @@ -900,8 +900,15 @@ 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); > > + len = strscpy(rspn_req->name, fc_host_symbolic_name(shost), > > + sizeof(rspn_req->name)); > > Could you please explain why do you copy to rspn_req->name instead > of rspn_req->rspn.fr_name? Please, ignore this (stupid Friday evening) quesiton. Although the use of sizeof() is right thing, FC_SYMBOLIC_NAME_SIZE is so ubiquotous in this source that it probably makes sense to address in a separate cleanup. @Steffen, @Benjamin, could you please comment in this and below? > > + /* > > + * It should be impossible for this to truncate, as > > + * sizeof(rspn_req->name) is equal to max size of > > + * fc_host_symbolic_name(shost), but check anyway. > > + */ > > + if (len < 0) > > + len = sizeof(rspn_req->name) - 1; Not sure if this check is really needed. It could have been done for strlcpy() also, but as you say - should not ever happen. > > rspn_req->rspn.fr_name_len = len; > > > > sg_init_one(&fc_req->sg_req, rspn_req, sizeof(*rspn_req)); Thanks!
Hey Kees, thanks for the patch. can you please send this patch to linux-scsi and CC the SCSI Maintainers (Martin and James) instead (having linux-s390 on CC is fine)? zFCP doesn't go via s390, being a SCSI driver. On Thu, Nov 16, 2023 at 11:14:35AM -0800, Kees Cook wrote: > 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(). > > Be explicitly robust in the face of truncation, which should be an > impossible state. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy [1] > Link: https://github.com/KSPP/linux/issues/89 [2] > Cc: Steffen Maier <maier@linux.ibm.com> > Cc: Benjamin Block <bblock@linux.ibm.com> > Cc: Heiko Carstens <hca@linux.ibm.com> > Cc: Vasily Gorbik <gor@linux.ibm.com> > Cc: Alexander Gordeev <agordeev@linux.ibm.com> > Cc: Christian Borntraeger <borntraeger@linux.ibm.com> > Cc: Sven Schnelle <svens@linux.ibm.com> > Cc: Azeem Shaikh <azeemshaikh38@gmail.com> > Cc: linux-s390@vger.kernel.org > Signed-off-by: Kees Cook <keescook@chromium.org> > --- > drivers/s390/scsi/zfcp_fc.c | 11 +++++++++-- > 1 file changed, 9 insertions(+), 2 deletions(-) > > diff --git a/drivers/s390/scsi/zfcp_fc.c b/drivers/s390/scsi/zfcp_fc.c > index 4f0d0e55f0d4..1a29f10767fc 100644 > --- a/drivers/s390/scsi/zfcp_fc.c > +++ b/drivers/s390/scsi/zfcp_fc.c > @@ -900,8 +900,15 @@ 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); > + len = strscpy(rspn_req->name, fc_host_symbolic_name(shost), > + sizeof(rspn_req->name)); This is corrct. > + /* > + * It should be impossible for this to truncate, as > + * sizeof(rspn_req->name) is equal to max size of > + * fc_host_symbolic_name(shost), but check anyway. > + */ > + if (len < 0) > + len = sizeof(rspn_req->name) - 1; I'd rather have a compile-time check, whether the array sizes of `rspn_req->name` and `fc_host_symbolic_name(shost)` run out of sync, or against our expectations. Something like: len = strscpy(rspn_req->name, fc_host_symbolic_name(shost), sizeof(rspn_req->name)); BUILD_BUG_ON(sizeof(rspn_req->name) != sizeof(fc_host_symbolic_name(shost)) || sizeof(rspn_req->name) != 1 << sizeof(rspn_req->rspn.fr_name_len) * 8); rspn_req->rspn.fr_name_len = len; Then the last assignment should also be correct; and if something changes - unexpectedly, because this follows a FC standard - we need to adapt the code anyway I'd think. Or am I missing something? > rspn_req->rspn.fr_name_len = len; > > sg_init_one(&fc_req->sg_req, rspn_req, sizeof(*rspn_req)); > -- > 2.34.1 >
diff --git a/drivers/s390/scsi/zfcp_fc.c b/drivers/s390/scsi/zfcp_fc.c index 4f0d0e55f0d4..1a29f10767fc 100644 --- a/drivers/s390/scsi/zfcp_fc.c +++ b/drivers/s390/scsi/zfcp_fc.c @@ -900,8 +900,15 @@ 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); + len = strscpy(rspn_req->name, fc_host_symbolic_name(shost), + sizeof(rspn_req->name)); + /* + * It should be impossible for this to truncate, as + * sizeof(rspn_req->name) is equal to max size of + * fc_host_symbolic_name(shost), but check anyway. + */ + if (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));