Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp851021lqh; Thu, 28 Mar 2024 20:44:20 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVBHAZTP3YZVZ9PJb9klLguuuRuRZ06iZ9OhelKkOARjej4pK8cCVdn2cmK4GWdO9emC7g/lKNSQCb6cV5PSdF2aOhHPBe/m9I94HNYjw== X-Google-Smtp-Source: AGHT+IGoGj9Hi6cB6fVENWTxosrZM4gpN6m7hOC3fFMEAEhhqrVl2vJd3+HZkZyvC8uiMmy2uKD7 X-Received: by 2002:a17:907:e9f:b0:a4e:5ce:8444 with SMTP id ho31-20020a1709070e9f00b00a4e05ce8444mr919448ejc.10.1711683860203; Thu, 28 Mar 2024 20:44:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711683860; cv=pass; d=google.com; s=arc-20160816; b=0ymz0El6agTboajYQ7cgkZYZ6h5sEjepJcHMZ9KiD2PNm2TbOuOLb5Zy+/A5oRS816 eljvV16s55hN4Ir1M3rcsyP52/wDtFVFnosGy3gq3gUni9ATReVPajwNVHuNmLL3aWiC Y9nNtg0TiCzx9b7RliSf/2DXH7A3zhO/iM4sNewSF14+ZJgc4UnPM1SC8tUswiDKt6/L 7FY0DE9DE7reDmRuPc7OrVfSpdkS6lVZt5b0tO3/wWMa/Gg+sQuUac2bPmoOkhIA88FK lP1ka8MSBjlbv4iN2jCQlqGU9r3B/Lm4HJSG17cXYV46uzp9UUm0fo9b6o1wfkmcvYNQ lu0Q== 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=iYyfCUJffNflR102beFTLmybYVFgFGdBqWkYnepitbw=; fh=423Ku6dGqxJS2JgZh6fZrEHzB4mzOTEh5GEWbgtRHhA=; b=I7aHznsUImFt4Ou3I5kxzUBr9Q1oW+q3GHI0u2Xt4YWFCqE+coVUNF9EI1ssu2++5+ Fn6ESKJi0q5yND2bI3wrs9af2byeURLm49NnOSIXLpCdV5KCPKbfnLgXx0aCaQ4prJ5n xnArqYN8paZRx9s0TpPhaIYtfUP+T1bWGG+JXfr0IANqYldlib0zaqVIp0rJSyZolbKf QtEj0YWSOxjsRG6T1yUJbVUzOiHjJOtsmz3cjKvXe1bmyTaEyz19rLLK6oAO/BfSVC8E 4G+S0QLkYheIYgWo5S7zH0YeVd9Dix4QiWU7lOeS60HvOMD0vBax0w4WjH+ZUGWIhZts wAxA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=U3xGBqgq; 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-124092-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124092-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a11-20020a1709065f8b00b00a46327b4fd8si1308368eju.645.2024.03.28.20.44.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 20:44:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124092-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=U3xGBqgq; 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-124092-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124092-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 am.mirrors.kernel.org (Postfix) with ESMTPS id E9F5F1F22CBB for ; Fri, 29 Mar 2024 03:44:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B7E3839FD9; Fri, 29 Mar 2024 03:44:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="U3xGBqgq" Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 634CE748F for ; Fri, 29 Mar 2024 03:44:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711683851; cv=none; b=TscRcLv/e7PyYxMKRv9kQKA3YR1RVN/KRojE7dbTgJBAgFZDg6f9F1nxyQa4r0d+SMrlOPtsuzCyQ1TuHa4dNnN2B7zTSGTyeJoqHKVStfgMreYOe6nYs/l8zw3LZqFuyGN8PHXpLwM+nWFuQEp6CIL7EJLo/k0ZLOaO0oeEfO8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711683851; c=relaxed/simple; bh=Hn6PBrUxtM9PEhALDsKdQiFNiP/Av1s0kzCsSuzmZlw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=WzWhIIAqtE4NDxRdG1YDnF6+SLd19wwn6iYqSafnm/eo2TD2gnD8K17TJGfLoaCjBos09YLiHG7v4vjAPU/yDd3lxx3y/QcThBTQ/JS7oAcUBzSGc8q4KfBMMnGCG/HulgJjKojHTD7UoCap7MnQnFfz3uA4sUjXGwjYGOv8/i8= 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=U3xGBqgq; arc=none smtp.client-ip=209.85.210.173 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-pf1-f173.google.com with SMTP id d2e1a72fcca58-6e782e955adso1528978b3a.3 for ; Thu, 28 Mar 2024 20:44:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1711683850; x=1712288650; 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=iYyfCUJffNflR102beFTLmybYVFgFGdBqWkYnepitbw=; b=U3xGBqgq1ai8g4wb/Amvm1W3TYvkHCDNlrMoNffhbPLkSzf8Fi4qrgqw5BpIlhXdjE UZyNEANF+YOh5wqDiaYdZF9+eCEeLx9WVsxlp6rn4ehMMcIX7gwFfmBfNIqXHbcGmAk9 3eu+JXp8asy/RvUufdSlg+PhAZvAPP3Tv8eSk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711683850; x=1712288650; 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=iYyfCUJffNflR102beFTLmybYVFgFGdBqWkYnepitbw=; b=jie+RoodsxkuJWLXaZkTf6VjPL5f9Y675KceEn10hp2/v9iyx1kLxja96LymS14lB6 0Fbi6TVjcS04hvKiBx8dM2eIr1hOqKNENBFOzYOPFyoyFobxIUlqxqw6P0apvItZT985 hZatY3o+n2qsQMfCabI0AHsF97raGtn5IEreqFX3c+j7H3/Qr6tfrpzxPw2advbpJALK EgBU9IZxsGEr4jZma1dEqokC+QRzoDizlsNWrckmMdyOVrC/4W/TDd6M5aooFx60W4Oe HvirpN1ouGWs26NC7JZid6P8boZ9yI9RhB2FIzGzwlEQq7jgp/vzSKVRZwOrvnL9J12D Dw5g== X-Forwarded-Encrypted: i=1; AJvYcCVK82Mgf6esvyG2GKODjgEpUlfVX4Mc5b5n6f4HDRyyJaazfjL9x3dauSl7zGe+JBtJAVNB3BEylhBFtTJ0WXLfMt/HDHpxfryJSx85 X-Gm-Message-State: AOJu0Yw5riVtMChDfCHMl1mf3l74X9UobDkR35C0FOedx59KjUT712x7 ZFf8P+EB6FQM1N8/GeQRcAC9nVpEuBfYyurKFsyBRw/ZntosAsEaGqEVscxiug== X-Received: by 2002:a05:6a20:1595:b0:1a3:8984:b334 with SMTP id h21-20020a056a20159500b001a38984b334mr1223944pzj.22.1711683849766; Thu, 28 Mar 2024 20:44:09 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id s1-20020a17090a074100b0029c3bac0aa8sm4691791pje.4.2024.03.28.20.44.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 20:44:09 -0700 (PDT) Date: Thu, 28 Mar 2024 20:44:08 -0700 From: Kees Cook To: Justin Stitt Cc: Steve French , Paulo Alcantara , Ronnie Sahlberg , Shyam Prasad N , Tom Talpey , Bharath SM , linux-cifs@vger.kernel.org, samba-technical@lists.samba.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] smb: client: replace deprecated strncpy with strscpy Message-ID: <202403282041.6A4E12EA@keescook> References: <20240328-strncpy-fs-smb-client-cifssmb-c-v1-1-30d12bcf500d@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: <20240328-strncpy-fs-smb-client-cifssmb-c-v1-1-30d12bcf500d@google.com> On Thu, Mar 28, 2024 at 09:44:48PM +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. > > In cifssmb.c: > Using strncpy with a length argument equal to strlen(src) is generally > dangerous because it can cause string buffers to not be NUL-terminated. > In this case, however, there was extra effort made to ensure the buffer > was NUL-terminated via a manual NUL-byte assignment. In an effort to rid > the kernel of strncpy() use, let's swap over to using strscpy() which > guarantees NUL-termination on the destination buffer. > > To handle the case where ea_name is NULL, let's use the ?: operator to > substitute in an empty string, thereby allowing strscpy to still > NUL-terminate the destintation string. Yeah. And for the non-NULL case, namelen is 0-255. And 255 comes from strnlen() as the limit of characters, so namelen + 1 will include the NUL terminator. > Interesting note: this flex array buffer may go on to also have some > value encoded after the NUL-termination: > | if (ea_value_len) > | memcpy(parm_data->list.name + name_len + 1, > | ea_value, ea_value_len); > > Now for smb2ops.c and smb2transport.c: > Both of these cases are simple, strncpy() is used to copy string > literals which have a length less than the destination buffer's size. We > can simply swap in the new 2-argument version of strscpy() introduced in > Commit e6584c3964f2f ("string: Allow 2-argument strscpy()"). > > 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 Reviewed-by: Kees Cook -- Kees Cook