Skip to content

hfs: fix timestamp wrapping and support timestamps after 2040

Viacheslav Dubeyko linux-fsdevel lkml 1 version New

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest3 patches 0 repliesNew

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3hfs: fix timestamp wrapped issuefs/hfs/hfs_fs.h fs/hfs/super.c+18 -20 replies
2/3hfs: store timestamps after February 2040 in spare catalog fieldsfs/hfs/catalog.c fs/hfs/hfs_fs.h fs/hfs/inode.c +1 more+149 -130 replies
3/3hfs: add KUnit tests for date/time conversionfs/hfs/Makefile fs/hfs/hfs_time_test.c+401 -10 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/3] hfs: fix timestamp wrapping and support timestamps after 2040

HFS keeps timestamps in 32-bit unsigned on-disk fields as the number of seconds since January 1, 1904 (local time). Such field can represent the time range from January 1, 1904 till February 6, 2040.

The current HFS driver has the same timestamp wrapping issue as the HFS+ driver had: it treats any on-disk timestamp under HFS_UTC_OFFSET (January 1, 1970) as a time between 2040 and 2106, and it calculates the on-disk value by means of adding the time offset to the lower 32 bits of the timestamp. As a result, any timestamp before January 1, 1970 is converted completely wrong.

This patchset is the HFS port of the HFS+ patchset "hfsplus: fix timestamp wrapping and support timestamps after 2040". It fixes the timestamp wrapping issue and extends the supported time range beyond February 2040.

The first patch corrects the logic of __hfs_m_to_utime() and __hfs_u_to_mtime(). The on-disk timestamp is interpreted as unsigned number of seconds since January 1, 1904, out of range values are clamped instead of wrapped, and hfs_fill_super() sets sb->s_time_min, sb->s_time_max and sb->s_time_gran (shifted by the local time zone offset). This patch restricts the supported time range by February 6, 2040.

The second patch extends the supported time range. HFS has no extended attributes on the volume. So, if the modification date cannot be represented by the 32-bit on-disk field, then the maximal value (0xFFFFFFFF) is stored into MdDat and the real value is kept in the spare bytes of the same catalog record as big-endian 64-bit number of seconds since January 1, 1904:

file record:      FndrInfo.fdIconID and FndrInfo.fdUnused[0..5]
directory record: Resrv[0..7]

fsck_hfs does not check these bytes. It is Linux specific extension of HFS on-disk layout. However, it is backward compatible: Mac OS sees the clamped timestamp (February 6, 2040). If another implementation updates MdDat, then the mark is removed and the stale value of the spare bytes is ignored. The extended value is updated together with MdDat in the same catalog record. So, no additional locking or b-tree update is required. Only MdDat is extended because HFS driver uses it for mtime, atime and ctime. The second patch raises sb->s_time_max to HFS_EXT_MAX_TIMESTAMP_SECS.

The third patch adds the hfs_time KUnit test suite that checks the date/time conversion logic and the extended timestamp support.

Tested on a KASAN and lockdep kernel: modification dates of files and directories (1904, 1950, February 6, 2040 and one second later, 2050, 2106, 2200, records created with the clock set to 2055, and timestamps moved back from the extended range) are preserved across remount, a timestamp before 1904 is clamped, and fsck.hfs reports the volume as OK. Without the patchset, the 1904, 1950, 2106 and 2200 timestamps are wrapped. The hfs_time and hfs_string KUnit suites pass.

Viacheslav Dubeyko (3):
hfs: fix timestamp wrapped issue
hfs: store timestamps after February 2040 in spare catalog fields
hfs: add KUnit tests for date/time conversion

 fs/hfs/Makefile        |   2 +-
 fs/hfs/catalog.c       |  10 +-
 fs/hfs/hfs_fs.h        |  74 +++++++-
 fs/hfs/hfs_time_test.c | 400 +++++++++++++++++++++++++++++++++++++++++
 fs/hfs/inode.c         |  88 ++++++++-
 fs/hfs/super.c         |   8 +
 6 files changed, 567 insertions(+), 15 deletions(-)
 create mode 100644 fs/hfs/hfs_time_test.c

base-commit: becaebec8e0f16edc0fd2426508e173f9d7bb5e9

Signature · 1 line
2.53.0