Skip to content

Commit 1e13f27

Browse files
angrqFlorian Westphal
authored andcommitted
netfilter: nft_counter: fix reset of counters on 32bit archs
nft_counter_reset() calls u64_stats_add() with a negative value to reset the counter. This will work on 64bit archs, hence the negative value added will wrap as a 64bit value which then can wrap the stat counter as well. On 32bit archs, the added negative value will wrap as a 32bit value and _not_ wrapping the stat counter properly. In most cases, this would just lead to a very large 32bit value being added to the stat counter. Fix by introducing u64_stats_sub(). Fixes: 4a1d3ac ("netfilter: nft_counter: Use u64_stats_t for statistic.") Signed-off-by: Anders Grahn <anders.grahn@gmail.com> Signed-off-by: Florian Westphal <fw@strlen.de>
1 parent 2f635ad commit 1e13f27

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

include/linux/u64_stats_sync.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ static inline void u64_stats_add(u64_stats_t *p, unsigned long val)
9797
local64_add(val, &p->v);
9898
}
9999

100+
static inline void u64_stats_sub(u64_stats_t *p, s64 val)
101+
{
102+
local64_sub(val, &p->v);
103+
}
104+
100105
static inline void u64_stats_inc(u64_stats_t *p)
101106
{
102107
local64_inc(&p->v);
@@ -145,6 +150,11 @@ static inline void u64_stats_add(u64_stats_t *p, unsigned long val)
145150
p->v += val;
146151
}
147152

153+
static inline void u64_stats_sub(u64_stats_t *p, s64 val)
154+
{
155+
p->v -= val;
156+
}
157+
148158
static inline void u64_stats_inc(u64_stats_t *p)
149159
{
150160
p->v++;

net/netfilter/nft_counter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ static void nft_counter_reset(struct nft_counter_percpu_priv *priv,
117117
nft_sync = this_cpu_ptr(&nft_counter_sync);
118118

119119
u64_stats_update_begin(nft_sync);
120-
u64_stats_add(&this_cpu->packets, -total->packets);
121-
u64_stats_add(&this_cpu->bytes, -total->bytes);
120+
u64_stats_sub(&this_cpu->packets, total->packets);
121+
u64_stats_sub(&this_cpu->bytes, total->bytes);
122122
u64_stats_update_end(nft_sync);
123123

124124
local_bh_enable();

0 commit comments

Comments
 (0)