Skip to contentSkip to conversation

ntfs3: fix negative timestamps corrupting stat output

PATCH New koraynilay 1 participant 1 message Nov 16 NTFS3 filesystem

lore
Share & tools
lore

koraynilay

author
#0 lore raw

PATCH v2 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().

Fixes 518013860432 ("fs/ntfs3: Support timestamps prior to epoch")
Assisted-by Gemini:3.8-flash-high antigravity-cli-1.5.7

---
v2 changes:
- fix uninitialized warning and resulting bug, because I had forgotten to test building it and didn't read with enough attention :(

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 | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/ntfs3/ntfs_fs.h +3 -2
index f400cf94d05b..9c77a90566ba 100644
@@ -1083,8 +1083,9 @@ 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;
+	s64 sec = div_s64_rem(t, _100ns2seconds, &t32);
+	s64 nsec = t32 * 100;
+	set_normalized_timespec64(ts, sec, nsec);
 }
 
 static inline struct ntfs_sb_info *ntfs_sb(struct super_block *sb)

base-commit: 0c895b1f6ab37a7c72a437818f307ef6e629051c

Signature · 1 line
2.56.0