Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp852340lqh; Thu, 28 Mar 2024 20:49:08 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUGy2MRNhU/vbXCnbJpsegXDnrWqVV6BXdWqCEjlt5GwjIDrPyOWnJPp3r4INrlyFYCyOdxf8fV9QjUFKh4aBkT+DLl8Uw0qwWdp1XNYQ== X-Google-Smtp-Source: AGHT+IHhKHIIpR+O28r0SFqSKRgRcrz+OfbRPOj32Yw09DF736aB1cZiQ3htG57tCRmBwF/DxwuO X-Received: by 2002:a17:902:db0e:b0:1e0:b5ee:e806 with SMTP id m14-20020a170902db0e00b001e0b5eee806mr1578987plx.10.1711684147894; Thu, 28 Mar 2024 20:49:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711684147; cv=pass; d=google.com; s=arc-20160816; b=vdDf8Vx0BufDLfvBL0sDjlAZNN58Ji7VcJOIuBpi4nTkBVuxxNAw2CHJ1l6/raB/7Q q1qP40FcRBm/14X02x4psERYVaD+7OZYjj53j3X63kdUIfkAmBMcm1/O2zQOdj8qn6tJ zMJwhopBUKhwDa7YqnW2j3Z6B28ahD6wAduiWqFx4tSsLFj2hMWOHMD6Ou2JE+5cFbG1 nHGQGgCczibSnaBy3mHf9G5hP9oXFg1l+Zsds7AB9WXsFaG0uyUikSxzq+s+BmWqDIHt aG2YQB9ecxc2enF9X41o3C0n8cRjoJQmGH5lcDblBJDdbpXMInxdBmtRhe0rSFx0rEu8 x1gA== 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=9NF1vh87Z3mwXJDlnEmyzUpBERWIz9npu85xKfyBrrY=; fh=JUHGOce2ajR6MipN+G/G9p/vjQL4JG8mIxawgnFbFPU=; b=kBr1RzdRBW4TXob+9KzTz4L6Lo8Kp2xur0hHsimmLuoXGXY23PQqkgsQ6Cbx6JP7b1 NtzPBHjuk5JocSuN+iwEcaLt/QGCYBfRqjy1IrIvJqU4SA+V7pgFr2z4/4d7gNYsK+zF gEA/gy4kaZyQQkX55h8+d0FYERnU7Go3svZLCDe6dYXYlvuqBHEKKegQPq4F6pJtrGdb sLpBJR+i+Eskxl6WTZ+OKnZUqfIRtZauNbBEZVmhGT6L3nvLumjb7RbzDoj/NXx9uNKP 4cG/EFOTl8um4hxuwyBqG254kGkQnYvJvoAv+5+4FdAKFmitkJxJW7Zg/z/7kApCyvdQ 4Hpw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MDx+ur1e; 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-124094-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124094-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id m23-20020a170902bb9700b001dda34e84c2si2642961pls.649.2024.03.28.20.49.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 20:49:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124094-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MDx+ur1e; 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-124094-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124094-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 8F760289FFC for ; Fri, 29 Mar 2024 03:49:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 912D439FD9; Fri, 29 Mar 2024 03:49:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="MDx+ur1e" Received: from mail-oa1-f48.google.com (mail-oa1-f48.google.com [209.85.160.48]) (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 5108E2E3E4 for ; Fri, 29 Mar 2024 03:48:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711684140; cv=none; b=Y8ml09k1YzXAtbtQ8ydpUxVTAuVrGL+FTV/XsFLlEkuLTcN4/MW3F84bKbcmma0J/tJwffwINV2uSx/stodIQrIIp6npDUbiWckQuU5Dy7Z7hWMGLR+KtnmSdM5kgiKeSTnx54vFQAxXwIQ8K2/AwCKan4/JDnGevStJltXgPIU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711684140; c=relaxed/simple; bh=iYpyGSKz67JfOWGPXQAIeOlsiBo0FH9Kf4YBYEJJw6Q=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=p8Le0x8FxjNL2pTabW+Ka6Bi0bEFOL1Gaezi09OYrNu8piLcoC5cHLp7mp24K0IWCOzivV/IvDMZ1Zd1oKlH9SUo9L7wjFWxW93E+CcEWTC0JtcYtmHLPphZ6J2ZlQSN+klWOR5Ylm8f69BszWd315V6jLYP/TG6xAUyuJLpf4c= 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=MDx+ur1e; arc=none smtp.client-ip=209.85.160.48 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-oa1-f48.google.com with SMTP id 586e51a60fabf-229a90b7aa9so817143fac.1 for ; Thu, 28 Mar 2024 20:48:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1711684138; x=1712288938; 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=9NF1vh87Z3mwXJDlnEmyzUpBERWIz9npu85xKfyBrrY=; b=MDx+ur1eKD3xF2h3SIYuVNRbtsDEGxYhTiQbkOEOaV2LbF6U643n16NIkJ/gVbxr6E WDbAfrYJXqFdgaJRFOXNYK5KYJObGOOsxtj0VHKuRGqAdqlspkW4iKeab6PfE9nDFD5n Vy6wf58oc00JtCbTGVxOViVEIgFRMQDGJ4fZ8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711684138; x=1712288938; 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=9NF1vh87Z3mwXJDlnEmyzUpBERWIz9npu85xKfyBrrY=; b=Owwp6GB6ctASRMp9rTRSk2LTb1YiIxX/COvQOZeUgjsjc7PCEw+o/0vjoCl8rCVr6O ZDsC0crcpzQ5xiak9/xuZOsux0+nkjmSVDrEOS7ThNN+o5S096K1LvSL7Pp6gXJweVTD DnrLnr/SrfYZZ85xRjQVXGbF1acekL7KyCl5BaWIEWhV/l0NN1Nd6DsspQV7mM4SPnEo FlXosGW9yuHBOt9qSY5QdWyiNjxt9r1MCRg6XhprhPDzZvr+q/WMZtOBO0k3h9eDVkNy tsoT2W7UaNDXHYOUf1slKsiUCAdyw8e1j7P7gJL+KD3dxvQLK/PQ7GWemMwMTP97xduq 3XHg== X-Forwarded-Encrypted: i=1; AJvYcCUDcrLNur0aUYu7BPs02mfH/X2f/VvUi/eLNtiJg9I8aMP7qxztjtTU/p6If64v4ShvblnThEV6XUp5OtBsBfGv0S2/BZb1S5Z+Mn+t X-Gm-Message-State: AOJu0YyPqNWjkX2EndWi1dLqADT98N7LYREkeHQUb875gXHPTEClH/8P Ao2mD+b4yCdK/K9QuaAbYHzO9dZFPcL0IPv4o6MQ2RsuJ5TeIPRSMIvqSEeBBg== X-Received: by 2002:a05:6870:42:b0:22a:8443:45bb with SMTP id 2-20020a056870004200b0022a844345bbmr873513oaz.47.1711684138431; Thu, 28 Mar 2024 20:48:58 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id y12-20020aa7804c000000b006e6b41511fdsm2221794pfm.94.2024.03.28.20.48.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 20:48:57 -0700 (PDT) Date: Thu, 28 Mar 2024 20:48:57 -0700 From: Kees Cook To: Justin Stitt Cc: Phillip Lougher , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] Squashfs: replace deprecated strncpy with strscpy Message-ID: <202403282044.30A82BA37@keescook> References: <20240328-strncpy-fs-squashfs-namei-c-v1-1-5c7bcbbeb675@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-squashfs-namei-c-v1-1-5c7bcbbeb675@google.com> On Thu, Mar 28, 2024 at 09:52:59PM +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. > > The previous code took special care of NUL-terminating the destination > buffer via a manual assignment. As such, there is no bug in the current > implementation. However, in an effort to rid the kernel of strscpy() typo: rid kernel of strncpy. :) > [2], Let's instead use strscpy() which guarantees this behavior [3]. To > ensure we can copy the same number of bytes as before, add 1 to the > length argument provided to strscpy(). > > 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 [2] > Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [3] > Cc: linux-hardening@vger.kernel.org > Signed-off-by: Justin Stitt > --- > Note: build-tested only. > > Found with: $ rg "strncpy\(" > --- > fs/squashfs/namei.c | 3 +-- > 1 file changed, 1 insertion(+), 2 deletions(-) > > diff --git a/fs/squashfs/namei.c b/fs/squashfs/namei.c > index 11e4539b9eae..6c4704ba8f42 100644 > --- a/fs/squashfs/namei.c > +++ b/fs/squashfs/namei.c > @@ -80,8 +80,7 @@ static int get_dir_index_using_name(struct super_block *sb, > } > > str = &index->name[SQUASHFS_NAME_LEN + 1]; > - strncpy(str, name, len); > - str[len] = '\0'; > + strscpy(str, name, len + 1); Otherwise, yeah, looks right. Reviewed-by: Kees Cook -Kees > > for (i = 0; i < i_count; i++) { > err = squashfs_read_metadata(sb, index, &index_start, > > --- > base-commit: 928a87efa42302a23bb9554be081a28058495f22 > change-id: 20240328-strncpy-fs-squashfs-namei-c-9d01b8975e53 > > Best regards, > -- > Justin Stitt > > -- Kees Cook