Skip to content

Commit

Permalink
fat: add simple validation for directory inode
Browse files Browse the repository at this point in the history
This detects simple corruption cases of directory, and tries to avoid
further damage to user data.

And performance impact of this validation should be very low, or not
measurable.

Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Reported-by: Vegard Nossum <vegard.nossum@oracle.com>
Tested-by: Vegard Nossum <vegard.nossum@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
OGAWAHirofumi authored and torvalds committed Jan 21, 2016
1 parent a513d86 commit a3082d5
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions fs/fat/inode.c
Expand Up @@ -449,6 +449,24 @@ static int fat_calc_dir_size(struct inode *inode)
return 0;
}

static int fat_validate_dir(struct inode *dir)
{
struct super_block *sb = dir->i_sb;

if (dir->i_nlink < 2) {
/* Directory should have "."/".." entries at least. */
fat_fs_error(sb, "corrupted directory (invalid entries)");
return -EIO;
}
if (MSDOS_I(dir)->i_start == 0 ||
MSDOS_I(dir)->i_start == MSDOS_SB(sb)->root_cluster) {
/* Directory should point valid cluster. */
fat_fs_error(sb, "corrupted directory (invalid i_start)");
return -EIO;
}
return 0;
}

/* doesn't deal with root inode */
int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
{
Expand All @@ -475,6 +493,10 @@ int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
MSDOS_I(inode)->mmu_private = inode->i_size;

set_nlink(inode, fat_subdirs(inode));

error = fat_validate_dir(inode);
if (error < 0)
return error;
} else { /* not a directory */
inode->i_generation |= 1;
inode->i_mode = fat_make_mode(sbi, de->attr,
Expand Down

0 comments on commit a3082d5

Please sign in to comment.