Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2234184rda; Tue, 24 Oct 2023 17:27:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGNAxEFOIqhDjdQ/iN+Fi+KHtZprBLdp7omEdRvP2T9YeU+PLyYB9WZz3E8KfD8rq/DySii X-Received: by 2002:a17:902:e841:b0:1ca:1be:63c8 with SMTP id t1-20020a170902e84100b001ca01be63c8mr11547112plg.11.1698193676213; Tue, 24 Oct 2023 17:27:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698193676; cv=none; d=google.com; s=arc-20160816; b=zqioFXsQqCzpOESguq5Hl8qRom+Tr4K2jn7S6qq0JTXTbNQ/wyctepAbxrgECZJc80 kJ+SvKTx3if4qPTw6AfLJpCGlF3VDeXUPCoGnnV0fdARLHbx5ZbqMqrdtfwtsw/SkjGj cNFoO3R6B4rj1UDpmtyOsMCOxP/7FLftJm9zCbhBF8iH5UU1v7NI+ohPt5eFn4kJqFft Idx+12bxtf07U6QuBhMSIkICQTJA94qx8E9xOQ5UGPQKsNhaUFE9VngJsFdlc4vdmbrg R3Td8CqoN2Y5bCsTJONX7St+FoTtmgGCbY2QxnS/f/26DSrbaC0NwNTVVRAxLcNfI7Sm tJKg== 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=TH/bxJhzk4YBMuPvj5C/RXj2mSj5ZNHWKAEzVgv6LAI=; fh=06jBU4kymo27Q3YbKB4DacilIvjP66py05wExB/zKM4=; b=VDCEw1lzrR6C9E9+a/g2ilY1Orw6QAtEJ7qlbCEgjLCJT3RmbOXuJsZcvTQjPXIswo tuXBjGV6jtpcJ7trKAoY9kP/495v/odK2A/slD58R0kUZ3oeWcrYr4vHvT3KczTHxQ0Y aoP16zYSyynrf0Ojo884tPjjw7eYzP7qsVHrcQEnWEvpDktAq10VcOwMJO44+Tt+i9w4 C6vGGKlYIYYGv17Z2dRPJ92mj6ek+sjE/2BEEg+R3k1EuTXK4HsCPzfHHTcgidBuwA4h WSp22YYsOMHIGXHzsFQlpqb/Izv4T6uKNJ5ppvjiopwAkYIkRmp9QZK8Pd1Y8q1tjTvT PAVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=elW45zfK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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. [23.128.96.38]) by mx.google.com with ESMTPS id o15-20020a170902d4cf00b001b9dda4e1e7si8951873plg.154.2023.10.24.17.27.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 17:27:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=elW45zfK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 E232C801D989; Tue, 24 Oct 2023 17:27:53 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344753AbjJYA1r (ORCPT + 99 others); Tue, 24 Oct 2023 20:27:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344718AbjJYA1q (ORCPT ); Tue, 24 Oct 2023 20:27:46 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B092ED7F for ; Tue, 24 Oct 2023 17:27:44 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-6b20577ef7bso4151849b3a.3 for ; Tue, 24 Oct 2023 17:27:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1698193664; x=1698798464; 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=TH/bxJhzk4YBMuPvj5C/RXj2mSj5ZNHWKAEzVgv6LAI=; b=elW45zfKHiHj1TAULAWxgVgKeQv8BHQRj+pr3j0026rN6lz4+0DpEhpKyKnvC0eu8/ de577vi3jiMu5qa0zKFPdyk7mBVQk02BCZv26IFxhvihrhiA7I1pB3C4RAXDZcW6zw82 nFWL5L/7QyvvV9nxYxtAFXnRON+6aDbPvTmZg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698193664; x=1698798464; 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=TH/bxJhzk4YBMuPvj5C/RXj2mSj5ZNHWKAEzVgv6LAI=; b=XSV7OUThUiSKozNqSNCYYvrViBGUbfUeyFq76poSj1c+R7nmXjQGJ59u0l8WilkKCw 8/rL/WHGCprPEnWbAuDIR9FqtTCtQJ7jEnvEQfjI/jtOxtn3xpqTOyoXjAQSDOaMhvYy ndhuj7ivtrTouoUE50yoi27BYUGtzOW/qPRXnkPOq6liqRlWRU46qPtJRUr47HE/fFDU oUJ1XrgY+avOIulw1fRG/SyYC6WokeSitSzCNK8xaKI4vffQ1GSLcqO29RpFhr31p+66 9IJr+Tdbo52ecSoEk3uy2vyoUf5td+MkYZjGMjF7q1at+mSIiLQbI58DJ/UTl1d0Q8eK s4GA== X-Gm-Message-State: AOJu0Yzyx72c4XcjCE+TDEi3GlBQnQ1ZDTbmf4yKzHlHsgVW89/p3Jh/ TkU9Rz8YdkiETiqoPj/EOHn9bjAPtIxxQXJKwo4= X-Received: by 2002:a05:6a21:a583:b0:155:5c28:ea67 with SMTP id gd3-20020a056a21a58300b001555c28ea67mr5196136pzc.38.1698193664123; Tue, 24 Oct 2023 17:27:44 -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 t22-20020aa79476000000b0068e49cb1692sm8139544pfq.1.2023.10.24.17.27.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 17:27:43 -0700 (PDT) Date: Tue, 24 Oct 2023 17:27:42 -0700 From: Kees Cook To: Justin Stitt Cc: Adam Radford , "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: 3w-sas: replace deprecated strncpy with strscpy Message-ID: <202310241727.A4EB83D8@keescook> References: <20231023-strncpy-drivers-scsi-3w-sas-c-v1-1-4c40a1e99dfc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231023-strncpy-drivers-scsi-3w-sas-c-v1-1-4c40a1e99dfc@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 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]); Tue, 24 Oct 2023 17:27:54 -0700 (PDT) On Mon, Oct 23, 2023 at 07:50:57PM +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. > > This pattern of strncpy(dest, src, strlen(src)) is extremely bug-prone. > This pattern basically never results in NUL-terminated destination > strings unless `dest` was zero-initialized. The current implementation > may be accidentally correct as tw_dev is zero-allocated via: > > host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension)); > ... > tw_dev = shost_priv(host); > > ... wherein scsi_host_alloc zero-allocates host: > > shost = kzalloc(sizeof(struct Scsi_Host) + privsize, GFP_KERNEL); > > Also, further suggesting this change is worthwhile is another strscpy() > usage in 32-9xxx.c: > > strscpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION, > sizeof(tw_dev->tw_compat_info.driver_version)); > > 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 not be accidentally correct, let's be definitely correct. > > 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 Looks legit. Reviewed-by: Kees Cook -- Kees Cook