public inbox for ntfs3@lists.linux.dev
 help / color / mirror / Atom feed
From: lei lu <llfamsec@gmail.com>
To: almaz.alexandrovich@paragon-software.com, ntfs3@lists.linux.dev
Cc: lizhi.xu@windriver.com,
	syzbot+a426cde6dee8c2884b0b@syzkaller.appspotmail.com,
	syzkaller-bugs@googlegroups.com
Subject: [PATCH] ntfs3: Add bounds checking to mi_enum_attr()
Date: Fri, 23 Aug 2024 21:05:03 +0800	[thread overview]
Message-ID: <20240823130503.177723-1-llfamsec@gmail.com> (raw)

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

Signed-off-by: lei lu <llfamsec@gmail.com>
---
 fs/ntfs3/record.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)

diff --git a/fs/ntfs3/record.c b/fs/ntfs3/record.c
index 6aa3a9d44df1..d47ca8ca270b 100644
--- a/fs/ntfs3/record.c
+++ b/fs/ntfs3/record.c
@@ -223,34 +223,35 @@ 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);
 		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;
+
 	if (attr->type == ATTR_END) {
 		/* End of enumeration. */
 		return NULL;
@@ -265,10 +266,6 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
 	if (t32 < prev_type)
 		return NULL;
 
-	/* Check overflow and boundary. */
-	if (off + asize < off || off + asize > used)
-		return NULL;
-
 	/* Check size of attribute. */
 	if (!attr->non_res) {
 		/* Check resident fields. */
-- 
2.34.1


             reply	other threads:[~2024-08-23 13:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-23 13:05 lei lu [this message]
2024-08-23 13:49 ` [PATCH v2] ntfs3: Add bounds checking to mi_enum_attr() lei lu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240823130503.177723-1-llfamsec@gmail.com \
    --to=llfamsec@gmail.com \
    --cc=almaz.alexandrovich@paragon-software.com \
    --cc=lizhi.xu@windriver.com \
    --cc=ntfs3@lists.linux.dev \
    --cc=syzbot+a426cde6dee8c2884b0b@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox