netfilter: conntrack: add and use nf_ct_get_real()
Subsystems (3)
Florian Westphal
maintainerPATCH 1/1 netfilter: conntrack: add and use nf_ct_get_real()
Like nf_ct_get() but returns NULL in case skb is still associated with a template conntrack object.
This patch doesn't fix crashes, hence no fixes tag, but the missing template checks in these functions allow for undesireable resp. non-deterministic behaviour. Example:
1. netns A: "ct zone set N" attaches shared per-CPU template. Then, CONNMARK --set-mark .. (or any unguarded nf_ct_get() caller reachable before nf_conntrack_in consumes the template) writes ct->mark = 0x1 into that shared object.
2. nf_conntrack_in() later runs, consumes the template for netns A's real connection — but the shared per-CPU object's mark field still holds 0x1.
3. netns B, same CPU, next packet hitting ct zone set reuses that same object (oldcnt == 1 again) and gets zone reset, but mark is still 0x1 from netns A until something explicitly overwrites it — visible to any subsequent ct mark read in netns B before it's set again.
Many places continue to use nf_ct_get(), because they fall into one of the following categories:
1. They actually do want a template, (e.g defrag hook for zone info)
2. Already do check the template bit
3. Cannot see a template in the existing pipeline,
e.g. NAT functions (template is removed in nf_conntrack_in), synproxy (its in INPUT hook, so after nf_conntrack_in).
net/sched is also left alone, I can't find a way to provide those places with a conntrack template. act_ct sets one, but then calls nf_conntrack_in() which consumes that template to make a real ct entry.
Same for OVS; I can't find a callpath that would result in arrival of skb with a template conntrack.
--- include/net/netfilter/nf_conntrack.h | 12 ++++++++++++ net/ipv4/netfilter/nf_socket_ipv4.c | 2 +- net/ipv6/netfilter/nf_socket_ipv6.c | 2 +- net/netfilter/nfnetlink_queue.c | 4 ++-- net/netfilter/nft_flow_offload.c | 2 +- net/netfilter/xt_CONNSECMARK.c | 4 ++-- net/netfilter/xt_HMARK.c | 2 +- net/netfilter/xt_connbytes.c | 2 +- net/netfilter/xt_connlabel.c | 2 +- net/netfilter/xt_connmark.c | 4 ++-- net/netfilter/xt_conntrack.c | 2 +- net/netfilter/xt_helper.c | 2 +- net/netfilter/xt_ipvs.c | 2 +- 13 files changed, 27 insertions(+), 15 deletions(-)
include/net/netfilter/nf_conntrack.h +12 -0
@@ -260,6 +260,18 @@ static inline int nf_ct_is_template(const struct nf_conn *ct)return test_bit (IPS_TEMPLATE_BIT , &ct->status); } +/* Like nf_ct_get(), but returns NULL for template conntracks. */ +static inline struct nf_conn * +nf_ct_get_real (const struct sk_buff *skb,enum ip_conntrack_info *ctinfo) +{ +struct nf_conn *ct =nf_ct_get (skb, ctinfo); + +if (ct &&nf_ct_is_template (ct)) +return NULL ; + +return ct; +} +/* It's confirmed if it is, or has been in the hash table. */ static inline int nf_ct_is_confirmed (const struct nf_conn *ct) {
net/ipv4/netfilter/nf_socket_ipv4.c +1 -1
@@ -130,7 +130,7 @@ struct sock *nf_sk_lookup_slow_v4(struct net *net, const struct sk_buff *skb,* case this is a reply packet of an established * SNAT-ted connection. */ - ct =nf_ct_get (skb, &ctinfo); + ct =nf_ct_get_real (skb, &ctinfo); if (ct && ((iph->protocol != IPPROTO_ICMP && ctinfo == IP_CT_ESTABLISHED_REPLY) ||
net/ipv6/netfilter/nf_socket_ipv6.c +1 -1
@@ -145,7 +145,7 @@ struct sock *nf_sk_lookup_slow_v6(struct net *net, const struct sk_buff *skb,* case this is a reply packet of an established * SNAT-ted connection. */ - ct =nf_ct_get (skb, &ctinfo); + ct =nf_ct_get_real (skb, &ctinfo); if (ct && ((tproto != IPPROTO_ICMPV6 && ctinfo == IP_CT_ESTABLISHED_REPLY) ||
net/netfilter/nfnetlink_queue.c +2 -2
@@ -786,7 +786,7 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,#if IS_ENABLED (CONFIG_NF_CONNTRACK )if (queue->flags &NFQA_CFG_F_CONNTRACK ) {if (nfnl_ct !=NULL ) { - ct =nf_ct_get (entskb, &ctinfo); + ct =nf_ct_get_real (entskb, &ctinfo);if (ct !=NULL ) size += nfnl_ct->build_size (ct); }
@@ -1730,7 +1730,7 @@ static struct nf_conn *nfqnl_ct_parse(const struct nfnl_ct_hook *nfnl_ct,#if IS_ENABLED (CONFIG_NF_CONNTRACK )struct nf_conn *ct; - ct =nf_ct_get (entry->skb, ctinfo); + ct =nf_ct_get_real (entry->skb, ctinfo);if (ct ==NULL )return NULL ;
net/netfilter/nft_flow_offload.c +1 -1
@@ -64,7 +64,7 @@ static void nft_flow_offload_eval(const struct nft_expr *expr,if (nft_flow_offload_skip (pkt->skb,nft_pf (pkt))) goto out; - ct =nf_ct_get (pkt->skb, &ctinfo); + ct =nf_ct_get_real (pkt->skb, &ctinfo);if (!ct) goto out;
net/netfilter/xt_CONNSECMARK.c +2 -2
@@ -35,7 +35,7 @@ static void secmark_save(const struct sk_buff *skb)struct nf_conn *ct;enum ip_conntrack_info ctinfo; - ct =nf_ct_get (skb, &ctinfo); + ct =nf_ct_get_real (skb, &ctinfo);if (ct && !ct->secmark) { ct->secmark = skb->secmark;nf_conntrack_event_cache (IPCT_SECMARK , ct);
@@ -53,7 +53,7 @@ static void secmark_restore(struct sk_buff *skb)const struct nf_conn *ct;enum ip_conntrack_info ctinfo; - ct =nf_ct_get (skb, &ctinfo); + ct =nf_ct_get_real (skb, &ctinfo);if (ct && ct->secmark) skb->secmark = ct->secmark; }
net/netfilter/xt_HMARK.c +1 -1
@@ -79,7 +79,7 @@ hmark_ct_set_htuple(const struct sk_buff *skb, struct hmark_tuple *t, {#if IS_ENABLED (CONFIG_NF_CONNTRACK )enum ip_conntrack_info ctinfo; -struct nf_conn *ct =nf_ct_get (skb, &ctinfo); +struct nf_conn *ct =nf_ct_get_real (skb, &ctinfo);struct nf_conntrack_tuple *otuple;struct nf_conntrack_tuple *rtuple;
net/netfilter/xt_connbytes.c +1 -1
@@ -30,7 +30,7 @@ connbytes_mt(const struct sk_buff *skb, struct xt_action_param *par)const struct nf_conn_acct *acct;const struct nf_conn_counter *counters; - ct =nf_ct_get (skb, &ctinfo); + ct =nf_ct_get_real (skb, &ctinfo);if (!ct)return false;
net/netfilter/xt_connlabel.c +1 -1
@@ -25,7 +25,7 @@ connlabel_mt(const struct sk_buff *skb, struct xt_action_param *par)struct nf_conn *ct;bool invert = info->options &XT_CONNLABEL_OP_INVERT ; - ct =nf_ct_get (skb, &ctinfo); + ct =nf_ct_get_real (skb, &ctinfo);if (ct ==NULL )return invert;
net/netfilter/xt_connmark.c +2 -2
@@ -32,7 +32,7 @@ connmark_tg_shift(struct sk_buff *skb, const struct xt_connmark_tginfo2 *info)u_int32_t newmark;u_int32_t oldmark; - ct =nf_ct_get (skb, &ctinfo); + ct =nf_ct_get_real (skb, &ctinfo);if (ct ==NULL )return XT_CONTINUE ;
@@ -134,7 +134,7 @@ connmark_mt(const struct sk_buff *skb, struct xt_action_param *par)enum ip_conntrack_info ctinfo;const struct nf_conn *ct; - ct =nf_ct_get (skb, &ctinfo); + ct =nf_ct_get_real (skb, &ctinfo);if (ct ==NULL )return false;
net/netfilter/xt_conntrack.c +1 -1
@@ -167,7 +167,7 @@ conntrack_mt(const struct sk_buff *skb, struct xt_action_param *par,const struct nf_conn *ct;unsigned int statebit; - ct =nf_ct_get (skb, &ctinfo); + ct =nf_ct_get_real (skb, &ctinfo);if (ct) statebit =XT_CONNTRACK_STATE_BIT (ctinfo);
net/netfilter/xt_helper.c +1 -1
@@ -30,7 +30,7 @@ helper_mt(const struct sk_buff *skb, struct xt_action_param *par)enum ip_conntrack_info ctinfo;bool ret = info->invert; - ct =nf_ct_get (skb, &ctinfo); + ct =nf_ct_get_real (skb, &ctinfo);if (!ct || !ct->master)return ret;
net/netfilter/xt_ipvs.c +1 -1
@@ -115,7 +115,7 @@ ipvs_mt(const struct sk_buff *skb, struct xt_action_param *par)if (data->bitmask &XT_IPVS_DIR ) {enum ip_conntrack_info ctinfo; -struct nf_conn *ct =nf_ct_get (skb, &ctinfo); +struct nf_conn *ct =nf_ct_get_real (skb, &ctinfo);if (ct ==NULL ) { match = false;
Signature · 1 line
2.55.0