LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] fix xt_time_match() never matches on Sundays
@ 2008-03-06 10:53 Andrew Schulman
  0 siblings, 0 replies; only message in thread
From: Andrew Schulman @ 2008-03-06 10:53 UTC (permalink / raw)
  To: linux-kernel

xt_time_match() in net/netfilter/xt_time.c in kernel 2.6.24 never matches on
Sundays.  On my host I have a rule like

    iptables -A OUTPUT -m time --weekdays Sun -j REJECT

and it never matches.  The problem is in localtime_2(), which uses

    r->weekday = (4 + r->dse) % 7;

to map the epoch day onto a weekday in {0,...,6}.  In particular this gives 0
for Sundays.  But 0 has to be wrong; a weekday of 0 can never match.
xt_time_match() has

    if (!(info->weekdays_match & (1 << current_time.weekday)))
        return false;

and when current_time.weekday = 0, the result of the & is always zero, even when
info->weekdays_match = XT_TIME_ALL_WEEKDAYS = 0xFE.

The solution is to map Sunday onto 7, not 0.  Either of the following patches
does it; they're equivalent.  The first is more compact, the second is probably
clearer.  With the patch (#1) installed, I've confirmed on my host that
'iptables -m time --weekdays Sun' matches again.

Andrew.

Only in linux-source-2.6.24/net/netfilter: .#xt_time.c
Only in linux-source-2.6.24/net/netfilter: #xt_time.c#
diff -ur linux-source-2.6.24.orig/net/netfilter/xt_time.c linux-source-2.6.24/net/netfilter/xt_time.c
--- linux-source-2.6.24.orig/net/netfilter/xt_time.c	2008-01-24 17:58:37.000000000 -0500
+++ linux-source-2.6.24/net/netfilter/xt_time.c	2008-03-05 20:01:37.000000000 -0500
@@ -96,7 +96,8 @@
 	r->dse = time / 86400;
 
 	/* 1970-01-01 (w=0) was a Thursday (4). */
-	r->weekday = (4 + r->dse) % 7;
+	/* -1 and +1 map Sunday onto 7, instead of 0. */
+	r->weekday = (4 + r->dse - 1) % 7 + 1;
 }
 
 static void localtime_3(struct xtm *r, time_t time)
diff -ur linux-source-2.6.24.orig/net/netfilter/xt_time.c linux-source-2.6.24/net/netfilter/xt_time.c
--- linux-source-2.6.24.orig/net/netfilter/xt_time.c	2008-01-24 17:58:37.000000000 -0500
+++ linux-source-2.6.24/net/netfilter/xt_time.c	2008-03-05 20:03:15.000000000 -0500
@@ -97,6 +97,9 @@
 
 	/* 1970-01-01 (w=0) was a Thursday (4). */
 	r->weekday = (4 + r->dse) % 7;
+	/* Map Sunday onto 7, not 0. */
+	if (r->weekday == 0)
+		r->weekday = 7;
 }
 
 static void localtime_3(struct xtm *r, time_t time)


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

only message in thread, other threads:[~2008-03-06 11:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-06 10:53 [PATCH] fix xt_time_match() never matches on Sundays Andrew Schulman

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).