Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: fdmanana@kernel.org
To: linux-btrfs@vger.kernel.org
Cc: erosca@de.adit-jv.com, Maksim.Paimushkin@se.bosch.com,
	Matthias.Thomae@de.bosch.com, Sebastian.Unger@bosch.com,
	Dirk.Behme@de.bosch.com, Eugeniu.Rosca@bosch.com, wqu@suse.com,
	dsterba@suse.com, stable@vger.kernel.org,
	Filipe Manana <fdmanana@suse.com>,
	Ian Johnson <ian@ianjohnson.dev>
Subject: [PATCH 3/4 for 5.15 stable] btrfs: refresh dir last index during a rewinddir(3) call
Date: Thu, 25 Jan 2024 11:59:37 +0000	[thread overview]
Message-ID: <acbd885da4e8e7076c11bbcc31e0f6090cc10201.1706183427.git.fdmanana@suse.com> (raw)
In-Reply-To: <cover.1706183427.git.fdmanana@suse.com>

From: Filipe Manana <fdmanana@suse.com>

commit e60aa5da14d01fed8411202dbe4adf6c44bd2a57 upstream.

When opening a directory we find what's the index of its last entry and
then store it in the directory's file handle private data (struct
btrfs_file_private::last_index), so that in the case new directory entries
are added to a directory after an opendir(3) call we don't end up in an
infinite loop (see commit 9b378f6ad48c ("btrfs: fix infinite directory
reads")) when calling readdir(3).

However once rewinddir(3) is called, POSIX states [1] that any new
directory entries added after the previous opendir(3) call, must be
returned by subsequent calls to readdir(3):

  "The rewinddir() function shall reset the position of the directory
   stream to which dirp refers to the beginning of the directory.
   It shall also cause the directory stream to refer to the current
   state of the corresponding directory, as a call to opendir() would
   have done."

We currently don't refresh the last_index field of the struct
btrfs_file_private associated to the directory, so after a rewinddir(3)
we are not returning any new entries added after the opendir(3) call.

Fix this by finding the current last index of the directory when llseek
is called against the directory.

This can be reproduced by the following C program provided by Ian Johnson:

   #include <dirent.h>
   #include <stdio.h>

   int main(void) {
     DIR *dir = opendir("test");

     FILE *file;
     file = fopen("test/1", "w");
     fwrite("1", 1, 1, file);
     fclose(file);

     file = fopen("test/2", "w");
     fwrite("2", 1, 1, file);
     fclose(file);

     rewinddir(dir);

     struct dirent *entry;
     while ((entry = readdir(dir))) {
        printf("%s\n", entry->d_name);
     }
     closedir(dir);
     return 0;
   }

Reported-by: Ian Johnson <ian@ianjohnson.dev>
Link: https://lore.kernel.org/linux-btrfs/YR1P0S.NGASEG570GJ8@ianjohnson.dev/
Fixes: 9b378f6ad48c ("btrfs: fix infinite directory reads")
CC: stable@vger.kernel.org # 6.5+
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/inode.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index b144e346f24c..b7047604d255 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -6222,6 +6222,19 @@ static int btrfs_opendir(struct inode *inode, struct file *file)
 	return 0;
 }
 
+static loff_t btrfs_dir_llseek(struct file *file, loff_t offset, int whence)
+{
+	struct btrfs_file_private *private = file->private_data;
+	int ret;
+
+	ret = btrfs_get_dir_last_index(BTRFS_I(file_inode(file)),
+				       &private->last_index);
+	if (ret)
+		return ret;
+
+	return generic_file_llseek(file, offset, whence);
+}
+
 struct dir_entry {
 	u64 ino;
 	u64 offset;
@@ -11087,7 +11100,7 @@ static const struct inode_operations btrfs_dir_inode_operations = {
 };
 
 static const struct file_operations btrfs_dir_file_operations = {
-	.llseek		= generic_file_llseek,
+	.llseek		= btrfs_dir_llseek,
 	.read		= generic_read_dir,
 	.iterate_shared	= btrfs_real_readdir,
 	.open		= btrfs_opendir,
-- 
2.40.1


  parent reply	other threads:[~2024-01-25 12:00 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-25 11:59 [PATCH 0/4 for 5.15 stable] btrfs: some directory fixes for stable 5.15 fdmanana
2024-01-25 11:59 ` [PATCH 1/4 for 5.15 stable] btrfs: fix infinite directory reads fdmanana
2024-01-26 19:06   ` Eugeniu Rosca
2024-01-25 11:59 ` [PATCH 2/4 for 5.15 stable] btrfs: set last dir index to the current last index when opening dir fdmanana
2024-01-26 19:07   ` Eugeniu Rosca
2024-01-25 11:59 ` fdmanana [this message]
2024-01-26 19:08   ` [PATCH 3/4 for 5.15 stable] btrfs: refresh dir last index during a rewinddir(3) call Eugeniu Rosca
2024-01-25 11:59 ` [PATCH 4/4 for 5.15 stable] btrfs: fix race between reading a directory and adding entries to it fdmanana
2024-01-26 19:09   ` Eugeniu Rosca
2024-01-26 18:55 ` [PATCH 0/4 for 5.15 stable] btrfs: some directory fixes for stable 5.15 Eugeniu Rosca
2024-01-27 18:02   ` Filipe Manana
2024-01-29 13:39     ` Eugeniu Rosca
2024-01-27  1:15 ` Greg KH
2024-01-27 17:58   ` Filipe Manana
2024-01-27 18:18     ` Filipe Manana
2024-01-27 21:19       ` Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=acbd885da4e8e7076c11bbcc31e0f6090cc10201.1706183427.git.fdmanana@suse.com \
    --to=fdmanana@kernel.org \
    --cc=Dirk.Behme@de.bosch.com \
    --cc=Eugeniu.Rosca@bosch.com \
    --cc=Maksim.Paimushkin@se.bosch.com \
    --cc=Matthias.Thomae@de.bosch.com \
    --cc=Sebastian.Unger@bosch.com \
    --cc=dsterba@suse.com \
    --cc=erosca@de.adit-jv.com \
    --cc=fdmanana@suse.com \
    --cc=ian@ianjohnson.dev \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=wqu@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox