diff --git a/fs/ntfs/dir.c b/fs/ntfs/dir.c index fce10baa2b93..9fb5bb3d3512 100644 --- a/fs/ntfs/dir.c +++ b/fs/ntfs/dir.c @@ -308,6 +308,20 @@ found_it: ia_mapping = ia_vi->i_mapping; descend_into_child_node: + /* + * The child node has to be inside $INDEX_ALLOCATION. A directory that + * lacks the attribute gets an empty index inode that is not + * non-resident, and reading a folio from it locks the mft record that + * is already locked here. + */ + if (!NInoNonResident(NTFS_I(ia_vi)) || vcn < 0 || + (u64)vcn >= (u64)i_size_read(ia_vi) >> dir_ni->itype.index.vcn_size_bits) { + ntfs_error(sb, + "Child node vcn %lld is outside $INDEX_ALLOCATION in directory inode 0x%llx.", + vcn, dir_ni->mft_no); + err = -EIO; + goto err_out; + } /* * Convert vcn to index into the index allocation attribute in units * of PAGE_SIZE and map the page cache page, reading it from diff --git a/fs/ntfs/index.c b/fs/ntfs/index.c index 322cb371dbb3..b4462f760e3b 100644 --- a/fs/ntfs/index.c +++ b/fs/ntfs/index.c @@ -43,9 +43,11 @@ static int ntfs_index_entry_inconsistent(const struct ntfs_volume *vol, } else { if (collation_rule == COLLATION_FILE_NAME) { - if ((offsetof(struct index_entry, key.file_name.file_name) + - ie->key.file_name.file_name_length * sizeof(__le16)) > - le16_to_cpu(ie->length)) { + if (le16_to_cpu(ie->key_length) < + offsetof(struct file_name_attr, file_name) || + (offsetof(struct file_name_attr, file_name) + + ie->key.file_name.file_name_length * sizeof(__le16)) > + le16_to_cpu(ie->key_length)) { ntfs_error(vol->sb, "File name overflow from index entry in inode %lld\n", (long long)inum); @@ -99,12 +101,30 @@ static s64 ntfs_ib_pos_to_vcn(struct ntfs_index_context *icx, s64 pos) return pos >> icx->vcn_size_bits; } +/* Open the index allocation inode if no earlier step of the operation did. */ +static int ntfs_icx_ia_ensure(struct ntfs_index_context *icx) +{ + if (!icx->ia_ni) { + icx->ia_ni = ntfs_ia_open(icx, icx->idx_ni); + if (!icx->ia_ni) + return -EIO; + } + /* Without $INDEX_ALLOCATION the index inode is an empty resident stub. */ + if (!NInoNonResident(icx->ia_ni)) + return -EIO; + return 0; +} + static int ntfs_ib_write(struct ntfs_index_context *icx, struct index_block *ib) { s64 ret, vcn = le64_to_cpu(ib->index_block_vcn); ntfs_debug("vcn: %lld\n", vcn); + ret = ntfs_icx_ia_ensure(icx); + if (ret) + return ret; + ret = pre_write_mst_fixup((struct ntfs_record *)ib, icx->block_size); if (ret) return -EIO; @@ -805,6 +825,10 @@ static int ntfs_ib_read(struct ntfs_index_context *icx, s64 vcn, struct index_bl ntfs_debug("vcn: %lld\n", vcn); + ret = ntfs_icx_ia_ensure(icx); + if (ret) + return ret; + pos = ntfs_ib_vcn_to_pos(icx, vcn); ret = ntfs_inode_attr_pread(VFS_I(icx->ia_ni), pos, icx->block_size, (u8 *)dst); diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c index 72f85fc07b6e..b459d3e6e5fa 100644 --- a/fs/ntfs/iomap.c +++ b/fs/ntfs/iomap.c @@ -487,7 +487,7 @@ remap_rl: ntfs_error(vol->sb, "Failed to merge runlists"); up_write(&ni->runlist.lock); mutex_unlock(&mrec_ni->mrec_lock); - kvfree(rlc); + kfree(rlc); return PTR_ERR(rl); } diff --git a/fs/ntfs/named_stream.c b/fs/ntfs/named_stream.c index 95a3c05f9821..70c589207713 100644 --- a/fs/ntfs/named_stream.c +++ b/fs/ntfs/named_stream.c @@ -1632,6 +1632,10 @@ int ntfs_ioctl_list_streams(struct file *filp, unsigned long arg) ubuf = (void __user *)arg + sizeof(hdr); mutex_lock(&ni->mrec_lock); + if (NInoBeingDeleted(ni) || !inode->i_nlink) { + mutex_unlock(&ni->mrec_lock); + return -ENOENT; + } actx = ntfs_attr_get_search_ctx(ni, NULL); if (!actx) { mutex_unlock(&ni->mrec_lock); @@ -1731,7 +1735,7 @@ int ntfs_ioctl_list_streams(struct file *filp, unsigned long arg) } entry_size = ALIGN(name_offset + name_len, 8); - if (offset > required - entry_size) { + if (offset > required || entry_size > required - offset) { ret = -EOVERFLOW; kfree(sn); sn = NULL;