Commit 3c158f7f authored by Patrick McHarrdy's avatar Patrick McHarrdy Committed by David S. Miller

[NETFILTER]: nf_conntrack: fix helper module unload races

When a helper module is unloaded all conntracks refering to it have their
helper pointer NULLed out, leading to lots of races. In most places this
can be fixed by proper use of RCU (they do already check for != NULL,
but in a racy way), additionally nf_conntrack_expect_related needs to
bail out when no helper is present.

Also remove two paranoid BUG_ONs in nf_conntrack_proto_gre that are racy
and not worth fixing.
Signed-off-by: default avatarPatrick McHarrdy <kaber@trash.net>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 51055be8
...@@ -133,6 +133,7 @@ static unsigned int ipv4_conntrack_help(unsigned int hooknum, ...@@ -133,6 +133,7 @@ static unsigned int ipv4_conntrack_help(unsigned int hooknum,
struct nf_conn *ct; struct nf_conn *ct;
enum ip_conntrack_info ctinfo; enum ip_conntrack_info ctinfo;
struct nf_conn_help *help; struct nf_conn_help *help;
struct nf_conntrack_helper *helper;
/* This is where we call the helper: as the packet goes out. */ /* This is where we call the helper: as the packet goes out. */
ct = nf_ct_get(*pskb, &ctinfo); ct = nf_ct_get(*pskb, &ctinfo);
...@@ -140,11 +141,13 @@ static unsigned int ipv4_conntrack_help(unsigned int hooknum, ...@@ -140,11 +141,13 @@ static unsigned int ipv4_conntrack_help(unsigned int hooknum,
return NF_ACCEPT; return NF_ACCEPT;
help = nfct_help(ct); help = nfct_help(ct);
if (!help || !help->helper) if (!help)
return NF_ACCEPT; return NF_ACCEPT;
/* rcu_read_lock()ed by nf_hook_slow */
return help->helper->help(pskb, helper = rcu_dereference(help->helper);
skb_network_offset(*pskb) + ip_hdrlen(*pskb), if (!helper)
return NF_ACCEPT;
return helper->help(pskb, skb_network_offset(*pskb) + ip_hdrlen(*pskb),
ct, ctinfo); ct, ctinfo);
} }
......
...@@ -160,6 +160,7 @@ static unsigned int ipv6_confirm(unsigned int hooknum, ...@@ -160,6 +160,7 @@ static unsigned int ipv6_confirm(unsigned int hooknum,
{ {
struct nf_conn *ct; struct nf_conn *ct;
struct nf_conn_help *help; struct nf_conn_help *help;
struct nf_conntrack_helper *helper;
enum ip_conntrack_info ctinfo; enum ip_conntrack_info ctinfo;
unsigned int ret, protoff; unsigned int ret, protoff;
unsigned int extoff = (u8 *)(ipv6_hdr(*pskb) + 1) - (*pskb)->data; unsigned int extoff = (u8 *)(ipv6_hdr(*pskb) + 1) - (*pskb)->data;
...@@ -172,7 +173,11 @@ static unsigned int ipv6_confirm(unsigned int hooknum, ...@@ -172,7 +173,11 @@ static unsigned int ipv6_confirm(unsigned int hooknum,
goto out; goto out;
help = nfct_help(ct); help = nfct_help(ct);
if (!help || !help->helper) if (!help)
goto out;
/* rcu_read_lock()ed by nf_hook_slow */
helper = rcu_dereference(help->helper);
if (!helper)
goto out; goto out;
protoff = nf_ct_ipv6_skip_exthdr(*pskb, extoff, &pnum, protoff = nf_ct_ipv6_skip_exthdr(*pskb, extoff, &pnum,
...@@ -182,7 +187,7 @@ static unsigned int ipv6_confirm(unsigned int hooknum, ...@@ -182,7 +187,7 @@ static unsigned int ipv6_confirm(unsigned int hooknum,
return NF_ACCEPT; return NF_ACCEPT;
} }
ret = help->helper->help(pskb, protoff, ct, ctinfo); ret = helper->help(pskb, protoff, ct, ctinfo);
if (ret != NF_ACCEPT) if (ret != NF_ACCEPT)
return ret; return ret;
out: out:
......
...@@ -350,9 +350,15 @@ static void death_by_timeout(unsigned long ul_conntrack) ...@@ -350,9 +350,15 @@ static void death_by_timeout(unsigned long ul_conntrack)
{ {
struct nf_conn *ct = (void *)ul_conntrack; struct nf_conn *ct = (void *)ul_conntrack;
struct nf_conn_help *help = nfct_help(ct); struct nf_conn_help *help = nfct_help(ct);
struct nf_conntrack_helper *helper;
if (help && help->helper && help->helper->destroy) if (help) {
help->helper->destroy(ct); rcu_read_lock();
helper = rcu_dereference(help->helper);
if (helper && helper->destroy)
helper->destroy(ct);
rcu_read_unlock();
}
write_lock_bh(&nf_conntrack_lock); write_lock_bh(&nf_conntrack_lock);
/* Inside lock so preempt is disabled on module removal path. /* Inside lock so preempt is disabled on module removal path.
...@@ -661,6 +667,7 @@ init_conntrack(const struct nf_conntrack_tuple *tuple, ...@@ -661,6 +667,7 @@ init_conntrack(const struct nf_conntrack_tuple *tuple,
unsigned int dataoff) unsigned int dataoff)
{ {
struct nf_conn *conntrack; struct nf_conn *conntrack;
struct nf_conn_help *help;
struct nf_conntrack_tuple repl_tuple; struct nf_conntrack_tuple repl_tuple;
struct nf_conntrack_expect *exp; struct nf_conntrack_expect *exp;
u_int32_t features = 0; u_int32_t features = 0;
...@@ -691,6 +698,7 @@ init_conntrack(const struct nf_conntrack_tuple *tuple, ...@@ -691,6 +698,7 @@ init_conntrack(const struct nf_conntrack_tuple *tuple,
write_lock_bh(&nf_conntrack_lock); write_lock_bh(&nf_conntrack_lock);
exp = find_expectation(tuple); exp = find_expectation(tuple);
help = nfct_help(conntrack);
if (exp) { if (exp) {
DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n", DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
conntrack, exp); conntrack, exp);
...@@ -698,7 +706,7 @@ init_conntrack(const struct nf_conntrack_tuple *tuple, ...@@ -698,7 +706,7 @@ init_conntrack(const struct nf_conntrack_tuple *tuple,
__set_bit(IPS_EXPECTED_BIT, &conntrack->status); __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
conntrack->master = exp->master; conntrack->master = exp->master;
if (exp->helper) if (exp->helper)
nfct_help(conntrack)->helper = exp->helper; rcu_assign_pointer(help->helper, exp->helper);
#ifdef CONFIG_NF_CONNTRACK_MARK #ifdef CONFIG_NF_CONNTRACK_MARK
conntrack->mark = exp->master->mark; conntrack->mark = exp->master->mark;
#endif #endif
...@@ -708,10 +716,11 @@ init_conntrack(const struct nf_conntrack_tuple *tuple, ...@@ -708,10 +716,11 @@ init_conntrack(const struct nf_conntrack_tuple *tuple,
nf_conntrack_get(&conntrack->master->ct_general); nf_conntrack_get(&conntrack->master->ct_general);
NF_CT_STAT_INC(expect_new); NF_CT_STAT_INC(expect_new);
} else { } else {
struct nf_conn_help *help = nfct_help(conntrack); if (help) {
/* not in hash table yet, so not strictly necessary */
if (help) rcu_assign_pointer(help->helper,
help->helper = __nf_ct_helper_find(&repl_tuple); __nf_ct_helper_find(&repl_tuple));
}
NF_CT_STAT_INC(new); NF_CT_STAT_INC(new);
} }
...@@ -893,7 +902,8 @@ void nf_conntrack_alter_reply(struct nf_conn *ct, ...@@ -893,7 +902,8 @@ void nf_conntrack_alter_reply(struct nf_conn *ct,
helper = __nf_ct_helper_find(newreply); helper = __nf_ct_helper_find(newreply);
if (helper) if (helper)
memset(&help->help, 0, sizeof(help->help)); memset(&help->help, 0, sizeof(help->help));
help->helper = helper; /* not in hash table yet, so not strictly necessary */
rcu_assign_pointer(help->helper, helper);
} }
write_unlock_bh(&nf_conntrack_lock); write_unlock_bh(&nf_conntrack_lock);
} }
......
...@@ -337,6 +337,10 @@ int nf_conntrack_expect_related(struct nf_conntrack_expect *expect) ...@@ -337,6 +337,10 @@ int nf_conntrack_expect_related(struct nf_conntrack_expect *expect)
NF_CT_ASSERT(master_help); NF_CT_ASSERT(master_help);
write_lock_bh(&nf_conntrack_lock); write_lock_bh(&nf_conntrack_lock);
if (!master_help->helper) {
ret = -ESHUTDOWN;
goto out;
}
list_for_each_entry(i, &nf_conntrack_expect_list, list) { list_for_each_entry(i, &nf_conntrack_expect_list, list) {
if (expect_matches(i, expect)) { if (expect_matches(i, expect)) {
/* Refresh timer: if it's dying, ignore.. */ /* Refresh timer: if it's dying, ignore.. */
......
...@@ -93,7 +93,7 @@ static inline int unhelp(struct nf_conntrack_tuple_hash *i, ...@@ -93,7 +93,7 @@ static inline int unhelp(struct nf_conntrack_tuple_hash *i,
if (help && help->helper == me) { if (help && help->helper == me) {
nf_conntrack_event(IPCT_HELPER, ct); nf_conntrack_event(IPCT_HELPER, ct);
help->helper = NULL; rcu_assign_pointer(help->helper, NULL);
} }
return 0; return 0;
} }
......
...@@ -171,21 +171,29 @@ ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct) ...@@ -171,21 +171,29 @@ ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
{ {
struct nfattr *nest_helper; struct nfattr *nest_helper;
const struct nf_conn_help *help = nfct_help(ct); const struct nf_conn_help *help = nfct_help(ct);
struct nf_conntrack_helper *helper;
if (!help || !help->helper) if (!help)
return 0; return 0;
rcu_read_lock();
helper = rcu_dereference(help->helper);
if (!helper)
goto out;
nest_helper = NFA_NEST(skb, CTA_HELP); nest_helper = NFA_NEST(skb, CTA_HELP);
NFA_PUT(skb, CTA_HELP_NAME, strlen(help->helper->name), help->helper->name); NFA_PUT(skb, CTA_HELP_NAME, strlen(helper->name), helper->name);
if (help->helper->to_nfattr) if (helper->to_nfattr)
help->helper->to_nfattr(skb, ct); helper->to_nfattr(skb, ct);
NFA_NEST_END(skb, nest_helper); NFA_NEST_END(skb, nest_helper);
out:
rcu_read_unlock();
return 0; return 0;
nfattr_failure: nfattr_failure:
rcu_read_unlock();
return -1; return -1;
} }
...@@ -842,7 +850,7 @@ ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[]) ...@@ -842,7 +850,7 @@ ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[])
if (help && help->helper) { if (help && help->helper) {
/* we had a helper before ... */ /* we had a helper before ... */
nf_ct_remove_expectations(ct); nf_ct_remove_expectations(ct);
help->helper = NULL; rcu_assign_pointer(help->helper, NULL);
} }
return 0; return 0;
...@@ -866,7 +874,7 @@ ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[]) ...@@ -866,7 +874,7 @@ ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[])
/* need to zero data of old helper */ /* need to zero data of old helper */
memset(&help->help, 0, sizeof(help->help)); memset(&help->help, 0, sizeof(help->help));
help->helper = helper; rcu_assign_pointer(help->helper, helper);
return 0; return 0;
} }
...@@ -950,6 +958,7 @@ ctnetlink_create_conntrack(struct nfattr *cda[], ...@@ -950,6 +958,7 @@ ctnetlink_create_conntrack(struct nfattr *cda[],
struct nf_conn *ct; struct nf_conn *ct;
int err = -EINVAL; int err = -EINVAL;
struct nf_conn_help *help; struct nf_conn_help *help;
struct nf_conntrack_helper *helper = NULL;
ct = nf_conntrack_alloc(otuple, rtuple); ct = nf_conntrack_alloc(otuple, rtuple);
if (ct == NULL || IS_ERR(ct)) if (ct == NULL || IS_ERR(ct))
...@@ -980,14 +989,17 @@ ctnetlink_create_conntrack(struct nfattr *cda[], ...@@ -980,14 +989,17 @@ ctnetlink_create_conntrack(struct nfattr *cda[],
#endif #endif
help = nfct_help(ct); help = nfct_help(ct);
if (help) if (help) {
help->helper = nf_ct_helper_find_get(rtuple); helper = nf_ct_helper_find_get(rtuple);
/* not in hash table yet so not strictly necessary */
rcu_assign_pointer(help->helper, helper);
}
add_timer(&ct->timeout); add_timer(&ct->timeout);
nf_conntrack_hash_insert(ct); nf_conntrack_hash_insert(ct);
if (help && help->helper) if (helper)
nf_ct_helper_put(help->helper); nf_ct_helper_put(helper);
return 0; return 0;
......
...@@ -100,7 +100,6 @@ int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir, ...@@ -100,7 +100,6 @@ int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir,
struct nf_conn_help *help = nfct_help(ct); struct nf_conn_help *help = nfct_help(ct);
struct nf_ct_gre_keymap **kmp, *km; struct nf_ct_gre_keymap **kmp, *km;
BUG_ON(strcmp(help->helper->name, "pptp"));
kmp = &help->help.ct_pptp_info.keymap[dir]; kmp = &help->help.ct_pptp_info.keymap[dir];
if (*kmp) { if (*kmp) {
/* check whether it's a retransmission */ /* check whether it's a retransmission */
...@@ -137,7 +136,6 @@ void nf_ct_gre_keymap_destroy(struct nf_conn *ct) ...@@ -137,7 +136,6 @@ void nf_ct_gre_keymap_destroy(struct nf_conn *ct)
enum ip_conntrack_dir dir; enum ip_conntrack_dir dir;
DEBUGP("entering for ct %p\n", ct); DEBUGP("entering for ct %p\n", ct);
BUG_ON(strcmp(help->helper->name, "pptp"));
write_lock_bh(&nf_ct_gre_lock); write_lock_bh(&nf_ct_gre_lock);
for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) { for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment