ntfs3: fix negative timestamps corrupting stat output
PATCH 1/1 ntfs3: fix negative timestamps corrupting stat output
When reading a timestamp before epoch with sub-second precision, ntfs3 converts it from on-disk NT format to Unix format without normalizing it, leading to the nanoseconds field being used as a negative number. This leads to a garbled stat command output and violates a struct timespec64 invariant, as documented in the set_normalized_timespec64() kernel-doc:
[...]
* Note: The tv_nsec part is always in the range of 0 <= tv_nsec < NSEC_PER_SEC.
* For negative values only the tv_sec field is negative !
*/
Reproducer, with /dev/loop0 formatted as NTFS and mounted at /mnt:
mount -t ntfs3 /dev/loop0 /mnt
touch -d "1969-07-20 20:17:40.123456789" /mnt/test.txt stat -c %y /mnt/test.txt
# remount to evict caches umount /mnt mount -t ntfs3 /dev/loop0 /mnt stat -c %y /mnt/test.txt
which prints (stat output is garbled because of negative value):
1969-07-20 20:17:40.123456700 +0000 (from cache) 1969-07-20 20:17:41.()*+,--00 +0000 (from disk, after remount)
Fix this by using set_normalized_timespec64() in nt2kernel().
---
Small bug found by sashiko[1] while implementing ptime[2].
Thanks.
[1]: https://sashiko.dev/#/message/20261115180343.576773-4-koray.fra%40gmail.com
[2]: https://lore.kernel.org/all/[email protected]/T/#u
fs/ntfs3/ntfs_fs.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/ntfs3/ntfs_fs.h +2 -2
@@ -1083,8 +1083,8 @@ static inline void nt2kernel(const __le64 tm, struct timespec64 *ts)/* use signed 64 bit to support timestamps prior to epoch. xfstest 258. */ s64 t =le64_to_cpu (tm) - _100ns2seconds * SecondsToStartOf1970; - ts->tv_sec =div_s64_rem (t, _100ns2seconds, &t32); - ts->tv_nsec = t32 *100 ; +set_normalized_timespec64 (ts,div_s64_rem (t, _100ns2seconds, &t32), + (s64 )t32 *100 ); }static inline struct ntfs_sb_info *ntfs_sb (struct super_block *sb)
base-commit: 0c895b1f6ab37a7c72a437818f307ef6e629051c
Signature · 1 line
2.56.0