diff --git a/fs/ntfs3/xattr.c b/fs/ntfs3/xattr.c index 7a81369a1173..594ef6860b93 100644 --- a/fs/ntfs3/xattr.c +++ b/fs/ntfs3/xattr.c @@ -146,26 +146,29 @@ static int ntfs_read_ea(struct ntfs_inode *ni, struct EA_FULL **ea, for (off = 0; off < size; off += ea_size) { const struct EA_FULL *ef = Add2Ptr(ea_p, off); u32 bytes = size - off; + size_t need; /* Check if we can use field ea->size. */ if (bytes < sizeof(ef->size)) goto out1; - if (ef->size) { - ea_size = le32_to_cpu(ef->size); - if (ea_size > bytes) - goto out1; - continue; - } - /* Check if we can use fields ef->name_len and ef->elength. */ if (bytes < offsetof(struct EA_FULL, name)) goto out1; - ea_size = ALIGN(struct_size(ef, name, - 1 + ef->name_len + - le16_to_cpu(ef->elength)), - 4); + /* Size needed to hold this record's name and value. */ + need = struct_size(ef, name, + 1 + ef->name_len + le16_to_cpu(ef->elength)); + + if (ef->size) { + ea_size = le32_to_cpu(ef->size); + /* ef->size must fit the list and cover the record. */ + if (ea_size > bytes || ea_size < need) + goto out1; + continue; + } + + ea_size = ALIGN(need, 4); if (ea_size > bytes) goto out1; }