From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 309AE1FB7 for ; Wed, 25 Oct 2023 00:30:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="hK2ejIUo" Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACF7D10E3 for ; Tue, 24 Oct 2023 17:30:12 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6bd73395bceso269840b3a.0 for ; Tue, 24 Oct 2023 17:30:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1698193812; x=1698798612; 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=yiGBchuBAWV4UdaNqAqp/OW8eIkIliqczAGndCUHeX4=; b=hK2ejIUogxY6Q2SKGHpszonmJ4/xeAL1NuLO7rDGYGawrCu+xGkvb/XkpUwhQb4Y64 cSzHmWVH05RXdgySQLdvSTvNTIIKkc5F9MVx0iTYCPnn9WftWULHEo7RBJmGXijXfhRR qeswt4Jy3pI+eR2oG9avHJcyhfCRCSmYuQF6k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698193812; x=1698798612; 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=yiGBchuBAWV4UdaNqAqp/OW8eIkIliqczAGndCUHeX4=; b=Fw5lgPC8NMIrAJX1gUxG0VP1YsX7cbjUufl5D+LlLHGGwqCD03P1uqRRWQYri8C2uP bwA7h4uuBzp+SGSCL9rv3o1ur8mKgUJa0mR92gfVc5eNiyVLCnus9iOaMhqU6mg/wwm2 4tYmshhHgNcVmkFQLTzYv7k/ebc2ZqbS7v8esF0Do+QSKnpOsZ/2QMpVaQ0x2FJS1ZWs qa6FYwIhvir90vF+TOPcz5QUAXq9+GQ9LVVPRlF1PYz0jPaGajbtrjBuUtc5WtkS/2JT x5zV5I7ap5zsNx0fOskKzh03tC11ABqW+Z+/11SymrdHBj7lGgJCWCctEbwkD9EfCh2l zUHg== X-Gm-Message-State: AOJu0YxOxQKMJXoKUPCbaiD6IhUxgAnPvMdIqh5VfSy2U7Id3tW8tgIi piigWxVQAWutap1IBL1s/4s70eG0xOnS1jKzxfQ= X-Google-Smtp-Source: AGHT+IFxp+JzhxcUoVKHvslrPps8Dw4oEec667yj2IQDPVIScvroTp5bGUX0pzhx54mNHPlMRJFIAA== X-Received: by 2002:aa7:900b:0:b0:692:6417:728a with SMTP id m11-20020aa7900b000000b006926417728amr20734036pfo.14.1698193812121; Tue, 24 Oct 2023 17:30:12 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id z21-20020aa79f95000000b0069323619f69sm8400317pfr.143.2023.10.24.17.30.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 17:30:11 -0700 (PDT) Date: Tue, 24 Oct 2023 17:30:11 -0700 From: Kees Cook To: Justin Stitt Cc: "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] scsi: ch: replace deprecated strncpy with strscpy Message-ID: <202310241729.1740ED4F@keescook> References: <20231023-strncpy-drivers-scsi-ch-c-v1-1-dc67ba8075a3@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231023-strncpy-drivers-scsi-ch-c-v1-1-dc67ba8075a3@google.com> On Mon, Oct 23, 2023 at 08:20:14PM +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. > > These labels get copied out to the user so lets make sure they are > NUL-terminated and NUL-padded. > > vparams is already memset to 0 so we don't need to do any NUL-padding > (like what strncpy() is doing). > > 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. > > Let's also opt to use the more idiomatic strscpy() usage of: > (dest, src, sizeof(dest)) as this more closely ties the destination > buffer to the length. > > 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 Yup, these all look correct. I can confirm the open-coded "16" matches the now-used sizeof(). Reviewed-by: Kees Cook -Kees > --- > Note: build-tested only. > > Found with: $ rg "strncpy\(" > --- > drivers/scsi/ch.c | 12 ++++++++---- > 1 file changed, 8 insertions(+), 4 deletions(-) > > diff --git a/drivers/scsi/ch.c b/drivers/scsi/ch.c > index cb0a399be1cc..2b864061e073 100644 > --- a/drivers/scsi/ch.c > +++ b/drivers/scsi/ch.c > @@ -659,19 +659,23 @@ static long ch_ioctl(struct file *file, > memset(&vparams,0,sizeof(vparams)); > if (ch->counts[CHET_V1]) { > vparams.cvp_n1 = ch->counts[CHET_V1]; > - strncpy(vparams.cvp_label1,vendor_labels[0],16); > + strscpy(vparams.cvp_label1, vendor_labels[0], > + sizeof(vparams.cvp_label1)); > } > if (ch->counts[CHET_V2]) { > vparams.cvp_n2 = ch->counts[CHET_V2]; > - strncpy(vparams.cvp_label2,vendor_labels[1],16); > + strscpy(vparams.cvp_label2, vendor_labels[1], > + sizeof(vparams.cvp_label2)); > } > if (ch->counts[CHET_V3]) { > vparams.cvp_n3 = ch->counts[CHET_V3]; > - strncpy(vparams.cvp_label3,vendor_labels[2],16); > + strscpy(vparams.cvp_label3, vendor_labels[2], > + sizeof(vparams.cvp_label3)); > } > if (ch->counts[CHET_V4]) { > vparams.cvp_n4 = ch->counts[CHET_V4]; > - strncpy(vparams.cvp_label4,vendor_labels[3],16); > + strscpy(vparams.cvp_label4, vendor_labels[3], > + sizeof(vparams.cvp_label4)); > } > if (copy_to_user(argp, &vparams, sizeof(vparams))) > return -EFAULT; > > --- > base-commit: 9c5d00cb7b6bbc5a7965d9ab7d223b5402d1f02c > change-id: 20231023-strncpy-drivers-scsi-ch-c-23b1cdac43cc > > Best regards, > -- > Justin Stitt > > -- Kees Cook