Skip to content

Commit

Permalink
ext4: deal with legacy signed xattr name hash values
Browse files Browse the repository at this point in the history
We potentially have old hashes of the xattr names generated on systems
with signed 'char' types.  Now that everybody uses '-funsigned-char',
those hashes will no longer match.

This only happens if you use xattrs names that have the high bit set,
which probably doesn't happen in practice, but the xfstest generic/454
shows it.

Instead of adding a new "signed xattr hash filesystem" bit and having to
deal with all the possible combinations, just calculate the hash both
ways if the first one fails, and always generate new hashes with the
proper unsigned char version.

Reported-by: kernel test robot <[email protected]>
Link: https://lore.kernel.org/oe-lkp/[email protected]
Link: https://lore.kernel.org/all/CAHk-=whUNjwqZXa-MH9KMmc_CpQpoFKFjAB9ZKHuu=TbsouT4A@mail.gmail.com/
Exposed-by: 3bc753c ("kbuild: treat char as always unsigned")
Cc: Eric Biggers <[email protected]>
Cc: Andreas Dilger <[email protected]>
Cc: Theodore Ts'o <[email protected]>,
Cc: Jason Donenfeld <[email protected]>
Cc: Masahiro Yamada <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
torvalds committed Jan 21, 2023
1 parent f883675 commit f3bbac3
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions fs/ext4/xattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
struct mb_cache_entry **);
static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
size_t value_count);
static __le32 ext4_xattr_hash_entry_signed(char *name, size_t name_len, __le32 *value,
size_t value_count);
static void ext4_xattr_rehash(struct ext4_xattr_header *);

static const struct xattr_handler * const ext4_xattr_handler_map[] = {
Expand Down Expand Up @@ -470,8 +472,21 @@ ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
tmp_data = cpu_to_le32(hash);
e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
&tmp_data, 1);
if (e_hash != entry->e_hash)
return -EFSCORRUPTED;
/* All good? */
if (e_hash == entry->e_hash)
return 0;

/*
* Not good. Maybe the entry hash was calculated
* using the buggy signed char version?
*/
e_hash = ext4_xattr_hash_entry_signed(entry->e_name, entry->e_name_len,
&tmp_data, 1);
if (e_hash == entry->e_hash)
return 0;

/* Still no match - bad */
return -EFSCORRUPTED;
}
return 0;
}
Expand Down Expand Up @@ -3091,6 +3106,28 @@ static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
return cpu_to_le32(hash);
}

/*
* ext4_xattr_hash_entry_signed()
*
* Compute the hash of an extended attribute incorrectly.
*/
static __le32 ext4_xattr_hash_entry_signed(char *name, size_t name_len, __le32 *value, size_t value_count)
{
__u32 hash = 0;

while (name_len--) {
hash = (hash << NAME_HASH_SHIFT) ^
(hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
(signed char)*name++;
}
while (value_count--) {
hash = (hash << VALUE_HASH_SHIFT) ^
(hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
le32_to_cpu(*value++);
}
return cpu_to_le32(hash);
}

#undef NAME_HASH_SHIFT
#undef VALUE_HASH_SHIFT

Expand Down

0 comments on commit f3bbac3

Please sign in to comment.