hfs: fix timestamp wrapping and support timestamps after 2040
Subsystems (2)
Viacheslav Dubeyko
maintainercover 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
Viacheslav Dubeyko
maintainerPATCH 1/3 hfs: fix timestamp wrapped issue
The main reason of the issue is the logic:
cpu_to_be32(lower_32_bits(ut) + HFS_UTC_OFFSET)
At first, we take the lower 32 bits of the value and, then, we add the time offset. However, if we have negative value (timestamp before January 1, 1970), then we make completely wrong calculation. The reverse conversion has the same issue because it treats any on-disk timestamp under HFS_UTC_OFFSET (January 1, 1970) as a time between 2040 and 2106. Moreover, any timestamp after February 2106 is silently wrapped.
This patch corrects the logic of __hfs_m_to_utime() and __hfs_u_to_mtime(). Now, the on-disk timestamp is interpreted as unsigned number of seconds since January 1, 1904. As a result, the supported time range is from January 1, 1904 till February 6, 2040. HFS keeps timestamps in local time. So, the range is shifted by the local time zone offset. __hfs_u_to_mtime() clamps the value into the supported range instead of wrapping it.
The hfs_fill_super() logic defines sb->s_time_min, sb->s_time_max, and sb->s_time_gran. It means that VFS clamps the timestamps that are out of the supported range.
This is the HFS port of the HFS+ patch
"hfsplus: fix timestamp wrapped issue".
--- fs/hfs/hfs_fs.h | 16 ++++++++++++++-- fs/hfs/super.c | 4 ++++ 2 files changed, 18 insertions(+), 2 deletions(-)
fs/hfs/hfs_fs.h +14 -2
@@ -261,9 +261,18 @@ extern int hfs_mac2asc(struct super_block *sb,/* super.c */ extern void hfs_mark_mdb_dirty (struct super_block *sb); +/* +* time helpers: convert between 1904-base and 1970-base timestamps +* +* HFS keeps timestamps as unsigned 32-bit number of seconds since +* January 1, 1904 in local time. The on-disk timestamps cover the range +* from HFS_MIN_TIMESTAMP_SECS (Jan. 1, 1904) till HFS_MAX_TIMESTAMP_SECS +* (Feb. 6, 2040) shifted by the local time zone offset. The timestamps +* out of this range are clamped. +*/ static inline time64_t __hfs_m_to_utime (__be32 mt) { -time64_t ut = (u32)(be32_to_cpu (mt) -HFS_UTC_OFFSET ); +time64_t ut = (time64_t )be32_to_cpu (mt) -HFS_UTC_OFFSET ;return ut + sys_tz.tz_minuteswest *60 ; }
@@ -271,8 +280,11 @@ static inline time64_t __hfs_m_to_utime(__be32 mt)static inline __be32 __hfs_u_to_mtime (time64_t ut) { ut -= sys_tz.tz_minuteswest *60 ; + ut =clamp_t (time64_t, ut, +HFS_MIN_TIMESTAMP_SECS ,HFS_MAX_TIMESTAMP_SECS ); + ut +=HFS_UTC_OFFSET ; -return cpu_to_be32 (lower_32_bits (ut) +HFS_UTC_OFFSET ); +return cpu_to_be32 (lower_32_bits (ut)); }#define HFS_I (inode) (container_of(inode, struct hfs_inode_info, vfs_inode))#define HFS_SB (sb) ((struct hfs_sb_info *)(sb)->s_fs_info)
fs/hfs/super.c +4 -0
@@ -345,6 +345,10 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) sb->s_op = &hfs_super_operations; sb->s_xattr = hfs_xattr_handlers; sb->s_flags |=SB_NOATIME |SB_NODIRATIME ; + sb->s_time_gran =NSEC_PER_SEC ; +/* on-disk timestamps are in local time */ + sb->s_time_min =HFS_MIN_TIMESTAMP_SECS + sys_tz.tz_minuteswest *60 ; + sb->s_time_max =HFS_MAX_TIMESTAMP_SECS + sys_tz.tz_minuteswest *60 ;mutex_init (&sbi->mdb_lock);mutex_init (&sbi->bitmap_lock);
Signature · 1 line
2.53.0
Viacheslav Dubeyko
maintainerPATCH 2/3 hfs: store timestamps after February 2040 in spare catalog fields
In reply to Viacheslav DubeykoThe timestamp wrapping issue has been fixed but it restricts the supported time range to the one that the 32-bit on-disk field can represent: from January 1, 1904 till February 6, 2040 (local time). Any timestamp after February 6, 2040 is clamped now.
HFS has no extended attributes on the volume. Extend the supported time range by means of spare bytes of the catalog record. If the modification date cannot be represented by the 32-bit on-disk field, then the maximal value (0xFFFFFFFF, HFS_EXT_TIMESTAMP_MARK) is stored into MdDat and the real value is stored into the spare bytes of the same catalog record as big-endian 64-bit number of seconds since January 1, 1904 (local time, as MdDat itself):
file record: FndrInfo.fdIconID and FndrInfo.fdUnused[0..5] directory record: Resrv[0..7]
The fsck_hfs tool checks that StBlk, RStBlk, and Resrv of the file record are zero. As a result, these fields cannot be used. However, fsck_hfs does not check the Finder info of the file record and the reserved field of the directory record. Apple uses the same 8 bytes of the extended Finder info in HFS+ (document ID and date added).
Read path: if MdDat is equal to HFS_EXT_TIMESTAMP_MARK and the spare bytes contain the value that cannot be represented by MdDat, then hfs_cat_read_mtime() returns the extended value. Otherwise, the value of MdDat is used. If another implementation (for example, Mac OS) updates MdDat, then the mark is removed and the stale value of the spare bytes is ignored. If the spare bytes contain zero or garbage, then HFS_MAX_TIMESTAMP_SECS is used.
Write path: hfs_cat_write_mtime() stores the modification date into the catalog record. The spare bytes are cleared if the timestamp becomes representable by MdDat again. The spare bytes are not touched for the records with regular MdDat because the record could be created by another implementation. The extended value is updated together with MdDat in the same catalog record. It means that it is protected by the catalog tree lock and no additional locking or b-tree update is required.
Only MdDat is extended because HFS driver uses it for mtime, atime, and ctime. The CrDat and BkDat fields are not exposed by HFS driver and they are clamped. The hfs_cat_build_record() uses the timestamp of the inode for the new record. As a result, sb->s_time_max is set to HFS_EXT_MAX_TIMESTAMP_SECS.
--- fs/hfs/catalog.c | 10 +++--- fs/hfs/hfs_fs.h | 58 +++++++++++++++++++++++++++++-- fs/hfs/inode.c | 88 +++++++++++++++++++++++++++++++++++++++++++++--- fs/hfs/super.c | 6 +++- 4 files changed, 149 insertions(+), 13 deletions(-)
fs/hfs/catalog.c +5 -5
@@ -35,14 +35,14 @@ void hfs_cat_build_key(struct super_block *sb, btree_key *key, u32 parent, conststatic int hfs_cat_build_record (hfs_cat_rec *rec,u32 cnid,struct inode *inode) { -__be32 mtime =hfs_mtime (); +time64_t mtime =inode_get_mtime_sec (inode);memset (rec,0 ,sizeof (*rec));if (S_ISDIR (inode->i_mode)) { rec->type =HFS_CDR_DIR ; rec->dir.DirID =cpu_to_be32 (cnid); - rec->dir.CrDat = mtime; - rec->dir.MdDat = mtime; + rec->dir.CrDat =__hfs_u_to_mtime (mtime); +hfs_cat_write_mtime (rec, mtime); rec->dir.BkDat =0 ; rec->dir.UsrInfo.frView =cpu_to_be16 (0xff );return sizeof (struct hfs_cat_dir );
@@ -53,8 +53,8 @@ static int hfs_cat_build_record(hfs_cat_rec *rec, u32 cnid, struct inode *inode)if (!(inode->i_mode &S_IWUSR )) rec->file.Flags |=HFS_FIL_LOCK ; rec->file.FlNum =cpu_to_be32 (cnid); - rec->file.CrDat = mtime; - rec->file.MdDat = mtime; + rec->file.CrDat =__hfs_u_to_mtime (mtime); +hfs_cat_write_mtime (rec, mtime); rec->file.BkDat =0 ; rec->file.UsrWds.fdType =HFS_SB (inode->i_sb)->s_type; rec->file.UsrWds.fdCreator =HFS_SB (inode->i_sb)->s_creator;
fs/hfs/hfs_fs.h +56 -2
73 lines not on this page · raw message
fs/hfs/inode.c +83 -5
129 lines not on this page · raw message
fs/hfs/super.c +5 -1
@@ -348,7 +348,11 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) sb->s_time_gran =NSEC_PER_SEC ;/* on-disk timestamps are in local time */ sb->s_time_min =HFS_MIN_TIMESTAMP_SECS + sys_tz.tz_minuteswest *60 ; - sb->s_time_max =HFS_MAX_TIMESTAMP_SECS + sys_tz.tz_minuteswest *60 ; +/* +* Timestamps after HFS_MAX_TIMESTAMP_SECS are stored in +* the spare bytes of the catalog record. +*/ + sb->s_time_max =HFS_EXT_MAX_TIMESTAMP_SECS ;mutex_init (&sbi->mdb_lock);mutex_init (&sbi->bitmap_lock);
Signature · 1 line
2.53.0
Viacheslav Dubeyko
maintainerPATCH 3/3 hfs: add KUnit tests for date/time conversion
In reply to Viacheslav DubeykoAdd KUnit tests for the HFS date/time conversion logic:
(1) the time range constants and the location of the spare
bytes in the catalog records;
(2) conversion of on-disk (1904-based) timestamps into
1970-based ones and vice versa;
(3) clamping of the timestamps out of the supported range
instead of wrapping;(4) round trip of the timestamps from the supported range;
(5) conversion and validation of the extended 64-bit timestamps;
(6) storing the timestamps after February 2040 in the spare bytes of the file and directory records without touching other fields;
(7) removing the stale extended value if the timestamp becomes
representable by MdDat again;
(8) ignoring the extended value if another implementation
has updated MdDat;(9) the mark without the valid extended value;
(10) preserving the spare bytes of another implementation.
HFS keeps timestamps in local time. So, the expected values are shifted by the local time zone offset.
--- fs/hfs/Makefile | 2 +- fs/hfs/hfs_time_test.c | 400 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 401 insertions(+), 1 deletion(-) create mode 100644 fs/hfs/hfs_time_test.c
fs/hfs/Makefile +1 -1
@@ -10,4 +10,4 @@ hfs-objs := bitmap.o bfind.o bnode.o brec.o btree.o \ part_tbl.o string.o super.o sysdep.o trans.o # KUnit tests -obj-$(CONFIG_HFS_KUNIT_TEST) += string_test.o +obj-$(CONFIG_HFS_KUNIT_TEST) += string_test.o hfs_time_test.o
fs/hfs/hfs_time_test.c new file +400 -0
400 lines not on this page · raw message
Signature · 1 line
2.53.0