summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2026-11-14 08:27:30 -0800
committerLinus Torvalds <torvalds@linux-foundation.org>2026-11-14 08:27:30 -0800
commit68271fbe7d14b6397e90df730c3967d3a481902b (patch)
treedcf01ef60e0df7fbeb50dfad0b6d2cc306865028
parentcba88960471ac67635080708b67ed5c2a2e4e9fd (diff)
parentbada7bbb11aaa6feba8904cf38ff833453f5b5d9 (diff)
Merge tag 'ntfs-for-7.4-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs
Pull ntfs fixes from Namjae Jeon: - Validate that $INDEX_ALLOCATION is open and non-resident before reading or writing index blocks, preventing NULL pointer dereferences when an earlier index operation has not opened it - Check that index allocation inodes are non-resident and child VCNs are non-negative and within bounds before directory lookup reads, preventing a deadlock when $INDEX_ALLOCATION is missing or damaged - Validate file names against the index entry's key length, preventing out-of-bounds reads when directory enumeration resumes using a copied key - Return -ENOENT for NTFS_IOC_LIST_STREAMS on deleted base files and fix unsigned underflow in its buffer bounds check, preventing out-of-bounds writes when the sizing and filling passes disagree - Use kfree() for the temporary runlist allocated with kmalloc() on merge failure, resolving the Coccinelle allocation/free mismatch warning * tag 'ntfs-for-7.4-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs: ntfs: open the index allocation before reading or writing an index block ntfs: do not read an index block outside $INDEX_ALLOCATION ntfs: silence kmalloc/kvfree mismatch warning ntfs: check the file name against the key length of an index entry ntfs: fix out-of-bounds write race in NTFS_IOC_LIST_STREAMS
-rw-r--r--fs/ntfs/dir.c14
-rw-r--r--fs/ntfs/index.c30
-rw-r--r--fs/ntfs/iomap.c2
-rw-r--r--fs/ntfs/named_stream.c6
4 files changed, 47 insertions, 5 deletions
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
@@ -309,6 +309,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
* disk if necessary.
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;