Skip to content

Commit a357b3f

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nat: add dependencies on conntrack module
MASQUERADE, S/DNAT and REDIRECT already call functions that depend on the conntrack module. However, since the conntrack hooks are now registered in a lazy fashion (i.e., only when needed) a symbol reference is not enough. Thus, when something is added to a nat table, make sure that it will see packets by calling nf_ct_netns_get() which will register the conntrack hooks in the current netns. An alternative would be to add these dependencies to the NAT table. However, that has problems when using non-modular builds -- we might register e.g. ipv6 conntrack before its initcall has run, leading to NULL deref crashes since its per-netns storage has not yet been allocated. Adding the dependency in the modules instead has the advantage that nat table also does not register its hooks until rules are added. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent ecb2421 commit a357b3f

4 files changed

Lines changed: 43 additions & 6 deletions

File tree

‎net/ipv4/netfilter/ipt_MASQUERADE.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static int masquerade_tg_check(const struct xt_tgchk_param *par)
4141
pr_debug("bad rangesize %u\n", mr->rangesize);
4242
return -EINVAL;
4343
}
44-
return 0;
44+
return nf_ct_netns_get(par->net, par->family);
4545
}
4646

4747
static unsigned int
@@ -59,6 +59,11 @@ masquerade_tg(struct sk_buff *skb, const struct xt_action_param *par)
5959
xt_out(par));
6060
}
6161

62+
static void masquerade_tg_destroy(const struct xt_tgdtor_param *par)
63+
{
64+
nf_ct_netns_put(par->net, par->family);
65+
}
66+
6267
static struct xt_target masquerade_tg_reg __read_mostly = {
6368
.name = "MASQUERADE",
6469
.family = NFPROTO_IPV4,
@@ -67,6 +72,7 @@ static struct xt_target masquerade_tg_reg __read_mostly = {
6772
.table = "nat",
6873
.hooks = 1 << NF_INET_POST_ROUTING,
6974
.checkentry = masquerade_tg_check,
75+
.destroy = masquerade_tg_destroy,
7076
.me = THIS_MODULE,
7177
};
7278

‎net/netfilter/xt_NETMAP.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ static int netmap_tg6_checkentry(const struct xt_tgchk_param *par)
6060

6161
if (!(range->flags & NF_NAT_RANGE_MAP_IPS))
6262
return -EINVAL;
63-
return 0;
63+
return nf_ct_netns_get(par->net, par->family);
64+
}
65+
66+
static void netmap_tg_destroy(const struct xt_tgdtor_param *par)
67+
{
68+
nf_ct_netns_put(par->net, par->family);
6469
}
6570

6671
static unsigned int
@@ -111,7 +116,7 @@ static int netmap_tg4_check(const struct xt_tgchk_param *par)
111116
pr_debug("bad rangesize %u.\n", mr->rangesize);
112117
return -EINVAL;
113118
}
114-
return 0;
119+
return nf_ct_netns_get(par->net, par->family);
115120
}
116121

117122
static struct xt_target netmap_tg_reg[] __read_mostly = {
@@ -127,6 +132,7 @@ static struct xt_target netmap_tg_reg[] __read_mostly = {
127132
(1 << NF_INET_LOCAL_OUT) |
128133
(1 << NF_INET_LOCAL_IN),
129134
.checkentry = netmap_tg6_checkentry,
135+
.destroy = netmap_tg_destroy,
130136
.me = THIS_MODULE,
131137
},
132138
{
@@ -141,6 +147,7 @@ static struct xt_target netmap_tg_reg[] __read_mostly = {
141147
(1 << NF_INET_LOCAL_OUT) |
142148
(1 << NF_INET_LOCAL_IN),
143149
.checkentry = netmap_tg4_check,
150+
.destroy = netmap_tg_destroy,
144151
.me = THIS_MODULE,
145152
},
146153
};

‎net/netfilter/xt_REDIRECT.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ static int redirect_tg6_checkentry(const struct xt_tgchk_param *par)
4040

4141
if (range->flags & NF_NAT_RANGE_MAP_IPS)
4242
return -EINVAL;
43-
return 0;
43+
44+
return nf_ct_netns_get(par->net, par->family);
45+
}
46+
47+
static void redirect_tg_destroy(const struct xt_tgdtor_param *par)
48+
{
49+
nf_ct_netns_put(par->net, par->family);
4450
}
4551

