Skip to content

Commit 4c2ed2a

Browse files
committed
Merge tag 'ntfs-for-7.1-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs
Pull ntfs fixes from Namjae Jeon: - Fix a NULL pointer dereference in ntfs_index_walk_down() by validating index block allocation - Fix a memory leak of the symlink target string in ntfs_reparse_set_wsl_symlink() during error paths - Prevent VCN overflow and validate lowest_vcn in ntfs_mapping_pairs_decompress() to avoid runlist corruption - Fix a page reference leak in ntfs_write_iomap_end_resident() when attribute search context allocation fails - Fix an invalid PTR_ERR() usage on a valid folio pointer in __ntfs_bitmap_set_bits_in_run() - Correct directory link counting by dropping nlink only when the MFT record link count reaches zero for WIN32/DOS aliases - Fix an uninitialized variable in ntfs_mapping_pairs_decompress() by returning an error pointer directly * tag 'ntfs-for-7.1-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs: ntfs: Use return instead of goto in ntfs_mapping_pairs_decompress() ntfs: drop nlink once for WIN32/DOS aliases ntfs: fix invalid PTR_ERR() usage in __ntfs_bitmap_set_bits_in_run() ntfs: fix error handling in ntfs_write_iomap_end_resident() ntfs: fix VCN overflow in ntfs_mapping_pairs_decompress() ntfs: fix WSL symlink target leak on reparse failure ntfs: fix NULL dereference in ntfs_index_walk_down()
2 parents f1a5e78 + 9e93540 commit 4c2ed2a

7 files changed

Lines changed: 72 additions & 25 deletions

File tree

fs/ntfs/bitmap.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
125125
struct address_space *mapping;
126126
struct folio *folio;
127127
u8 *kaddr;
128-
int pos, len;
128+
int pos, len, err;
129129
u8 bit;
130130
struct ntfs_inode *ni = NTFS_I(vi);
131131
struct ntfs_volume *vol = ni->vol;
@@ -201,8 +201,10 @@ int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
201201

202202
/* If we are not in the last page, deal with all subsequent pages. */
203203
while (index < end_index) {
204-
if (cnt <= 0)
204+
if (cnt <= 0) {
205+
err = -EIO;
205206
goto rollback;
207+
}
206208

207209
/* Update @index and get the next folio. */
208210
folio_mark_dirty(folio);
@@ -214,6 +216,7 @@ int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
214216
ntfs_error(vi->i_sb,
215217
"Failed to map subsequent page (error %li), aborting.",
216218
PTR_ERR(folio));
219+
err = PTR_ERR(folio);
217220
goto rollback;
218221
}
219222

@@ -265,7 +268,7 @@ int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
265268
* - @count - @cnt is the number of bits that have been modified
266269
*/
267270
if (is_rollback)
268-
return PTR_ERR(folio);
271+
return err;
269272
if (count != cnt)
270273
pos = __ntfs_bitmap_set_bits_in_run(vi, start_bit, count - cnt,
271274
value ? 0 : 1, true);
@@ -274,14 +277,14 @@ int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
274277
if (!pos) {
275278
/* Rollback was successful. */
276279
ntfs_error(vi->i_sb,
277-
"Failed to map subsequent page (error %li), aborting.",
278-
PTR_ERR(folio));
280+
"Failed to map subsequent page (error %i), aborting.",
281+
err);
279282
} else {
280283
/* Rollback failed. */
281284
ntfs_error(vi->i_sb,
282-
"Failed to map subsequent page (error %li) and rollback failed (error %i). Aborting and leaving inconsistent metadata. Unmount and run chkdsk.",
283-
PTR_ERR(folio), pos);
285+
"Failed to map subsequent page (error %i) and rollback failed (error %i). Aborting and leaving inconsistent metadata. Unmount and run chkdsk.",
286+
err, pos);
284287
NVolSetErrors(NTFS_SB(vi->i_sb));
285288
}
286-
return PTR_ERR(folio);
289+
return err;
287290
}

fs/ntfs/dir.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -911,16 +911,23 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)
911911

