Skip to contentSkip to conversation

ntfs: reject negative attribute read positions

PATCH Applied Hyunchul Lee 3 participants 8 messages Oct 28 – Oct 29 NTFS filesystem

lore
Share & tools
lore

Hyunchul Lee

author maintainer
#0 lore raw

PATCH 1/3 ntfs: reject negative attribute read positions

A corrupt index entry can produce a VCN whose byte offset overflows. Reject negative positions before they reach pointer arithmetic in ntfs_inode_attr_pread().

Fixes 0a8ac0c1fa0b ("ntfs: update directory operations")
---
 fs/ntfs/index.c | 39 ++++++++++++++++++++++++++++++++++++++-
 fs/ntfs/index.h |  1 +
 fs/ntfs/inode.c |  4 +++-
 3 files changed, 42 insertions(+), 2 deletions(-)
fs/ntfs/index.c +38 -1
index 66411a712b14..e6ba1a08741b 100644
@@ -14,6 +14,8 @@
  * Copyright (c) 2007-2021 Jean-Pierre Andre
  */
 
+#include <linux/overflow.h>
+
 #include "collate.h"
 #include "index.h"
 #include "ntfs.h"
@@ -255,6 +257,32 @@ static s64 ntfs_ie_get_vcn(struct index_entry *ie)
 	return le64_to_cpup(ntfs_ie_get_vcn_addr(ie));
 }
 
