mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ntfs3: fix negative timestamps corrupting stat output
@ 2026-11-16 21:20 koraynilay
  0 siblings, 0 replies; only message in thread
From: koraynilay @ 2026-11-16 21:20 UTC (permalink / raw)
  To: Konstantin Komarov, ntfs3; +Cc: linux-kernel, koraynilay

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")
Cc: stable@vger.kernel.org
Assisted-by: Gemini:3.8-flash-high antigravity-cli-1.5.7
Signed-off-by: koraynilay <koray.fra@gmail.com>
---
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/20261115180343.576773-1-koray.fra@gmail.com/T/#u

 fs/ntfs3/ntfs_fs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/ntfs3/ntfs_fs.h b/fs/ntfs3/ntfs_fs.h
index f400cf94d05b..1f3e5d201767 100644
--- a/fs/ntfs3/ntfs_fs.h
+++ b/fs/ntfs3/ntfs_fs.h
@@ -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
-- 
2.56.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-16 21:20 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 21:20 [PATCH] ntfs3: fix negative timestamps corrupting stat output koraynilay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®