mirror of
https://github.com/torvalds/linux.git
synced 2026-11-20 13:03:48 +00:00
Merge tag 'ntfs-for-7.4-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs
Pull ntfs fixes from Namjae Jeon: - Don't allow compression to be enabled through system.ntfs_attrib on volumes with clusters larger than 4 KiB, preventing out-of-bounds accesses in the compressed read path. - Don't allow resident attribute values to overlap the attribute header, preventing malformed records from causing writes outside the MFT record buffer. - Fix recursive acquisition of the mapping invalidate lock when writing to non-resident named streams through NTFS_IOC_STREAM_WRITE. - Keep the file size unchanged when extending the initialized size fails, avoiding unintended truncation and page-cache removal. - Avoid integer overflow when limiting attribute reads to the attribute size. - Initialize the promoted index entry's child VCN before inserting it into the parent during an index split. - Validate index entry VCNs and return an error for negative attribute read positions before they reach pointer arithmetic. - Fix cleanup after ACL initialization fails in __ntfs_create(), avoiding an erroneous free of $MFT, unmapping an unmapped record, and unlocking mutexes that were never held. * tag 'ntfs-for-7.4-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs: ntfs: don't allow compression on volumes with clusters larger than 4K ntfs: don't allow resident values to overlap the attribute header ntfs: fix a self-deadlock on writes to non-resident named streams ntfs: keep the file size on a failed initialized-size extension ntfs: avoid overflow when limiting attribute read length ntfs: initialize VCN when inserting index entries ntfs: reject negative attribute read positions ntfs: Fix error handling in __ntfs_create()
This commit is contained in:
+6
-3
@@ -704,17 +704,20 @@ static bool ntfs_resident_attr_value_get(const struct attr_record *a,
|
||||
struct ntfs_resident_attr_value *value)
|
||||
{
|
||||
u32 attr_len;
|
||||
u32 min_len;
|
||||
u16 value_offset;
|
||||
|
||||
attr_len = le32_to_cpu(a->length);
|
||||
if (attr_len < offsetof(struct attr_record, data.resident.reserved) +
|
||||
sizeof(a->data.resident.reserved))
|
||||
min_len = offsetof(struct attr_record, data.resident.reserved) +
|
||||
sizeof(a->data.resident.reserved);
|
||||
if (attr_len < min_len)
|
||||
return false;
|
||||
|
||||
value->len = le32_to_cpu(a->data.resident.value_length);
|
||||
value_offset = le16_to_cpu(a->data.resident.value_offset);
|
||||
|
||||
if (value->len > attr_len || value_offset > attr_len - value->len)
|
||||
if (value_offset < min_len || value->len > attr_len ||
|
||||
value_offset > attr_len - value->len)
|
||||
return false;
|
||||
|
||||
value->data = (const u8 *)a + value_offset;
|
||||
|
||||
@@ -721,6 +721,11 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
|
||||
goto err_out;
|
||||
}
|
||||
|
||||
if ((new_aflags & ATTR_IS_COMPRESSED) && ni->vol->cluster_size > 4096) {
|
||||
err = -EOPNOTSUPP;
|
||||
goto err_out;
|
||||
}
|
||||
|
||||
if (!a->non_resident) {
|
||||
if (!(new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED))) {
|
||||
err = 0;
|
||||
|
||||
+39
-2
@@ -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;
|
||||
}
|
||||
@@ -1547,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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
+29
-9
@@ -1034,6 +1034,7 @@ view_index_meta:
|
||||
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;
|
||||
@@ -2554,8 +2556,12 @@ int ntfs_show_options(struct seq_file *sf, struct dentry *root)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
|
||||
const loff_t new_size)
|
||||
/*
|
||||
* The caller must hold @vi's mapping invalidate lock.
|
||||
*/
|
||||
static int __ntfs_extend_initialized_size(struct inode *vi,
|
||||
const loff_t offset,
|
||||
const loff_t new_size)
|
||||
{
|
||||
struct ntfs_inode *ni = NTFS_I(vi);
|
||||
struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
|
||||
@@ -2563,10 +2569,11 @@ int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
|
||||
unsigned long flags;
|
||||
int err = 0;
|
||||
|
||||
lockdep_assert_held_write(&vi->i_mapping->invalidate_lock);
|
||||
|
||||
if (!NInoNonResident(ni))
|
||||
return -EINVAL;
|
||||
|
||||
filemap_invalidate_lock(vi->i_mapping);
|
||||
read_lock_irqsave(&ni->size_lock, flags);
|
||||
old_init_size = ni->initialized_size;
|
||||
read_unlock_irqrestore(&ni->size_lock, flags);
|
||||
@@ -2599,9 +2606,17 @@ int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
|
||||
mutex_lock(&mrec_ni->mrec_lock);
|
||||
err = ntfs_attr_set_initialized_size(ni, new_size);
|
||||
mutex_unlock(&mrec_ni->mrec_lock);
|
||||
if (err)
|
||||
truncate_setsize(vi, old_init_size);
|
||||
out:
|
||||
return err;
|
||||
}
|
||||
|
||||
int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
|
||||
const loff_t new_size)
|
||||
{
|
||||
int err;
|
||||
|
||||
filemap_invalidate_lock(vi->i_mapping);
|
||||
err = __ntfs_extend_initialized_size(vi, offset, new_size);
|
||||
filemap_invalidate_unlock(vi->i_mapping);
|
||||
return err;
|
||||
}
|
||||
@@ -3715,11 +3730,11 @@ 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;
|
||||
}
|
||||
if (pos + count > isize)
|
||||
if (count > isize - pos)
|
||||
count = isize - pos;
|
||||
|
||||
if (!NInoNonResident(ni)) {
|
||||
@@ -4087,8 +4102,13 @@ s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool s
|
||||
mutex_unlock(&mrec_ni->mrec_lock);
|
||||
/* Attribute writes bypass iomap's delayed-allocation preparation. */
|
||||
if (ntfs_inode_is_named_stream(ni)) {
|
||||
/* A failed write must not expose any newly initialized data. */
|
||||
ret = ntfs_extend_initialized_size(vi, pos + count, pos + count);
|
||||
/*
|
||||
* A failed write must not expose any newly initialized
|
||||
* data. The stream ioctl caller holds the mapping
|
||||
* invalidate lock.
|
||||
*/
|
||||
ret = __ntfs_extend_initialized_size(vi, pos + count,
|
||||
pos + count);
|
||||
modified |= ni->initialized_size != old_init_size;
|
||||
if (!ret)
|
||||
ret = ntfs_attr_fallocate(ni, pos, count, true);
|
||||
|
||||
+4
-2
@@ -472,8 +472,10 @@ static struct ntfs_inode *__ntfs_create(const struct mnt_idmap *idmap, struct in
|
||||
#ifdef CONFIG_NTFS_FS_POSIX_ACL
|
||||
if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
|
||||
err = ntfs_init_acl(idmap, vi, dir);
|
||||
if (err)
|
||||
goto err_out;
|
||||
if (err) {
|
||||
iput(vi);
|
||||
return ERR_PTR(err);
|
||||
}
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user