Skip to contentSkip to conversation

fs: Test failure fixes and fuzzer fixes

PATCH Superseded Andrew Hamilton 3 participants 13 messages May 20, 2025 – May 22, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover fs: Test failure fixes and fuzzer fixes

Correct some NTFS test failures introduced by previous fixes for CVEs. With these changes, the NTFS tests run successfully.

Also correct some fuzzer identified crashes and hangs (in NTFS and one that may in theory impact HFS too).

Changes since v1:
- Daniel Kiper review comments addressed:
- v2 patch 1&2: Split v1 patch #1 into two (v2 patch #1 and #2).
- v2 patch 1-4: Improve commit descriptions.
- v2 patch 1-4: Wrap commit comments before 80 chars.
- Vladimir Serbinenko review comments addressed:
- v2 patch 3: Change fshelp error code to GRUB_ERR_BAD_FS per review.
- v2 patch 3: Remove translation tag / call from fshelp per review.

Andrew Hamilton (4):
fs/ntfs: Correct regression with run list calculation fs/ntfs: Correct attribute vs attribute list validation fs/fshelp: Avoid possible NULL pointer deference fs/ntfs: Correct possible access violations and hangs

 grub-core/fs/fshelp.c |   5 +-
 grub-core/fs/ntfs.c   | 147 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 123 insertions(+), 29 deletions(-)
Signature · 7 lines
2.39.5


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

PATCH v2 1/4 fs/ntfs: Correct regression with run list calculation

Correct ntfs_test test failures around attempting to validate attribute run list values. The calculation was incorrect for the 'curr' variable. With previous calculation, some file systems would fail validation despite being well-formed and valid. This was caused by incrementing 'curr' by min_size which included both the (already accounted for) min_size as well as the size of the run list. Correct by making a new variable 'run_size' to denote the current run list size to increment both 'curr' and 'min_size' separately.

Fixes 067b6d225 (fs/ntfs: Implement attribute verification)
---
 grub-core/fs/ntfs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
grub-core/fs/ntfs.c +5 -2
index b3117bf92..3eb70111b 100644
@@ -83,6 +83,7 @@ validate_attribute (grub_uint8_t *attr, void *end)
 {
   grub_size_t attr_size = 0;
   grub_size_t min_size = 0;
+  grub_size_t run_size = 0;
   grub_size_t spare = (grub_uint8_t *) end - attr;
   /*
    * Just used as a temporary variable to try and deal with cases where someone
@@ -174,8 +175,10 @@ validate_attribute (grub_uint8_t *attr, void *end)
 	   * These directly follow the header byte, so we use them to update
 	   * the minimum size.
 	   */
-	  min_size += (attr[curr] & 0x7) + ((attr[curr] >> 4) & 0x7);
-	  curr += min_size;
+	  run_size = (attr[curr] & 0x7) + ((attr[curr] >> 4) & 0x7);
+	  curr += run_size;
+	  curr++;
+	  min_size += run_size;
 	  min_size++;
 	  if (min_size > attr_size)
 	    goto fail;
Signature · 7 lines
2.39.5


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] fs/ntfs: Correct regression with run list calculation

Regards
Vladimir 'phcoder' Serbinenko

Quoted 44 lines · Andrew Hamilton

Correct ntfs_test test failures around attempting to validate attribute run list values. The calculation was incorrect for the 'curr' variable. With previous calculation, some file systems would fail validation despite being well-formed and valid. This was caused by incrementing 'curr' by min_size which included both the (already accounted for) min_size as well as the size of the run list. Correct by making a new variable 'run_size' to denote the current run list size to increment both 'curr' and 'min_size' separately.

Fixes 067b6d225 (fs/ntfs: Implement attribute verification)
---
 grub-core/fs/ntfs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
grub-core/fs/ntfs.c +1 -0
index b3117bf92..3eb70111b 100644
@@ -83,6 +83,7 @@ validate_attribute (grub_uint8_t *attr, void *end)
 {
   grub_size_t attr_size = 0;
   grub_size_t min_size = 0;
+  grub_size_t run_size = 0;
   grub_size_t spare = (grub_uint8_t *) end - attr;
   /*
    * Just used as a temporary variable to try and deal with cases where

someone

grub-core/fs/ntfs.c +0 -0
@@ -174,8 +175,10 @@ validate_attribute (grub_uint8_t *attr, void *end)
           * These directly follow the header byte, so we use them to

update
* the minimum size.
*/

grub-core/fs/ntfs.c +4 -3
-         min_size += (attr[curr] & 0x7) + ((attr[curr] >> 4) & 0x7);
-         curr += min_size;
+         run_size = (attr[curr] & 0x7) + ((attr[curr] >> 4) & 0x7);
+         curr += run_size;
+         curr++;
+         min_size += run_size;
          min_size++;
          if (min_size > attr_size)
            goto fail;
--
Quoted 3 lines

2.39.5

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw

Re: [PATCH v2 1/4] fs/ntfs: Correct regression with run list calculation

In reply to Andrew Hamilton
Quoted 29 lines · Andrew Hamilton

Correct ntfs_test test failures around attempting to validate attribute run list values. The calculation was incorrect for the 'curr' variable. With previous calculation, some file systems would fail validation despite being well-formed and valid. This was caused by incrementing 'curr' by min_size which included both the (already accounted for) min_size as well as the size of the run list. Correct by making a new variable 'run_size' to denote the current run list size to increment both 'curr' and 'min_size' separately.

Fixes 067b6d225 (fs/ntfs: Implement attribute verification)
---
 grub-core/fs/ntfs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
grub-core/fs/ntfs.c +1 -0
index b3117bf92..3eb70111b 100644
@@ -83,6 +83,7 @@ validate_attribute (grub_uint8_t *attr, void *end)
 {
   grub_size_t attr_size = 0;
   grub_size_t min_size = 0;
+  grub_size_t run_size = 0;
   grub_size_t spare = (grub_uint8_t *) end - attr;
   /*
    * Just used as a temporary variable to try and deal with cases where someone

grub-core/fs/ntfs.c validate_attribute (grub_uint8_t *attr, void *end) · line 177

Quoted 3 lines
grub-core/fs/ntfs.c +0 -0
@@ -174,8 +175,10 @@ validate_attribute (grub_uint8_t *attr, void *end)
 	   * These directly follow the header byte, so we use them to update
 	   * the minimum size.
 	   */

If you update the code below should not you update the comment above?

grub-core/fs/ntfs.c validate_attribute (grub_uint8_t *attr, void *end) · line 180

Quoted 5 lines
grub-core/fs/ntfs.c +3 -2
-	  min_size += (attr[curr] & 0x7) + ((attr[curr] >> 4) & 0x7);
-	  curr += min_size;
+	  run_size = (attr[curr] & 0x7) + ((attr[curr] >> 4) & 0x7);
+	  curr += run_size;
+	  curr++;

This...

grub-core/fs/ntfs.c validate_attribute (grub_uint8_t *attr, void *end) · line 182

Quoted 2 lines
grub-core/fs/ntfs.c +1 -0
+	  min_size += run_size;
 	  min_size++;

... and this look strange even if they are correct. So, to make things clear I would extend comment above with description why additional increments are needed.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw

Re: [PATCH v2 1/4] fs/ntfs: Correct regression with run list calculation

Sounds good, I’ll make the update and send out a v3.

Thanks
Andrew

Quoted 46 lines · Daniel Kiper
Quoted 21 lines · Andrew Hamilton

Correct ntfs_test test failures around attempting to validate attribute run list values. The calculation was incorrect for the 'curr' variable. With previous calculation, some file systems would fail validation despite being well-formed and valid. This was caused by incrementing 'curr' by min_size which included both the (already accounted for) min_size as well as the size of the run list. Correct by making a new variable 'run_size' to denote the current run list size to increment both 'curr' and 'min_size' separately.

Fixes 067b6d225 (fs/ntfs: Implement attribute verification)
---
 grub-core/fs/ntfs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
grub-core/fs/ntfs.c +0 -0
index b3117bf92..3eb70111b 100644

grub-core/fs/ntfs.c validate_attribute (grub_uint8_t *attr, void *end) · line 89

Quoted 7 lines
grub-core/fs/ntfs.c +1 -0
@@ -83,6 +83,7 @@ validate_attribute (grub_uint8_t *attr, void *end)
 {
   grub_size_t attr_size = 0;
   grub_size_t min_size = 0;
+  grub_size_t run_size = 0;
   grub_size_t spare = (grub_uint8_t *) end - attr;
   /*
    * Just used as a temporary variable to try and deal with cases where

someone

grub-core/fs/ntfs.c validate_attribute (grub_uint8_t *attr, void *end) · line 175

Quoted 2 lines
grub-core/fs/ntfs.c +0 -0
@@ -174,8 +175,10 @@ validate_attribute (grub_uint8_t *attr, void *end)
         * These directly follow the header byte, so we use them to

update

grub-core/fs/ntfs.c validate_attribute (grub_uint8_t *attr, void *end) · line 177

Quoted 2 lines
grub-core/fs/ntfs.c +0 -0
         * the minimum size.
         */

If you update the code below should not you update the comment above?

grub-core/fs/ntfs.c validate_attribute (grub_uint8_t *attr, void *end) · line 180

Quoted 5 lines
grub-core/fs/ntfs.c +3 -2
-       min_size += (attr[curr] & 0x7) + ((attr[curr] >> 4) & 0x7);
-       curr += min_size;
+       run_size = (attr[curr] & 0x7) + ((attr[curr] >> 4) & 0x7);
+       curr += run_size;
+       curr++;

This...

Quoted 9 lines

grub-core/fs/ntfs.c validate_attribute (grub_uint8_t *attr, void *end) · line 182

Quoted 2 lines
grub-core/fs/ntfs.c +1 -0
+       min_size += run_size;
        min_size++;

... and this look strange even if they are correct. So, to make things clear I would extend comment above with description why additional increments are needed.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw

PATCH v2 2/4 fs/ntfs: Correct attribute vs attribute list validation

In reply to Andrew Hamilton

Correct ntfs_test test failures around attempting to validate attribute list entries as attributes. The NTFS code uses common logic in some places to parse both attributes and attribute_lists which complicates validation. Attribute lists contain different headers including a different size of the length field (2 bytes) at offset 4 instead of the 4 byte length field used in attributes at offset 4. There are other differences as well, but attempting to validate attribute list types using attribute header validation was causing failure of the NTFS test suite. This change restores some of the validation logic which may be shared between attributes and attribute lists to be closer to the original logic prior to fixes for previous CVEs. A following commit will address some of the implications of removing this validation logic by correcting some fuzzer failures (some which are exposed by removing the validation in some of the cases).

Fixes 067b6d225 (fs/ntfs: Implement attribute verification)
---
 grub-core/fs/ntfs.c | 45 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 11 deletions(-)
grub-core/fs/ntfs.c +34 -11
index 3eb70111b..0d087acd8 100644
@@ -219,7 +219,7 @@ validate_attribute (grub_uint8_t *attr, void *end)
 
 /* Return the next attribute if it exists, otherwise return NULL. */
 static grub_uint8_t *
-next_attribute (grub_uint8_t *curr_attribute, void *end)
+next_attribute (grub_uint8_t *curr_attribute, void *end, bool validate)
 {
   grub_uint8_t *next = curr_attribute;
 
@@ -231,7 +231,7 @@ next_attribute (grub_uint8_t *curr_attribute, void *end)
     return NULL;
 
   next += u16at (curr_attribute, 4);
-  if (validate_attribute (next, end) == false)
+  if (validate && validate_attribute (next, end) == false)
     return NULL;
 
   return next;
@@ -316,13 +316,16 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 {
   grub_uint8_t *mft_end;
 
+  /* GRUB_NTFS_AF_ALST indicates the attribute list type */
   if (at->flags & GRUB_NTFS_AF_ALST)
     {
     retry:
       while (at->attr_nxt)
 	{
 	  at->attr_cur = at->attr_nxt;
-	  at->attr_nxt = next_attribute (at->attr_cur, at->attr_end);
+	  /* Go to the next attribute in the list but do not validate */
+	  /* because this is the attribute list type. */
+	  at->attr_nxt = next_attribute (at->attr_cur, at->attr_end, false);
 	  if ((*at->attr_cur == attr) || (attr == 0))
 	    {
 	      grub_uint8_t *new_pos, *end;
@@ -378,7 +381,9 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 		    {
 		      return new_pos;
 		    }
-		  new_pos = next_attribute (new_pos, end);
+		    /* Go to the next attribute in the list but do not validate */
+		    /* because this is the attribute list type. */
+		    new_pos = next_attribute (new_pos, end, false);
 		}
 	      grub_error (GRUB_ERR_BAD_FS,
 			  "can\'t find 0x%X in attribute list",
@@ -392,7 +397,19 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
   mft_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
   while (at->attr_cur >= at->mft->buf && at->attr_cur < mft_end && *at->attr_cur != 0xFF)
     {
-      at->attr_nxt = next_attribute (at->attr_cur, at->end);
+      /* We can't use validate_attribute here because this logic
+       * seems to be used for both parsing through attributes
+       * and attribute lists. */
+      grub_uint16_t nsize = u16at (at->attr_cur, 4);
+      if (at->attr_cur + nsize >= at->end ||
+          at->attr_cur + GRUB_NTFS_ATTRIBUTE_HEADER_SIZE >= at->end)
+      {
+        at->attr_nxt = at->attr_cur;
+        break;
+      }
+      else
+        at->attr_nxt = at->attr_cur + nsize;
+
       if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
 	at->attr_end = at->attr_cur;
       if ((*at->attr_cur == attr) || (attr == 0))
@@ -439,14 +456,18 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
       /* From this point on pa_end is the end of the buffer */
       at->end = pa_end;
 
-      if (validate_attribute (at->attr_nxt, pa_end) == false)
-	return NULL;
+      if (at->attr_end >= pa_end || at->attr_nxt >= pa_end)
+        return NULL;
 
       while (at->attr_nxt)
 	{
 	  if ((*at->attr_nxt == attr) || (attr == 0))
 	    break;
-	  at->attr_nxt = next_attribute (at->attr_nxt, pa_end);
+
+	  grub_uint16_t nxt_offset = u16at (at->attr_nxt, 4);
+	  at->attr_nxt += nxt_offset;
+	  if (nxt_offset == 0 || at->attr_nxt >= (pa_end - 4))
+	    at->attr_nxt = NULL;
 	}
 
       if (at->attr_nxt >= at->attr_end || at->attr_nxt == NULL)
@@ -471,7 +492,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 						  + 1));
 	  pa = at->attr_nxt + u16at (pa, 4);
 
-	  if (validate_attribute (pa, pa_end) == true)
+	  if (pa >= pa_end)
 	    pa = NULL;
 
 	  while (pa)
@@ -490,7 +511,9 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 		   u32at (pa, 0x10) * (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR),
 		   at->mft->data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0, 0))
 		return NULL;
-	      pa = next_attribute (pa, pa_end);
+	      pa += u16at(pa, 4);
+	      if (pa >= pa_end)
+	        pa = NULL;
 	    }
 	  at->attr_nxt = at->attr_cur;
 	  at->flags &= ~GRUB_NTFS_AF_GPOS;
@@ -737,7 +760,7 @@ read_attr (struct grub_ntfs_attr *at, grub_uint8_t *dest, grub_disk_addr_t ofs,
 	  if (u32at (pa, 8) > vcn)
 	    break;
 	  at->attr_nxt = pa;
-	  pa = next_attribute (pa, at->attr_end);
+	  pa = next_attribute (pa, at->attr_end, true);
 	}
     }
   pp = find_attr (at, attr);
Signature · 7 lines
2.39.5


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#6 lore raw

Re: [PATCH v2 2/4] fs/ntfs: Correct attribute vs attribute list validation

Quoted 48 lines · Andrew Hamilton

Correct ntfs_test test failures around attempting to validate attribute list entries as attributes. The NTFS code uses common logic in some places to parse both attributes and attribute_lists which complicates validation. Attribute lists contain different headers including a different size of the length field (2 bytes) at offset 4 instead of the 4 byte length field used in attributes at offset 4. There are other differences as well, but attempting to validate attribute list types using attribute header validation was causing failure of the NTFS test suite. This change restores some of the validation logic which may be shared between attributes and attribute lists to be closer to the original logic prior to fixes for previous CVEs. A following commit will address some of the implications of removing this validation logic by correcting some fuzzer failures (some which are exposed by removing the validation in some of the cases).

Fixes 067b6d225 (fs/ntfs: Implement attribute verification)
---
 grub-core/fs/ntfs.c | 45 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 11 deletions(-)
grub-core/fs/ntfs.c +2 -2
index 3eb70111b..0d087acd8 100644
@@ -219,7 +219,7 @@ validate_attribute (grub_uint8_t *attr, void *end)
 
 /* Return the next attribute if it exists, otherwise return NULL. */
 static grub_uint8_t *
-next_attribute (grub_uint8_t *curr_attribute, void *end)
+next_attribute (grub_uint8_t *curr_attribute, void *end, bool validate)
 {
   grub_uint8_t *next = curr_attribute;
 
@@ -231,7 +231,7 @@ next_attribute (grub_uint8_t *curr_attribute, void *end)
     return NULL;
 
   next += u16at (curr_attribute, 4);
-  if (validate_attribute (next, end) == false)
+  if (validate && validate_attribute (next, end) == false)
     return NULL;
 
   return next;
@@ -316,13 +316,16 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 {
   grub_uint8_t *mft_end;
 

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 327

Quoted 10 lines
grub-core/fs/ntfs.c +3 -1
+  /* GRUB_NTFS_AF_ALST indicates the attribute list type */
   if (at->flags & GRUB_NTFS_AF_ALST)
     {
     retry:
       while (at->attr_nxt)
 	{
 	  at->attr_cur = at->attr_nxt;
-	  at->attr_nxt = next_attribute (at->attr_cur, at->attr_end);
+	  /* Go to the next attribute in the list but do not validate */
+	  /* because this is the attribute list type. */

May I ask you to stick to the GRUB coding style? [1]

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 385

Quoted 11 lines
grub-core/fs/ntfs.c +3 -1
+	  at->attr_nxt = next_attribute (at->attr_cur, at->attr_end, false);
 	  if ((*at->attr_cur == attr) || (attr == 0))
 	    {
 	      grub_uint8_t *new_pos, *end;
@@ -378,7 +381,9 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 		    {
 		      return new_pos;
 		    }
-		  new_pos = next_attribute (new_pos, end);
+		    /* Go to the next attribute in the list but do not validate */
+		    /* because this is the attribute list type. */

Ditto.

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 402

Quoted 12 lines
grub-core/fs/ntfs.c +4 -1
+		    new_pos = next_attribute (new_pos, end, false);
 		}
 	      grub_error (GRUB_ERR_BAD_FS,
 			  "can\'t find 0x%X in attribute list",
@@ -392,7 +397,19 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
   mft_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
   while (at->attr_cur >= at->mft->buf && at->attr_cur < mft_end && *at->attr_cur != 0xFF)
     {
-      at->attr_nxt = next_attribute (at->attr_cur, at->end);
+      /* We can't use validate_attribute here because this logic
+       * seems to be used for both parsing through attributes
+       * and attribute lists. */

Ditto.

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 403

Quoted 1 line
grub-core/fs/ntfs.c +1 -0
+      grub_uint16_t nsize = u16at (at->attr_cur, 4);

Please do not mix code with variable definitions. Define variables (and initialize if needed) at the beginning of a function or a given block. Separate definitions with code with one empty line.

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 405

Quoted 2 lines
grub-core/fs/ntfs.c +2 -0
+      if (at->attr_cur + nsize >= at->end ||
+          at->attr_cur + GRUB_NTFS_ATTRIBUTE_HEADER_SIZE >= at->end)

if (at->attr_cur + grub_max (GRUB_NTFS_ATTRIBUTE_HEADER_SIZE, nsize) >= at->end))

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 411

Quoted 6 lines
grub-core/fs/ntfs.c +6 -0
+      {
+        at->attr_nxt = at->attr_cur;
+        break;
+      }
+      else
+        at->attr_nxt = at->attr_cur + nsize;

Is there any chance for overflow here? And in the "if" above?

Quoted 9 lines
grub-core/fs/ntfs.c +0 -2
       if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
 	at->attr_end = at->attr_cur;
       if ((*at->attr_cur == attr) || (attr == 0))
@@ -439,14 +456,18 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
       /* From this point on pa_end is the end of the buffer */
       at->end = pa_end;
 
-      if (validate_attribute (at->attr_nxt, pa_end) == false)
-	return NULL;

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 467

Quoted 10 lines
grub-core/fs/ntfs.c +4 -1
+      if (at->attr_end >= pa_end || at->attr_nxt >= pa_end)
+        return NULL;
 
       while (at->attr_nxt)
 	{
 	  if ((*at->attr_nxt == attr) || (attr == 0))
 	    break;
-	  at->attr_nxt = next_attribute (at->attr_nxt, pa_end);
+
+	  grub_uint16_t nxt_offset = u16at (at->attr_nxt, 4);

Again, please move this to the beginning of this code block.

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 469

Quoted 2 lines
grub-core/fs/ntfs.c +2 -0
+	  at->attr_nxt += nxt_offset;
+	  if (nxt_offset == 0 || at->attr_nxt >= (pa_end - 4))

I think at least "(pa_end - 4)" begs for explanation in the comment.

Quoted 14 lines
grub-core/fs/ntfs.c +2 -1
+	    at->attr_nxt = NULL;
 	}
 
       if (at->attr_nxt >= at->attr_end || at->attr_nxt == NULL)
@@ -471,7 +492,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 						  + 1));
 	  pa = at->attr_nxt + u16at (pa, 4);
 
-	  if (validate_attribute (pa, pa_end) == true)
+	  if (pa >= pa_end)
 	    pa = NULL;
 
 	  while (pa)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 514

Quoted 5 lines
grub-core/fs/ntfs.c +1 -1
@@ -490,7 +511,9 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 		   u32at (pa, 0x10) * (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR),
 		   at->mft->data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0, 0))
 		return NULL;
-	      pa = next_attribute (pa, pa_end);
+	      pa += u16at(pa, 4);

When you introduce math are you sure there is no chance for overflow?

And a nit, missing space before "("...

Daniel

[1] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Comments

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#7 lore raw

Re: [PATCH v2 2/4] fs/ntfs: Correct attribute vs attribute list validation

Quoted 1 line

May I ask you to stick to the GRUB coding style? [1]

I have corrected the comment style in v3.

Quoted 2 lines
Quoted 2 lines

+ at->attr_nxt = next_attribute (at->attr_cur, at->attr_end,

false);

Quoted 4 lines

Review find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 4 lines
        if ((*at->attr_cur == attr) || (attr == 0))
          {
            grub_uint8_t *new_pos, *end;
(quoted diff) +0 -0
@@ -378,7 +381,9 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

Quoted 5 lines

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)

Quoted 5 lines
grub-core/fs/ntfs.c +1 -1
                  {
                    return new_pos;
                  }
-               new_pos = next_attribute (new_pos, end);
+                 /* Go to the next attribute in the list but do not

validate */

Quoted 3 lines

Review find_attr (struct grub_ntfs_attr *at, grub_uint8_t · line 385

Quoted 1 line
(quoted diff) +1 -0
+                 /* because this is the attribute list type. */

Ditto.

I have corrected the comment style in v3.

Quoted 6 lines

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 6 lines
grub-core/fs/ntfs.c +1 -0
+                 new_pos = next_attribute (new_pos, end, false);
              }
            grub_error (GRUB_ERR_BAD_FS,
                        "can\'t find 0x%X in attribute list",
@@ -392,7 +397,19 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

Quoted 1 line

Review find_attr (struct grub_ntfs_attr *at, grub_uint8_t · line 397

Quoted 1 line
(quoted diff) +0 -0
   mft_end = at->mft->buf + (at->mft->data->mft_size <<

GRUB_NTFS_BLK_SHR);

Quoted 1 line

Review find_attr (struct grub_ntfs_attr *at, grub_uint8_t · line 398

Quoted 1 line
(quoted diff) +0 -0
   while (at->attr_cur >= at->mft->buf && at->attr_cur < mft_end &&

*at->attr_cur != 0xFF)

Quoted 7 lines

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 402

Quoted 5 lines
grub-core/fs/ntfs.c +3 -1
     {
-      at->attr_nxt = next_attribute (at->attr_cur, at->end);
+      /* We can't use validate_attribute here because this logic
+       * seems to be used for both parsing through attributes
+       * and attribute lists. */

Ditto.

I have corrected the comment style in v3.

Quoted 6 lines

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 403

Quoted 2 lines
grub-core/fs/ntfs.c +1 -0
+      grub_uint16_t nsize = u16at (at->attr_cur, 4);

Please do not mix code with variable definitions. Define variables (and initialize if needed) at the beginning of a function or a given block. Separate definitions with code with one empty line.

I have made this change in v3.

Quoted 5 lines

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 405

Quoted 3 lines
grub-core/fs/ntfs.c +2 -0
+      if (at->attr_cur + nsize >= at->end ||
+          at->attr_cur + GRUB_NTFS_ATTRIBUTE_HEADER_SIZE >= at->end)

if (at->attr_cur + grub_max (GRUB_NTFS_ATTRIBUTE_HEADER_SIZE, nsize) >=

at->end))
I have made this change in v3.

Quoted 9 lines

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 411

Quoted 7 lines
grub-core/fs/ntfs.c +6 -0
+      {
+        at->attr_nxt = at->attr_cur;
+        break;
+      }
+      else
+        at->attr_nxt = at->attr_cur + nsize;

Is there any chance for overflow here? And in the "if" above?

I don't see a way for an overflow to realistically happen here since this is doing pointer math to calculate a new address and the value being added is 16
bits max. I assume we're at least on a 32-bit address system so we should be
okay assuming the memory isn't allocated less than 65536 bytes from the end (0xFFFFFFFF).

Quoted 5 lines

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at,

Quoted 5 lines
grub-core/fs/ntfs.c +0 -0
       if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
      at->attr_end = at->attr_cur;
       if ((*at->attr_cur == attr) || (attr == 0))
@@ -439,14 +456,18 @@ find_attr (struct grub_ntfs_attr *at,

grub_uint8_t attr)

Quoted 16 lines
Quoted 5 lines
grub-core/fs/ntfs.c +0 -2
       /* From this point on pa_end is the end of the buffer */
       at->end = pa_end;
 
-      if (validate_attribute (at->attr_nxt, pa_end) == false)
-     return NULL;

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 467

Quoted 10 lines
grub-core/fs/ntfs.c +4 -1
+      if (at->attr_end >= pa_end || at->attr_nxt >= pa_end)
+        return NULL;
 
       while (at->attr_nxt)
      {
        if ((*at->attr_nxt == attr) || (attr == 0))
          break;
-       at->attr_nxt = next_attribute (at->attr_nxt, pa_end);
+
+       grub_uint16_t nxt_offset = u16at (at->attr_nxt, 4);
Quoted 1 line

Again, please move this to the beginning of this code block.

I have made this change in v3.

Quoted 5 lines

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 469

Quoted 3 lines
grub-core/fs/ntfs.c +2 -0
+       at->attr_nxt += nxt_offset;
+       if (nxt_offset == 0 || at->attr_nxt >= (pa_end - 4))

I think at least "(pa_end - 4)" begs for explanation in the comment.

Added commentary for this in v3.

Quoted 6 lines

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 6 lines
grub-core/fs/ntfs.c +1 -0
+         at->attr_nxt = NULL;
      }
 
       if (at->attr_nxt >= at->attr_end || at->attr_nxt == NULL)
@@ -471,7 +492,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

Quoted 9 lines

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 9 lines
grub-core/fs/ntfs.c +1 -1
                                                + 1));
        pa = at->attr_nxt + u16at (pa, 4);
 
-       if (validate_attribute (pa, pa_end) == true)
+       if (pa >= pa_end)
          pa = NULL;
 
        while (pa)
@@ -490,7 +511,9 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

Quoted 1 line

Review find_attr (struct grub_ntfs_attr *at, grub_uint8_t · line 511

Quoted 1 line
(quoted diff) +0 -0
                 u32at (pa, 0x10) * (at->mft->data->mft_size <<

GRUB_NTFS_BLK_SHR),

Quoted 6 lines

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 514

Quoted 4 lines
grub-core/fs/ntfs.c +1 -1
                 at->mft->data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0, 0))
              return NULL;
-           pa = next_attribute (pa, pa_end);
+           pa += u16at(pa, 4);

When you introduce math are you sure there is no chance for overflow?

I think we should be safe from overflow here since a bit above this there is a check that includes "if ((pa >= pa_end) ||" that will trigger an error. So for a minimum 32-bit address system we should be safe since this is pointer arithmetic adding a 16-bit value.

Quoted 2 lines

And a nit, missing space before "("...

Corrected this in v3.

Thanks,
Andrew

Quoted 171 lines · Daniel Kiper

grub-core/fs/ntfs.c next_attribute (grub_uint8_t *curr_attribute, void

Quoted 36 lines · Andrew Hamilton

Correct ntfs_test test failures around attempting to validate attribute list entries as attributes. The NTFS code uses common logic in some places to parse both attributes and attribute_lists which complicates validation. Attribute lists contain different headers including a different size of the length field (2 bytes) at offset 4 instead of the 4 byte length field used in attributes at offset 4. There are other differences as well, but attempting to validate attribute list types using attribute header validation was causing failure of the NTFS test suite. This change restores some of the validation logic which may be shared between attributes and attribute lists to be closer to the original logic prior to fixes for previous CVEs. A following commit will address some of the implications of removing this validation logic by correcting some fuzzer failures (some which are exposed by removing the validation in some of the cases).

Fixes 067b6d225 (fs/ntfs: Implement attribute verification)
---
 grub-core/fs/ntfs.c | 45 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 11 deletions(-)
grub-core/fs/ntfs.c +1 -1
index 3eb70111b..0d087acd8 100644
@@ -219,7 +219,7 @@ validate_attribute (grub_uint8_t *attr, void *end)
 
 /* Return the next attribute if it exists, otherwise return NULL. */
 static grub_uint8_t *
-next_attribute (grub_uint8_t *curr_attribute, void *end)
+next_attribute (grub_uint8_t *curr_attribute, void *end, bool validate)
 {
   grub_uint8_t *next = curr_attribute;
 
@@ -231,7 +231,7 @@ next_attribute (grub_uint8_t *curr_attribute, void

*end)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 9 lines
grub-core/fs/ntfs.c +1 -1
     return NULL;
 
   next += u16at (curr_attribute, 4);
-  if (validate_attribute (next, end) == false)
+  if (validate && validate_attribute (next, end) == false)
     return NULL;
 
   return next;
@@ -316,13 +316,16 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 327

Quoted 13 lines
grub-core/fs/ntfs.c +3 -1
 {
   grub_uint8_t *mft_end;
 
+  /* GRUB_NTFS_AF_ALST indicates the attribute list type */
   if (at->flags & GRUB_NTFS_AF_ALST)
     {
     retry:
       while (at->attr_nxt)
      {
        at->attr_cur = at->attr_nxt;
-       at->attr_nxt = next_attribute (at->attr_cur, at->attr_end);
+       /* Go to the next attribute in the list but do not validate */
+       /* because this is the attribute list type. */

May I ask you to stick to the GRUB coding style? [1]

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t · line 328

Quoted 1 line
grub-core/fs/ntfs.c +1 -0
+       at->attr_nxt = next_attribute (at->attr_cur, at->attr_end,

false);

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 4 lines
grub-core/fs/ntfs.c +0 -0
        if ((*at->attr_cur == attr) || (attr == 0))
          {
            grub_uint8_t *new_pos, *end;
@@ -378,7 +381,9 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)

Quoted 5 lines
grub-core/fs/ntfs.c +1 -1
                  {
                    return new_pos;
                  }
-               new_pos = next_attribute (new_pos, end);
+                 /* Go to the next attribute in the list but do not

validate */

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t · line 385

Quoted 1 line
grub-core/fs/ntfs.c +1 -0
+                 /* because this is the attribute list type. */

Ditto.

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 5 lines
grub-core/fs/ntfs.c +1 -0
+                 new_pos = next_attribute (new_pos, end, false);
              }
            grub_error (GRUB_ERR_BAD_FS,
                        "can\'t find 0x%X in attribute list",
@@ -392,7 +397,19 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t · line 397

Quoted 1 line
grub-core/fs/ntfs.c +0 -0
   mft_end = at->mft->buf + (at->mft->data->mft_size <<

GRUB_NTFS_BLK_SHR);

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t · line 398

Quoted 1 line
grub-core/fs/ntfs.c +0 -0
   while (at->attr_cur >= at->mft->buf && at->attr_cur < mft_end &&

*at->attr_cur != 0xFF)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 402

Quoted 5 lines
grub-core/fs/ntfs.c +3 -1
     {
-      at->attr_nxt = next_attribute (at->attr_cur, at->end);
+      /* We can't use validate_attribute here because this logic
+       * seems to be used for both parsing through attributes
+       * and attribute lists. */

Ditto.

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 403

Quoted 1 line
grub-core/fs/ntfs.c +1 -0
+      grub_uint16_t nsize = u16at (at->attr_cur, 4);

Please do not mix code with variable definitions. Define variables (and initialize if needed) at the beginning of a function or a given block. Separate definitions with code with one empty line.

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 405

Quoted 2 lines
grub-core/fs/ntfs.c +2 -0
+      if (at->attr_cur + nsize >= at->end ||
+          at->attr_cur + GRUB_NTFS_ATTRIBUTE_HEADER_SIZE >= at->end)

if (at->attr_cur + grub_max (GRUB_NTFS_ATTRIBUTE_HEADER_SIZE, nsize) >= at->end))

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 411

Quoted 6 lines
grub-core/fs/ntfs.c +6 -0
+      {
+        at->attr_nxt = at->attr_cur;
+        break;
+      }
+      else
+        at->attr_nxt = at->attr_cur + nsize;

Is there any chance for overflow here? And in the "if" above?

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 4 lines
grub-core/fs/ntfs.c +0 -0
       if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
      at->attr_end = at->attr_cur;
       if ((*at->attr_cur == attr) || (attr == 0))
@@ -439,14 +456,18 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

Quoted 5 lines
grub-core/fs/ntfs.c +0 -2
       /* From this point on pa_end is the end of the buffer */
       at->end = pa_end;
 
-      if (validate_attribute (at->attr_nxt, pa_end) == false)
-     return NULL;

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 467

Quoted 10 lines
grub-core/fs/ntfs.c +4 -1
+      if (at->attr_end >= pa_end || at->attr_nxt >= pa_end)
+        return NULL;
 
       while (at->attr_nxt)
      {
        if ((*at->attr_nxt == attr) || (attr == 0))
          break;
-       at->attr_nxt = next_attribute (at->attr_nxt, pa_end);
+
+       grub_uint16_t nxt_offset = u16at (at->attr_nxt, 4);

Again, please move this to the beginning of this code block.

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 469

Quoted 2 lines
grub-core/fs/ntfs.c +2 -0
+       at->attr_nxt += nxt_offset;
+       if (nxt_offset == 0 || at->attr_nxt >= (pa_end - 4))

I think at least "(pa_end - 4)" begs for explanation in the comment.

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 5 lines
grub-core/fs/ntfs.c +1 -0
+         at->attr_nxt = NULL;
      }
 
       if (at->attr_nxt >= at->attr_end || at->attr_nxt == NULL)
@@ -471,7 +492,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 9 lines
grub-core/fs/ntfs.c +1 -1
                                                + 1));
        pa = at->attr_nxt + u16at (pa, 4);
 
-       if (validate_attribute (pa, pa_end) == true)
+       if (pa >= pa_end)
          pa = NULL;
 
        while (pa)
@@ -490,7 +511,9 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t · line 511

Quoted 1 line
grub-core/fs/ntfs.c +0 -0
                 u32at (pa, 0x10) * (at->mft->data->mft_size <<

GRUB_NTFS_BLK_SHR),

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) · line 514

Quoted 4 lines
grub-core/fs/ntfs.c +1 -1
                 at->mft->data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0, 0))
              return NULL;
-           pa = next_attribute (pa, pa_end);
+           pa += u16at(pa, 4);
Quoted 9 lines

When you introduce math are you sure there is no chance for overflow?

And a nit, missing space before "("...

Daniel

[1]
https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Comments

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#8 lore raw

PATCH v2 3/4 fs/fshelp: Avoid possible NULL pointer deference

In reply to Andrew Hamilton

Avoid attempting to defererence a NULL pointer to call read_symlink when the given filesystem does not provide a read_symlink function. This could be triggered if the calling filesystem had a file marked as a symlink. This appears possible for HFS and was observed during fuzzing of NTFS.

---
 grub-core/fs/fshelp.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
grub-core/fs/fshelp.c +4 -1
index cb41934b4..15278fb80 100644
@@ -226,7 +226,10 @@ find_file (char *currpath,
 	    return grub_error (GRUB_ERR_SYMLINK_LOOP,
 			       N_("too deep nesting of symlinks"));
 
-	  symlink = read_symlink (ctx->currnode->node);
+	  if (read_symlink != NULL)
+	    symlink = read_symlink (ctx->currnode->node);
+	  else
+	    return grub_error (GRUB_ERR_BAD_FS, "read_symlink is NULL");
 
 	  if (!symlink)
 	    return grub_errno;
Signature · 7 lines
2.39.5


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by Daniel Kiper on v2 3/4
#9 lore raw

Re: [PATCH v2 3/4] fs/fshelp: Avoid possible NULL pointer deference

Quoted 7 lines · Andrew Hamilton

Avoid attempting to defererence a NULL pointer to call read_symlink when the given filesystem does not provide a read_symlink function. This could be triggered if the calling filesystem had a file marked as a symlink. This appears possible for HFS and was observed during fuzzing of NTFS.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#10 lore raw

PATCH v2 4/4 fs/ntfs: Correct possible access violations and hangs

In reply to Andrew Hamilton

Correct several memory access violations and hangs found during fuzzing. The issues fixed here could occur if certain specific malformed NTFS file systems were presented to GRUB. Currently, GRUB does not allow NTFS file system access when lockdown mode is enforced, so these should be of minimal impact.

The changes made in this commit generally correct issues where pointers into data buffers were being calculated using lengths read from the NTFS file system without sufficient bounds / sanity checking; attempting to iterate through a buffer using a length read from the NTFS file system without confirming the length is larger than 0 (possible hang / infinite loop); or attempting to access elements of a structure to free them, when the structure pointer is null.

---
 grub-core/fs/ntfs.c | 95 ++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 80 insertions(+), 15 deletions(-)
grub-core/fs/ntfs.c +80 -15
index 0d087acd8..45fff2400 100644
@@ -246,6 +246,7 @@ fixup (grub_uint8_t *buf, grub_size_t len, const grub_uint8_t *magic)
   grub_uint16_t ss;
   grub_uint8_t *pu;
   grub_uint16_t us;
+  grub_uint16_t pu_offset;
 
   COMPILE_TIME_ASSERT ((1 << GRUB_NTFS_BLK_SHR) == GRUB_DISK_SECTOR_SIZE);
 
@@ -255,7 +256,10 @@ fixup (grub_uint8_t *buf, grub_size_t len, const grub_uint8_t *magic)
   ss = u16at (buf, 6) - 1;
   if (ss != len)
     return grub_error (GRUB_ERR_BAD_FS, "size not match");
-  pu = buf + u16at (buf, 4);
+  pu_offset = u16at (buf, 4);
+  if (pu_offset >= (len * GRUB_DISK_SECTOR_SIZE - (2 * ss)))
+    return grub_error (GRUB_ERR_BAD_FS, "pu offset size incorrect");
+  pu = buf + pu_offset;
   us = u16at (pu, 0);
   buf -= 2;
   while (ss > 0)
@@ -373,6 +377,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 
 	      new_pos = &at->emft_buf[first_attr_off (at->emft_buf)];
 	      end = &at->emft_buf[emft_buf_size];
+	      at->end = end;
 
 	      while (new_pos && *new_pos != 0xFF)
 		{
@@ -395,7 +400,8 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
     }
   at->attr_cur = at->attr_nxt;
   mft_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
-  while (at->attr_cur >= at->mft->buf && at->attr_cur < mft_end && *at->attr_cur != 0xFF)
+  while (at->attr_cur >= at->mft->buf && at->attr_cur < (mft_end - 4)
+         && *at->attr_cur != 0xFF)
     {
       /* We can't use validate_attribute here because this logic
        * seems to be used for both parsing through attributes
@@ -412,7 +418,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 
       if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
 	at->attr_end = at->attr_cur;
-      if ((*at->attr_cur == attr) || (attr == 0))
+      if ((*at->attr_cur == attr) || (attr == 0) || (nsize == 0))
 	return at->attr_cur;
       at->attr_cur = at->attr_nxt;
     }
@@ -442,13 +448,23 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 	      return NULL;
 	    }
 	  at->attr_nxt = at->edat_buf;
-	  at->attr_end = at->edat_buf + u32at (pa, 0x30);
+	  grub_uint32_t edat_offset = u32at (pa, 0x30);
+	  if (edat_offset >= n) {
+	    grub_error (GRUB_ERR_BAD_FS, "edat offset is out of bounds");
+	    return NULL;
+	  }
+	  at->attr_end = at->edat_buf + edat_offset;
 	  pa_end = at->edat_buf + n;
 	}
       else
 	{
 	  at->attr_nxt = at->attr_end + res_attr_data_off (pa);
-	  at->attr_end = at->attr_end + u32at (pa, 4);
+	  grub_uint32_t edat_offset = u32at (pa, 4);
+	  if ((at->attr_end + edat_offset) >= (at->end)) {
+	    grub_error (GRUB_ERR_BAD_FS, "edat offset is out of bounds");
+	    return NULL;
+	  }
+	  at->attr_end = at->attr_end + edat_offset;
 	  pa_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
 	}
       at->flags |= GRUB_NTFS_AF_ALST;
@@ -470,7 +486,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 	    at->attr_nxt = NULL;
 	}
 
-      if (at->attr_nxt >= at->attr_end || at->attr_nxt == NULL)
+      if ((at->attr_nxt + GRUB_NTFS_ATTRIBUTE_HEADER_SIZE) >= at->attr_end || at->attr_nxt == NULL)
 	return NULL;
 
       if ((at->flags & GRUB_NTFS_AF_MMFT) && (attr == GRUB_NTFS_AT_DATA))
@@ -537,13 +553,16 @@ locate_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft,
     return NULL;
   if ((at->flags & GRUB_NTFS_AF_ALST) == 0)
     {
+      /* Used to make sure we're not stuck in a loop. */
+      grub_uint8_t *last_pa = NULL;
       while (1)
 	{
 	  pa = find_attr (at, attr);
-	  if (pa == NULL)
+	  if (pa == NULL || pa == last_pa)
 	    break;
 	  if (at->flags & GRUB_NTFS_AF_ALST)
 	    return pa;
+	  last_pa = pa;
 	}
       grub_errno = GRUB_ERR_NONE;
       free_attr (at);
@@ -639,6 +658,7 @@ read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa, grub_uint8_t *dest,
 	   grub_disk_read_hook_t read_hook, void *read_hook_data)
 {
   struct grub_ntfs_rlst cc, *ctx;
+  grub_uint8_t *end_ptr = (pa + len);
 
   if (len == 0)
     return 0;
@@ -671,7 +691,13 @@ read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa, grub_uint8_t *dest,
       return 0;
     }
 
-  ctx->cur_run = pa + u16at (pa, 0x20);
+  grub_uint16_t run_offset = u16at (pa, 0x20);
+  if ((run_offset + pa) >= end_ptr || ((run_offset + pa) >= (at->end)))
+    {
+      return grub_error (GRUB_ERR_BAD_FS, "run offset out of range");
+    }
+
+  ctx->cur_run = pa + run_offset;
 
   ctx->next_vcn = u32at (pa, 0x10);
   ctx->curr_lcn = 0;
@@ -734,6 +760,8 @@ read_attr (struct grub_ntfs_attr *at, grub_uint8_t *dest, grub_disk_addr_t ofs,
   grub_uint8_t *pp;
   grub_err_t ret;
 
+  if (at == NULL || at->attr_cur == NULL)
+    return grub_error (GRUB_ERR_BAD_FS, "attribute not found");
   save_cur = at->attr_cur;
   at->attr_nxt = at->attr_cur;
   attr = *at->attr_nxt;
@@ -830,8 +858,11 @@ init_file (struct grub_ntfs_file *mft, grub_uint64_t mftno)
 static void
 free_file (struct grub_ntfs_file *mft)
 {
-  free_attr (&mft->attr);
-  grub_free (mft->buf);
+  if (mft)
+  {
+    free_attr (&mft->attr);
+    grub_free (mft->buf);
+  }
 }
 
 static char *
@@ -924,7 +955,12 @@ list_file (struct grub_ntfs_file *diro, grub_uint8_t *pos, grub_uint8_t *end_pos
 
 	  grub_free (ustr);
 	}
-      pos += u16at (pos, 8);
+	grub_uint16_t pos_incr = u16at (pos, 8);
+	if (pos_incr > 0)
+	  pos += pos_incr;
+	else
+	  return 0;
+
     }
   return 0;
 }
@@ -1035,6 +1071,8 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
   int ret = 0;
   grub_size_t bitmap_len;
   struct grub_ntfs_file *mft;
+  /* Used to make sure we're not stuck in a loop. */
+  grub_uint8_t *last_pos = NULL;
 
   mft = (struct grub_ntfs_file *) dir;
 
@@ -1054,18 +1092,22 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
   while (1)
     {
       cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ROOT);
-      if (cur_pos == NULL)
+      if (cur_pos == NULL || cur_pos == last_pos)
 	{
 	  grub_error (GRUB_ERR_BAD_FS, "no $INDEX_ROOT");
 	  goto done;
 	}
 
+      last_pos = cur_pos;
+
       /* Resident, Namelen=4, Offset=0x18, Flags=0x00, Name="$I30" */
       if ((u32at (cur_pos, 8) != 0x180400) ||
 	  (u32at (cur_pos, 0x18) != 0x490024) ||
 	  (u32at (cur_pos, 0x1C) != 0x300033))
 	continue;
       cur_pos += res_attr_data_off (cur_pos);
+      if(cur_pos >= at->end)
+        continue;
       if (*cur_pos != 0x30)	/* Not filename index */
 	continue;
       break;
@@ -1084,10 +1126,18 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
   /* No need to check errors here, as it will already be fine */
   init_attr (at, mft);
 
+  last_pos = NULL;
   while ((cur_pos = find_attr (at, GRUB_NTFS_AT_BITMAP)) != NULL)
     {
       int ofs;
 
+      if (cur_pos == last_pos)
+      {
+        grub_error (GRUB_ERR_BAD_FS, "bitmap attribute loop");
+        goto done;
+      }
+      last_pos = cur_pos;
+
       ofs = cur_pos[0xA];
       /* Namelen=4, Name="$I30" */
       if ((cur_pos[9] == 4) &&
@@ -1135,7 +1185,17 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
                               "fails to read non-resident $BITMAP");
                   goto done;
                 }
-              bitmap_len = u32at (cur_pos, 0x30);
+                grub_uint32_t tmp_len = u32at (cur_pos, 0x30);
+                if (tmp_len <= bitmap_len)
+                {
+                  bitmap_len = tmp_len;
+                }
+                else
+                {
+                  grub_error (GRUB_ERR_BAD_FS,
+                    "bitmap len too large for non-resident $BITMAP");
+                  goto done;
+                }
             }
 
           bitmap = bmp;
@@ -1144,6 +1204,7 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
     }
 
   free_attr (at);
+  last_pos = NULL;
   cur_pos = locate_attr (at, mft, GRUB_NTFS_AT_INDEX_ALLOCATION);
   while (cur_pos != NULL)
     {
@@ -1153,6 +1214,9 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
 	  (u32at (cur_pos, 0x44) == 0x300033))
 	break;
       cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ALLOCATION);
+      if (cur_pos == last_pos)
+        break;
+      last_pos = cur_pos;
     }
 
   if ((!cur_pos) && (bitmap))
@@ -1480,7 +1544,7 @@ grub_ntfs_label (grub_device_t device, char **label)
 
   pa = find_attr (&mft->attr, GRUB_NTFS_AT_VOLUME_NAME);
 
-  if (pa >= mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR))
+  if (pa == NULL || pa >= mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR))
     {
       grub_error (GRUB_ERR_BAD_FS, "can\'t parse volume label");
       goto fail;
@@ -1498,7 +1562,8 @@ grub_ntfs_label (grub_device_t device, char **label)
 
       len = res_attr_data_len (pa) / 2;
       pa += res_attr_data_off (pa);
-      if (mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR) - pa >= 2 * len)
+      if (mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR) - pa >= 2 * len &&
+          pa >= mft->buf && (pa + len < (mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR))))
         *label = get_utf8 (pa, len);
       else
         grub_error (GRUB_ERR_BAD_FS, "can\'t parse volume label");
Signature · 7 lines
2.39.5


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#11 lore raw

Re: [PATCH v2 4/4] fs/ntfs: Correct possible access violations and hangs

I think you should change the order of this patch and patch #3.

Quoted 6 lines · Andrew Hamilton

Correct several memory access violations and hangs found during fuzzing. The issues fixed here could occur if certain specific malformed NTFS file systems were presented to GRUB. Currently, GRUB does not allow NTFS file system access when lockdown mode is enforced, so these should be of minimal impact.

Quoted 7 lines

The changes made in this commit generally correct issues where pointers into data buffers were being calculated using lengths read from the NTFS file system without sufficient bounds / sanity checking; attempting to iterate through a buffer using a length read from the NTFS file system without confirming the length is larger than 0 (possible hang / infinite loop); or attempting to access elements of a structure to free them, when the structure pointer is null.

s/null/NULL/

Is it possible to group the changes logically and put them in separate patches? The leftovers can be put into one last patch.

Quoted 92 lines
---
 grub-core/fs/ntfs.c | 95 ++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 80 insertions(+), 15 deletions(-)
grub-core/fs/ntfs.c +22 -6
index 0d087acd8..45fff2400 100644
@@ -246,6 +246,7 @@ fixup (grub_uint8_t *buf, grub_size_t len, const grub_uint8_t *magic)
   grub_uint16_t ss;
   grub_uint8_t *pu;
   grub_uint16_t us;
+  grub_uint16_t pu_offset;
 
   COMPILE_TIME_ASSERT ((1 << GRUB_NTFS_BLK_SHR) == GRUB_DISK_SECTOR_SIZE);
 
@@ -255,7 +256,10 @@ fixup (grub_uint8_t *buf, grub_size_t len, const grub_uint8_t *magic)
   ss = u16at (buf, 6) - 1;
   if (ss != len)
     return grub_error (GRUB_ERR_BAD_FS, "size not match");
