stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jiri Slaby <jslaby@suse.cz>
To: stable@vger.kernel.org
Cc: David Turner <novalis@novalis.org>, Theodore Ts'o <tytso@mit.edu>,
	Jiri Slaby <jslaby@suse.cz>
Subject: [patch added to 3.12-stable] ext4: Fix handling of extended tv_sec
Date: Mon, 15 Feb 2016 17:12:36 +0100	[thread overview]
Message-ID: <1455552767-7413-8-git-send-email-jslaby@suse.cz> (raw)
In-Reply-To: <1455552767-7413-1-git-send-email-jslaby@suse.cz>

From: David Turner <novalis@novalis.org>

This patch has been added to the 3.12 stable tree. If you have any
objections, please let us know.

===============

commit a4dad1ae24f850410c4e60f22823cba1289b8d52 upstream.

In ext4, the bottom two bits of {a,c,m}time_extra are used to extend
the {a,c,m}time fields, deferring the year 2038 problem to the year
2446.

When decoding these extended fields, for times whose bottom 32 bits
would represent a negative number, sign extension causes the 64-bit
extended timestamp to be negative as well, which is not what's
intended.  This patch corrects that issue, so that the only negative
{a,c,m}times are those between 1901 and 1970 (as per 32-bit signed
timestamps).

Some older kernels might have written pre-1970 dates with 1,1 in the
extra bits.  This patch treats those incorrectly-encoded dates as
pre-1970, instead of post-2311, until kernel 4.20 is released.
Hopefully by then e2fsck will have fixed up the bad data.

Also add a comment explaining the encoding of ext4's extra {a,c,m}time
bits.

Signed-off-by: David Turner <novalis@novalis.org>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Reported-by: Mark Harris <mh8928@yahoo.com>
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=23732
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
---
 fs/ext4/ext4.h | 51 ++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 44 insertions(+), 7 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 29c4e30bf4ca..11c7cb060a55 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -26,6 +26,7 @@
 #include <linux/seqlock.h>
 #include <linux/mutex.h>
 #include <linux/timer.h>
+#include <linux/version.h>
 #include <linux/wait.h>
 #include <linux/blockgroup_lock.h>
 #include <linux/percpu_counter.h>
@@ -723,19 +724,55 @@ struct move_extent {
 	<= (EXT4_GOOD_OLD_INODE_SIZE +			\
 	    (einode)->i_extra_isize))			\
 
