Commit 6c71b489 authored by Jan Kara's avatar Jan Kara

ext2: Strengthen xattr block checks

Check every entry in xattr block for validity in ext2_xattr_set() to
detect on disk corruption early. Also since e_value_block field in xattr
entry is never != 0 in a valid filesystem, just remove checks for it
once we have established entries are valid.
Reviewed-by: default avatarChengguang Xu <cgxu519@zoho.com.cn>
Signed-off-by: default avatarJan Kara <jack@suse.cz>
parent 8cd0f2ba
...@@ -218,6 +218,8 @@ ext2_xattr_get(struct inode *inode, int name_index, const char *name, ...@@ -218,6 +218,8 @@ ext2_xattr_get(struct inode *inode, int name_index, const char *name,
EXT2_XATTR_NEXT(entry); EXT2_XATTR_NEXT(entry);
if ((char *)next >= end) if ((char *)next >= end)
goto bad_block; goto bad_block;
if (!ext2_xattr_entry_valid(entry, inode->i_sb->s_blocksize))
goto bad_block;
if (name_index == entry->e_name_index && if (name_index == entry->e_name_index &&
name_len == entry->e_name_len && name_len == entry->e_name_len &&
memcmp(name, entry->e_name, name_len) == 0) memcmp(name, entry->e_name, name_len) == 0)
...@@ -229,9 +231,6 @@ ext2_xattr_get(struct inode *inode, int name_index, const char *name, ...@@ -229,9 +231,6 @@ ext2_xattr_get(struct inode *inode, int name_index, const char *name,
error = -ENODATA; error = -ENODATA;
goto cleanup; goto cleanup;
found: found:
if (!ext2_xattr_entry_valid(entry, inode->i_sb->s_blocksize))
goto bad_block;
size = le32_to_cpu(entry->e_value_size); size = le32_to_cpu(entry->e_value_size);
if (ext2_xattr_cache_insert(ea_block_cache, bh)) if (ext2_xattr_cache_insert(ea_block_cache, bh))
ea_idebug(inode, "cache insert failed"); ea_idebug(inode, "cache insert failed");
...@@ -304,6 +303,8 @@ ext2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size) ...@@ -304,6 +303,8 @@ ext2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
if ((char *)next >= end) if ((char *)next >= end)
goto bad_block; goto bad_block;
if (!ext2_xattr_entry_valid(entry, inode->i_sb->s_blocksize))
goto bad_block;
entry = next; entry = next;
} }
if (ext2_xattr_cache_insert(ea_block_cache, bh)) if (ext2_xattr_cache_insert(ea_block_cache, bh))
...@@ -446,7 +447,9 @@ ext2_xattr_set(struct inode *inode, int name_index, const char *name, ...@@ -446,7 +447,9 @@ ext2_xattr_set(struct inode *inode, int name_index, const char *name,
struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(last); struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(last);
if ((char *)next >= end) if ((char *)next >= end)
goto bad_block; goto bad_block;
if (!last->e_value_block && last->e_value_size) { if (!ext2_xattr_entry_valid(last, sb->s_blocksize))
goto bad_block;
if (last->e_value_size) {
size_t offs = le16_to_cpu(last->e_value_offs); size_t offs = le16_to_cpu(last->e_value_offs);
if (offs < min_offs) if (offs < min_offs)
min_offs = offs; min_offs = offs;
...@@ -489,12 +492,7 @@ ext2_xattr_set(struct inode *inode, int name_index, const char *name, ...@@ -489,12 +492,7 @@ ext2_xattr_set(struct inode *inode, int name_index, const char *name,
error = -EEXIST; error = -EEXIST;
if (flags & XATTR_CREATE) if (flags & XATTR_CREATE)
goto cleanup; goto cleanup;
if (!here->e_value_block && here->e_value_size) { free += EXT2_XATTR_SIZE(le32_to_cpu(here->e_value_size));
if (!ext2_xattr_entry_valid(here, sb->s_blocksize))
goto bad_block;
free += EXT2_XATTR_SIZE(
le32_to_cpu(here->e_value_size));
}
free += EXT2_XATTR_LEN(name_len); free += EXT2_XATTR_LEN(name_len);
} }
error = -ENOSPC; error = -ENOSPC;
...@@ -559,7 +557,7 @@ ext2_xattr_set(struct inode *inode, int name_index, const char *name, ...@@ -559,7 +557,7 @@ ext2_xattr_set(struct inode *inode, int name_index, const char *name,
here->e_name_len = name_len; here->e_name_len = name_len;
memcpy(here->e_name, name, name_len); memcpy(here->e_name, name, name_len);
} else { } else {
if (!here->e_value_block && here->e_value_size) { if (here->e_value_size) {
char *first_val = (char *)header + min_offs; char *first_val = (char *)header + min_offs;
size_t offs = le16_to_cpu(here->e_value_offs); size_t offs = le16_to_cpu(here->e_value_offs);
char *val = (char *)header + offs; char *val = (char *)header + offs;
...@@ -586,7 +584,7 @@ ext2_xattr_set(struct inode *inode, int name_index, const char *name, ...@@ -586,7 +584,7 @@ ext2_xattr_set(struct inode *inode, int name_index, const char *name,
last = ENTRY(header+1); last = ENTRY(header+1);
while (!IS_LAST_ENTRY(last)) { while (!IS_LAST_ENTRY(last)) {
size_t o = le16_to_cpu(last->e_value_offs); size_t o = le16_to_cpu(last->e_value_offs);
if (!last->e_value_block && o < offs) if (o < offs)
last->e_value_offs = last->e_value_offs =
cpu_to_le16(o + size); cpu_to_le16(o + size);
last = EXT2_XATTR_NEXT(last); last = EXT2_XATTR_NEXT(last);
......
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