[09/16] netfilter: nft_meta: use 64-bit time arithmetic

Message ID 20191108213257.3097633-10-arnd@arndb.de
State Accepted
Commit 6408c40c39d8eee5caaf97f5219b7dd4e041cc59
Headers show
Series
  • [01/16] staging: exfat: use prandom_u32() for i_generation
Related show

Commit Message

Arnd Bergmann Nov. 8, 2019, 9:32 p.m.
On 32-bit architectures, get_seconds() returns an unsigned 32-bit
time value, which also matches the type used in the nft_meta
code. This will not overflow in year 2038 as a time_t would, but
it still suffers from the overflow problem later on in year 2106.

Change this instance to use the time64_t type consistently
and avoid the deprecated get_seconds().

The nft_meta_weekday() calculation potentially gets a little slower
on 32-bit architectures, but now it has the same behavior as on
64-bit architectures and does not overflow.

Fixes: 63d10e12b00d ("netfilter: nft_meta: support for time matching")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>

---
 net/netfilter/nft_meta.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

-- 
2.20.0

Comments

Phil Sutter Nov. 9, 2019, 11:20 a.m. | #1
On Fri, Nov 08, 2019 at 10:32:47PM +0100, Arnd Bergmann wrote:
> On 32-bit architectures, get_seconds() returns an unsigned 32-bit

> time value, which also matches the type used in the nft_meta

> code. This will not overflow in year 2038 as a time_t would, but

> it still suffers from the overflow problem later on in year 2106.


I wonder if the assumption that people will still use nft_meta 80 years
from now is an optimistic or pessimistic one. :)

> Change this instance to use the time64_t type consistently

> and avoid the deprecated get_seconds().

> 

> The nft_meta_weekday() calculation potentially gets a little slower

> on 32-bit architectures, but now it has the same behavior as on

> 64-bit architectures and does not overflow.

> 

> Fixes: 63d10e12b00d ("netfilter: nft_meta: support for time matching")

> Signed-off-by: Arnd Bergmann <arnd@arndb.de>


Acked-by: Phil Sutter <phil@nwl.cc>
Pablo Neira Ayuso Nov. 15, 2019, 10:44 p.m. | #2
On Fri, Nov 08, 2019 at 10:32:47PM +0100, Arnd Bergmann wrote:
> On 32-bit architectures, get_seconds() returns an unsigned 32-bit

> time value, which also matches the type used in the nft_meta

> code. This will not overflow in year 2038 as a time_t would, but

> it still suffers from the overflow problem later on in year 2106.

> 

> Change this instance to use the time64_t type consistently

> and avoid the deprecated get_seconds().

> 

> The nft_meta_weekday() calculation potentially gets a little slower

> on 32-bit architectures, but now it has the same behavior as on

> 64-bit architectures and does not overflow.


Applied, thanks Arnd.

Patch

diff --git a/net/netfilter/nft_meta.c b/net/netfilter/nft_meta.c
index 317e3a9e8c5b..dda1e55d5801 100644
--- a/net/netfilter/nft_meta.c
+++ b/net/netfilter/nft_meta.c
@@ -33,19 +33,19 @@ 
 
 static DEFINE_PER_CPU(struct rnd_state, nft_prandom_state);
 
-static u8 nft_meta_weekday(unsigned long secs)
+static u8 nft_meta_weekday(time64_t secs)
 {
 	unsigned int dse;
 	u8 wday;
 
 	secs -= NFT_META_SECS_PER_MINUTE * sys_tz.tz_minuteswest;
-	dse = secs / NFT_META_SECS_PER_DAY;
+	dse = div_u64(secs, NFT_META_SECS_PER_DAY);
 	wday = (4 + dse) % NFT_META_DAYS_PER_WEEK;
 
 	return wday;
 }
 
-static u32 nft_meta_hour(unsigned long secs)
+static u32 nft_meta_hour(time64_t secs)
 {
 	struct tm tm;
 
@@ -250,10 +250,10 @@  void nft_meta_get_eval(const struct nft_expr *expr,
 		nft_reg_store64(dest, ktime_get_real_ns());
 		break;
 	case NFT_META_TIME_DAY:
-		nft_reg_store8(dest, nft_meta_weekday(get_seconds()));
+		nft_reg_store8(dest, nft_meta_weekday(ktime_get_real_seconds()));
 		break;
 	case NFT_META_TIME_HOUR:
-		*dest = nft_meta_hour(get_seconds());
+		*dest = nft_meta_hour(ktime_get_real_seconds());
 		break;
 	default:
 		WARN_ON(1);