Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp607441rdb; Thu, 30 Nov 2023 13:13:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IHx+Ni4DukAHN57CkJpChHQmy/71t/LaCK2MbOm0/bJxbXAIBUATbNI92HjjMZUL2qXohis X-Received: by 2002:aa7:930e:0:b0:6b4:6b34:8cef with SMTP id cz14-20020aa7930e000000b006b46b348cefmr21297504pfb.34.1701378783465; Thu, 30 Nov 2023 13:13:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701378783; cv=none; d=google.com; s=arc-20160816; b=jB6LclpNXtnGA/hSDMr6H4ZLWmN4F8on+xIhBrObJx/+Wfhrce/F1Tf06Rmu7DqwnP RXuDv40ooiKd62l8ImY88cti9CE77X5/mKzcjWDC04hrnSgrZ7XgrB3xOe8Qmqjf9P3X OEuut7kV/1ambI5CQIeB28wNoaF1hNOzOC3WFotH0c6mugL7+cZFacIskMX7msTTAgUz K2C4YKLtnX/cJH30e9fu+9KvpoCHvkyMc4PatgfAsOz9oVSmEgq2d8YHGUoYSaJgfvqL saz85xCt2h1jpvulqaNhA9G/l2ItnAt5YzJCYaAkElWTU5hp6TN7gpwGl3EBRDefySX1 qdUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=oMtD4zdAr6X+H2Qpe4emJrdJbxSyHd1ONAvar3QH9oM=; fh=xCJeqJeMoFnHEkHK6CNL/rrIGzxgKOjqKfcpIXmGfAA=; b=U94gWUtwbjB+dLZ1q5Y7y7UO4fWzm63WkoOtESLb9wI793/gM5uKpe00dZvsqcMdOo am+xnwZVaD0rgsmPvzWmHy7NoOE2WeZmXgTx6gr6VEyRc4IAvHxNKW1jcmRXTX1Mfk3L wFkm9SWNkJNTrnV6RVusmFd/SuxpG3GsFUxv0RfP7c1LAbKcF+jdWiv/sKbfR05olXxN JBB/4sx1wWkql4nBEVQZhqqDmAms38IP6rN8Dqo5MZcMKAuSicdAdhZjH2niUtfmzgDb 3RGHUrwvo8dFGZ3cfaZAyrzEIKQmNhRT4rmOLOmzvvtJ5NUvzP6hfhnn73pNGsptCT9g tiHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ZxjbbZ7v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id z5-20020a656645000000b005a9e4c3d350si1954683pgv.743.2023.11.30.13.13.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 13:13:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ZxjbbZ7v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (Postfix) with ESMTP id AD4ED802A3D3; Thu, 30 Nov 2023 13:13:00 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376827AbjK3VMo (ORCPT + 99 others); Thu, 30 Nov 2023 16:12:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376386AbjK3VMn (ORCPT ); Thu, 30 Nov 2023 16:12:43 -0500 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE52AC4 for ; Thu, 30 Nov 2023 13:12:49 -0800 (PST) Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1d05212a7c5so583525ad.0 for ; Thu, 30 Nov 2023 13:12:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1701378769; x=1701983569; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=oMtD4zdAr6X+H2Qpe4emJrdJbxSyHd1ONAvar3QH9oM=; b=ZxjbbZ7v3ERljlXrOCR6AGCmGn3s2F6tauVzLoYjiY0soTCy1vwYq32XAVwpWoNXuQ ayN+Hxaag9GIbyNfTLpN6FG8FNmJcfpDgp5OVjn74F58oaaEdnQsoD8T2vQmGPE7tA+r KfQ9L/DHX8gJi8oTPVlyb0SLAuSrjJrd2TB04= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701378769; x=1701983569; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=oMtD4zdAr6X+H2Qpe4emJrdJbxSyHd1ONAvar3QH9oM=; b=Yc9V2NIe0+mLreAdDQ1dU5EHrlpI6G04VeCXh9IRBnTFDfHManbn2j5AHfh83a9U3o aOlQfInIl3aCNG9deT7rd2s7D5/2ai1hFTPuWWktW/8bioDko0vYopCjwiWbHeWDg6WY MU6efOFjjO3FKe7AUWDZe0NBkcJzViZjQou540v6MrA4SQ6+2C/+xRnwfA21AysUy3gT kfKACFwIil88/wWPmSqIR16dA0Mp7W3uz3D41cZbIru7i4aoRkaAimUBQKVkridCVgY+ lJv7OteXKaCvzMbbPb6D8zjD5DMJ4JT66CEl6nYo43pi0G4s4zfzOBv/QkR167zmTsa7 gU3w== X-Gm-Message-State: AOJu0Yym4+lQwWvFuki/kX4t9RdveyGiOoy+PXOVuaaU+4RHppsuL5dY 0wSso08QnKiUjtA8K9uCUnIx2Q== X-Received: by 2002:a17:902:7409:b0:1cf:a2e7:f851 with SMTP id g9-20020a170902740900b001cfa2e7f851mr23180716pll.32.1701378769265; Thu, 30 Nov 2023 13:12:49 -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 y9-20020a170902b48900b001bf044dc1a6sm1867755plr.39.2023.11.30.13.12.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 13:12:48 -0800 (PST) Date: Thu, 30 Nov 2023 13:12:48 -0800 From: Kees Cook To: Justin Stitt Cc: Tyrel Datwyler , Michael Ellerman , Nicholas Piggin , Christophe Leroy , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] scsi: ibmvfc: replace deprecated strncpy with strscpy Message-ID: <202311301311.717549FB@keescook> References: <20231030-strncpy-drivers-scsi-ibmvscsi-ibmvfc-c-v1-1-5a4909688435@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231030-strncpy-drivers-scsi-ibmvscsi-ibmvfc-c-v1-1-5a4909688435@google.com> 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 groat.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 (groat.vger.email [0.0.0.0]); Thu, 30 Nov 2023 13:13:00 -0800 (PST) On Mon, Oct 30, 2023 at 07:04:33PM +0000, Justin Stitt wrote: > strncpy() is deprecated for use on NUL-terminated destination strings > [1] and as such we should prefer more robust and less ambiguous string > interfaces. > > We expect these fields to be NUL-terminated as the property names from > which they are derived are also NUL-terminated. > > Moreover, NUL-padding is not required as our destination buffers are > already NUL-allocated and any future NUL-byte assignments are redundant > (like the ones that strncpy() does). > ibmvfc_probe() -> > | struct ibmvfc_host *vhost; > | struct Scsi_Host *shost; > ... > | shost = scsi_host_alloc(&driver_template, sizeof(*vhost)); > ... **side note: is this a bug? Looks like a type to me ^^^^^** I think this is the "privsize", so *vhost is correct, IIUC. > ... > | vhost = shost_priv(shost); I.e. vhost is a part of the shost allocation > > ... where shost_priv() is: > | static inline void *shost_priv(struct Scsi_Host *shost) > | { > | return (void *)shost->hostdata; > | } > > .. and: > scsi_host_alloc() -> > | shost = kzalloc(sizeof(struct Scsi_Host) + privsize, GFP_KERNEL); As seen here. :) > > And for login_info->..., NUL-padding is also not required as it is > explicitly memset to 0: > | memset(login_info, 0, sizeof(*login_info)); > > Considering the above, a suitable replacement is `strscpy` [2] due to > the fact that it guarantees NUL-termination on the destination buffer > without unnecessarily NUL-padding. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] > Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2] > Link: https://github.com/KSPP/linux/issues/90 > Cc: linux-hardening@vger.kernel.org > Signed-off-by: Justin Stitt Yeah, this conversion looks correct to me too. Reviewed-by: Kees Cook -Kees > --- > Note: build-tested only. > > Found with: $ rg "strncpy\(" > --- > drivers/scsi/ibmvscsi/ibmvfc.c | 12 +++++++----- > 1 file changed, 7 insertions(+), 5 deletions(-) > > diff --git a/drivers/scsi/ibmvscsi/ibmvfc.c b/drivers/scsi/ibmvscsi/ibmvfc.c > index ce9eb00e2ca0..e73a39b1c832 100644 > --- a/drivers/scsi/ibmvscsi/ibmvfc.c > +++ b/drivers/scsi/ibmvscsi/ibmvfc.c > @@ -1455,7 +1455,7 @@ static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost) > > name = of_get_property(rootdn, "ibm,partition-name", NULL); > if (name) > - strncpy(vhost->partition_name, name, sizeof(vhost->partition_name)); > + strscpy(vhost->partition_name, name, sizeof(vhost->partition_name)); > num = of_get_property(rootdn, "ibm,partition-no", NULL); > if (num) > vhost->partition_number = *num; > @@ -1498,13 +1498,15 @@ static void ibmvfc_set_login_info(struct ibmvfc_host *vhost) > login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token); > login_info->async.len = cpu_to_be32(async_crq->size * > sizeof(*async_crq->msgs.async)); > - strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME); > - strncpy(login_info->device_name, > - dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME); > + strscpy(login_info->partition_name, vhost->partition_name, > + sizeof(login_info->partition_name)); > + > + strscpy(login_info->device_name, > + dev_name(&vhost->host->shost_gendev), sizeof(login_info->device_name)); > > location = of_get_property(of_node, "ibm,loc-code", NULL); > location = location ? location : dev_name(vhost->dev); > - strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME); > + strscpy(login_info->drc_name, location, sizeof(login_info->drc_name)); > } > > /** > > --- > base-commit: ffc253263a1375a65fa6c9f62a893e9767fbebfa > change-id: 20231030-strncpy-drivers-scsi-ibmvscsi-ibmvfc-c-ccfce3255d58 > > Best regards, > -- > Justin Stitt > -- Kees Cook