4652
/* FIXME: Take multiple ranges --RR */
@@ -56,7 +62,7 @@ static int redirect_tg4_check(const struct xt_tgchk_param *par)
5662
pr_debug("bad rangesize %u.\n", mr->rangesize);
5763
return -EINVAL;
5864
}
59-
return 0;
65+
return nf_ct_netns_get(par->net, par->family);
6066
}
6167

6268
static unsigned int
@@ -72,6 +78,7 @@ static struct xt_target redirect_tg_reg[] __read_mostly = {
7278
.revision = 0,
7379
.table = "nat",
7480
.checkentry = redirect_tg6_checkentry,
81+
.destroy = redirect_tg_destroy,
7582
.target = redirect_tg6,
7683
.targetsize = sizeof(struct nf_nat_range),
7784
.hooks = (1 << NF_INET_PRE_ROUTING) |
@@ -85,6 +92,7 @@ static struct xt_target redirect_tg_reg[] __read_mostly = {
8592
.table = "nat",
8693
.target = redirect_tg4,
8794
.checkentry = redirect_tg4_check,
95+
.destroy = redirect_tg_destroy,
8896
.targetsize = sizeof(struct nf_nat_ipv4_multi_range_compat),
8997
.hooks = (1 << NF_INET_PRE_ROUTING) |
9098
(1 << NF_INET_LOCAL_OUT),

‎net/netfilter/xt_nat.c‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,17 @@ static int xt_nat_checkentry_v0(const struct xt_tgchk_param *par)
2323
par->target->name);
2424
return -EINVAL;
2525
}
26-
return 0;
26+
return nf_ct_netns_get(par->net, par->family);
27+
}
28+
29+
static int xt_nat_checkentry(const struct xt_tgchk_param *par)
30+
{
31+
return nf_ct_netns_get(par->net, par->family);
32+
}
33+
34+
static void xt_nat_destroy(const struct xt_tgdtor_param *par)
35+
{
36+
nf_ct_netns_put(par->net, par->family);
2737
}
2838

2939
static void xt_nat_convert_range(struct nf_nat_range *dst,
@@ -106,6 +116,7 @@ static struct xt_target xt_nat_target_reg[] __read_mostly = {
106116
.name = "SNAT",
107117
.revision = 0,
108118
.checkentry = xt_nat_checkentry_v0,
119+
.destroy = xt_nat_destroy,
109120
.target = xt_snat_target_v0,
110121
.targetsize = sizeof(struct nf_nat_ipv4_multi_range_compat),
111122
.family = NFPROTO_IPV4,
@@ -118,6 +129,7 @@ static struct xt_target xt_nat_target_reg[] __read_mostly = {
118129
.name = "DNAT",
119130
.revision = 0,
120131
.checkentry = xt_nat_checkentry_v0,
132+
.destroy = xt_nat_destroy,
121133
.target = xt_dnat_target_v0,
122134
.targetsize = sizeof(struct nf_nat_ipv4_multi_range_compat),
123135
.family = NFPROTO_IPV4,
@@ -129,6 +141,8 @@ static struct xt_target xt_nat_target_reg[] __read_mostly = {
129141
{
130142
.name = "SNAT",
131143
.revision = 1,
144+
.checkentry = xt_nat_checkentry,
145+
.destroy = xt_nat_destroy,
132146
.target = xt_snat_target_v1,
133147
.targetsize = sizeof(struct nf_nat_range),
134148
.table = "nat",
@@ -139,6 +153,8 @@ static struct xt_target xt_nat_target_reg[] __read_mostly = {
139153
{
140154
.name = "DNAT",
141155
.revision = 1,
156+
.checkentry = xt_nat_checkentry,
157+
.destroy = xt_nat_destroy,
142158
.target = xt_dnat_target_v1,
143159
.targetsize = sizeof(struct nf_nat_range),
144160
.table = "nat",

0 commit comments

Comments
 (0)