Skip to content

Commit acc738f

Browse files
Jan Engelhardtkaber
authored andcommitted
netfilter: xtables: avoid pointer to self
Commit 7845447 (netfilter: iptables: lock free counters) broke a number of modules whose rule data referenced itself. A reallocation would not reestablish the correct references, so it is best to use a separate struct that does not fall under RCU. Signed-off-by: Jan Engelhardt <[email protected]> Signed-off-by: Patrick McHardy <[email protected]>
1 parent 95ba434 commit acc738f

File tree

6 files changed

+88
-31
lines changed

6 files changed

+88
-31
lines changed

include/linux/netfilter/xt_limit.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44
/* timings are in milliseconds. */
55
#define XT_LIMIT_SCALE 10000
66

7+
struct xt_limit_priv;
8+
79
/* 1/10,000 sec period => max of 10,000/sec. Min rate is then 429490
810
seconds, or one every 59 hours. */
911
struct xt_rateinfo {
1012
u_int32_t avg; /* Average secs between packets * scale */
1113
u_int32_t burst; /* Period multiplier for upper limit. */
1214

1315
/* Used internally by the kernel */
14-
unsigned long prev;
15-
u_int32_t credit;
16+
unsigned long prev; /* moved to xt_limit_priv */
17+
u_int32_t credit; /* moved to xt_limit_priv */
1618
u_int32_t credit_cap, cost;
1719

18-
/* Ugly, ugly fucker. */
19-
struct xt_rateinfo *master;
20+
struct xt_limit_priv *master;
2021
};
2122
#endif /*_XT_RATE_H*/

include/linux/netfilter/xt_quota.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ enum xt_quota_flags {
66
};
77
#define XT_QUOTA_MASK 0x1
88

9+
struct xt_quota_priv;
10+
911
struct xt_quota_info {
1012
u_int32_t flags;
1113
u_int32_t pad;
1214

1315
/* Used internally by the kernel */
1416
aligned_u64 quota;
15-
struct xt_quota_info *master;
17+
struct xt_quota_priv *master;
1618
};
1719

1820
#endif /* _XT_QUOTA_H */

include/linux/netfilter/xt_statistic.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ enum xt_statistic_flags {
1313
};
1414
#define XT_STATISTIC_MASK 0x1
1515

16+
struct xt_statistic_priv;
17+
1618
struct xt_statistic_info {
1719
u_int16_t mode;
1820
u_int16_t flags;
@@ -23,11 +25,10 @@ struct xt_statistic_info {
2325
struct {
2426
u_int32_t every;
2527
u_int32_t packet;
26-
/* Used internally by the kernel */
27-
u_int32_t count;
28+
u_int32_t count; /* unused */
2829
} nth;
2930
} u;
30-
struct xt_statistic_info *master __attribute__((aligned(8)));
31+
struct xt_statistic_priv *master __attribute__((aligned(8)));
3132
};
3233

3334
#endif /* _XT_STATISTIC_H */

net/netfilter/xt_limit.c

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#include <linux/netfilter/x_tables.h>
1515
#include <linux/netfilter/xt_limit.h>
1616

