Re: [PATCH v2] fat: stop reading directory entries past the end-of-directory marker
From: OGAWA Hirofumi
Date: Wed Jun 10 2026 - 02:39:34 EST
Matteo Croce <technoboy85@xxxxxxxxx> writes:
> @@ -1310,12 +1333,15 @@ int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
> bh = prev = NULL;
> pos = 0;
> err = -ENOSPC;
> + saw_eod = false;
> while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
> /* check the maximum size of directory */
> if (pos >= FAT_MAX_DIR_SIZE)
> goto error;
>
> if (IS_FREE(de->name)) {
> + if (de->name[0] == 0)
> + saw_eod = true;
> if (prev != bh) {
> get_bh(bh);
> bhs[nr_bhs] = prev = bh;
> @@ -1325,6 +1351,12 @@ int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
> if (free_slots == nr_slots)
> goto found;
> } else {
> + if (saw_eod) {
> + fat_msg_ratelimit(sb, KERN_WARNING,
> + "allocated dir entry found after end-of-directory marker (i_pos %lld); please run fsck",
> + MSDOS_I(dir)->i_pos);
> + saw_eod = false;
Although this doesn't catch all cases, if it found the corruption, maybe
we should make FS read-only (fat_fs_error()), and return -EIO error? Is
there the reason to allow continue to write?
Thanks.
> + }
> for (i = 0; i < nr_bhs; i++)
> brelse(bhs[i]);
> prev = NULL;
--
OGAWA Hirofumi <hirofumi@xxxxxxxxxxxxxxxxxx>