netfilter: nfnetlink_cthelper: use {READ,WRITE}_ONCE for accessing helper flags
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v4 1/3 netfilter: nfnetlink_cthelper: use {READ,WRITE}_ONCE for accessing helper flags
Conntrack helper flags are accessed from packet and netlink dump path. While updates are protected by the nfnl_mutex, use {READ,WRITE}_ONCE() to access this flags.
---
v4: no changes.
net/netfilter/nfnetlink_cthelper.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
net/netfilter/nfnetlink_cthelper.c +14 -7
@@ -41,8 +41,9 @@ static int nfnl_userspace_cthelper(struct sk_buff *skb, unsigned int protoff, struct nf_conn *ct, enum ip_conntrack_info ctinfo) { - const struct nf_conn_help *help; struct nf_conntrack_helper *helper; +const struct nf_conn_help *help; +unsigned int helper_flags; help =nfct_help (ct);if (help ==NULL )
@@ -53,8 +54,10 @@ nfnl_userspace_cthelper(struct sk_buff *skb, unsigned int protoff,if (helper ==NULL )return NF_DROP ; + helper_flags =READ_ONCE (helper->flags); +/* This is a user-space helper not yet configured, skip. */ -if ((helper->flags & +if ((helper_flags & (NF_CT_HELPER_F_USERSPACE |NF_CT_HELPER_F_CONFIGURED )) ==NF_CT_HELPER_F_USERSPACE )return NF_ACCEPT ;
@@ -404,10 +407,10 @@ nfnl_cthelper_update(const struct nlattr * const tb[],switch (status) {case NFCT_HELPER_STATUS_ENABLED : - helper->flags |=NF_CT_HELPER_F_CONFIGURED ; +WRITE_ONCE (helper->flags, helper->flags |NF_CT_HELPER_F_CONFIGURED );break ;case NFCT_HELPER_STATUS_DISABLED : - helper->flags &= ~NF_CT_HELPER_F_CONFIGURED ; +WRITE_ONCE (helper->flags, helper->flags & ~NF_CT_HELPER_F_CONFIGURED );break ; } }
@@ -529,8 +532,8 @@ static int nfnl_cthelper_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, int event, struct nf_conntrack_helper *helper) { + unsigned int flags = portid ? NLM_F_MULTI : 0, helper_flags;struct nlmsghdr *nlh; -unsigned int flags = portid ?NLM_F_MULTI :0 ;int status; event =nfnl_msg_type (NFNL_SUBSYS_CTHELPER , event);
@@ -554,7 +557,8 @@ nfnl_cthelper_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,if (nla_put_be32 (skb,NFCTH_PRIV_DATA_LEN ,htonl (helper->data_len))) goto nla_put_failure; -if (helper->flags &NF_CT_HELPER_F_CONFIGURED ) + helper_flags =READ_ONCE (helper->flags); +if (helper_flags &NF_CT_HELPER_F_CONFIGURED ) status =NFCT_HELPER_STATUS_ENABLED ;else status =NFCT_HELPER_STATUS_DISABLED ;
@@ -575,6 +579,7 @@ static int nfnl_cthelper_dump_table(struct sk_buff *skb, struct netlink_callback *cb) { struct nf_conntrack_helper *cur, *last; +unsigned int helper_flags;rcu_read_lock (); last = (struct nf_conntrack_helper *)cb->args[1 ];
@@ -583,8 +588,10 @@ nfnl_cthelper_dump_table(struct sk_buff *skb, struct netlink_callback *cb)hlist_for_each_entry_rcu (cur, &nf_ct_helper_hash[cb->args[0 ]], hnode) { + helper_flags =READ_ONCE (cur->flags); +/* skip non-userspace conntrack helpers. */ -if (!(cur->flags &NF_CT_HELPER_F_USERSPACE )) +if (!(helper_flags &NF_CT_HELPER_F_USERSPACE ))continue ;if (cb->args[1 ]) {
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v4 2/3 netfilter: conntrack: add dead flag to helpers
Add a new NF_CT_HELPER_F_DEAD helper flag to notify the packet path that this helper is going away. Thus, helpers are effectively disabled and no new expectations are created while removing the expectations created by this helper as well as unhelping the existing conntrack entries.
Add the check for NF_CT_HELPER_F_DEAD in the packet path to:
- Conntrack confirmation path which invokes the helper callback.
- Propagation of helper to conntrack via expectation.
- OVS ct helper invocation.
nf_conntrack_helper_unregister() is only called under nfnl_mutex when unregistering the helper, else by helper module which already owns this helper from the module removal path, therefore, concurrent update of helper flags via nfnetlink_cthelper is not possible, but packet path and netlink dump path can read these flags locklessly.
---
v4: no changes
AI does not understand ct->gen_id check in __nf_ct_ext_find() ensure access to stale helper area is not possible.
include/net/netfilter/nf_conntrack_helper.h | 8 ++++++++ net/netfilter/nf_conntrack_core.c | 2 +- net/netfilter/nf_conntrack_helper.c | 5 ++++- net/netfilter/nf_conntrack_ovs.c | 3 +++ net/netfilter/nf_conntrack_proto.c | 2 +- 5 files changed, 17 insertions(+), 3 deletions(-)
include/net/netfilter/nf_conntrack_helper.h +8 -0
@@ -25,6 +25,7 @@ struct module;enum nf_ct_helper_flags {NF_CT_HELPER_F_USERSPACE = (1 <<0 ),NF_CT_HELPER_F_CONFIGURED = (1 <<1 ), +NF_CT_HELPER_F_DEAD = (1 <<2 ), };#define NF_CT_HELPER_NAME_LEN 16
@@ -63,6 +64,13 @@ struct nf_conntrack_helper {char nat_mod_name[NF_CT_HELPER_NAME_LEN ]; }; +static inline bool nf_ct_helper_alive (const struct nf_conntrack_helper *helper) +{ +unsigned int helper_flags =READ_ONCE (helper->flags); + +return likely (!(helper_flags &NF_CT_HELPER_F_DEAD )); +} +/* Must be kept in sync with the classes defined by helpers */ #define NF_CT_MAX_EXPECT_CLASSES 4
net/netfilter/nf_conntrack_core.c +1 -1
@@ -1818,7 +1818,7 @@ init_conntrack(struct net *net, struct nf_conn *tmpl,/* exp->master safe, refcnt bumped in nf_ct_find_expectation */ ct->master = exp->master; assign_helper =rcu_dereference (exp->assign_helper); -if (assign_helper) { +if (assign_helper &&nf_ct_helper_alive (assign_helper)) { help =nf_ct_helper_ext_add (ct,GFP_ATOMIC );if (help)rcu_assign_pointer (help->helper, assign_helper);
net/netfilter/nf_conntrack_helper.c +4 -1
@@ -418,8 +418,11 @@ void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me) nf_ct_helper_count--;mutex_unlock (&nf_ct_helper_mutex); +WRITE_ONCE (me->flags, me->flags |NF_CT_HELPER_F_DEAD ); +/* Make sure every nothing is still using the helper unless its a -* connection in the hash. +* connection in the hash, no more expectations are created after +* this rcu grace period. */ synchronize_rcu ();
net/netfilter/nf_conntrack_ovs.c +3 -0
@@ -28,6 +28,9 @@ int nf_ct_helper(struct sk_buff *skb, struct nf_conn *ct,if (!helper)return NF_ACCEPT ; +if (!nf_ct_helper_alive (helper)) +return NF_ACCEPT ; +if (helper->tuple.src.l3num !=NFPROTO_UNSPEC && helper->tuple.src.l3num != proto)return NF_ACCEPT ;
net/netfilter/nf_conntrack_proto.c +1 -1
@@ -174,7 +174,7 @@ unsigned int nf_confirm(void *priv,/* rcu_read_lock()ed by nf_hook */ helper =rcu_dereference (help->helper); -if (helper) { +if (helper &&nf_ct_helper_alive (helper)) { ret = helper->help (skb, protoff, ct, ctinfo);
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v4 3/3 netfilter: nf_conntrack_helper: add null check in nfct_help_data() calls
In reply to Pablo Neira AyusoWhen helper is removed, nf_ct_iterate_destroy() unhelps the conntrack entries. Then, the nf_ct_ext_find() might return NULL if the extension is stale via:
c56716c69ce1 ("netfilter: extensions: introduce extension genid count").
Add the null check to nfct_help_data() and helpers that call this function since packet path could be walking over helper while it is being removed.
---
v4: new in this series
include/net/netfilter/nf_conntrack_helper.h | 2 ++ net/ipv4/netfilter/nf_nat_h323.c | 6 ++++++ net/ipv4/netfilter/nf_nat_pptp.c | 6 ++++++ net/netfilter/nf_conntrack_ftp.c | 6 ++++++ net/netfilter/nf_conntrack_h323_main.c | 18 ++++++++++++++++++ net/netfilter/nf_conntrack_pptp.c | 12 ++++++++++++ net/netfilter/nf_conntrack_proto_gre.c | 6 ++++++ net/netfilter/nf_conntrack_sane.c | 3 +++ net/netfilter/nf_conntrack_sip.c | 21 +++++++++++++++++++++ net/netfilter/nf_nat_sip.c | 9 +++++++++ 10 files changed, 89 insertions(+)
include/net/netfilter/nf_conntrack_helper.h +2 -0
@@ -140,6 +140,8 @@ static inline void *nfct_help_data(const struct nf_conn *ct)struct nf_conn_help *help; help =nf_ct_ext_find (ct,NF_CT_EXT_HELPER ); +if (!help) +return NULL ;return (void *)help->data; }
net/ipv4/netfilter/nf_nat_h323.c +6 -0
@@ -100,6 +100,9 @@ static int set_sig_addr(struct sk_buff *skb, struct nf_conn *ct,__be16 port;union nf_inet_addr addr; +if (!info) +return -1 ; +for (i =0 ; i < count; i++) {if (get_h225_addr (ct, *data, &taddr[i], &addr, &port)) { if (addr.ip == ct->tuplehash[dir].tuple.src.u3.ip &&
@@ -184,6 +187,9 @@ static int nat_rtp_rtcp(struct sk_buff *skb, struct nf_conn *ct,int i;u_int16_t nated_port; +if (!info) +return -1 ; +/* Set expectations for NAT */ rtp_exp->saved_proto.udp.port = rtp_exp->tuple.dst.u.udp.port; rtp_exp->expectfn = nf_nat_follow_master;
net/ipv4/netfilter/nf_nat_pptp.c +6 -0
@@ -58,6 +58,8 @@ static void pptp_nat_expected(struct nf_conn *ct, nat_pptp_info = &nat->help.nat_pptp_info; ct_pptp_info =nfct_help_data (master); +if (!ct_pptp_info) +return ;/* And here goes the grand finale of corrosion... */ if (exp->dir ==IP_CT_DIR_ORIGINAL ) {
@@ -137,6 +139,8 @@ pptp_outbound_pkt(struct sk_buff *skb, nat_pptp_info = &nat->help.nat_pptp_info; ct_pptp_info =nfct_help_data (ct); +if (!ct_pptp_info) +return NF_DROP ; new_callid = ct_pptp_info->pns_call_id;
@@ -209,6 +213,8 @@ pptp_exp_gre(struct nf_conntrack_expect *expect_orig, nat_pptp_info = &nat->help.nat_pptp_info; ct_pptp_info =nfct_help_data (ct); +if (!ct_pptp_info) +return ;/* save original PAC call ID in nat_info */ nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
net/netfilter/nf_conntrack_ftp.c +6 -0
@@ -381,6 +381,9 @@ static int help(struct sk_buff *skb,int found =0 , ends_in_nl;nf_nat_ftp_hook_fn *nf_nat_ftp; +if (!ct_ftp_info) +return NF_DROP ; +/* Until there's been traffic both ways, don't look in packets. */ if (ctinfo !=IP_CT_ESTABLISHED && ctinfo !=IP_CT_ESTABLISHED_REPLY ) {
@@ -542,6 +545,9 @@ static int nf_ct_ftp_from_nlattr(struct nlattr *attr, struct nf_conn *ct) {struct nf_ct_ftp_master *ftp =nfct_help_data (ct); +if (!ftp) +return -1 ; +/* This conntrack has been injected from user-space, always pick up * sequence tracking. Otherwise, the first FTP command after the * failover breaks.
net/netfilter/nf_conntrack_h323_main.c +18 -0
@@ -76,6 +76,9 @@ static int get_tpkt_data(struct sk_buff *skb, unsigned int protoff,int tpktlen;int tpktoff; +if (!info) +return 0 ; +/* Get TCP header */ th =skb_header_pointer (skb, protoff,sizeof (_tcph), &_tcph);if (th ==NULL )
@@ -1215,6 +1218,9 @@ static int expect_q931(struct sk_buff *skb, struct nf_conn *ct,union nf_inet_addr addr;struct nf_conntrack_expect *exp; +if (!info) +return -1 ; +/* Look for the first related address */ for (i =0 ; i < count; i++) { if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
@@ -1328,6 +1334,9 @@ static int process_rrq(struct sk_buff *skb, struct nf_conn *ct,const struct nfct_h323_nat_hooks *nathook;int ret; +if (!info) +return -1 ; +pr_debug ("nf_ct_ras: RRQ\n" ); ret = expect_q931(skb, ct, ctinfo, protoff, data,
@@ -1366,6 +1375,9 @@ static int process_rcf(struct sk_buff *skb, struct nf_conn *ct,int ret;struct nf_conntrack_expect *exp; +if (!info) +return -1 ; +pr_debug ("nf_ct_ras: RCF\n" ); nathook =rcu_dereference (nfct_h323_nat_hook);
@@ -1416,6 +1428,9 @@ static int process_urq(struct sk_buff *skb, struct nf_conn *ct,int dir =CTINFO2DIR (ctinfo);int ret; +if (!info) +return -1 ; +pr_debug ("nf_ct_ras: URQ\n" ); nathook =rcu_dereference (nfct_h323_nat_hook);
@@ -1450,6 +1465,9 @@ static int process_arq(struct sk_buff *skb, struct nf_conn *ct,__be16 port;union nf_inet_addr addr; +if (!info) +return 0 ; +pr_debug ("nf_ct_ras: ARQ\n" ); nathook =rcu_dereference (nfct_h323_nat_hook);
net/netfilter/nf_conntrack_pptp.c +12 -0
@@ -164,6 +164,9 @@ static void pptp_destroy_siblings(struct nf_conn *ct)const struct nf_ct_pptp_master *ct_pptp_info =nfct_help_data (ct);struct nf_conntrack_tuple t; +if (!ct_pptp_info) +return ; +nf_ct_gre_keymap_destroy (ct);/* try original (pns->pac) tuple */
@@ -261,6 +264,9 @@ pptp_inbound_pkt(struct sk_buff *skb, unsigned int protoff,u_int16_t msg;__be16 cid =0 , pcid =0 ; +if (!info) +return NF_DROP ; + msg =ntohs (ctlh->messageType);pr_debug ("inbound control message %s\n" ,pptp_msg_name (msg));
@@ -388,6 +394,9 @@ pptp_outbound_pkt(struct sk_buff *skb, unsigned int protoff,u_int16_t msg;__be16 cid =0 , pcid =0 ; +if (!info) +return NF_DROP ; + msg =ntohs (ctlh->messageType);pr_debug ("outbound control message %s\n" ,pptp_msg_name (msg));
@@ -506,6 +515,9 @@ conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,int ret;u_int16_t msg; +if (!info) +return NF_DROP ; +#if IS_ENABLED (CONFIG_NF_NAT )if (!nf_ct_is_confirmed (ct) && (ct->status &IPS_NAT_MASK )) {struct nf_conn_nat *nat =nf_ct_ext_find (ct,NF_CT_EXT_NAT );
net/netfilter/nf_conntrack_proto_gre.c +6 -0
@@ -96,6 +96,9 @@ int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir,struct nf_ct_pptp_master *ct_pptp_info =nfct_help_data (ct);struct nf_ct_gre_keymap **kmp, *km; +if (!ct_pptp_info) +return -ENOENT ; + kmp = &ct_pptp_info->keymap[dir];if (*kmp) {/* check whether it's a retransmission */
@@ -131,6 +134,9 @@ void nf_ct_gre_keymap_destroy(struct nf_conn *ct)struct nf_ct_pptp_master *ct_pptp_info =nfct_help_data (ct);enum ip_conntrack_dir dir; +if (!ct_pptp_info) +return ; +pr_debug ("entering for ct %p\n" , ct);spin_lock_bh (&keymap_lock);
net/netfilter/nf_conntrack_sane.c +3 -0
@@ -74,6 +74,9 @@ static int help(struct sk_buff *skb,struct sane_reply_net_start repl; } buf; +if (!ct_sane_info) +return NF_DROP ; +/* Until there's been traffic both ways, don't look in packets. */ if (ctinfo !=IP_CT_ESTABLISHED && ctinfo !=IP_CT_ESTABLISHED_REPLY )
net/netfilter/nf_conntrack_sip.c +21 -0
@@ -1227,6 +1227,9 @@ static int process_invite_response(struct sk_buff *skb, unsigned int protoff,struct nf_conn *ct =nf_ct_get (skb, &ctinfo);struct nf_ct_sip_master *ct_sip_info =nfct_help_data (ct); +if (!ct_sip_info) +return NF_DROP ; +if ((code >=100 && code <=199 ) || (code >=200 && code <=299 ))return process_sdp (skb, protoff, dataoff, dptr, datalen, cseq);
@@ -1244,6 +1247,9 @@ static int process_update_response(struct sk_buff *skb, unsigned int protoff,struct nf_conn *ct =nf_ct_get (skb, &ctinfo);struct nf_ct_sip_master *ct_sip_info =nfct_help_data (ct); +if (!ct_sip_info) +return NF_DROP ; +if ((code >=100 && code <=199 ) || (code >=200 && code <=299 ))return process_sdp (skb, protoff, dataoff, dptr, datalen, cseq);
@@ -1261,6 +1267,9 @@ static int process_prack_response(struct sk_buff *skb, unsigned int protoff,struct nf_conn *ct =nf_ct_get (skb, &ctinfo);struct nf_ct_sip_master *ct_sip_info =nfct_help_data (ct); +if (!ct_sip_info) +return NF_DROP ; +if ((code >=100 && code <=199 ) || (code >=200 && code <=299 ))return process_sdp (skb, protoff, dataoff, dptr, datalen, cseq);
@@ -1279,6 +1288,9 @@ static int process_invite_request(struct sk_buff *skb, unsigned int protoff,struct nf_ct_sip_master *ct_sip_info =nfct_help_data (ct);unsigned int ret; +if (!ct_sip_info) +return NF_DROP ; +flush_expectations (ct, true); ret =process_sdp (skb, protoff, dataoff, dptr, datalen, cseq);if (ret ==NF_ACCEPT )
@@ -1321,6 +1333,9 @@ static int process_register_request(struct sk_buff *skb, unsigned int protoff,unsigned int expires =0 ;int ret; +if (!ct_sip_info) +return NF_DROP ; +/* Expected connections can not register again. */ if (ct->status &IPS_EXPECTED )return NF_ACCEPT ;
@@ -1421,6 +1436,9 @@ static int process_register_response(struct sk_buff *skb, unsigned int protoff,unsigned int expires =0 ;int in_contact =0 , ret; +if (!ct_sip_info) +return NF_DROP ; +/* According to RFC 3261, "UAs MUST NOT send a new registration until * they have received a final response from the registrar for the * previous one or the previous REGISTER request has timed out".
@@ -1550,6 +1568,9 @@ static int process_sip_request(struct sk_buff *skb, unsigned int protoff,union nf_inet_addr addr;__be16 port; +if (!ct_sip_info) +return NF_DROP ; +/* Many Cisco IP phones use a high source port for SIP requests, but * listen for the response on port 5060. If we are the local * router for one of these phones, save the port number from the
net/netfilter/nf_nat_sip.c +9 -0
@@ -106,6 +106,9 @@ static int map_addr(struct sk_buff *skb, unsigned int protoff,union nf_inet_addr newaddr;__be16 newport; +if (!ct_sip_info) +return 0 ; +if (nf_inet_addr_cmp (&ct->tuplehash[dir].tuple.src.u3, addr) && ct->tuplehash[dir].tuple.src.u.udp.port == port) { newaddr = ct->tuplehash[!dir].tuple.dst.u3;
@@ -158,6 +161,9 @@ static unsigned int nf_nat_sip(struct sk_buff *skb, unsigned int protoff,__be16 port;int request, in_header; +if (!ct_sip_info) +return NF_DROP ; +/* Basic rules: requests and responses. */ if (strncasecmp (*dptr,"SIP/2.0" ,strlen ("SIP/2.0" )) !=0 ) { if (ct_sip_parse_request(ct, *dptr, *datalen,
@@ -390,6 +396,9 @@ static unsigned int nf_nat_sip_expect(struct sk_buff *skb, unsigned int protoff,char buffer[INET6_ADDRSTRLEN +sizeof ("[]:nnnnn" )];unsigned int buflen; +if (!ct_sip_info) +return NF_DROP ; +/* Connection will come from reply */ if (nf_inet_addr_cmp (&ct->tuplehash[dir].tuple.src.u3, &ct->tuplehash[!dir].tuple.dst.u3))
Signature · 1 line
2.47.3