exfat: fix unexpected EOF while reading dir
authorYuezhang Mo <Yuezhang.Mo@sony.com>
Thu, 22 Sep 2022 06:43:47 +0000 (14:43 +0800)
committerNamjae Jeon <linkinjeon@kernel.org>
Mon, 27 Feb 2023 12:14:44 +0000 (21:14 +0900)
If the position is not aligned with the dentry size, the return
value of readdir() will be NULL and errno is 0, which means the
end of the directory stream is reached.

If the position is aligned with dentry size, but there is no file
or directory at the position, exfat_readdir() will continue to
get dentry from the next dentry. So the dentry gotten by readdir()
may not be at the position.

After this commit, if the position is not aligned with the dentry
size, round the position up to the dentry size and continue to get
the dentry.

Fixes: ca06197382bd ("exfat: add directory operations")
Cc: stable@vger.kernel.org # v5.7+
Reported-by: Wang Yugui <wangyugui@e16-tech.com>
Signed-off-by: Yuezhang Mo <Yuezhang.Mo@sony.com>
Reviewed-by: Andy Wu <Andy.Wu@sony.com>
Reviewed-by: Aoyama Wataru <wataru.aoyama@sony.com>
Reviewed-by: Sungjong Seo <sj1557.seo@samsung.com>
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
fs/exfat/dir.c

index 1dfa67f307f1737cfcd39eada98f4dbf272c4484..1122bee3b63444ae10d5cdd6aa10b11927d27e7b 100644 (file)
@@ -234,10 +234,7 @@ static int exfat_iterate(struct file *file, struct dir_context *ctx)
                fake_offset = 1;
        }
 
-       if (cpos & (DENTRY_SIZE - 1)) {
-               err = -ENOENT;
-               goto unlock;
-       }
+       cpos = round_up(cpos, DENTRY_SIZE);
 
        /* name buffer should be allocated before use */
        err = exfat_alloc_namebuf(nb);