public inbox for ntfs3@lists.linux.dev
 help / color / mirror / Atom feed
* [PATCH] ntfs3: Add bounds checking to mi_enum_attr()
@ 2024-08-23 13:05 lei lu
  2024-08-23 13:49 ` [PATCH v2] " lei lu
  0 siblings, 1 reply; 4+ messages in thread
From: lei lu @ 2024-08-23 13:05 UTC (permalink / raw)
  To: almaz.alexandrovich, ntfs3
  Cc: lizhi.xu, syzbot+a426cde6dee8c2884b0b, syzkaller-bugs

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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2] ntfs3: Add bounds checking to mi_enum_attr()
@ 2024-08-23 13:39 lei lu
  2024-08-26  7:06 ` Konstantin Komarov
  0 siblings, 1 reply; 4+ messages in thread
From: lei lu @ 2024-08-23 13:39 UTC (permalink / raw)
  To: almaz.alexandrovich, ntfs3
  Cc: lizhi.xu, syzbot+a426cde6dee8c2884b0b, syzkaller-bugs

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 | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/fs/ntfs3/record.c b/fs/ntfs3/record.c
index 6aa3a9d44df1..973e2a371bc2 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)
 		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);
@@ -265,6 +256,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;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] ntfs3: Add bounds checking to mi_enum_attr()
  2024-08-23 13:05 [PATCH] ntfs3: Add bounds checking to mi_enum_attr() lei lu
@ 2024-08-23 13:49 ` lei lu
  0 siblings, 0 replies; 4+ messages in thread
From: lei lu @ 2024-08-23 13:49 UTC (permalink / raw)
  To: llfamsec
  Cc: almaz.alexandrovich, lizhi.xu, ntfs3, syzbot+a426cde6dee8c2884b0b,
	syzkaller-bugs

Hi all,

This patch ignores the last attr is just 8 bytes and type field
is ATTR_END. I have already send V2.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] ntfs3: Add bounds checking to mi_enum_attr()
  2024-08-23 13:39 lei lu
@ 2024-08-26  7:06 ` Konstantin Komarov
  0 siblings, 0 replies; 4+ messages in thread
From: Konstantin Komarov @ 2024-08-26  7:06 UTC (permalink / raw)
  To: lei lu, ntfs3; +Cc: lizhi.xu, syzbot+a426cde6dee8c2884b0b, syzkaller-bugs

On 23.08.2024 16:39, lei lu wrote:
> 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 | 23 ++++++++++-------------
>   1 file changed, 10 insertions(+), 13 deletions(-)
>
> diff --git a/fs/ntfs3/record.c b/fs/ntfs3/record.c
> index 6aa3a9d44df1..973e2a371bc2 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)
>   		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);
> @@ -265,6 +256,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;
Hi Lei Lu,
I saw your patch and have started working on it, including checks and 
testing.

Regards, Konstantin



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-08-26  7:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-23 13:05 [PATCH] ntfs3: Add bounds checking to mi_enum_attr() lei lu
2024-08-23 13:49 ` [PATCH v2] " lei lu
  -- strict thread matches above, loose matches on Subject: below --
2024-08-23 13:39 lei lu
2024-08-26  7:06 ` Konstantin Komarov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox