Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by Pablo Neira Ayuso
Fri, 13 Nov 2026 03:12
[PATCH net-next 06/15] netfilter: xt_time: fix monthday computation for dates past 2039
Author: Pablo Neira Ayuso
Date: Fri, 13 Nov 2026 03:12
Date: Fri, 13 Nov 2026 03:12
139 lines
12822 bytes
12822 bytes
From: Fernando Fernandez Mancera <fmancera@suse.de>
iocaltime_3() derives the year from days_since_epoch[], which only has
entries up to 2039. For any date from 2040-01-01 on, the lookup stays on
the 2039 entry, so the day-of-year offset is 365 or more. That value is
used to compute r->monthday, which ends up larger than 31 (32 on
2040-01-01, and it wraps in the u8 later in the year).
Use time64_to_tm() instead of the table walk. It is always built in,
handles any time64_t, and always returns tm_mday in 1-31, so the table
and its 2039 limit go away. The result for 1970-2039 is unchanged.
Only the real-time clock reaches localtime_3(), so this is not
triggerable before the clock passes 2040-01-01 UTC.
This significantly reduces the size of xt_time object.
add/remove: 7/10 grow/shrink: 0/1 up/down: 143/-490 (-347)
Total: Before=1493, After=1146, chg -23.24%
Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
net/netfilter/xt_time.c | 82 +++--------------------------------------
1 file changed, 6 insertions(+), 76 deletions(-)
diff --git a/net/netfilter/xt_time.c b/net/netfilter/xt_time.c
index 2065fce8ef81..f0c4c1d1bc4a 100644
--- a/net/netfilter/xt_time.c
+++ b/net/netfilter/xt_time.c
@@ -13,8 +13,8 @@
#include <linux/ktime.h>
#include <linux/module.h>
-#include <linux/rtc.h>
#include <linux/skbuff.h>
+#include <linux/time.h>
#include <linux/types.h>
#include <linux/netfilter/x_tables.h>
#include <linux/netfilter/xt_time.h>
@@ -31,38 +31,7 @@ struct xtm {
extern struct timezone sys_tz; /* ouch */
-static const u_int16_t days_since_year[] = {
- 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334,
-};
-
-static const u_int16_t days_since_leapyear[] = {
- 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335,
-};
-
-/*
- * Since time progresses forward, it is best to organize this array in reverse,
- * to minimize lookup time.
- */
-enum {
- DSE_FIRST = 2039,
- SECONDS_PER_DAY = 86400,
-};
-static const u_int16_t days_since_epoch[] = {
- /* 2039 - 2030 */
- 25202, 24837, 24472, 24106, 23741, 23376, 23011, 22645, 22280, 21915,
- /* 2029 - 2020 */
- 21550, 21184, 20819, 20454, 20089, 19723, 19358, 18993, 18628, 18262,
- /* 2019 - 2010 */
- 17897, 17532, 17167, 16801, 16436, 16071, 15706, 15340, 14975, 14610,
- /* 2009 - 2000 */
- 14245, 13879, 13514, 13149, 12784, 12418, 12053, 11688, 11323, 10957,
- /* 1999 - 1990 */
- 10592, 10227, 9862, 9496, 9131, 8766, 8401, 8035, 7670, 7305,
- /* 1989 - 1980 */
- 6940, 6574, 6209, 5844, 5479, 5113, 4748, 4383, 4018, 3652,
- /* 1979 - 1970 */
- 3287, 2922, 2557, 2191, 1826, 1461, 1096, 730, 365, 0,
-};
+#define SECONDS_PER_DAY 86400
/*
* Each network packet has a (nano)seconds-since-the-epoch (SSTE) timestamp.
@@ -102,51 +71,12 @@ static inline void localtime_2(struct xtm *r, time64_t time)
static void localtime_3(struct xtm *r, time64_t time)
{
- unsigned int year, i, w = r->dse;
-
- /*
- * In each year, a certain number of days-since-the-epoch have passed.
- * Find the year that is closest to said days.
- *
- * Consider, for example, w=21612 (2029-03-04). Loop will abort on
- * dse[i] <= w, which happens when dse[i] == 21550. This implies
- * year == 2009. w will then be 62.
- */
- for (i = 0, year = DSE_FIRST; days_since_epoch[i] > w;
- ++i, --year)
- /* just loop */;
-
- w -= days_since_epoch[i];
+ struct tm tm;
- /*
- * By now we have the current year, and the day of the year.
- * r->yearday = w;
- *
- * On to finding the month (like above). In each month, a certain
- * number of days-since-New Year have passed, and find the closest
- * one.
- *
- * Consider w=62 (in a non-leap year). Loop will abort on
- * dsy[i] < w, which happens when dsy[i] == 31+28 (i == 2).
- * Concludes i == 2, i.e. 3rd month => March.
- *
- * (A different approach to use would be to subtract a monthlength
- * from w repeatedly while counting.)
- */
- if (is_leap_year(year)) {
- /* use days_since_leapyear[] in a leap year */
- for (i = ARRAY_SIZE(days_since_leapyear) - 1;
- i > 0 && days_since_leapyear[i] > w; --i)
- /* just loop */;
- r->monthday = w - days_since_leapyear[i] + 1;
- } else {
- for (i = ARRAY_SIZE(days_since_year) - 1;
- i > 0 && days_since_year[i] > w; --i)
- /* just loop */;
- r->monthday = w - days_since_year[i] + 1;
- }
+ time64_to_tm(time, 0, &tm);
- r->month = i + 1;
+ r->monthday = tm.tm_mday; /* 1-31 */
+ r->month = tm.tm_mon + 1; /* 1-12 */
}
static bool
--
2.47.3
Re: [PATCH net-next 06/15] netfilter: xt_time: fix monthday computation for dates past 2039
Author: netdev-bot+sashiko
Date: Sat, 14 Nov 2026 03:23
Date: Sat, 14 Nov 2026 03:23
126 lines
12351 bytes
12351 bytes
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit "netfilter: xt_time: fix monthday computation for
dates past 2039" fixes a long-standing bug but has no `Fixes:` tag andβ¦
- [Low] Two comments in net/netfilter/xt_time.c no longer match the code
after this patch.
--
netfilter: xt_time: fix monthday computation for dates past 2039
This replaces the days_since_epoch[] table walk in localtime_3() with
time64_to_tm(). With the change, r->monthday stays in the 1-31 range for
dates from 2040-01-01 on. Removing the lookup tables also shrinks the
xt_time object.
> netfilter: xt_time: fix monthday computation for dates past 2039
>
> iocaltime_3() derives the year from days_since_epoch[], which only has
> entries up to 2039. For any date from 2040-01-01 on, the lookup stays on
> the 2039 entry, so the day-of-year offset is 365 or more. That value is
> used to compute r->monthday, which ends up larger than 31 (32 on
> 2040-01-01, and it wraps in the u8 later in the year).
[ ... ]
> Only the real-time clock reaches localtime_3(), so this is not
> triggerable before the clock passes 2040-01-01 UTC.
[Severity: Low]
This isn't a bug, but there's a typo in the first paragraph:
iocaltime_3() should be localtime_3().
Should the first paragraph also say what the out-of-range value leads to?
time_mt() uses it as a shift count:
if (!(info->monthdays_match & (1U << current_time.monthday)))
return false;
On 2040-01-01 monthday is 32. Later in 2040 it wraps in the u8 (for
example, 397 becomes 141). A shift of 32 or more is undefined behaviour,
so UBSAN would report shift-out-of-bounds. Monthday rules would then
match or miss arbitrary days.
Is the "not triggerable before ... 2040-01-01 UTC" statement accurate?
A CAP_SYS_TIME holder can set CLOCK_REALTIME past 2040 today with
settimeofday() or clock_settime(). A bad RTC or NTP source can do the
same.
There is also the XT_TIME_LOCAL_TZ adjustment in time_mt():
if (info->flags & XT_TIME_LOCAL_TZ)
/* Adjust for local timezone */
stamp -= 60 * sys_tz.tz_minuteswest;
kernel/time/time.c limits tz_minuteswest to +/-15*60. East of UTC, the
local stamp can therefore reach 2040 up to 15 hours before UTC does.
The patch also has no Fixes: tag. days_since_epoch[], the
DSE_FIRST = 2039 year walk and the monthday computation all come from
ee4411a1b1e0. Should this carry:
Fixes: ee4411a1b1e0 ("[NETFILTER]: x_tables: add xt_time match")
Cc: stable@vger.kernel.org
That would let stable and long-term kernels that will still run in 2040,
such as CIP SLTS, pick up the fix. Other fixes in the same netfilter
batch carry Fixes: tags, for example "netfilter: flowtable: do not clear
the offload bit twice" and "netfilter: nfnetlink_queue: make skb
writable before writing NFQA_L2HDR".
> diff --git a/net/netfilter/xt_time.c b/net/netfilter/xt_time.c
> index 2065fce8ef81b..f0c4c1d1bc4a0 100644
> --- a/net/netfilter/xt_time.c
> +++ b/net/netfilter/xt_time.c
[ ... ]
> @@ -102,51 +71,12 @@ static inline void localtime_2(struct xtm *r, time64_t time)
>
> static void localtime_3(struct xtm *r, time64_t time)
> {
> - unsigned int year, i, w = r->dse;
> -
> - /*
> - * In each year, a certain number of days-since-the-epoch have passed.
> - * Find the year that is closest to said days.
[ ... ]
> + time64_to_tm(time, 0, &tm);
>
> - r->month = i + 1;
> + r->monthday = tm.tm_mday; /* 1-31 */
> + r->month = tm.tm_mon + 1; /* 1-12 */
> }
[Severity: Low]
The dietlibc-derived tables and table walk are gone now. Does the file
header comment still match the code?
* It is using some modified code from dietlibc (localtime() function)
* that you can find at https://www.fefe.de/dietlibc/
Date conversion now comes from time64_to_tm() in kernel/time/timeconv.c.
The comment in localtime_2() still says it handles monthday:
/*
* Here comes the rest (weekday, monthday). First, divide the SSTE
* by seconds-per-day to get the number of _days_ since the epoch.
*/
But localtime_2() only sets r->dse and r->weekday. That comment was
already inaccurate before this patch. Now localtime_3() doesn't read
r->dse at all, so the mismatch is larger.
Could both comments be updated to match the new code?
--
Sashiko AI review Β· https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261113031251.2437074-2-pablo%40netfilter.org
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads