Skip to contentSkip to conversation

fs/ntfs: Check at->attr_cur after calling next_attribute()

PATCH In review B Horn 6 participants 9 messages Feb 28, 2025 – Mar 20, 2025

lore
Share & tools
lore

B Horn

author
#0 lore raw

PATCH 1/1 fs/ntfs: Check at->attr_cur after calling next_attribute()

On some NTFS volumes GRUB would enter an infinite loop when next_attribute() returned NULL, which can happen in normal cases when the end of the attribute list is reached.
This would trigger a NULL deref, but as the null page is mapped on the majority of firmware, an infinite loop would occur as the while loop didn't make any progress.

Fixing this by verifying the value of at->attr_cur on the next iteration of the loop, after it has been set to the result of next_attribute(). Also removing the redundant check against mft_end as the next_attribute() should handle that now.
A pointer to the end of the buffer is stored in at->end, which is initialized the same way as mft_end was.

---
 grub-core/fs/ntfs.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
grub-core/fs/ntfs.c +1 -4
index 960833a34..77531e627 100644
@@ -311,8 +311,6 @@ free_attr (struct grub_ntfs_attr *at)
 static grub_uint8_t *
 find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 {
-  grub_uint8_t *mft_end;
-
   if (at->flags & GRUB_NTFS_AF_ALST)
     {
     retry:
@@ -386,8 +384,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
       return NULL;
     }
   at->attr_cur = at->attr_nxt;
-  mft_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
-  while (at->attr_cur < mft_end && *at->attr_cur != 0xFF)
+  while (at->attr_cur && *at->attr_cur != 0xFF)
     {
       at->attr_nxt = next_attribute (at->attr_cur, at->end);
       if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
Signature · 7 lines
2.48.1


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

On some NTFS volumes GRUB would enter an infinite loop when next_attribute() returned NULL, which can happen in normal cases when the end of the attribute list is reached.
This would trigger a NULL deref, but as the null page is mapped on the majority of firmware, an infinite loop would occur as the while loop didn't make any progress.

Fixing this by verifying the value of at->attr_cur on the next iteration of the loop, after it has been set to the result of next_attribute(). Also removing the redundant check against mft_end as the next_attribute() should handle that now.
A pointer to the end of the buffer is stored in at->end, which is initialized the same way as mft_end was.

Quoted 4 lines

Thank you, it works fine here.

Regards
Andreas

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

#2 lore raw
In reply to B Horn

Why do you remove boundary check? Maybe it's better to add the second boundary check from below

Quoted 46 lines · B Horn

On some NTFS volumes GRUB would enter an infinite loop when next_attribute() returned NULL, which can happen in normal cases when the end of the attribute list is reached.
This would trigger a NULL deref, but as the null page is mapped on the majority of firmware, an infinite loop would occur as the while loop didn't make any progress.

Fixing this by verifying the value of at->attr_cur on the next iteration of the loop, after it has been set to the result of next_attribute(). Also removing the redundant check against mft_end as the next_attribute() should handle that now.
A pointer to the end of the buffer is stored in at->end, which is initialized the same way as mft_end was.

---
 grub-core/fs/ntfs.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
grub-core/fs/ntfs.c +1 -4
index 960833a34..77531e627 100644
@@ -311,8 +311,6 @@ free_attr (struct grub_ntfs_attr *at)
 static grub_uint8_t *
 find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
 {
-  grub_uint8_t *mft_end;
-
   if (at->flags & GRUB_NTFS_AF_ALST)
     {
     retry:
@@ -386,8 +384,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t
 attr)
       return NULL;
     }
   at->attr_cur = at->attr_nxt;
-  mft_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
-  while (at->attr_cur < mft_end && *at->attr_cur != 0xFF)
+  while (at->attr_cur && *at->attr_cur != 0xFF)
     {
       at->attr_nxt = next_attribute (at->attr_cur, at->end);
       if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
Quoted 9 lines

--
2.48.1

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

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

author
#3 lore raw
Quoted 1 line · Vladimir 'phcoder' Serbinenko

Why do you remove boundary check? Maybe it's better to add the second boundary check from below

The issue stems from my original patches being rebased over fs/ntfs: Fix out-of-bounds read, with both my patches and that commit fixing the same fuzzed NTFS issue in the while-loop. I felt it was better to get it to known good state as I verified my original patches against quite a few NTFS volumes including several that caused the original OOB reads.

Keeping the mft_end checks doesn't hurt after adding the attr_cur check back in, just not much point as at->end should always be the same value.

Quoted 63 lines

Le ven. 28 févr. 2025, 16:11, B Horn <[email protected] <mailto:[email protected]>> a écrit :

    On some NTFS volumes GRUB would enter an infinite loop when
    next_attribute() returned NULL, which can happen in normal cases when
    the end of the attribute list is reached.
    This would trigger a NULL deref, but as the null page is mapped on the
    majority of firmware, an infinite loop would occur as the while loop
    didn't make any progress.

    Fixing this by verifying the value of at->attr_cur on the next iteration
    of the loop, after it has been set to the result of next_attribute().
    Also removing the redundant check against mft_end as the
    next_attribute() should handle that now.
    A pointer to the end of the buffer is stored in at->end, which is
    initialized the same way as mft_end was.

    Fixes: https://savannah.gnu.org/bugs/index.php?66855 <https://
    savannah.gnu.org/bugs/index.php?66855>

    Reported-by: Andreas Klauer <[email protected]
    <mailto:[email protected]>>
    Signed-off-by: B Horn <[email protected] <mailto:[email protected]>>
    ---
      grub-core/fs/ntfs.c | 5 +----
      1 file changed, 1 insertion(+), 4 deletions(-)

    diff --git a/grub-core/fs/ntfs.c b/grub-core/fs/ntfs.c
    index 960833a34..77531e627 100644
    --- a/grub-core/fs/ntfs.c
    +++ b/grub-core/fs/ntfs.c
    @@ -311,8 +311,6 @@ free_attr (struct grub_ntfs_attr *at)
      static grub_uint8_t *
      find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
      {
    -  grub_uint8_t *mft_end;
    -
        if (at->flags & GRUB_NTFS_AF_ALST)
          {
          retry:
    @@ -386,8 +384,7 @@ find_attr (struct grub_ntfs_attr *at,
    grub_uint8_t attr)
            return NULL;
          }
        at->attr_cur = at->attr_nxt;
    -  mft_end = at->mft->buf + (at->mft->data->mft_size <<
    GRUB_NTFS_BLK_SHR);
    -  while (at->attr_cur < mft_end && *at->attr_cur != 0xFF)
    +  while (at->attr_cur && *at->attr_cur != 0xFF)
          {
            at->attr_nxt = next_attribute (at->attr_cur, at->end);
            if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
    -- 
    2.48.1


    _______________________________________________
    Grub-devel mailing list
    [email protected] <mailto:[email protected]>
    https://lists.gnu.org/mailman/listinfo/grub-devel <https://
    lists.gnu.org/mailman/listinfo/grub-devel>
Quoted 4 lines

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

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

#4 lore raw

Hi,

This patch also fixes an issue introduced by the original patch involving dual booting: grub-mount seg faults when trying to mount an ntfs volume, e.g., when grub-mkconfig is run. There are bugs for this on fedora [1] and arch [2], so further review of this patch is really appreciated!

thanks!
marta

[1] https://bugzilla.redhat.com/show_bug.cgi?id=2350327
[2]
https://gitlab.archlinux.org/archlinux/packaging/packages/grub/-/issues/11

Quoted 86 lines · B Horn
Quoted 2 lines · Vladimir 'phcoder' Serbinenko

Why do you remove boundary check? Maybe it's better to add the second boundary check from below

The issue stems from my original patches being rebased over `fs/ntfs:
Fix out-of-bounds read`, with both my patches and that commit fixing the same fuzzed NTFS issue in the while-loop. I felt it was better to get it to known good state as I verified my original patches against quite a few NTFS volumes including several that caused the original OOB reads.

Keeping the mft_end checks doesn't hurt after adding the attr_cur check back in, just not much point as at->end should always be the same value.

Quoted 8 lines

Le ven. 28 févr. 2025, 16:11, B Horn <[email protected] <mailto:[email protected]>> a écrit :

On some NTFS volumes GRUB would enter an infinite loop when next_attribute() returned NULL, which can happen in normal cases when the end of the attribute list is reached.
This would trigger a NULL deref, but as the null page is mapped on

the

Quoted 4 lines

majority of firmware, an infinite loop would occur as the while loop didn't make any progress.

Fixing this by verifying the value of at->attr_cur on the next

iteration

Quoted 52 lines
    of the loop, after it has been set to the result of next_attribute().
    Also removing the redundant check against mft_end as the
    next_attribute() should handle that now.
    A pointer to the end of the buffer is stored in at->end, which is
    initialized the same way as mft_end was.

    Fixes: https://savannah.gnu.org/bugs/index.php?66855 <https://
    savannah.gnu.org/bugs/index.php?66855>

    Reported-by: Andreas Klauer <[email protected]
    <mailto:[email protected]>>
    Signed-off-by: B Horn <[email protected] <mailto:[email protected]>>
    ---
      grub-core/fs/ntfs.c | 5 +----
      1 file changed, 1 insertion(+), 4 deletions(-)

    diff --git a/grub-core/fs/ntfs.c b/grub-core/fs/ntfs.c
    index 960833a34..77531e627 100644
    --- a/grub-core/fs/ntfs.c
    +++ b/grub-core/fs/ntfs.c
    @@ -311,8 +311,6 @@ free_attr (struct grub_ntfs_attr *at)
      static grub_uint8_t *
      find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
      {
    -  grub_uint8_t *mft_end;
    -
        if (at->flags & GRUB_NTFS_AF_ALST)
          {
          retry:
    @@ -386,8 +384,7 @@ find_attr (struct grub_ntfs_attr *at,
    grub_uint8_t attr)
            return NULL;
          }
        at->attr_cur = at->attr_nxt;
    -  mft_end = at->mft->buf + (at->mft->data->mft_size <<
    GRUB_NTFS_BLK_SHR);
    -  while (at->attr_cur < mft_end && *at->attr_cur != 0xFF)
    +  while (at->attr_cur && *at->attr_cur != 0xFF)
          {
            at->attr_nxt = next_attribute (at->attr_cur, at->end);
            if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
    --
    2.48.1


    _______________________________________________
    Grub-devel mailing list
    [email protected] <mailto:[email protected]>
    https://lists.gnu.org/mailman/listinfo/grub-devel <https://
    lists.gnu.org/mailman/listinfo/grub-devel>
Quoted 4 lines

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

Quoted 5 lines

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

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

#5 lore raw
In reply to B Horn

For some fuzzers I’m working on, l happened to run into the same issue this patch is addressing and used a fix similar to what Vladimir suggested (keeping mft_end check and adding attr_cur != NULL check). This fixes the issue for me in my fuzzer so far.

Thanks,
Andrew

Quoted 86 lines · B Horn
Quoted 2 lines · Vladimir 'phcoder' Serbinenko

Why do you remove boundary check? Maybe it's better to add the second boundary check from below

The issue stems from my original patches being rebased over `fs/ntfs:
Fix out-of-bounds read`, with both my patches and that commit fixing the same fuzzed NTFS issue in the while-loop. I felt it was better to get it to known good state as I verified my original patches against quite a few NTFS volumes including several that caused the original OOB reads.

Keeping the mft_end checks doesn't hurt after adding the attr_cur check back in, just not much point as at->end should always be the same value.

Quoted 8 lines

Le ven. 28 févr. 2025, 16:11, B Horn <[email protected] <mailto:[email protected]>> a écrit :

On some NTFS volumes GRUB would enter an infinite loop when next_attribute() returned NULL, which can happen in normal cases when the end of the attribute list is reached.
This would trigger a NULL deref, but as the null page is mapped on

the

Quoted 4 lines

majority of firmware, an infinite loop would occur as the while loop didn't make any progress.

Fixing this by verifying the value of at->attr_cur on the next

iteration

Quoted 52 lines
    of the loop, after it has been set to the result of next_attribute().
    Also removing the redundant check against mft_end as the
    next_attribute() should handle that now.
    A pointer to the end of the buffer is stored in at->end, which is
    initialized the same way as mft_end was.

    Fixes: https://savannah.gnu.org/bugs/index.php?66855 <https://
    savannah.gnu.org/bugs/index.php?66855>

    Reported-by: Andreas Klauer <[email protected]
    <mailto:[email protected]>>
    Signed-off-by: B Horn <[email protected] <mailto:[email protected]>>
    ---
      grub-core/fs/ntfs.c | 5 +----
      1 file changed, 1 insertion(+), 4 deletions(-)

    diff --git a/grub-core/fs/ntfs.c b/grub-core/fs/ntfs.c
    index 960833a34..77531e627 100644
    --- a/grub-core/fs/ntfs.c
    +++ b/grub-core/fs/ntfs.c
    @@ -311,8 +311,6 @@ free_attr (struct grub_ntfs_attr *at)
      static grub_uint8_t *
      find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
      {
    -  grub_uint8_t *mft_end;
    -
        if (at->flags & GRUB_NTFS_AF_ALST)
          {
          retry:
    @@ -386,8 +384,7 @@ find_attr (struct grub_ntfs_attr *at,
    grub_uint8_t attr)
            return NULL;
          }
        at->attr_cur = at->attr_nxt;
    -  mft_end = at->mft->buf + (at->mft->data->mft_size <<
    GRUB_NTFS_BLK_SHR);
    -  while (at->attr_cur < mft_end && *at->attr_cur != 0xFF)
    +  while (at->attr_cur && *at->attr_cur != 0xFF)
          {
            at->attr_nxt = next_attribute (at->attr_cur, at->end);
            if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
    --
    2.48.1


    _______________________________________________
    Grub-devel mailing list
    [email protected] <mailto:[email protected]>
    https://lists.gnu.org/mailman/listinfo/grub-devel <https://
    lists.gnu.org/mailman/listinfo/grub-devel>
Quoted 4 lines

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

Quoted 5 lines

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

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

#6 lore raw

Unless someone else is already working on this, I started working on a patch for this as well as fixes to correct the NTFS Grub test failures - I’ll hopefully submit something for review in a few days.

Once it is available - any additional testing that can be done would be appreciated.

Thanks,
Andrew

Quoted 5 lines · Andrew Hamilton

For some fuzzers I’m working on, l happened to run into the same issue this patch is addressing and used a fix similar to what Vladimir suggested (keeping mft_end check and adding attr_cur != NULL check). This fixes the issue for me in my fuzzer so far.

Quoted 99 lines

Thanks,
Andrew

Quoted 93 lines · B Horn
Quoted 2 lines · Vladimir 'phcoder' Serbinenko

Why do you remove boundary check? Maybe it's better to add the second boundary check from below

The issue stems from my original patches being rebased over `fs/ntfs:
Fix out-of-bounds read`, with both my patches and that commit fixing the same fuzzed NTFS issue in the while-loop. I felt it was better to get it to known good state as I verified my original patches against quite a few NTFS volumes including several that caused the original OOB reads.

Keeping the mft_end checks doesn't hurt after adding the attr_cur check back in, just not much point as at->end should always be the same value.

Quoted 6 lines

Le ven. 28 févr. 2025, 16:11, B Horn <[email protected] <mailto:[email protected]>> a écrit :

On some NTFS volumes GRUB would enter an infinite loop when next_attribute() returned NULL, which can happen in normal cases

when

Quoted 2 lines

the end of the attribute list is reached.
This would trigger a NULL deref, but as the null page is mapped on

the

Quoted 4 lines

majority of firmware, an infinite loop would occur as the while loop didn't make any progress.

Fixing this by verifying the value of at->attr_cur on the next

iteration

Quoted 1 line

of the loop, after it has been set to the result of

next_attribute().

Quoted 51 lines
    Also removing the redundant check against mft_end as the
    next_attribute() should handle that now.
    A pointer to the end of the buffer is stored in at->end, which is
    initialized the same way as mft_end was.

    Fixes: https://savannah.gnu.org/bugs/index.php?66855 <https://
    savannah.gnu.org/bugs/index.php?66855>

    Reported-by: Andreas Klauer <[email protected]
    <mailto:[email protected]>>
    Signed-off-by: B Horn <[email protected] <mailto:[email protected]>>
    ---
      grub-core/fs/ntfs.c | 5 +----
      1 file changed, 1 insertion(+), 4 deletions(-)

    diff --git a/grub-core/fs/ntfs.c b/grub-core/fs/ntfs.c
    index 960833a34..77531e627 100644
    --- a/grub-core/fs/ntfs.c
    +++ b/grub-core/fs/ntfs.c
    @@ -311,8 +311,6 @@ free_attr (struct grub_ntfs_attr *at)
      static grub_uint8_t *
      find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
      {
    -  grub_uint8_t *mft_end;
    -
        if (at->flags & GRUB_NTFS_AF_ALST)
          {
          retry:
    @@ -386,8 +384,7 @@ find_attr (struct grub_ntfs_attr *at,
    grub_uint8_t attr)
            return NULL;
          }
        at->attr_cur = at->attr_nxt;
    -  mft_end = at->mft->buf + (at->mft->data->mft_size <<
    GRUB_NTFS_BLK_SHR);
    -  while (at->attr_cur < mft_end && *at->attr_cur != 0xFF)
    +  while (at->attr_cur && *at->attr_cur != 0xFF)
          {
            at->attr_nxt = next_attribute (at->attr_cur, at->end);
            if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
    --
    2.48.1


    _______________________________________________
    Grub-devel mailing list
    [email protected] <mailto:[email protected]>
    https://lists.gnu.org/mailman/listinfo/grub-devel <https://
    lists.gnu.org/mailman/listinfo/grub-devel>
Quoted 4 lines

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

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

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

#7 lore raw
Quoted 6 lines · Andrew Hamilton

Unless someone else is already working on this, I started working on a patch for this as well as fixes to correct the NTFS Grub test failures - I’ll hopefully submit something for review in a few days.

Once it is available - any additional testing that can be done would be appreciated.

Andrew, please post B Horn's updated patch taking into account Vladimir's suggestion. It looks the issue is hitting more and more people.

It would be nice if you CC folks who are involved in the discussion here and in another thread [1].

Daniel

[1] https://lore.kernel.org/grub-devel/[email protected]/T/#mbc3b22d20d31d2765fbc4a2ac205194041dfd6b7

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

#8 lore raw

Will do! I’ll do this later today after work. I was attempting to chase down some additional ntfs test failures (around hard links on an ntfs file system) even with the fix mentioned but maybe to expedite the main issue people are having I’ll put that on my queue for after this first fix.

Thanks!
Andrew

Quoted 12 lines · Daniel Kiper
Quoted 1 line · Andrew Hamilton

Unless someone else is already working on this, I started working on a

patch

Quoted 5 lines

for this as well as fixes to correct the NTFS Grub test failures - I’ll hopefully submit something for review in a few days.

Once it is available - any additional testing that can be done would be appreciated.

Andrew, please post B Horn's updated patch taking into account Vladimir's suggestion. It looks the issue is hitting more and more people.

Quoted 8 lines

It would be nice if you CC folks who are involved in the discussion here and in another thread [1].

Daniel

[1]
https://lore.kernel.org/grub-devel/[email protected]/T/#mbc3b22d20d31d2765fbc4a2ac205194041dfd6b7

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