17+
struct xt_limit_priv {
18+
unsigned long prev;
19+
uint32_t credit;
20+
};
21+
1722
MODULE_LICENSE("GPL");
1823
MODULE_AUTHOR("Herve Eychenne <[email protected]>");
1924
MODULE_DESCRIPTION("Xtables: rate-limit match");
@@ -60,18 +65,18 @@ static DEFINE_SPINLOCK(limit_lock);
6065
static bool
6166
limit_mt(const struct sk_buff *skb, const struct xt_match_param *par)
6267
{
63-
struct xt_rateinfo *r =
64-
((const struct xt_rateinfo *)par->matchinfo)->master;
68+
const struct xt_rateinfo *r = par->matchinfo;
69+
struct xt_limit_priv *priv = r->master;
6570
unsigned long now = jiffies;
6671

6772
spin_lock_bh(&limit_lock);
68-
r->credit += (now - xchg(&r->prev, now)) * CREDITS_PER_JIFFY;
69-
if (r->credit > r->credit_cap)
70-
r->credit = r->credit_cap;
73+
priv->credit += (now - xchg(&priv->prev, now)) * CREDITS_PER_JIFFY;
74+
if (priv->credit > r->credit_cap)
75+
priv->credit = r->credit_cap;
7176

72-
if (r->credit >= r->cost) {
77+
if (priv->credit >= r->cost) {
7378
/* We're not limited. */
74-
r->credit -= r->cost;
79+
priv->credit -= r->cost;
7580
spin_unlock_bh(&limit_lock);
7681
return true;
7782
}
@@ -95,6 +100,7 @@ user2credits(u_int32_t user)
95100
static bool limit_mt_check(const struct xt_mtchk_param *par)
96101
{
97102
struct xt_rateinfo *r = par->matchinfo;
103+
struct xt_limit_priv *priv;
98104

99105
/* Check for overflow. */
100106
if (r->burst == 0
@@ -104,19 +110,30 @@ static bool limit_mt_check(const struct xt_mtchk_param *par)
104110
return false;
105111
}
106112

107-
/* For SMP, we only want to use one set of counters. */
108-
r->master = r;
113+
priv = kmalloc(sizeof(*priv), GFP_KERNEL);
114+
if (priv == NULL)
115+
return -ENOMEM;
116+
117+
/* For SMP, we only want to use one set of state. */
118+
r->master = priv;
109119
if (r->cost == 0) {
110120
/* User avg in seconds * XT_LIMIT_SCALE: convert to jiffies *
111121
128. */
112-
r->prev = jiffies;
113-
r->credit = user2credits(r->avg * r->burst); /* Credits full. */
122+
priv->prev = jiffies;
123+
priv->credit = user2credits(r->avg * r->burst); /* Credits full. */
114124
r->credit_cap = user2credits(r->avg * r->burst); /* Credits full. */
115125
r->cost = user2credits(r->avg);
116126
}
117127
return true;
118128
}
119129

130+
static void limit_mt_destroy(const struct xt_mtdtor_param *par)
131+
{
132+
const struct xt_rateinfo *info = par->matchinfo;
133+
134+
kfree(info->master);
135+
}
136+
120137
#ifdef CONFIG_COMPAT
121138
struct compat_xt_rateinfo {
122139
u_int32_t avg;
@@ -167,6 +184,7 @@ static struct xt_match limit_mt_reg __read_mostly = {
167184
.family = NFPROTO_UNSPEC,
168185
.match = limit_mt,
169186
.checkentry = limit_mt_check,
187+
.destroy = limit_mt_destroy,
170188
.matchsize = sizeof(struct xt_rateinfo),
171189
#ifdef CONFIG_COMPAT
172190
.compatsize = sizeof(struct compat_xt_rateinfo),

net/netfilter/xt_quota.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <linux/netfilter/x_tables.h>
1010
#include <linux/netfilter/xt_quota.h>
1111

12+
struct xt_quota_priv {
13+
uint64_t quota;
14+
};
15+
1216
MODULE_LICENSE("GPL");
1317
MODULE_AUTHOR("Sam Johnston <[email protected]>");
1418
MODULE_DESCRIPTION("Xtables: countdown quota match");
@@ -20,18 +24,20 @@ static DEFINE_SPINLOCK(quota_lock);
2024
static bool
2125
quota_mt(const struct sk_buff *skb, const struct xt_match_param *par)
2226
{
23-
struct xt_quota_info *q =
24-
((const struct xt_quota_info *)par->matchinfo)->master;
27+
struct xt_quota_info *q = (void *)par->matchinfo;
28+
struct xt_quota_priv *priv = q->master;
2529
bool ret = q->flags & XT_QUOTA_INVERT;
2630

2731
spin_lock_bh(&quota_lock);
28-
if (q->quota >= skb->len) {
29-
q->quota -= skb->len;
32+
if (priv->quota >= skb->len) {
33+
priv->quota -= skb->len;
3034
ret = !ret;
3135
} else {
3236
/* we do not allow even small packets from now on */
33-
q->quota = 0;
37+
priv->quota = 0;
3438
}
39+
/* Copy quota back to matchinfo so that iptables can display it */
40+
q->quota = priv->quota;
3541
spin_unlock_bh(&quota_lock);
3642

3743
return ret;
@@ -43,17 +49,28 @@ static bool quota_mt_check(const struct xt_mtchk_param *par)
4349

4450
if (q->flags & ~XT_QUOTA_MASK)
4551
return false;
46-
/* For SMP, we only want to use one set of counters. */
47-
q->master = q;
52+
53+
q->master = kmalloc(sizeof(*q->master), GFP_KERNEL);
54+
if (q->master == NULL)
55+
return -ENOMEM;
56+
4857
return true;
4958
}
5059

60+
static void quota_mt_destroy(const struct xt_mtdtor_param *par)
61+
{
62+
const struct xt_quota_info *q = par->matchinfo;
63+
64+
kfree(q->master);
65+
}
66+
5167
static struct xt_match quota_mt_reg __read_mostly = {
5268
.name = "quota",
5369
.revision = 0,
5470
.family = NFPROTO_UNSPEC,
5571
.match = quota_mt,
5672
.checkentry = quota_mt_check,
73+
.destroy = quota_mt_destroy,
5774
.matchsize = sizeof(struct xt_quota_info),
5875
.me = THIS_MODULE,
5976
};

net/netfilter/xt_statistic.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#include <linux/netfilter/xt_statistic.h>
1717
#include <linux/netfilter/x_tables.h>
1818

19+
struct xt_statistic_priv {
20+
uint32_t count;
21+
};
22+
1923
MODULE_LICENSE("GPL");
2024
MODULE_AUTHOR("Patrick McHardy <[email protected]>");
2125
MODULE_DESCRIPTION("Xtables: statistics-based matching (\"Nth\", random)");
@@ -27,7 +31,7 @@ static DEFINE_SPINLOCK(nth_lock);
2731
static bool
2832
statistic_mt(const struct sk_buff *skb, const struct xt_match_param *par)
2933
{
30-
struct xt_statistic_info *info = (void *)par->matchinfo;
34+
const struct xt_statistic_info *info = par->matchinfo;
3135
bool ret = info->flags & XT_STATISTIC_INVERT;
3236

3337
switch (info->mode) {
@@ -36,10 +40,9 @@ statistic_mt(const struct sk_buff *skb, const struct xt_match_param *par)
3640
ret = !ret;
3741
break;
3842
case XT_STATISTIC_MODE_NTH:
39-
info = info->master;
4043
spin_lock_bh(&nth_lock);
41-
if (info->u.nth.count++ == info->u.nth.every) {
42-
info->u.nth.count = 0;
44+
if (info->master->count++ == info->u.nth.every) {
45+
info->master->count = 0;
4346
ret = !ret;
4447
}
4548
spin_unlock_bh(&nth_lock);
@@ -56,16 +59,31 @@ static bool statistic_mt_check(const struct xt_mtchk_param *par)
5659
if (info->mode > XT_STATISTIC_MODE_MAX ||
5760
info->flags & ~XT_STATISTIC_MASK)
5861
return false;
59-
info->master = info;
62+
63+
info->master = kzalloc(sizeof(*info->master), GFP_KERNEL);
64+
if (info->master == NULL) {
65+
printk(KERN_ERR KBUILD_MODNAME ": Out of memory\n");
66+
return false;
67+
}
68+
info->master->count = info->u.nth.count;
69+
6070
return true;
6171
}
6272

73+
static void statistic_mt_destroy(const struct xt_mtdtor_param *par)
74+
{
75+
const struct xt_statistic_info *info = par->matchinfo;
76+
77+
kfree(info->master);
78+
}
79+
6380
static struct xt_match xt_statistic_mt_reg __read_mostly = {
6481
.name = "statistic",
6582
.revision = 0,
6683
.family = NFPROTO_UNSPEC,
6784
.match = statistic_mt,
6885
.checkentry = statistic_mt_check,
86+
.destroy = statistic_mt_destroy,
6987
.matchsize = sizeof(struct xt_statistic_info),
7088
.me = THIS_MODULE,
7189
};

0 commit comments

Comments
 (0)