+/*
+ * We use an encoding that preserves the times for extra epoch "00":
+ *
+ * extra  msb of                         adjust for signed
+ * epoch  32-bit                         32-bit tv_sec to
+ * bits   time    decoded 64-bit tv_sec  64-bit tv_sec      valid time range
+ * 0 0    1    -0x80000000..-0x00000001  0x000000000 1901-12-13..1969-12-31
+ * 0 0    0    0x000000000..0x07fffffff  0x000000000 1970-01-01..2038-01-19
+ * 0 1    1    0x080000000..0x0ffffffff  0x100000000 2038-01-19..2106-02-07
+ * 0 1    0    0x100000000..0x17fffffff  0x100000000 2106-02-07..2174-02-25
+ * 1 0    1    0x180000000..0x1ffffffff  0x200000000 2174-02-25..2242-03-16
+ * 1 0    0    0x200000000..0x27fffffff  0x200000000 2242-03-16..2310-04-04
+ * 1 1    1    0x280000000..0x2ffffffff  0x300000000 2310-04-04..2378-04-22
+ * 1 1    0    0x300000000..0x37fffffff  0x300000000 2378-04-22..2446-05-10
+ *
+ * Note that previous versions of the kernel on 64-bit systems would
+ * incorrectly use extra epoch bits 1,1 for dates between 1901 and
+ * 1970.  e2fsck will correct this, assuming that it is run on the
+ * affected filesystem before 2242.
+ */
+
 static inline __le32 ext4_encode_extra_time(struct timespec *time)
 {
-       return cpu_to_le32((sizeof(time->tv_sec) > 4 ?
-			   (time->tv_sec >> 32) & EXT4_EPOCH_MASK : 0) |
-                          ((time->tv_nsec << EXT4_EPOCH_BITS) & EXT4_NSEC_MASK));
+	u32 extra = sizeof(time->tv_sec) > 4 ?
+		((time->tv_sec - (s32)time->tv_sec) >> 32) & EXT4_EPOCH_MASK : 0;
+	return cpu_to_le32(extra | (time->tv_nsec << EXT4_EPOCH_BITS));
 }
 
 static inline void ext4_decode_extra_time(struct timespec *time, __le32 extra)
 {
-       if (sizeof(time->tv_sec) > 4)
-	       time->tv_sec |= (__u64)(le32_to_cpu(extra) & EXT4_EPOCH_MASK)
-			       << 32;
-       time->tv_nsec = (le32_to_cpu(extra) & EXT4_NSEC_MASK) >> EXT4_EPOCH_BITS;
+	if (unlikely(sizeof(time->tv_sec) > 4 &&
+			(extra & cpu_to_le32(EXT4_EPOCH_MASK)))) {
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
+		/* Handle legacy encoding of pre-1970 dates with epoch
+		 * bits 1,1.  We assume that by kernel version 4.20,
+		 * everyone will have run fsck over the affected
+		 * filesystems to correct the problem.  (This
+		 * backwards compatibility may be removed before this
+		 * time, at the discretion of the ext4 developers.)
+		 */
+		u64 extra_bits = le32_to_cpu(extra) & EXT4_EPOCH_MASK;
+		if (extra_bits == 3 && ((time->tv_sec) & 0x80000000) != 0)
+			extra_bits = 0;
+		time->tv_sec += extra_bits << 32;
+#else
+		time->tv_sec += (u64)(le32_to_cpu(extra) & EXT4_EPOCH_MASK) << 32;
+#endif
+	}
+	time->tv_nsec = (le32_to_cpu(extra) & EXT4_NSEC_MASK) >> EXT4_EPOCH_BITS;
 }
 
 #define EXT4_INODE_SET_XTIME(xtime, inode, raw_inode)			       \
-- 
2.7.1


  parent reply	other threads:[~2016-02-15 16:12 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-15 16:12 [patch added to 3.12-stable] USB: serial: ftdi_sio: add support for Yaesu SCU-18 cable Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] USB: cp210x: add ID for IAI USB to RS485 adaptor Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] USB: serial: option: Adding support for Telit LE922 Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] USB: option: fix Cinterion AHxx enumeration Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] tty: Fix GPF in flush_to_ldisc() Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] tty: Fix unsafe ldisc reference via ioctl(TIOCGETD) Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] xhci: fix usb2 resume timing and races Jiri Slaby
2016-02-15 16:12 ` Jiri Slaby [this message]
2016-02-15 16:12 ` [patch added to 3.12-stable] crypto: af_alg - Disallow bind/setkey/... after accept(2) Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] crypto: af_alg - Fix socket double-free when accept fails Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] AHCI: Fix softreset failed issue of Port Multiplier Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] libata: disable forced PORTS_IMPL for >= AHCI 1.3 Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] ahci: Intel DNV device IDs SATA Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] crypto: algif_hash - wait for crypto_ahash_init() to complete Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] EVM: Use crypto_memneq() for digest comparisons Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] crypto: user - lock crypto_alg_list on alg dump Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] FS-Cache: Increase reference of parent after registering, netfs success Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] FS-Cache: Don't override netfs's primary_index if registering failed Jiri Slaby
2016-02-15 16:12 ` [patch added to 3.12-stable] binfmt_elf: Don't clobber passed executable's file header Jiri Slaby

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1455552767-7413-8-git-send-email-jslaby@suse.cz \
    --to=jslaby@suse.cz \
    --cc=novalis@novalis.org \
    --cc=stable@vger.kernel.org \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).