Skip to contentSkip to conversation

fat: Allow out-of-range FAT modification timestamps

PATCH In review Tomasz Kramkowski 3 participants 3 messages Aug 16, 2021 – Sep 2, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fat: Allow out-of-range FAT modification timestamps

20def1a3c introduced support for file modification times to allow comparison of file ages on EFI systems. This patch used grub_datetime2unixtime which uses a 32 bit unix timestamp and as a result did not allow the full range of times that FAT timestamps do.

In some situations a file with a timestamp of 1970-01-01 gets transferred to a FAT partition, the timestamp ends up as 2098-01-01 because of FAT's use of the 1980-01-01 DOS epoch and lack of negative timestamps.

Since 2098 is after 2038, this date cannot fit in a 32 bit timestamp.

Ideally grub should use 64 bit timestamps but I have not investigated what kind of work would be required to support this.

This fixes bug #60565.

---
 grub-core/fs/fat.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
grub-core/fs/fat.c +4 -3
index dd82e4ee3..34589d7db 100644
@@ -1020,16 +1020,17 @@ grub_fat_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook,
       info.mtimeset = grub_exfat_timestamp (grub_le_to_cpu32 (ctxt.entry.type_specific.file.m_time),
 					    ctxt.entry.type_specific.file.m_time_tenth,
 					    &info.mtime);
+      if (info.mtimeset == 0)
+	grub_dprintf("exfat", "invalid modification timestamp for %s\n", path);
 #else
       if (ctxt.dir.attr & GRUB_FAT_ATTR_VOLUME_ID)
 	continue;
       info.mtimeset = grub_fat_timestamp (grub_le_to_cpu16 (ctxt.dir.w_time),
 					  grub_le_to_cpu16 (ctxt.dir.w_date),
 					  &info.mtime);
-#endif
       if (info.mtimeset == 0)
-	grub_error (GRUB_ERR_OUT_OF_RANGE,
-		    "invalid modification timestamp for %s", path);
+	grub_dprintf("fat", "invalid modification timestamp for %s\n", path);
+#endif
 
       if (hook (ctxt.filename, &info, hook_data))
 	break;
Signature · 1 line
2.32.0
#1 lore raw
Quoted 10 lines · Tomasz Kramkowski via Grub-devel

20def1a3c introduced support for file modification times to allow comparison of file ages on EFI systems. This patch used grub_datetime2unixtime which uses a 32 bit unix timestamp and as a result did not allow the full range of times that FAT timestamps do.

In some situations a file with a timestamp of 1970-01-01 gets transferred to a FAT partition, the timestamp ends up as 2098-01-01 because of FAT's use of the 1980-01-01 DOS epoch and lack of negative timestamps.

Quoted 4 lines

Since 2098 is after 2038, this date cannot fit in a 32 bit timestamp.

Ideally grub should use 64 bit timestamps but I have not investigated what kind of work would be required to support this.

Field mtime of struct grub_dirhook_info is already 64bit. See commit 81f1962393f4 ("fs: Use 64-bit type for filesystem timestamp"). Function grub_datetime2unixtime needs to be fixed: The 2037 check should be removed and the code has to be adjusted to correctly treat year 2100.

grub_unixtime2datetime already handles 64bit timestamps but ignores that year 2100 is not a leap year:

grub_unixtime2datetime(4107585600) = 2100-02-29 12:00:00

Quoted 15 lines

This fixes bug #60565.

---
  grub-core/fs/fat.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
grub-core/fs/fat.c +0 -0
index dd82e4ee3..34589d7db 100644

grub-core/fs/fat.c grub_fat_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook, · line 1024

Quoted 5 lines
grub-core/fs/fat.c +2 -0
@@ -1020,16 +1020,17 @@ grub_fat_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook,
        info.mtimeset = grub_exfat_timestamp (grub_le_to_cpu32 (ctxt.entry.type_specific.file.m_time),
  					    ctxt.entry.type_specific.file.m_time_tenth,
  					    &info.mtime);
+      if (info.mtimeset == 0)
+	grub_dprintf("exfat", "invalid modification timestamp for %s\n", path);

According to the commit message there is nothing invalid about the modification timestamp. It is a GRUB deficiency that it cannot handle the same date range as FAT and exFAT.

