Skip to contentSkip to conversation

[[email protected]: Bug#1021846: grub-install is broken since 2.06-3: error: unknown filesystem]

DISCUSSION Steve McIntyre 7 participants 9 messages Nov 15, 2022 – Dec 9, 2022

lore
Share & tools
lore
#0 lore raw

Hi all!

программист некто (in CC) reported this bug a few weeks back in Debian. Since I applied the bundle of filesystem bounds-checking fixes a few months back, he can't run grub-install. He's done the work to determine that the patch that breaks things for him is

2d014248d540c7e087934a94b6e7a2aa7fc2c704 fs/f2fs: Do not read past the end of nat journal entries

The full thread of our discussion is at https://bugs.debian.org/1021846

I don't have any knowledge of f2fs to go any further here. Help please! :-)

----- Forwarded message from программист некто <[email protected]> -----

From: программист некто <[email protected]>
To: [email protected]
Date: Sat, 15 Oct 2022 23:54:36 +0300
Subject: Bug#1021846: grub-install is broken since 2.06-3: error: unknown filesystem
Message-Id: <[email protected]>

Package: grub-pc
Version: 2.06-3~deb11u1
Severity: critical

Hello. Since version 2.06-3, grub-install is broken: it fails with "error: unknown filesystem". I test command /usr/sbin/grub-install -v /dev/sda
in some versions. Results in mail attachments.
Versions older than 2.06-3 works without error (2.06-2 and lower).
Tested versions: 2.04-20, 2.06-1, 2.06-2, 2.06-3~deb10u1, 2.06-3~deb11u1, 2.06-4.

Disk partitions:

# fdisk --list-details
Disk /dev/sda: 29,82 GiB, 32017047552 bytes, 62533296 sectors
Disk model: TS32GSSD370S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc7177f7e

Device Boot Start End Sectors Id Type Start-C/H/S End-C/H/S Attrs /dev/sda1 2048 22763519 22761472 83 Linux 4/4/1 1023/254/2 /dev/sda2 * 25866240 62531583 36665344 7 HPFS/ 1023/254/2 1023/254/2 80

$ disktype /dev/sda1
--- /dev/sda1
Block device, size 10.85 GiB (11653873664 bytes)
F2FS file system (version 1.14)
$ disktype /dev/sda2
--- /dev/sda2
Block device, size 17.48 GiB (18772656128 bytes)
NTFS file system
Volume size 17.48 GiB (18772652032 bytes, 36665336 sectors)

----- End forwarded message -----

Signature · 5 lines
Steve McIntyre, Cambridge, UK.                                [email protected]
  Mature Sporty Personal
  More Innovation More Adult
  A Man in Dandism
  Powered Midship Specialty
#1 lore raw

Adding Daniel Axtens...

Quoted 64 lines · Steve McIntyre

Hi all!

программист некто (in CC) reported this bug a few weeks back in Debian. Since I applied the bundle of filesystem bounds-checking fixes a few months back, he can't run grub-install. He's done the work to determine that the patch that breaks things for him is

2d014248d540c7e087934a94b6e7a2aa7fc2c704 fs/f2fs: Do not read past the end of nat journal entries

The full thread of our discussion is at https://bugs.debian.org/1021846

I don't have any knowledge of f2fs to go any further here. Help please! :-)

----- Forwarded message from программист некто <[email protected]> -----

From: программист некто <[email protected]>
To: [email protected]
Date: Sat, 15 Oct 2022 23:54:36 +0300
Subject: Bug#1021846: grub-install is broken since 2.06-3: error: unknown filesystem
Message-Id: <[email protected]>

Package: grub-pc
Version: 2.06-3~deb11u1
Severity: critical

Hello. Since version 2.06-3, grub-install is broken: it fails with "error: unknown filesystem". I test command /usr/sbin/grub-install -v /dev/sda
in some versions. Results in mail attachments.
Versions older than 2.06-3 works without error (2.06-2 and lower).
Tested versions: 2.04-20, 2.06-1, 2.06-2, 2.06-3~deb10u1, 2.06-3~deb11u1, 2.06-4.

Disk partitions:

# fdisk --list-details
Disk /dev/sda: 29,82 GiB, 32017047552 bytes, 62533296 sectors
Disk model: TS32GSSD370S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc7177f7e

Device Boot Start End Sectors Id Type Start-C/H/S End-C/H/S Attrs /dev/sda1 2048 22763519 22761472 83 Linux 4/4/1 1023/254/2 /dev/sda2 * 25866240 62531583 36665344 7 HPFS/ 1023/254/2 1023/254/2 80

$ disktype /dev/sda1
--- /dev/sda1
Block device, size 10.85 GiB (11653873664 bytes)
F2FS file system (version 1.14)
$ disktype /dev/sda2
--- /dev/sda2
Block device, size 17.48 GiB (18772656128 bytes)
NTFS file system
Volume size 17.48 GiB (18772652032 bytes, 36665336 sectors)

----- End forwarded message -----
--
Steve McIntyre, Cambridge, UK. [email protected]
Mature Sporty Personal
More Innovation More Adult
A Man in Dandism
Powered Midship Specialty

#2 lore raw

Hi Steve,

It seems invalid Commit id which you reported. It should be 4bd9877f62166b7e369773ab92fe24a39f6515f8 did you applied below patch and tested? Could you please confirm that.

fs/f2fs: Do not read past the end of nat journal entries

https://git.savannah.gnu.org/cgit/grub.git/patch/?id=4bd9877f62166b7e369773ab92fe24a39f6515f8

thanks,
Sudhakar Kuppusamy

Quoted 68 lines · Daniel Kiper

Adding Daniel Axtens...

Quoted 65 lines · Steve McIntyre

Hi all!

программист некто (in CC) reported this bug a few weeks back in Debian. Since I applied the bundle of filesystem bounds-checking fixes a few months back, he can't run grub-install. He's done the work to determine that the patch that breaks things for him is

2d014248d540c7e087934a94b6e7a2aa7fc2c704 fs/f2fs: Do not read past the end of nat journal entries

The full thread of our discussion is at https://bugs.debian.org/1021846

I don't have any knowledge of f2fs to go any further here. Help please! :-)

----- Forwarded message from программист некто
<[email protected]> -----

From: программист некто <[email protected]>
To: [email protected]
Date: Sat, 15 Oct 2022 23:54:36 +0300
Subject: Bug#1021846: grub-install is broken since 2.06-3: error: unknown filesystem
Message-Id: <[email protected]>

Package: grub-pc
Version: 2.06-3~deb11u1
Severity: critical

Hello. Since version 2.06-3, grub-install is broken: it fails with "error: unknown filesystem". I test command /usr/sbin/grub-install -v /dev/sda
in some versions. Results in mail attachments.
Versions older than 2.06-3 works without error (2.06-2 and lower).
Tested versions: 2.04-20, 2.06-1, 2.06-2, 2.06-3~deb10u1, 2.06-3~deb11u1, 2.06-4.

Disk partitions:

# fdisk --list-details
Disk /dev/sda: 29,82 GiB, 32017047552 bytes, 62533296 sectors
Disk model: TS32GSSD370S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc7177f7e

Device Boot Start End Sectors Id Type Start-C/H/S End-C/H/S Attrs /dev/sda1 2048 22763519 22761472 83 Linux 4/4/1 1023/254/2 /dev/sda2 * 25866240 62531583 36665344 7 HPFS/ 1023/254/2 1023/254/2 80

$ disktype /dev/sda1
--- /dev/sda1
Block device, size 10.85 GiB (11653873664 bytes)
F2FS file system (version 1.14)
$ disktype /dev/sda2
--- /dev/sda2
Block device, size 17.48 GiB (18772656128 bytes)
NTFS file system
Volume size 17.48 GiB (18772652032 bytes, 36665336 sectors)

----- End forwarded message -----
--
Steve McIntyre, Cambridge, UK. [email protected]
Mature Sporty Personal
More Innovation More Adult
A Man in Dandism
Powered Midship Specialty

#3 lore raw

Hi Sudhakar!

Quoted 9 lines · sudhakar

Hi Steve,

It seems invalid Commit id which you reported. It should be 4bd9877f62166b7e369773ab92fe24a39f6515f8
did you applied below patch and tested? Could you please confirm that.

fs/f2fs: Do not read past the end of nat journal entries

https://git.savannah.gnu.org/cgit/grub.git/patch/?id=4bd9877f62166b7e369773ab92fe24a39f6515f8

It's exactly the same patch, just the commit hash is different when pulled into our 2.06 tree.

Cheers,

Steve

Signature · 2 lines
Steve McIntyre, Cambridge, UK.                                [email protected]
Welcome my son, welcome to the machine.
#4 lore raw
In reply to Steve McIntyre
Quoted 7 lines · Steve McIntyre

Hi all!

программист некто (in CC) reported this bug a few weeks back in Debian. Since I applied the bundle of filesystem bounds-checking fixes a few months back, he can't run grub-install. He's done the work to determine that the patch that breaks things for him is

Quoted 5 lines

2d014248d540c7e087934a94b6e7a2aa7fc2c704 fs/f2fs: Do not read past the end of nat journal entries

The full thread of our discussion is at https://bugs.debian.org/1021846

I don't have any knowledge of f2fs to go any further here. Help please! :-)

I don't know much about f2fs either, but has the value of n been captured versus NAT_JOURNAL_ENTRIES in the failing case? Might be useful to know how much it's going over by.

Be well,
--Robbie

#5 lore raw
In reply to Steve McIntyre
Quoted 7 lines · Steve McIntyre

Hi all!

программист некто (in CC) reported this bug a few weeks back in Debian. Since I applied the bundle of filesystem bounds-checking fixes a few months back, he can't run grub-install. He's done the work to determine that the patch that breaks things for him is

Quoted 5 lines

2d014248d540c7e087934a94b6e7a2aa7fc2c704 fs/f2fs: Do not read past the end of nat journal entries

The full thread of our discussion is at https://bugs.debian.org/1021846

I don't have any knowledge of f2fs to go any further here. Help please! :-)

Ergh, apologies for the regression.

[somewhat off-topic: The fix came from a crash derived from fuzzing. I am not really knowledgeable about f2fs either - I was just trying to do my best based on what we could derive from the existing driver. In general, filesystems are a nightmare for fuzzing fixes because testing beyond the (quite decent!) tests that the grub test-suite runs is very challenging. There is usually no-one who is both involved in grub security and an expert on any given file system either. We do the best we can. Sadly our regression rate has been climbing, so we may need to come up with some other way to secure file systems or get access to sufficient expertise in the future.]

I had a massive, massive work-in-progress spiel where I looked at this code and compared the linux code and counted sizes and so on and so forth. I was getting nowhere. But eventually I realised I had just made an off-by-one error in the test. You're allowed to have up to n = NAT_JOURNAL_ENTRIES entries _inclusive_, because the loop below uses i < n, not i <= n. D'oh.

Please try the following:

grub-core/fs/f2fs.c +1 -1
index df6beb544cbd..855e24618c2b 100644
@@ -650,7 +650,7 @@ get_blkaddr_from_nat_journal (struct grub_f2fs_data *data, grub_uint32_t nid,
   grub_uint16_t n = grub_le_to_cpu16 (data->nat_j.n_nats);
   grub_uint16_t i;
 
-  if (n >= NAT_JOURNAL_ENTRIES)
+  if (n > NAT_JOURNAL_ENTRIES)
     return grub_error (GRUB_ERR_BAD_FS,
                        "invalid number of nat journal entries");
 

If for some reason that doesn't work, please add the following debug code and report the results:

grub-core/fs/f2fs.c +8 -0
index 855e24618c2b..6e49a6d17b7a 100644
@@ -643,6 +643,10 @@ get_nat_journal (struct grub_f2fs_data *data)
   return err;
 }
 
+#ifdef GRUB_UTIL
+#include <stdio.h>
+#endif
+
 static grub_err_t
 get_blkaddr_from_nat_journal (struct grub_f2fs_data *data, grub_uint32_t nid,
                               grub_uint32_t *blkaddr)
@@ -650,6 +654,10 @@ get_blkaddr_from_nat_journal (struct grub_f2fs_data *data, grub_uint32_t nid,
   grub_uint16_t n = grub_le_to_cpu16 (data->nat_j.n_nats);
   grub_uint16_t i;
 
+#ifdef GRUB_UTIL
+  fprintf(stderr, "%s: n = %hu\n", __func__, n);
+#endif
+
   if (n > NAT_JOURNAL_ENTRIES)
     return grub_error (GRUB_ERR_BAD_FS,
                        "invalid number of nat journal entries");

Amusingly the debug code shows that the grub-fs-tester tests always have n = 0, which makes sense for a test that doesn't really stress the file-system, and also explains why we didn't catch the bug when it was introduced.

Kind regards,
Daniel

Quoted 65 lines

----- Forwarded message from программист некто <[email protected]> -----

From: программист некто <[email protected]>
To: [email protected]
Date: Sat, 15 Oct 2022 23:54:36 +0300
Subject: Bug#1021846: grub-install is broken since 2.06-3: error: unknown filesystem
Message-Id: <[email protected]>

Package: grub-pc
Version: 2.06-3~deb11u1
Severity: critical

Hello. Since version 2.06-3, grub-install is broken: it fails with "error: unknown filesystem". I test command /usr/sbin/grub-install -v /dev/sda
in some versions. Results in mail attachments.
Versions older than 2.06-3 works without error (2.06-2 and lower).
Tested versions: 2.04-20, 2.06-1, 2.06-2, 2.06-3~deb10u1, 2.06-3~deb11u1, 2.06-4.

Disk partitions:

# fdisk --list-details
Disk /dev/sda: 29,82 GiB, 32017047552 bytes, 62533296 sectors
Disk model: TS32GSSD370S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc7177f7e

Device Boot Start End Sectors Id Type Start-C/H/S End-C/H/S Attrs /dev/sda1 2048 22763519 22761472 83 Linux 4/4/1 1023/254/2 /dev/sda2 * 25866240 62531583 36665344 7 HPFS/ 1023/254/2 1023/254/2 80

$ disktype /dev/sda1
--- /dev/sda1
Block device, size 10.85 GiB (11653873664 bytes)
F2FS file system (version 1.14)
$ disktype /dev/sda2
--- /dev/sda2
Block device, size 17.48 GiB (18772656128 bytes)
NTFS file system
Volume size 17.48 GiB (18772652032 bytes, 36665336 sectors)

----- End forwarded message -----

Signature · 11 lines
Steve McIntyre, Cambridge, UK.                                [email protected]
  Mature Sporty Personal
  More Innovation More Adult
  A Man in Dandism
  Powered Midship Specialty


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

Hi Daniel!

Quoted 15 lines · Daniel Axtens
Quoted 11 lines · Steve McIntyre

программист некто (in CC) reported this bug a few weeks back in Debian. Since I applied the bundle of filesystem bounds-checking fixes a few months back, he can't run grub-install. He's done the work to determine that the patch that breaks things for him is

2d014248d540c7e087934a94b6e7a2aa7fc2c704 fs/f2fs: Do not read past the end of nat journal entries

The full thread of our discussion is at https://bugs.debian.org/1021846

I don't have any knowledge of f2fs to go any further here. Help please! :-)

Ergh, apologies for the regression.

Quoted 10 lines

[somewhat off-topic: The fix came from a crash derived from fuzzing. I am not really knowledgeable about f2fs either - I was just trying to do my best based on what we could derive from the existing driver. In general, filesystems are a nightmare for fuzzing fixes because testing beyond the (quite decent!) tests that the grub test-suite runs is very challenging. There is usually no-one who is both involved in grub security and an expert on any given file system either. We do the best we can. Sadly our regression rate has been climbing, so we may need to come up with some other way to secure file systems or get access to sufficient expertise in the future.]

ACK. I used to develop amd maintain filesystems as a day job, I understand the issue! Writing good and comprehensive tests is hard, and therefore quite rare!

Quoted 6 lines

I had a massive, massive work-in-progress spiel where I looked at this code and compared the linux code and counted sizes and so on and so forth. I was getting nowhere. But eventually I realised I had just made an off-by-one error in the test. You're allowed to have up to n = NAT_JOURNAL_ENTRIES entries _inclusive_, because the loop below uses i < n, not i <= n. D'oh.

Doh indeed! :-)

Quoted 36 lines

Please try the following:

grub-core/fs/f2fs.c +1 -1
index df6beb544cbd..855e24618c2b 100644
@@ -650,7 +650,7 @@ get_blkaddr_from_nat_journal (struct grub_f2fs_data *data, grub_uint32_t nid,
  grub_uint16_t n = grub_le_to_cpu16 (data->nat_j.n_nats);
  grub_uint16_t i;
 
-  if (n >= NAT_JOURNAL_ENTRIES)
+  if (n > NAT_JOURNAL_ENTRIES)
    return grub_error (GRUB_ERR_BAD_FS,
                       "invalid number of nat journal entries");
 

If for some reason that doesn't work, please add the following debug code and report the results:

grub-core/fs/f2fs.c +4 -0
index 855e24618c2b..6e49a6d17b7a 100644
@@ -643,6 +643,10 @@ get_nat_journal (struct grub_f2fs_data *data)
  return err;
 }
 
+#ifdef GRUB_UTIL
+#include <stdio.h>
+#endif
+

static grub_err_t
get_blkaddr_from_nat_journal (struct grub_f2fs_data *data, grub_uint32_t nid,

                              grub_uint32_t *blkaddr)

grub-core/fs/f2fs.c get_blkaddr_from_nat_journal (struct grub_f2fs_data *data, grub_uint32_t nid, · line 663

Quoted 10 lines
grub-core/fs/f2fs.c +4 -0
@@ -650,6 +654,10 @@ get_blkaddr_from_nat_journal (struct grub_f2fs_data *data, grub_uint32_t nid,
  grub_uint16_t n = grub_le_to_cpu16 (data->nat_j.n_nats);
  grub_uint16_t i;
 
+#ifdef GRUB_UTIL
+  fprintf(stderr, "%s: n = %hu\n", __func__, n);
+#endif
+
  if (n > NAT_JOURNAL_ENTRIES)
    return grub_error (GRUB_ERR_BAD_FS,
                       "invalid number of nat journal entries");

программист некто: could you please try these changes and report back?

Quoted 4 lines

Amusingly the debug code shows that the grub-fs-tester tests always have n = 0, which makes sense for a test that doesn't really stress the file-system, and also explains why we didn't catch the bug when it was introduced.

Right.

Signature · 4 lines
Steve McIntyre, Cambridge, UK.                                [email protected]
  Armed with "Valor": "Centurion" represents quality of Discipline,
  Honor, Integrity and Loyalty. Now you don't have to be a Caesar to
  concord the digital world while feeling safe and proud.
#7 lore raw

Certainly, I will try these changes. 03.12.2022, 20:44, "Steve McIntyre" <[email protected]>:
программист некто: could you please try these changes and report back?

--
Steve McIntyre, Cambridge, UK. [email protected]
  Armed with "Valor": "Centurion" represents quality of Discipline,
  Honor, Integrity and Loyalty. Now you don't have to be a Caesar to
  concord the digital world while feeling safe and proud.

#8 lore raw
In reply to Daniel Axtens

Daniel Axtens <[email protected]> on Sat, 2022/12/03 01:41:

Quoted 7 lines

Please try the following:

grub-core/fs/f2fs.c +0 -0
index df6beb544cbd..855e24618c2b 100644

grub-core/fs/f2fs.c get_blkaddr_from_nat_journal (struct grub_f2fs_data · line 655

Quoted 7 lines
grub-core/fs/f2fs.c +1 -1
@@ -650,7 +650,7 @@ get_blkaddr_from_nat_journal (struct grub_f2fs_data
 *data, grub_uint32_t nid, grub_uint16_t n = grub_le_to_cpu16
 (data->nat_j.n_nats); grub_uint16_t i;
 
-  if (n >= NAT_JOURNAL_ENTRIES)
+  if (n > NAT_JOURNAL_ENTRIES)
     return grub_error (GRUB_ERR_BAD_FS,
                        "invalid number of nat journal entries");

I have a confirmation in Arch downstream bug report that this indeed does fix the issue.

https://bugs.archlinux.org/task/76168#comment213505

Pleas commit into master. Thanks a lot!

Signature · 3 lines
main(a){char*c=/*    Schoene Gruesse                         */"B?IJj;MEH"
"CX:;",b;for(a/*    Best regards             my address:    */=0;b=c[a++];)
putchar(b-1/(/*    Chris            cc -ox -xc - && ./x    */b/42*2-3)*42);}