+static int ntfs_index_entry_vcn_inconsistent(const struct ntfs_volume *vol,
+					     struct index_entry *ie,
+					     u8 vcn_size_bits, u64 inum)
+{
+	s64 vcn, pos;
+
+	if (le16_to_cpu(ie->length) <
+	    sizeof(struct index_entry_header) + sizeof(s64)) {
+		ntfs_error(vol->sb,
+			   "Index entry with node flag has no VCN in inode %llu.",
+			   (unsigned long long)inum);
+		return -EIO;
+	}
+
+	vcn = ntfs_ie_get_vcn(ie);
+	if (vcn < 0 ||
+	    check_shl_overflow(vcn, vcn_size_bits, &pos)) {
+		ntfs_error(vol->sb,
+			   "Invalid index entry VCN %lld in inode %llu.",
+			   vcn, (unsigned long long)inum);
+		return -EIO;
+	}
+
+	return 0;
+}
+
 static struct index_entry *ntfs_ie_get_first(struct index_header *ih)
 {
 	return (struct index_entry *)((u8 *)ih + le32_to_cpu(ih->entries_offset));
@@ -326,10 +354,13 @@ static int ntfs_index_header_inconsistent(struct ntfs_volume *vol,
 
 int ntfs_index_entries_inconsistent(const struct ntfs_volume *vol,
 				    const struct index_header *ih,
+				    u32 block_size,
 				    __le32 collation_rule, u64 inum)
 {
 	struct index_entry *ie;
 	u8 *index_end = (u8 *)ih + le32_to_cpu(ih->index_length);
+	u8 vcn_size_bits = vol->cluster_size <= block_size ?
+			vol->cluster_size_bits : NTFS_BLOCK_SIZE_BITS;
 
 	for (ie = ntfs_ie_get_first((struct index_header *)ih);
 	      ; ie = ntfs_ie_get_next(ie)) {
@@ -348,6 +379,11 @@ int ntfs_index_entries_inconsistent(const struct ntfs_volume *vol,
 			return -EIO;
 		}
 
+		if ((ie->flags & INDEX_ENTRY_NODE) &&
+		    ntfs_index_entry_vcn_inconsistent(vol, ie,
+						      vcn_size_bits, inum))
+			return -EIO;
+
 		if (ntfs_ie_end(ie))
 			break;
 
@@ -566,7 +602,8 @@ int ntfs_index_block_inconsistent(struct ntfs_volume *vol,
 		return -EIO;
 	}
 
-	if (ntfs_index_entries_inconsistent(vol, &ib->index, cr, inum))
+	if (ntfs_index_entries_inconsistent(vol, &ib->index, block_size,
+					    cr, inum))
 		return -EIO;
 	return 0;
 }
fs/ntfs/index.h +1 -0
index a898e348f2c9..bc2f1bbdfba4 100644
@@ -98,6 +98,7 @@ int ntfs_index_block_inconsistent(struct ntfs_volume *vol,
 				  __le32 cr, u64 inum);
 int ntfs_index_entries_inconsistent(const struct ntfs_volume *vol,
 				    const struct index_header *ih,
+				    u32 block_size,
 				    __le32 collation_rule, u64 inum);
 struct ntfs_index_context *ntfs_index_ctx_get(struct ntfs_inode *ni, __le16 *name,
 		u32 name_len);
fs/ntfs/inode.c +3 -1
index 206db4e99ef1..b8a1af8c4cea 100644
@@ -1034,6 +1034,7 @@ static int ntfs_read_locked_inode(struct inode *vi)
 				le16_to_cpu(a->data.resident.value_offset));
 		if (ntfs_index_root_inconsistent(ni->vol, a, ir, ni->mft_no) ||
 		    ntfs_index_entries_inconsistent(ni->vol, &ir->index,
+						    le32_to_cpu(ir->index_block_size),
 						    ir->collation_rule, ni->mft_no)) {
 			ntfs_error(vi->i_sb, "Directory index is corrupt.");
 			goto unm_err_out;
@@ -1684,6 +1685,7 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
 	ir = (struct index_root *)((u8 *)a + le16_to_cpu(a->data.resident.value_offset));
 	if (ntfs_index_root_inconsistent(vol, a, ir, ni->mft_no) ||
 	    ntfs_index_entries_inconsistent(vol, &ir->index,
+					    le32_to_cpu(ir->index_block_size),
 					    ir->collation_rule, ni->mft_no)) {
 		ntfs_error(vi->i_sb, "Index is corrupt.");
 		goto unm_err_out;
@@ -3715,7 +3717,7 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)
 		}
 	}
 	isize = i_size_read(vi);
-	if (pos > isize) {
+	if (pos < 0 || pos > isize) {
 		mutex_unlock(&mrec_ni->mrec_lock);
 		return -EINVAL;
 	}
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 2/3 ntfs: initialize VCN when inserting index entries

When a leaf index entry is promoted during a split, ntfs_ih_insert() extends a copied entry with the NODE VCN field. Initialize that field before copying the new entry into the parent index. The promoted entry must reference the old child VCN, while the existing parent entry is updated to reference the newly allocated right child.

Fixes 0a8ac0c1fa0b ("ntfs: update directory operations")
---
 fs/ntfs/index.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ntfs/index.c +1 -1
index e6ba1a08741b..322cb371dbb3 100644
@@ -1584,9 +1584,9 @@ static int ntfs_ih_insert(struct index_header *ih, struct index_entry *orig_ie,
 	ie_node = ntfs_ie_get_by_pos(ih, pos);
 	old_vcn = ntfs_ie_get_vcn(ie_node);
 	ntfs_ie_set_vcn(ie_node, new_vcn);
+	ntfs_ie_set_vcn(ie, old_vcn);
 
 	ntfs_ie_insert(ih, ie, ie_node);
-	ntfs_ie_set_vcn(ie_node, old_vcn);
 out:
 	kfree(ie);
 	return ret;
Signature · 1 line
2.43.0
maintainer Applied
#2 lore raw

Re: [PATCH 2/3] ntfs: initialize VCN when inserting index entries

Quoted 10 lines · Hyunchul Lee

When a leaf index entry is promoted during a split, ntfs_ih_insert() extends a copied entry with the NODE VCN field. Initialize that field before copying the new entry into the parent index. The promoted entry must reference the old child VCN, while the existing parent entry is updated to reference the newly allocated right child.

Fixes 0a8ac0c1fa0b ("ntfs: update directory operations")

Applied it to #ntfs-next.
Thanks!

author maintainer
#3 lore raw

PATCH 3/3 ntfs: avoid overflow when limiting attribute read length

In reply to Hyunchul Lee

ntfs_inode_attr_pread() limits a read to the attribute size using pos + count. A large count can overflow the signed sum and bypass the attribute EOF boundary check. Compare count with isize - pos instead, after validating pos, so the read length limit cannot wrap.

Fixes af0db57d4293 ("ntfs: update inode operations")
---
 fs/ntfs/inode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ntfs/inode.c +1 -1
index b8a1af8c4cea..54940e22668b 100644
@@ -3721,7 +3721,7 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)
 		mutex_unlock(&mrec_ni->mrec_lock);
 		return -EINVAL;
 	}
-	if (pos + count > isize)
+	if (count > isize - pos)
 		count = isize - pos;
 
 	if (!NInoNonResident(ni)) {
Signature · 1 line
2.43.0
Reviewed-by Baolin Liu on v1 3/3
#4 lore raw

Re: [PATCH 3/3] ntfs: avoid overflow when limiting attribute read length

Quoted 16 lines · Hyunchul Lee

ntfs_inode_attr_pread() limits a read to the attribute size using pos + count. A large count can overflow the signed sum and bypass the attribute EOF boundary check. Compare count with isize - pos instead, after validating pos, so the read length limit cannot wrap.

Fixes af0db57d4293 ("ntfs: update inode operations")
---
  fs/ntfs/inode.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/ntfs/inode.c +0 -0
index b8a1af8c4cea..54940e22668b 100644

fs/ntfs/inode.c s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf) · line 3727

Quoted 8 lines
fs/ntfs/inode.c +1 -1
@@ -3721,7 +3721,7 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)
  		mutex_unlock(&mrec_ni->mrec_lock);
  		return -EINVAL;
  	}
