Commit 358c3675 authored by Daeho Jeong's avatar Daeho Jeong Committed by Kamal Mostafa

ext4: avoid modifying checksum fields directly during checksum verification

BugLink: http://bugs.launchpad.net/bugs/1624037

commit b47820ed upstream.

We temporally change checksum fields in buffers of some types of
metadata into '0' for verifying the checksum values. By doing this
without locking the buffer, some metadata's checksums, which are
being committed or written back to the storage, could be damaged.
In our test, several metadata blocks were found with damaged metadata
checksum value during recovery process. When we only verify the
checksum value, we have to avoid modifying checksum fields directly.
Signed-off-by: default avatarDaeho Jeong <daeho.jeong@samsung.com>
Signed-off-by: default avatarYoungjin Gil <youngjin.gil@samsung.com>
Signed-off-by: default avatarTheodore Ts'o <tytso@mit.edu>
Reviewed-by: default avatarDarrick J. Wong <darrick.wong@oracle.com>
Cc: Török Edwin <edwin@etorok.net>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarTim Gardner <tim.gardner@canonical.com>
Signed-off-by: default avatarKamal Mostafa <kamal@canonical.com>
parent 436c8500
...@@ -51,25 +51,31 @@ static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, ...@@ -51,25 +51,31 @@ static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
struct ext4_inode_info *ei) struct ext4_inode_info *ei)
{ {
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
__u16 csum_lo;
__u16 csum_hi = 0;
__u32 csum; __u32 csum;
__u16 dummy_csum = 0;
int offset = offsetof(struct ext4_inode, i_checksum_lo);
unsigned int csum_size = sizeof(dummy_csum);
csum_lo = le16_to_cpu(raw->i_checksum_lo); csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset);
raw->i_checksum_lo = 0; csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size);
if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && offset += csum_size;
EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
csum_hi = le16_to_cpu(raw->i_checksum_hi); EXT4_GOOD_OLD_INODE_SIZE - offset);
raw->i_checksum_hi = 0;
}
csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
EXT4_INODE_SIZE(inode->i_sb)); offset = offsetof(struct ext4_inode, i_checksum_hi);
csum = ext4_chksum(sbi, csum, (__u8 *)raw +
raw->i_checksum_lo = cpu_to_le16(csum_lo); EXT4_GOOD_OLD_INODE_SIZE,
if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && offset - EXT4_GOOD_OLD_INODE_SIZE);
EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
raw->i_checksum_hi = cpu_to_le16(csum_hi); csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum,
csum_size);
offset += csum_size;
csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
EXT4_INODE_SIZE(inode->i_sb) -
offset);
}
}
return csum; return csum;
} }
......
...@@ -420,15 +420,14 @@ static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent, ...@@ -420,15 +420,14 @@ static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
struct ext4_inode_info *ei = EXT4_I(inode); struct ext4_inode_info *ei = EXT4_I(inode);
__u32 csum; __u32 csum;
__le32 save_csum;
int size; int size;
__u32 dummy_csum = 0;
int offset = offsetof(struct dx_tail, dt_checksum);
size = count_offset + (count * sizeof(struct dx_entry)); size = count_offset + (count * sizeof(struct dx_entry));
save_csum = t->dt_checksum;
t->dt_checksum = 0;
csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size); csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail)); csum = ext4_chksum(sbi, csum, (__u8 *)t, offset);
t->dt_checksum = save_csum; csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
return cpu_to_le32(csum); return cpu_to_le32(csum);
} }
......
...@@ -2055,23 +2055,25 @@ static int ext4_fill_flex_info(struct super_block *sb) ...@@ -2055,23 +2055,25 @@ static int ext4_fill_flex_info(struct super_block *sb)
static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group, static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
struct ext4_group_desc *gdp) struct ext4_group_desc *gdp)
{ {
int offset; int offset = offsetof(struct ext4_group_desc, bg_checksum);
__u16 crc = 0; __u16 crc = 0;
__le32 le_group = cpu_to_le32(block_group); __le32 le_group = cpu_to_le32(block_group);
struct ext4_sb_info *sbi = EXT4_SB(sb); struct ext4_sb_info *sbi = EXT4_SB(sb);
if (ext4_has_metadata_csum(sbi->s_sb)) { if (ext4_has_metadata_csum(sbi->s_sb)) {
/* Use new metadata_csum algorithm */ /* Use new metadata_csum algorithm */
__le16 save_csum;
__u32 csum32; __u32 csum32;
__u16 dummy_csum = 0;
save_csum = gdp->bg_checksum;
gdp->bg_checksum = 0;
csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group, csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
sizeof(le_group)); sizeof(le_group));
csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
sbi->s_desc_size); csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
gdp->bg_checksum = save_csum; sizeof(dummy_csum));
offset += sizeof(dummy_csum);
if (offset < sbi->s_desc_size)
csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
sbi->s_desc_size - offset);
crc = csum32 & 0xFFFF; crc = csum32 & 0xFFFF;
goto out; goto out;
...@@ -2081,8 +2083,6 @@ static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group, ...@@ -2081,8 +2083,6 @@ static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
if (!ext4_has_feature_gdt_csum(sb)) if (!ext4_has_feature_gdt_csum(sb))
return 0; return 0;
offset = offsetof(struct ext4_group_desc, bg_checksum);
crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid)); crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group)); crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
crc = crc16(crc, (__u8 *)gdp, offset); crc = crc16(crc, (__u8 *)gdp, offset);
......
...@@ -123,17 +123,18 @@ static __le32 ext4_xattr_block_csum(struct inode *inode, ...@@ -123,17 +123,18 @@ static __le32 ext4_xattr_block_csum(struct inode *inode,
{ {
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
__u32 csum; __u32 csum;
__le32 save_csum;
__le64 dsk_block_nr = cpu_to_le64(block_nr); __le64 dsk_block_nr = cpu_to_le64(block_nr);
__u32 dummy_csum = 0;
int offset = offsetof(struct ext4_xattr_header, h_checksum);
save_csum = hdr->h_checksum;
hdr->h_checksum = 0;
csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr, csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
sizeof(dsk_block_nr)); sizeof(dsk_block_nr));
csum = ext4_chksum(sbi, csum, (__u8 *)hdr, csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
EXT4_BLOCK_SIZE(inode->i_sb)); csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
offset += sizeof(dummy_csum);
csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
EXT4_BLOCK_SIZE(inode->i_sb) - offset);
hdr->h_checksum = save_csum;
return cpu_to_le32(csum); return cpu_to_le32(csum);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment