Received: by 2002:a05:7412:798b:b0:fc:a2b0:25d7 with SMTP id fb11csp42347rdb; Wed, 21 Feb 2024 16:31:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU8CiR0ON8M3hFhiulSP1yvWMdu4Hmd1eP5Gec9Ikz2lnJhCGwIXG92JcVIGm31m5LacnB4HtrlavaUs4T2uUviz24lW4oiVc73FvdvsA== X-Google-Smtp-Source: AGHT+IEct/S0igyWvPL/BbFDh9uasMKQG1SmVq+3HNdUxGgY3Sj/WOHoAk42KwMDLKmBZUkwSijh X-Received: by 2002:a17:90a:6e41:b0:29a:24c6:5cd2 with SMTP id s1-20020a17090a6e4100b0029a24c65cd2mr2439566pjm.32.1708561876272; Wed, 21 Feb 2024 16:31:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708561876; cv=pass; d=google.com; s=arc-20160816; b=jj33KgWfIs9ZVfCeDnAQOVcKwhxqTq55w+4b4eILRpTfmwc1GJ171eu8sFVSvY58y/ IZDRPE3RnuH/a3UijOIl2ry+zhsb/Wh+dqOVkOvVpPsiQ6reXcFMPFktXjEmSF8X49WJ hB2/Z/pTRP2ikR4l0GkRxcD/4/Zp8tqC9WR7jlksHshEFQemOG0v/Ee1EHFDd4/mrRBb cVVci7An7jMmpa8kQ9QyMzf/EhvGwVpbaQnfx22dn8ngg4OjKF240sRBi2pkEmrMEZzH IcbkE+1FykOaXJJPfLXYot/NVpeLCJFIbGQY92BHK6h7eijYRIWuwH2LM7tKhCJe2A6j fn5Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=cphzXha2ug67Fvg8bHldw5MnRmdUMWu6AQ/eN1G60FM=; fh=lc97tBVFXylzhHqy1L2xYUKlzNuGiI2fI0/3jPfcqdU=; b=BvV6sz8pOfy2FaS81p7i133172o7JezkI1pvXfGRBQre8KtKBEH3bPyFuUZRhLlJe+ Zk+XJeGYWP2yQtu4KODxf4Zc7EglcCf8m2ocB5cl5hBrw9dAxNeoTeuG0+sQq1i9JDCO oEsNCrOVKP+CPstzBXECBOFEA7PZvIH5W4+brMZafi6h8uFCW6sjruCSPm+bqyEjX0eX lfSynSISdBTf93Z+iIwk9bmLNfoikrMpczLeTG8r3lvwDzV7PEaBaSvoTCfpWy6ib/vz s9gPOqCySne+2MINgEcabQIMcug5U7dypl5YRR8xTHLcjoBtWcN6gR1WAoHyh8yZb9An IGqg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=FewjVpXr; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-75725-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75725-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id bg13-20020a17090b0d8d00b00299521eddabsi2391882pjb.85.2024.02.21.16.31.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 16:31:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75725-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=FewjVpXr; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-75725-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75725-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 59CB0B232BE for ; Thu, 22 Feb 2024 00:30:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 84394101EC; Thu, 22 Feb 2024 00:30:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="FewjVpXr" Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3DBD22919 for ; Thu, 22 Feb 2024 00:30:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708561835; cv=none; b=AVXyn4qpNW3RDq5j2Cd5vAlW8PZAPDm4EF5Vn12+KUoBVPHR9yH06YFkMGeZ9dUuEM1sAL4QpvszhT9xVLonnCqUVXWgOb00jAlHv7BcNludvOLC1TvSAJa7sSZBSIGuYk5ZV1yOHN8mZGs+pxY/yHt/mscCRUFs1sSoedYLf0E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708561835; c=relaxed/simple; bh=0a+PXUr4/M4QxUhsk3p3ht76EXkTT/vUlf3xCYroLtw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=u7GkDU3WLXOJ0Htv0czbRRq3ZV8RvHe1FPpzUmUz7af3JUnTymKsoP0BcrzUt1yzpH0qndFwlHQQzjGJJJ7PRAFfnzbGLvXjkYBaPr5t0WCs7XeOe3Cmmw15syqiFXEY1HA13DXSLlpg3JnQMXkxgSYB/Tr3G+82uTgyUQ4dW7g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=FewjVpXr; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-29041136f73so881354a91.0 for ; Wed, 21 Feb 2024 16:30:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1708561833; x=1709166633; 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=cphzXha2ug67Fvg8bHldw5MnRmdUMWu6AQ/eN1G60FM=; b=FewjVpXrIrXCAek5YMytycaY9A9MQ2RoFd3GLoRK2WyUbT57vK63pWTQjLE7j7ugiX nPWzTjI6JiIvnZ6HIzDShW6NbJYJs2Qo3e26peOn8PjRFSgB1gIlF5l4r9fFkVB11kVU N9pxagOfd4lKZF/jSVOGF/q8qW7XLoBsp1oRk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708561833; x=1709166633; 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=cphzXha2ug67Fvg8bHldw5MnRmdUMWu6AQ/eN1G60FM=; b=tdwjQ0NfYS7+GhpUFH2ZHLBeHYKwJzPHQPtruycf0ehA8Q7+VfxpGLb7u9DexNIGXe 7wwTy9azLj5TBEF/qMisNlDQTdKRFIOcvlXg6GTIT6bT7u0YknC0hSUjNbuGA3ZLo9Br V5vX4lIssf+4BWiJcj2vEb111/aJBNfQ6LSfl1etWb5Il/+SDHAB4l+vRpGvACxINiRZ kwNpRB861i0/6RGC38DQcmTwEuQFXKLFSWj8oExBaKB6VAj1NleqL2tht+VUbXp35bMS evoQ1SsAbDLZ7F1uQPNGXIpbsWq6qXh17j/FiU/Y3ZJx7ocXdfFrhw1XnY9RBEGEhVvB Ws4A== X-Forwarded-Encrypted: i=1; AJvYcCWLjzouRKjozk8AJ78a34gjXUVjgVkhRrxe/lg54Q6GZl7bNcPBzLVIYxLVnquR6R09Muov0H1ewGwyWYkln0/bdZiIqJ/YDxetHNii X-Gm-Message-State: AOJu0Yy8pPgyTZyC4Lj6ENCI9ot+R3leZKtQaz7E/VZoql1KIU4uk9qx 40ILCeZVUMrOkUhH1ehWOZ7g4MUS7CMg6G5u1VK7V1972l31WJxnT6fcxjeLoA== X-Received: by 2002:a17:90a:b902:b0:299:3258:4053 with SMTP id p2-20020a17090ab90200b0029932584053mr14295400pjr.15.1708561833583; Wed, 21 Feb 2024 16:30:33 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id e21-20020a17090ac21500b00298dd684b8csm10445951pjt.32.2024.02.21.16.30.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 16:30:33 -0800 (PST) Date: Wed, 21 Feb 2024 16:30:32 -0800 From: Kees Cook To: Justin Stitt Cc: Hannes Reinecke , "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 v2] scsi: libfc: replace deprecated strncpy with memcpy Message-ID: <202402211630.184FE822FB@keescook> References: <20240221-strncpy-drivers-scsi-libfc-fc_encode-h-v2-1-019a0889c5ca@google.com> Precedence: bulk X-Mailing-List: linux-kernel@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: <20240221-strncpy-drivers-scsi-libfc-fc_encode-h-v2-1-019a0889c5ca@google.com> On Wed, Feb 21, 2024 at 11:50:26PM +0000, Justin Stitt wrote: > strncpy() is deprecated [1] and as such we should use different apis to > copy string data. > > We can see that ct is NUL-initialized with fc_ct_hdr_fill: > | ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rspn) + len, > ... > > In fc_ct_hdr_fill(): > | memset(ct, 0, ct_plen); > > We also calculate the length of the source string: > | len = strnlen(fc_host_symbolic_name(lport->host), 255); > > ...then this argument is used in strncpy(), which is bad because the > pattern of (dest, src, strlen(src)) usually leaves the destination > buffer without NUL-termination. However, it looks as though we do not > require NUL-termination since fr_name is part of a seq_buf-like > structure wherein its length is monitored: > | struct fc_ns_rspn { > | struct fc_ns_fid fr_fid; /* port ID object */ > | __u8 fr_name_len; > | char fr_name[]; > | } __attribute__((__packed__)); > > So, this is really just a byte copy into a length-bounded buffer. Let's > use memcpy(). > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] > Link: https://github.com/KSPP/linux/issues/90 > Cc: linux-hardening@vger.kernel.org > Signed-off-by: Justin Stitt Thanks for the refresh! This looks right to me. Reviewed-by: Kees Cook -- Kees Cook