So the message could be:
"GRUB cannot handle the modification timestamp for %s".

grub-core/fs/fat.c grub_fat_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook, · line 1032

Quoted 11 lines
grub-core/fs/fat.c +1 -3
  #else
        if (ctxt.dir.attr & GRUB_FAT_ATTR_VOLUME_ID)
  	continue;
        info.mtimeset = grub_fat_timestamp (grub_le_to_cpu16 (ctxt.dir.w_time),
  					  grub_le_to_cpu16 (ctxt.dir.w_date),
  					  &info.mtime);
-#endif
        if (info.mtimeset == 0)
-	grub_error (GRUB_ERR_OUT_OF_RANGE,
-		    "invalid modification timestamp for %s", path);
+	grub_dprintf("fat", "invalid modification timestamp for %s\n", path);

I suggest to use grub_error() for both messages.

Best regards

Heinrich

Quoted 5 lines
grub-core/fs/fat.c +1 -0
+#endif
 
        if (hook (ctxt.filename, &info, hook_data))
  	break;
#2 lore raw
Quoted 54 lines · Heinrich Schuchardt
Quoted 10 lines · Tomasz Kramkowski via Grub-devel

20def1a3c introduced support for file modification times to allow comparison of file ages on EFI systems. This patch used grub_datetime2unixtime which uses a 32 bit unix timestamp and as a result did not allow the full range of times that FAT timestamps do.

In some situations a file with a timestamp of 1970-01-01 gets transferred to a FAT partition, the timestamp ends up as 2098-01-01 because of FAT's use of the 1980-01-01 DOS epoch and lack of negative timestamps.

Quoted 4 lines

Since 2098 is after 2038, this date cannot fit in a 32 bit timestamp.

Ideally grub should use 64 bit timestamps but I have not investigated what kind of work would be required to support this.

Field mtime of struct grub_dirhook_info is already 64bit. See commit 81f1962393f4 ("fs: Use 64-bit type for filesystem timestamp"). Function grub_datetime2unixtime needs to be fixed: The 2037 check should be removed and the code has to be adjusted to correctly treat year 2100.

grub_unixtime2datetime already handles 64bit timestamps but ignores that year 2100 is not a leap year:

grub_unixtime2datetime(4107585600) = 2100-02-29 12:00:00

Quoted 15 lines

This fixes bug #60565.

---
  grub-core/fs/fat.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
grub-core/fs/fat.c +0 -0
index dd82e4ee3..34589d7db 100644

grub-core/fs/fat.c grub_fat_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook, · line 1024

Quoted 5 lines
grub-core/fs/fat.c +2 -0
@@ -1020,16 +1020,17 @@ grub_fat_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook,
        info.mtimeset = grub_exfat_timestamp (grub_le_to_cpu32 (ctxt.entry.type_specific.file.m_time),
  					    ctxt.entry.type_specific.file.m_time_tenth,
  					    &info.mtime);
+      if (info.mtimeset == 0)
+	grub_dprintf("exfat", "invalid modification timestamp for %s\n", path);

According to the commit message there is nothing invalid about the modification timestamp. It is a GRUB deficiency that it cannot handle the same date range as FAT and exFAT.

So the message could be:
"GRUB cannot handle the modification timestamp for %s".

Quoted 13 lines

grub-core/fs/fat.c grub_fat_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook, · line 1032

Quoted 11 lines
grub-core/fs/fat.c +1 -3
  #else
        if (ctxt.dir.attr & GRUB_FAT_ATTR_VOLUME_ID)
  	continue;
        info.mtimeset = grub_fat_timestamp (grub_le_to_cpu16 (ctxt.dir.w_time),
  					  grub_le_to_cpu16 (ctxt.dir.w_date),
  					  &info.mtime);
-#endif
        if (info.mtimeset == 0)
-	grub_error (GRUB_ERR_OUT_OF_RANGE,
-		    "invalid modification timestamp for %s", path);
+	grub_dprintf("fat", "invalid modification timestamp for %s\n", path);

I suggest to use grub_error() for both messages.

Tomasz, I agree with Heinrich. I am happy to take updated patch. Could you make it?

Daniel