ntfs: reject negative attribute read positions
Hyunchul Lee
maintainerPATCH 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().
--- 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
@@ -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
@@ -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
@@ -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
Hyunchul Lee
maintainerPATCH 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.
--- fs/ntfs/index.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ntfs/index.c +1 -1
@@ -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
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")Signed-off-by Hyunchul Lee <[email protected]>
Applied it to #ntfs-next.
Thanks!
Hyunchul Lee
maintainerPATCH 3/3 ntfs: avoid overflow when limiting attribute read length
In reply to Hyunchul Leentfs_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.
--- fs/ntfs/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
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)) {
Signature · 1 line
2.43.0
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")Signed-off-by Hyunchul Lee <[email protected]>--- fs/ntfs/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/ntfs/inode.c +0
-0
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.
Re: [PATCH 3/3] ntfs: avoid overflow when limiting attribute read length
In reply to Hyunchul LeeQuoted 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")Signed-off-by Hyunchul Lee <[email protected]>
Applied it to #ntfs-next.
Thanks!
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")Reported-by Robert Morris <[email protected]>Signed-off-by Hyunchul Lee <[email protected]>--- 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@@ -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@@ -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@@ -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.
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")Reported-by Robert Morris <[email protected]>Signed-off-by Hyunchul Lee <[email protected]>
Applied it to #ntfs-next.
Thanks!