-	if (pos + count > isize)
+	if (count > isize - pos)
  		count = isize - pos;
 
  	if (!NInoNonResident(ni)) {

Looks good to me.

maintainer Applied
#5 lore raw

Re: [PATCH 3/3] ntfs: avoid overflow when limiting attribute read length

In reply to Hyunchul Lee
Quoted 8 lines · Hyunchul Lee

ntfs_inode_attr_pread() limits a read to the attribute size using pos + count. A large count can overflow the signed sum and bypass the attribute EOF boundary check. Compare count with isize - pos instead, after validating pos, so the read length limit cannot wrap.

Fixes af0db57d4293 ("ntfs: update inode operations")

Applied it to #ntfs-next.
Thanks!

Reviewed-by Baolin Liu on v1 1/3
#6 lore raw
In reply to Hyunchul Lee
Quoted 129 lines · Hyunchul Lee

A corrupt index entry can produce a VCN whose byte offset overflows. Reject negative positions before they reach pointer arithmetic in ntfs_inode_attr_pread().

Fixes 0a8ac0c1fa0b ("ntfs: update directory operations")
---
  fs/ntfs/index.c | 39 ++++++++++++++++++++++++++++++++++++++-
  fs/ntfs/index.h |  1 +
  fs/ntfs/inode.c |  4 +++-
  3 files changed, 42 insertions(+), 2 deletions(-)
fs/ntfs/index.c +38 -1
index 66411a712b14..e6ba1a08741b 100644
@@ -14,6 +14,8 @@
   * Copyright (c) 2007-2021 Jean-Pierre Andre
   */
 
+#include <linux/overflow.h>
+
  #include "collate.h"
  #include "index.h"
  #include "ntfs.h"
@@ -255,6 +257,32 @@ static s64 ntfs_ie_get_vcn(struct index_entry *ie)
  	return le64_to_cpup(ntfs_ie_get_vcn_addr(ie));
  }
 