912912
if (next->flags & INDEX_ENTRY_NODE) {
913913
next = ntfs_index_walk_down(next, ictx);
914-
if (!next) {
915-
err = -EIO;
914+
if (IS_ERR(next)) {
915+
err = PTR_ERR(next);
916916
goto out;
917917
}
918918
}
919919

920920
if (next && !(next->flags & INDEX_ENTRY_END))
921921
goto nextdir;
922922

923-
while ((next = ntfs_index_next(next, ictx)) != NULL) {
923+
while (1) {
924+
next = ntfs_index_next(next, ictx);
925+
if (IS_ERR(next)) {
926+
err = PTR_ERR(next);
927+
goto out;
928+
}
929+
if (!next)
930+
break;
924931
nextdir:
925932
/* Check the consistency of an index entry */
926933
if (ntfs_index_entry_inconsistent(ictx, vol, next, COLLATION_FILE_NAME,

fs/ntfs/index.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,15 +1969,19 @@ int ntfs_index_remove(struct ntfs_inode *dir_ni, const void *key, const u32 keyl
19691969
struct index_entry *ntfs_index_walk_down(struct index_entry *ie, struct ntfs_index_context *ictx)
19701970
{
19711971
struct index_entry *entry;
1972+
struct index_block *ib;
19721973
s64 vcn;
19731974

19741975
entry = ie;
19751976
do {
19761977
vcn = ntfs_ie_get_vcn(entry);
19771978
if (ictx->is_in_root) {
1979+
ib = kvzalloc(ictx->block_size, GFP_NOFS);
1980+
if (!ib)
1981+
return ERR_PTR(-ENOMEM);
19781982
/* down from level zero */
19791983
ictx->ir = NULL;
1980-
ictx->ib = kvzalloc(ictx->block_size, GFP_NOFS);
1984+
ictx->ib = ib;
19811985
ictx->pindex = 1;
19821986
ictx->is_in_root = false;
19831987
} else {
@@ -1991,8 +1995,8 @@ struct index_entry *ntfs_index_walk_down(struct index_entry *ie, struct ntfs_ind
19911995
ictx->entry = ntfs_ie_get_first(&ictx->ib->index);
19921996
entry = ictx->entry;
19931997
} else
1994-
entry = NULL;
1995-
} while (entry && (entry->flags & INDEX_ENTRY_NODE));
1998+
entry = ERR_PTR(-EIO);
1999+
} while (!IS_ERR(entry) && (entry->flags & INDEX_ENTRY_NODE));
19962000

19972001
return entry;
19982002
}
@@ -2097,10 +2101,15 @@ struct index_entry *ntfs_index_next(struct index_entry *ie, struct ntfs_index_co
20972101

20982102
/* walk down if it has a subnode */
20992103
if (flags & INDEX_ENTRY_NODE) {
2100-
if (!ictx->ia_ni)
2104+
if (!ictx->ia_ni) {
21012105
ictx->ia_ni = ntfs_ia_open(ictx, ictx->idx_ni);
2106+
if (!ictx->ia_ni)
2107+
return ERR_PTR(-EIO);
2108+
}
21022109

21032110
next = ntfs_index_walk_down(next, ictx);
2111+
if (IS_ERR(next))
2112+
return next;
21042113
} else {
21052114

21062115
/* walk up it has no subnode, nor data */

fs/ntfs/iomap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,8 +788,7 @@ static int ntfs_write_iomap_end_resident(struct inode *inode, loff_t pos,
788788
ctx = ntfs_attr_get_search_ctx(ni, NULL);
789789
if (!ctx) {
790790
written = -ENOMEM;
791-
mutex_unlock(&ni->mrec_lock);
792-
return written;
791+
goto err_out;
793792
}
794793

795794
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
@@ -810,7 +809,8 @@ static int ntfs_write_iomap_end_resident(struct inode *inode, loff_t pos,
810809
memcpy(kattr + pos, iomap_inline_data(iomap, pos), written);
811810
mark_mft_record_dirty(ctx->ntfs_ino);
812811
err_out:
813-
ntfs_attr_put_search_ctx(ctx);
812+
if (ctx)
813+
ntfs_attr_put_search_ctx(ctx);
814814
put_page(ipage);
815815
mutex_unlock(&ni->mrec_lock);
816816
return written;

fs/ntfs/namei.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,8 @@ static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
945945

946946
ni_mrec = actx->base_mrec ? actx->base_mrec : actx->mrec;
947947
ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) - 1);
948-
drop_nlink(VFS_I(ni));
948+
if (!S_ISDIR(VFS_I(ni)->i_mode))
949+
drop_nlink(VFS_I(ni));
949950

950951
mark_mft_record_dirty(ni);
951952
if (looking_for_dos_name) {
@@ -955,6 +956,13 @@ static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
955956
goto search;
956957
}
957958

959+
/*
960+
* For directories, Drop VFS nlink only when mft record link count
961+
* becomes zero. Because we fixes VFS nlink to 1 for directories.
962+
*/
963+
if (S_ISDIR(VFS_I(ni)->i_mode) && !le16_to_cpu(ni_mrec->link_count))
964+
drop_nlink(VFS_I(ni));
965+
958966
/*
959967
* If hard link count is not equal to zero then we are done. In other
960968
* case there are no reference to this inode left, so we should free all
@@ -1221,7 +1229,8 @@ static int __ntfs_link(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
12211229
}
12221230
/* Increment hard links count. */
12231231
ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) + 1);
1224-
inc_nlink(VFS_I(ni));
1232+
if (!S_ISDIR(vi->i_mode))
1233+
inc_nlink(VFS_I(ni));
12251234

12261235
/* Done! */
12271236
mark_mft_record_dirty(ni);

fs/ntfs/reparse.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,6 @@ int ntfs_reparse_set_wsl_symlink(struct ntfs_inode *ni,
505505
struct reparse_point *reparse;
506506
struct wsl_link_reparse_data *data;
507507

508-
utarget = (char *)NULL;
509508
len = ntfs_ucstonls(ni->vol, target, target_len, &utarget, 0);
510509
if (len <= 0)
511510
return -EINVAL;
@@ -514,7 +513,7 @@ int ntfs_reparse_set_wsl_symlink(struct ntfs_inode *ni,
514513
reparse = kvzalloc(reparse_len, GFP_NOFS);
515514
if (!reparse) {
516515
err = -ENOMEM;
517-
kvfree(utarget);
516+
kfree(utarget);
518517
} else {
519518
data = (struct wsl_link_reparse_data *)reparse->reparse_data;
520519
reparse->reparse_tag = IO_REPARSE_TAG_LX_SYMLINK;
@@ -528,6 +527,8 @@ int ntfs_reparse_set_wsl_symlink(struct ntfs_inode *ni,
528527
kvfree(reparse);
529528
if (!err)
530529
ni->target = utarget;
530+
else
531+
kfree(utarget);
531532
}
532533
return err;
533534
}

fs/ntfs/runlist.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* Copyright (c) 2007-2022 Jean-Pierre Andre
1616
*/
1717

18+
#include <linux/overflow.h>
19+
1820
#include "ntfs.h"
1921
#include "attrib.h"
2022

@@ -739,6 +741,7 @@ struct runlist_element *ntfs_mapping_pairs_decompress(const struct ntfs_volume *
739741
int rlsize; /* Size of runlist buffer. */
740742
u16 rlpos; /* Current runlist position in units of struct runlist_elements. */
741743
u8 b; /* Current byte offset in buf. */
744+
u64 lowest_vcn; /* Raw on-disk lowest_vcn. */
742745

743746
#ifdef DEBUG
744747
/* Make sure attr exists and is non-resident. */
@@ -747,8 +750,14 @@ struct runlist_element *ntfs_mapping_pairs_decompress(const struct ntfs_volume *
747750
return ERR_PTR(-EINVAL);
748751
}
749752
#endif
753+
lowest_vcn = le64_to_cpu(attr->data.non_resident.lowest_vcn);
754+
/* Validate lowest_vcn from on-disk metadata to ensure it is sane. */
755+
if (overflows_type(lowest_vcn, vcn)) {
756+
ntfs_error(vol->sb, "Invalid lowest_vcn in mapping pairs.");
757+
return ERR_PTR(-EIO);
758+
}
750759
/* Start at vcn = lowest_vcn and lcn 0. */
751-
vcn = le64_to_cpu(attr->data.non_resident.lowest_vcn);
760+
vcn = lowest_vcn;
752761
lcn = 0;
753762
/* Get start of the mapping pairs array. */
754763
buf = (u8 *)attr +
@@ -823,8 +832,17 @@ struct runlist_element *ntfs_mapping_pairs_decompress(const struct ntfs_volume *
823832
* element.
824833
*/
825834
rl[rlpos].length = deltaxcn;
826-
/* Increment the current vcn by the current run length. */
827-
vcn += deltaxcn;
835+
/*
836+
* Increment the current vcn by the current run length.
837+
* Guard against s64 overflow from a crafted mapping
838+
* pairs array to preserve the monotonically-increasing
839+
* vcn invariant.
840+
*/
841+
if (unlikely(check_add_overflow(vcn, deltaxcn, &vcn))) {
842+
ntfs_error(vol->sb, "VCN overflow in mapping pairs array.");
843+
goto err_out;
844+
}
845+
828846
/*
829847
* There might be no lcn change at all, as is the case for
830848
* sparse clusters on NTFS 3.0+, in which case we set the lcn

0 commit comments

Comments
 (0)