Skip to content

Commit cad7c6f

Browse files
Zhan Xushengnamjaejeon
authored andcommitted
ntfs: fix VCN overflow in ntfs_mapping_pairs_decompress()
In ntfs_mapping_pairs_decompress(), lowest_vcn is read from on-disk metadata and used as the initial vcn without validation. A malformed value can introduce an invalid (e.g. negative) vcn, corrupting the runlist from the start. Additionally, the accumulation vcn += deltaxcn does not check for s64 overflow. A crafted mapping pairs array can wrap vcn to a negative value, breaking the monotonically- increasing invariant relied upon by ntfs_rl_vcn_to_lcn() and related helpers. Fix this by validating lowest_vcn and using check_add_overflow() for vcn accumulation. Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com> Reviewed-by: Hyunchul Lee <hyc.lee@gmail.com> Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
1 parent 2dd8c16 commit cad7c6f

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

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+
goto err_out;
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)