-  pu = buf + u16at (buf, 4);
+  pu_offset = u16at (buf, 4);
+  if (pu_offset >= (len * GRUB_DISK_SECTOR_SIZE - (2 * ss)))
+    return grub_error (GRUB_ERR_BAD_FS, "pu offset size incorrect");
+  pu = buf + pu_offset;
   us = u16at (pu, 0);
   buf -= 2;
   while (ss > 0)
@@ -373,6 +377,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 
 	      new_pos = &at->emft_buf[first_attr_off (at->emft_buf)];
 	      end = &at->emft_buf[emft_buf_size];
+	      at->end = end;
 
 	      while (new_pos && *new_pos != 0xFF)
 		{
@@ -395,7 +400,8 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
     }
   at->attr_cur = at->attr_nxt;
   mft_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
-  while (at->attr_cur >= at->mft->buf && at->attr_cur < mft_end && *at->attr_cur != 0xFF)
+  while (at->attr_cur >= at->mft->buf && at->attr_cur < (mft_end - 4)
+         && *at->attr_cur != 0xFF)
     {
       /* We can't use validate_attribute here because this logic
        * seems to be used for both parsing through attributes
@@ -412,7 +418,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 
       if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
 	at->attr_end = at->attr_cur;
-      if ((*at->attr_cur == attr) || (attr == 0))
+      if ((*at->attr_cur == attr) || (attr == 0) || (nsize == 0))
 	return at->attr_cur;
       at->attr_cur = at->attr_nxt;
     }
@@ -442,13 +448,23 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 	      return NULL;
 	    }
 	  at->attr_nxt = at->edat_buf;
-	  at->attr_end = at->edat_buf + u32at (pa, 0x30);
+	  grub_uint32_t edat_offset = u32at (pa, 0x30);
+	  if (edat_offset >= n) {
+	    grub_error (GRUB_ERR_BAD_FS, "edat offset is out of bounds");
+	    return NULL;
+	  }
+	  at->attr_end = at->edat_buf + edat_offset;
 	  pa_end = at->edat_buf + n;
 	}
       else
 	{
 	  at->attr_nxt = at->attr_end + res_attr_data_off (pa);
-	  at->attr_end = at->attr_end + u32at (pa, 4);
+	  grub_uint32_t edat_offset = u32at (pa, 4);
+	  if ((at->attr_end + edat_offset) >= (at->end)) {
+	    grub_error (GRUB_ERR_BAD_FS, "edat offset is out of bounds");
+	    return NULL;
+	  }
+	  at->attr_end = at->attr_end + edat_offset;
 	  pa_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
 	}
       at->flags |= GRUB_NTFS_AF_ALST;
@@ -470,7 +486,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 	    at->attr_nxt = NULL;
 	}
 
-      if (at->attr_nxt >= at->attr_end || at->attr_nxt == NULL)
+      if ((at->attr_nxt + GRUB_NTFS_ATTRIBUTE_HEADER_SIZE) >= at->attr_end || at->attr_nxt == NULL)
 	return NULL;
 
       if ((at->flags & GRUB_NTFS_AF_MMFT) && (attr == GRUB_NTFS_AT_DATA))

grub-core/fs/ntfs.c locate_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft, · line 557

Quoted 5 lines
grub-core/fs/ntfs.c +2 -0
@@ -537,13 +553,16 @@ locate_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft,
     return NULL;
   if ((at->flags & GRUB_NTFS_AF_ALST) == 0)
     {
+      /* Used to make sure we're not stuck in a loop. */
+      grub_uint8_t *last_pa = NULL;

Missing empty line...

Quoted 21 lines
grub-core/fs/ntfs.c +3 -1
       while (1)
 	{
 	  pa = find_attr (at, attr);
-	  if (pa == NULL)
+	  if (pa == NULL || pa == last_pa)
 	    break;
 	  if (at->flags & GRUB_NTFS_AF_ALST)
 	    return pa;
+	  last_pa = pa;
 	}
       grub_errno = GRUB_ERR_NONE;
       free_attr (at);
@@ -639,6 +658,7 @@ read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa, grub_uint8_t *dest,
 	   grub_disk_read_hook_t read_hook, void *read_hook_data)
 {
   struct grub_ntfs_rlst cc, *ctx;
+  grub_uint8_t *end_ptr = (pa + len);
 
   if (len == 0)
     return 0;

grub-core/fs/ntfs.c read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa, grub_uint8_t *dest, · line 694

Quoted 5 lines
grub-core/fs/ntfs.c +1 -1
@@ -671,7 +691,13 @@ read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa, grub_uint8_t *dest,
       return 0;
     }
 