+static int ntfs_index_entry_vcn_inconsistent(const struct ntfs_volume *vol,
+					     struct index_entry *ie,
+					     u8 vcn_size_bits, u64 inum)
+{
+	s64 vcn, pos;
+
+	if (le16_to_cpu(ie->length) <
+	    sizeof(struct index_entry_header) + sizeof(s64)) {
+		ntfs_error(vol->sb,
+			   "Index entry with node flag has no VCN in inode %llu.",
+			   (unsigned long long)inum);
+		return -EIO;
+	}
+
+	vcn = ntfs_ie_get_vcn(ie);
+	if (vcn < 0 ||
+	    check_shl_overflow(vcn, vcn_size_bits, &pos)) {
+		ntfs_error(vol->sb,
+			   "Invalid index entry VCN %lld in inode %llu.",
+			   vcn, (unsigned long long)inum);
+		return -EIO;
+	}
+
+	return 0;
+}
+
  static struct index_entry *ntfs_ie_get_first(struct index_header *ih)
  {
  	return (struct index_entry *)((u8 *)ih + le32_to_cpu(ih->entries_offset));
@@ -326,10 +354,13 @@ static int ntfs_index_header_inconsistent(struct ntfs_volume *vol,
 
  int ntfs_index_entries_inconsistent(const struct ntfs_volume *vol,
  				    const struct index_header *ih,
+				    u32 block_size,
  				    __le32 collation_rule, u64 inum)
  {
  	struct index_entry *ie;
  	u8 *index_end = (u8 *)ih + le32_to_cpu(ih->index_length);
+	u8 vcn_size_bits = vol->cluster_size <= block_size ?
+			vol->cluster_size_bits : NTFS_BLOCK_SIZE_BITS;
 
  	for (ie = ntfs_ie_get_first((struct index_header *)ih);
  	      ; ie = ntfs_ie_get_next(ie)) {
@@ -348,6 +379,11 @@ int ntfs_index_entries_inconsistent(const struct ntfs_volume *vol,
  			return -EIO;
  		}
 
+		if ((ie->flags & INDEX_ENTRY_NODE) &&
+		    ntfs_index_entry_vcn_inconsistent(vol, ie,
+						      vcn_size_bits, inum))
+			return -EIO;
+
  		if (ntfs_ie_end(ie))
  			break;
 
@@ -566,7 +602,8 @@ int ntfs_index_block_inconsistent(struct ntfs_volume *vol,
  		return -EIO;
  	}
 
-	if (ntfs_index_entries_inconsistent(vol, &ib->index, cr, inum))
+	if (ntfs_index_entries_inconsistent(vol, &ib->index, block_size,
+					    cr, inum))
  		return -EIO;
  	return 0;
  }
fs/ntfs/index.h +1 -0
index a898e348f2c9..bc2f1bbdfba4 100644
@@ -98,6 +98,7 @@ int ntfs_index_block_inconsistent(struct ntfs_volume *vol,
  				  __le32 cr, u64 inum);
  int ntfs_index_entries_inconsistent(const struct ntfs_volume *vol,
  				    const struct index_header *ih,
+				    u32 block_size,
  				    __le32 collation_rule, u64 inum);
  struct ntfs_index_context *ntfs_index_ctx_get(struct ntfs_inode *ni, __le16 *name,
  		u32 name_len);
fs/ntfs/inode.c +2 -0
index 206db4e99ef1..b8a1af8c4cea 100644
@@ -1034,6 +1034,7 @@ static int ntfs_read_locked_inode(struct inode *vi)
  				le16_to_cpu(a->data.resident.value_offset));
  		if (ntfs_index_root_inconsistent(ni->vol, a, ir, ni->mft_no) ||
  		    ntfs_index_entries_inconsistent(ni->vol, &ir->index,
+						    le32_to_cpu(ir->index_block_size),
  						    ir->collation_rule, ni->mft_no)) {
  			ntfs_error(vi->i_sb, "Directory index is corrupt.");
  			goto unm_err_out;
@@ -1684,6 +1685,7 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
  	ir = (struct index_root *)((u8 *)a + le16_to_cpu(a->data.resident.value_offset));
  	if (ntfs_index_root_inconsistent(vol, a, ir, ni->mft_no) ||
  	    ntfs_index_entries_inconsistent(vol, &ir->index,
+					    le32_to_cpu(ir->index_block_size),
  					    ir->collation_rule, ni->mft_no)) {
  		ntfs_error(vi->i_sb, "Index is corrupt.");
  		goto unm_err_out;

fs/ntfs/inode.c s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf) · line 3723

Quoted 8 lines
fs/ntfs/inode.c +1 -1
@@ -3715,7 +3717,7 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)
  		}
  	}
  	isize = i_size_read(vi);
-	if (pos > isize) {
+	if (pos < 0 || pos > isize) {
  		mutex_unlock(&mrec_ni->mrec_lock);
  		return -EINVAL;
  	}

Looks good to me.

maintainer Applied
#7 lore raw
In reply to Hyunchul Lee
Quoted 9 lines · Hyunchul Lee

A corrupt index entry can produce a VCN whose byte offset overflows. Reject negative positions before they reach pointer arithmetic in ntfs_inode_attr_pread().

Fixes 0a8ac0c1fa0b ("ntfs: update directory operations")

Applied it to #ntfs-next.
Thanks!