From: Mohan Karthik Commit dffc7f2f177b ("fs/ntfs3: allow readdir() to finish after directory mutations without rewinddir()") stores (void *)ni->dir.version in file->private_data when ntfs_readdir() starts at pos == 0, and rewinds ctx->pos to 3 at pos >= eod if file->private_data no longer matches ni->dir.version. This assumes the same struct file began enumeration at pos == 0. However, nfsd_readdir_open() opens a fresh struct file (with file->private_data == NULL) per NFS READDIR/READDIRPLUS RPC and seeks to the client's resume cookie via vfs_llseek(..., SEEK_SET). On continuation RPCs (cookie > 0), ntfs_readdir() skips the if (!pos) initialization and leaves file->private_data as NULL. When nfsd_readdir_next() calls iterate_dir() again at pos >= eod to probe for EOF on any directory with ni->dir.version != 0, NULL mismatches (void *)ni->dir.version and ntfs_readdir() spuriously rewinds to pos = 3. Over NFSv3/NFSv4, this produces 2x-3x duplicate directory entries or readdir loop aborts, breaking strict clients such as restic. Encode the saved version with bit 0 set via ntfs_dir_ver_cookie() so every recorded version, including 0, is non-NULL, and treat !file->private_data at pos >= eod as an unmodified/mid-stream walk that returns 0 without rewinding. Callers that start at pos == 0 or call rewinddir() (such as opendir()/unlink() loops in bonnie++) still record a non-NULL cookie and rewind across mutations as before, while mid-stream nfsd_readdir() and seekdir()/lseek() callers finish cleanly. Reproducer: export an ntfs3 directory over NFSv3/NFSv4, create enough entries that enumeration spans multiple READDIR RPCs (making ni->dir.version non-zero), and read the directory from an NFS client; or locally open that directory, lseek() to a non-zero dirent d_off, and read with getdents64() until EOF. Fixes: dffc7f2f177b ("fs/ntfs3: allow readdir() to finish after directory mutations without rewinddir()") Cc: stable@vger.kernel.org Signed-off-by: Mohan Karthik --- fs/ntfs3/dir.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/fs/ntfs3/dir.c b/fs/ntfs3/dir.c index 7ee1a5d..e8b65fb 100644 --- a/fs/ntfs3/dir.c +++ b/fs/ntfs3/dir.c @@ -448,6 +448,16 @@ static int ntfs_read_hdr(struct ntfs_sb_info *sbi, struct ntfs_inode *ni, } } +/* + * Tag dir.version with bit 0 set so any saved version (including 0) is + * non-NULL; NULL means ntfs_readdir() never started at pos == 0 on this + * struct file (e.g. seekdir() or nfsd_readdir() resuming mid-stream). + */ +static inline void *ntfs_dir_ver_cookie(size_t ver) +{ + return (void *)(((unsigned long)ver << 1) | 1UL); +} + /* * ntfs_readdir - file_operations::iterate_shared * @@ -479,12 +489,14 @@ static int ntfs_readdir(struct file *file, struct dir_context *ctx) * ni->dir.version increments each directory change. * Save the initial value of ni->dir.version. */ - file->private_data = (void *)ni->dir.version; + file->private_data = ntfs_dir_ver_cookie(ni->dir.version); } if (pos >= eod) { - if (file->private_data == (void *)ni->dir.version) { - /* No changes since first readdir. */ + void *cookie = ntfs_dir_ver_cookie(ni->dir.version); + + if (!file->private_data || file->private_data == cookie) { + /* Unchanged or opened mid-stream. */ return 0; } @@ -521,7 +533,7 @@ static int ntfs_readdir(struct file *file, struct dir_context *ctx) * https://web.archive.org/web/20220122122948/https:/support.apple.com/kb/TA21420?locale=en_US */ ctx->pos = pos = 3; - file->private_data = (void *)ni->dir.version; + file->private_data = cookie; } if (!dir_emit_dots(file, ctx)) -- 2.54.0