-  ctx->cur_run = pa + u16at (pa, 0x20);
+  grub_uint16_t run_offset = u16at (pa, 0x20);

Please move variable definition to proper place...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#12 lore raw

Re: [PATCH v2 4/4] fs/ntfs: Correct possible access violations and hangs

I've sent out a v3 just now with fixes for these.

Thanks,
Andrew

Quoted 162 lines · Daniel Kiper

I think you should change the order of this patch and patch #3.

Quoted 6 lines · Andrew Hamilton

Correct several memory access violations and hangs found during fuzzing. The issues fixed here could occur if certain specific malformed NTFS file systems were presented to GRUB. Currently, GRUB does not allow NTFS file system access when lockdown mode is enforced, so these should be of minimal impact.

Quoted 7 lines

The changes made in this commit generally correct issues where pointers into data buffers were being calculated using lengths read from the NTFS file system without sufficient bounds / sanity checking; attempting to iterate through a buffer using a length read from the NTFS file system without confirming the length is larger than 0 (possible hang / infinite loop); or attempting to access elements of a structure to free them, when the structure pointer is null.

s/null/NULL/

Is it possible to group the changes logically and put them in separate patches? The leftovers can be put into one last patch.

grub-core/fs/ntfs.c fixup (grub_uint8_t *buf, grub_size_t len, const

Quoted 10 lines
---
 grub-core/fs/ntfs.c | 95 ++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 80 insertions(+), 15 deletions(-)
grub-core/fs/ntfs.c +0 -0
index 0d087acd8..45fff2400 100644
@@ -246,6 +246,7 @@ fixup (grub_uint8_t *buf, grub_size_t len, const

grub_uint8_t *magic)

grub-core/fs/ntfs.c fixup (grub_uint8_t *buf, grub_size_t len, const grub_uint8_t *magic)

Quoted 6 lines
grub-core/fs/ntfs.c +1 -0
   grub_uint16_t ss;
   grub_uint8_t *pu;
   grub_uint16_t us;
+  grub_uint16_t pu_offset;
 
   COMPILE_TIME_ASSERT ((1 << GRUB_NTFS_BLK_SHR) ==

GRUB_DISK_SECTOR_SIZE);

grub-core/fs/ntfs.c fixup (grub_uint8_t *buf, grub_size_t len, const

Quoted 2 lines
grub-core/fs/ntfs.c +0 -0
@@ -255,7 +256,10 @@ fixup (grub_uint8_t *buf, grub_size_t len, const

grub_uint8_t *magic)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 12 lines
grub-core/fs/ntfs.c +4 -1
   ss = u16at (buf, 6) - 1;
   if (ss != len)
     return grub_error (GRUB_ERR_BAD_FS, "size not match");
-  pu = buf + u16at (buf, 4);
+  pu_offset = u16at (buf, 4);
+  if (pu_offset >= (len * GRUB_DISK_SECTOR_SIZE - (2 * ss)))
+    return grub_error (GRUB_ERR_BAD_FS, "pu offset size incorrect");
+  pu = buf + pu_offset;
   us = u16at (pu, 0);
   buf -= 2;
   while (ss > 0)
@@ -373,6 +377,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 8 lines
grub-core/fs/ntfs.c +1 -0
            new_pos = &at->emft_buf[first_attr_off (at->emft_buf)];
            end = &at->emft_buf[emft_buf_size];
+           at->end = end;
 
            while (new_pos && *new_pos != 0xFF)
              {
@@ -395,7 +400,8 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)

Quoted 3 lines
grub-core/fs/ntfs.c +0 -0
     }
   at->attr_cur = at->attr_nxt;
   mft_end = at->mft->buf + (at->mft->data->mft_size <<

GRUB_NTFS_BLK_SHR);

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t · line 398

Quoted 1 line
grub-core/fs/ntfs.c +0 -1
-  while (at->attr_cur >= at->mft->buf && at->attr_cur < mft_end &&

*at->attr_cur != 0xFF)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 6 lines
grub-core/fs/ntfs.c +2 -0
+  while (at->attr_cur >= at->mft->buf && at->attr_cur < (mft_end - 4)
+         && *at->attr_cur != 0xFF)
     {
       /* We can't use validate_attribute here because this logic
        * seems to be used for both parsing through attributes
@@ -412,7 +418,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 9 lines
grub-core/fs/ntfs.c +1 -1
       if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
      at->attr_end = at->attr_cur;
-      if ((*at->attr_cur == attr) || (attr == 0))
+      if ((*at->attr_cur == attr) || (attr == 0) || (nsize == 0))
      return at->attr_cur;
       at->attr_cur = at->attr_nxt;
     }
@@ -442,13 +448,23 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

Quoted 13 lines
grub-core/fs/ntfs.c +6 -1
            return NULL;
          }
        at->attr_nxt = at->edat_buf;
-       at->attr_end = at->edat_buf + u32at (pa, 0x30);
+       grub_uint32_t edat_offset = u32at (pa, 0x30);
+       if (edat_offset >= n) {
+         grub_error (GRUB_ERR_BAD_FS, "edat offset is out of bounds");
+         return NULL;
+       }
+       at->attr_end = at->edat_buf + edat_offset;
        pa_end = at->edat_buf + n;
      }
       else

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)

Quoted 10 lines
grub-core/fs/ntfs.c +6 -1
      {
        at->attr_nxt = at->attr_end + res_attr_data_off (pa);
-       at->attr_end = at->attr_end + u32at (pa, 4);
+       grub_uint32_t edat_offset = u32at (pa, 4);
+       if ((at->attr_end + edat_offset) >= (at->end)) {
+         grub_error (GRUB_ERR_BAD_FS, "edat offset is out of bounds");
+         return NULL;
+       }
+       at->attr_end = at->attr_end + edat_offset;
        pa_end = at->mft->buf + (at->mft->data->mft_size <<

GRUB_NTFS_BLK_SHR);

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 3 lines
grub-core/fs/ntfs.c +0 -0
      }
       at->flags |= GRUB_NTFS_AF_ALST;
@@ -470,7 +486,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t

attr)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)

Quoted 5 lines
grub-core/fs/ntfs.c +1 -1
          at->attr_nxt = NULL;
      }
 
-      if (at->attr_nxt >= at->attr_end || at->attr_nxt == NULL)
+      if ((at->attr_nxt + GRUB_NTFS_ATTRIBUTE_HEADER_SIZE) >=

at->attr_end || at->attr_nxt == NULL)

grub-core/fs/ntfs.c find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)

Quoted 3 lines
grub-core/fs/ntfs.c +0 -0
      return NULL;
 
       if ((at->flags & GRUB_NTFS_AF_MMFT) && (attr ==

GRUB_NTFS_AT_DATA))

grub-core/fs/ntfs.c locate_attr (struct grub_ntfs_attr *at, struct

Quoted 1 line
grub-core/fs/ntfs.c +0 -0
@@ -537,13 +553,16 @@ locate_attr (struct grub_ntfs_attr *at, struct

grub_ntfs_file *mft,

grub-core/fs/ntfs.c locate_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft, · line 557

Quoted 5 lines
grub-core/fs/ntfs.c +2 -0
     return NULL;
   if ((at->flags & GRUB_NTFS_AF_ALST) == 0)
     {
+      /* Used to make sure we're not stuck in a loop. */
+      grub_uint8_t *last_pa = NULL;

Missing empty line...

grub-core/fs/ntfs.c read_data (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 13 lines
grub-core/fs/ntfs.c +2 -1
       while (1)
      {
        pa = find_attr (at, attr);
-       if (pa == NULL)
+       if (pa == NULL || pa == last_pa)
          break;
        if (at->flags & GRUB_NTFS_AF_ALST)
          return pa;
+       last_pa = pa;
      }
       grub_errno = GRUB_ERR_NONE;
       free_attr (at);
@@ -639,6 +658,7 @@ read_data (struct grub_ntfs_attr *at, grub_uint8_t

*pa, grub_uint8_t *dest,

grub-core/fs/ntfs.c read_data (struct grub_ntfs_attr *at, grub_uint8_t

Quoted 8 lines
grub-core/fs/ntfs.c +1 -0
         grub_disk_read_hook_t read_hook, void *read_hook_data)
 {
   struct grub_ntfs_rlst cc, *ctx;
+  grub_uint8_t *end_ptr = (pa + len);
 
   if (len == 0)
     return 0;
@@ -671,7 +691,13 @@ read_data (struct grub_ntfs_attr *at, grub_uint8_t

*pa, grub_uint8_t *dest,

Quoted 3 lines
grub-core/fs/ntfs.c +0 -0
       return 0;
     }
 
Quoted 7 lines

grub-core/fs/ntfs.c read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa, grub_uint8_t *dest, · line 694

Quoted 2 lines
grub-core/fs/ntfs.c +1 -1
-  ctx->cur_run = pa + u16at (pa, 0x20);
+  grub_uint16_t run_offset = u16at (pa, 0x20);

Please move variable definition to proper place...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel