Skip to contentSkip to conversation

ntfs3: Add bounds checking to mi_enum_attr()

PATCH In review [email protected] 2 participants 2 messages Nov 28, 2024 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ntfs3: Add bounds checking to mi_enum_attr()

From: lei lu <[email protected]>

[ Upstream commit 556bdf27c2dd5c74a9caacbe524b943a6cd42d99 ]

Added bounds checking to make sure that every attr don't stray beyond valid memory region.

---
 fs/ntfs3/record.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
fs/ntfs3/record.c +10 -13
index 7ab452710572..a332b925cb37 100644
@@ -217,28 +217,19 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
 		prev_type = 0;
 		attr = Add2Ptr(rec, off);
 	} else {
-		/* Check if input attr inside record. */
+		/*
+		 * We don't need to check previous attr here. There is
+		 * a bounds checking in the previous round.
+		 */
 		off = PtrOffset(rec, attr);
-		if (off >= used)
-			return NULL;
 
 		asize = le32_to_cpu(attr->size);
-		if (asize < SIZEOF_RESIDENT) {
-			/* Impossible 'cause we should not return such attribute. */
-			return NULL;
-		}
-
-		/* Overflow check. */
-		if (off + asize < off)
-			return NULL;
 
 		prev_type = le32_to_cpu(attr->type);
 		attr = Add2Ptr(attr, asize);
 		off += asize;
 	}
 
-	asize = le32_to_cpu(attr->size);
-
 	/* Can we use the first field (attr->type). */
 	if (off + 8 > used) {
 		static_assert(ALIGN(sizeof(enum ATTR_TYPE), 8) == 8);
@@ -259,6 +250,12 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
 	if (t32 < prev_type)
 		return NULL;
 
+	asize = le32_to_cpu(attr->size);
+	if (asize < SIZEOF_RESIDENT) {
+		/* Impossible 'cause we should not return such attribute. */
+		return NULL;
+	}
+
 	/* Check overflow and boundary. */
 	if (off + asize < off || off + asize > used)
 		return NULL;
Signature · 1 line
2.34.1
maintainer
#1 lore raw

[ Sasha's backport helper bot ]

Hi,

The upstream commit SHA1 provided is correct: 556bdf27c2dd5c74a9caacbe524b943a6cd42d99

WARNING: Author mismatch between patch and upstream commit:
Backport author: [email protected] Commit author: lei lu <[email protected]>

Status in newer kernel trees:
6.12.y | Present (exact SHA1)
6.11.y | Present (different SHA1: 809f9b419c75) 6.6.y | Present (different SHA1: 22cdf3be7d34) 6.1.y | Not found

Note: The patch differs from the upstream commit:
---

tmp/tmp.MGvAiQzuxM renamed from - +9 -3
@@ -1,17 +1,20 @@
+[ Upstream commit 556bdf27c2dd5c74a9caacbe524b943a6cd42d99 ]
+
 Added bounds checking to make sure that every attr don't stray beyond
 valid memory region.
 
 Signed-off-by: lei lu <[email protected]>
 Signed-off-by: Konstantin Komarov <[email protected]>
+Signed-off-by: Bin Lan <[email protected]>
 ---
  fs/ntfs3/record.c | 23 ++++++++++-------------
  1 file changed, 10 insertions(+), 13 deletions(-)
 
 diff --git a/fs/ntfs3/record.c b/fs/ntfs3/record.c
-index 6c76503edc200..2a375247b3c09 100644
+index 7ab452710572..a332b925cb37 100644
 --- a/fs/ntfs3/record.c
 +++ b/fs/ntfs3/record.c
-@@ -223,28 +223,19 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
+@@ -217,28 +217,19 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
  		prev_type = 0;
  		attr = Add2Ptr(rec, off);
  	} else {
@@ -44,7 +47,7 @@
  	/* Can we use the first field (attr->type). */
  	if (off + 8 > used) {
  		static_assert(ALIGN(sizeof(enum ATTR_TYPE), 8) == 8);
-@@ -265,6 +256,12 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
+@@ -259,6 +250,12 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
  	if (t32 < prev_type)
  		return NULL;
  
@@ -57,3 +60,6 @@
  	/* Check overflow and boundary. */
  	if (off + asize < off || off + asize > used)
  		return NULL;
+-- 
+2.34.1
+

---

Results of testing on various branches:

| Branch                    | Patch Apply | Build Test |
|---------------------------|-------------|------------|
| stable/linux-6.1.y        |  Success    |  Success   |