From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 4B8A53A1A1 for ; Fri, 29 Mar 2024 04:11:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711685467; cv=none; b=DJKQ8TaVh8/MyRy8cMF3bGlQv4boH/h0pSQPPHM/r6sarMv5Y0MqUEP3hz702U6vhM4Pf1cif3BMXWOZPcrMiy5+5ulZ05w23QdWwmEO1CNPLLo6ix8WTR0jmml+BIyFIe8cgWVv/XPUgZEclOz5k1lO3OgS6avsb+LA+ueen7c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711685467; c=relaxed/simple; bh=Sej1B+KPaAd1YJSUCEcu/2wZgvFA6dEVc0yLCqlWKKc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=XOJ3inSSdfNBNA90Ul5qHTGakTkhOo6MnObJonIYforNCZE1IcnjSphEWhCJ10fuzszLzbPk3eT5LAKZamkecXxHQe4+523Ghib1FjKUKe4Kc7VYxT8lBTJFhAma+dmoZVv1kpmpFWQoa97awrqbBz82OWy/pEJ1rGHUCG1sDAk= 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=Bsc6whB1; arc=none smtp.client-ip=209.85.214.170 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Bsc6whB1" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1dddbe47ac1so18068435ad.1 for ; Thu, 28 Mar 2024 21:11:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1711685465; x=1712290265; 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=bUIGGacJ5zX9tZrBNytRyyuYA7UqfnaLQvQzbVYvhP4=; b=Bsc6whB1aT4BfVf2DYDvvXHdKALklrXOeLkXkrHf34upWS1iCcvc6AAISo9LKFWhsO 8lXeGDDPpfXbS8Thj6efTHMD9vImhs4EFze6Ek8JpC2MksAythzgnyOd97cOh+1Gwup0 lSGn8Z5ESeQZk11+HXsYWo94aZqhiZmqL1Kjc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711685465; x=1712290265; 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=bUIGGacJ5zX9tZrBNytRyyuYA7UqfnaLQvQzbVYvhP4=; b=b+kqDiB1StEajjxLgoTZC0U3bscqEtBGQm+jb0E0yXbpY4u8WghifO1K+S2RenaNcJ VLNfj9y4JF8CiU9/Q4h154kGiL3gX0Q1M476JoQshrfgTuBl5AtMNlzi/Dtylv0xdZOV ywMywBvAL2Lqqb0JvtFJbLSvOyjUjJw7SDXJjIRewq+10n3uGPpLOcPiSHv6QudAyebC Wq/oaGBjUzcqsGYUBbfKYhBcBD1jQ4ChKcqU40XFWKVjZE1dgdqI5j+Zd9gaYn+e/S2b KF87dpPg/B2hX57SIQivoVgbBauyNUO0j4ylQJw7D3WZE813i9HDhYFxkCtozNKI88/s SWGQ== X-Forwarded-Encrypted: i=1; AJvYcCX8MS8d8/dmF6khIiWNxr+AUIlOspXAb72WclnHbeuDpEHI5Ke9oZby3efQnbuosD8tzLvT3URCCgJff3UkVPabzmpKHjVu9f2gbN4xbU/T X-Gm-Message-State: AOJu0YzDo8ldhKlKoOnzYqwCiY9tG6kZslN+wrIWpvFGqo9WXqsCABf5 CmYSAYKrPfsXltOTlwrvyRememyrz2vLqMyp5n0RUuBv6NcXiAUkrnndFNOTfA== X-Google-Smtp-Source: AGHT+IF11IWkI0TTAlwnE00yKbuCp0K/lKMT2EevXSIyOW0sCRAJmY0MqvYYhF8S0cgcHj6ziGPeig== X-Received: by 2002:a17:902:cf04:b0:1e0:137e:cd66 with SMTP id i4-20020a170902cf0400b001e0137ecd66mr2022268plg.30.1711685465594; Thu, 28 Mar 2024 21:11:05 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id s2-20020a170902c64200b001e0e8e4f7e3sm2474716pls.206.2024.03.28.21.11.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:11:05 -0700 (PDT) Date: Thu, 28 Mar 2024 21:11:04 -0700 From: Kees Cook To: Justin Stitt Cc: Theodore Ts'o , Andreas Dilger , linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] ext4: replace deprecated strncpy with alternatives Message-ID: <202403282109.D174F1CE@keescook> References: <20240321-strncpy-fs-ext4-file-c-v1-1-36a6a09fef0c@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: <20240321-strncpy-fs-ext4-file-c-v1-1-36a6a09fef0c@google.com> On Thu, Mar 21, 2024 at 01:03:10AM +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 file.c: > s_last_mounted is marked as __nonstring meaning it does not need to be > NUL-terminated. Let's instead use strtomem_pad() to copy bytes from the > string source to the byte array destination -- while also ensuring to > pad with zeroes. > > in ioctl.c: > We can drop the memset and size argument in favor of using the new > 2-argument version of strscpy_pad() -- which was introduced with Commit > e6584c3964f2f ("string: Allow 2-argument strscpy()"). This guarantees > NUL-termination and NUL-padding on the destination buffer -- which seems > to be a requirement judging from this comment: > > | static int ext4_ioctl_getlabel(struct ext4_sb_info *sbi, char __user *user_label) > | { > | char label[EXT4_LABEL_MAX + 1]; > | > | /* > | * EXT4_LABEL_MAX must always be smaller than FSLABEL_MAX because > | * FSLABEL_MAX must include terminating null byte, while s_volume_name > | * does not have to. > | */ > > in super.c: > s_first_error_func is marked as __nonstring meaning we can take the same > approach as in file.c; just use strtomem_pad() > > 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 > --- > Note: build-tested only. > > Found with: $ rg "strncpy\(" > --- > fs/ext4/file.c | 3 +-- > fs/ext4/ioctl.c | 3 +-- > fs/ext4/super.c | 7 +++---- > 3 files changed, 5 insertions(+), 8 deletions(-) > > diff --git a/fs/ext4/file.c b/fs/ext4/file.c > index 54d6ff22585c..c675c0eb5f7e 100644 > --- a/fs/ext4/file.c > +++ b/fs/ext4/file.c > @@ -844,8 +844,7 @@ static int ext4_sample_last_mounted(struct super_block *sb, > if (err) > goto out_journal; > lock_buffer(sbi->s_sbh); > - strncpy(sbi->s_es->s_last_mounted, cp, > - sizeof(sbi->s_es->s_last_mounted)); > + strtomem_pad(sbi->s_es->s_last_mounted, cp, 0); > ext4_superblock_csum_set(sb); > unlock_buffer(sbi->s_sbh); > ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); > diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c > index 7160a71044c8..dab7acd49709 100644 > --- a/fs/ext4/ioctl.c > +++ b/fs/ext4/ioctl.c > @@ -1150,9 +1150,8 @@ static int ext4_ioctl_getlabel(struct ext4_sb_info *sbi, char __user *user_label > */ > BUILD_BUG_ON(EXT4_LABEL_MAX >= FSLABEL_MAX); > > - memset(label, 0, sizeof(label)); > lock_buffer(sbi->s_sbh); > - strncpy(label, sbi->s_es->s_volume_name, EXT4_LABEL_MAX); > + strscpy_pad(label, sbi->s_es->s_volume_name); > unlock_buffer(sbi->s_sbh); The only reason I can imagine the memset() being split here is to keep it out of the spinlock. For a non-fast-path ioctl, I can't imagine this being a meaningful reduction in lock contention, though. Reviewed-by: Kees Cook -- Kees Cook