Skip to contentSkip to conversation

netfilter: nf_conntrack_gre: fix gre keymap list corruption

PATCH Superseded Florian Westphal 1 participant 1 message May 15

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nf_conntrack_gre: fix gre keymap list corruption

Quoting reporter:
A race between GRE keymap insertion and destruction can corrupt the kernel list or use a freed object. nf_ct_gre_keymap_add() publishes a new keymap pointer before the embedded list_head is linked, while nf_ct_gre_keymap_destroy() can concurrently delete and free that same object. An unprivileged user can reach this through the PPTP conntrack helper by racing PPTP control messages or helper teardown, leading to KASAN-detectable list corruption/UAF in kernel context.

## Root Cause Analysis
exp_gre() installs GRE expectations for a PPTP control flow and then
adds two GRE keymap entries [..]

The add path publishes ct_pptp_info->keymap[dir] before linking the embedded list node [..]
Concurrent teardown deletes that partially initialized object.

Make add/destroy symmetric: install both, destroy both while under lock.

Furthermore, we should refuse to publish a new mapping in case ct is going away, else we may leak the allocation.

The "retrans" detection is strange: existing mapping is checked for key equality with the new mapping, then for "is on the list" via list walk.

But I can't see how an existing keymap entry can be NOT on list.

Change this to only check if we're asked to map same tuple again -- if so,
skip re-install, else signal failure.

Last, add a bug trap for the keymap list; it has to be empty when namespace is going away.

---
 .../linux/netfilter/nf_conntrack_proto_gre.h  |  7 +-
 net/netfilter/nf_conntrack_core.c             |  8 ++
 net/netfilter/nf_conntrack_pptp.c             |  8 +-
 net/netfilter/nf_conntrack_proto_gre.c        | 76 +++++++++++++------
 4 files changed, 67 insertions(+), 32 deletions(-)
include/linux/netfilter/nf_conntrack_proto_gre.h +4 -3
index 9ee7014400e8..ad5563f0f864 100644
@@ -18,9 +18,10 @@ struct nf_ct_gre_keymap {
 	struct rcu_head rcu;
 };
 
-/* add new tuple->key_reply pair to keymap */
-int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir,
-			 struct nf_conntrack_tuple *t);
+/* add tuple->key_reply pairs to keymap */
+bool nf_ct_gre_keymap_add(struct nf_conn *ct,
+			  const struct nf_conntrack_tuple *orig,
+			  const struct nf_conntrack_tuple *repl);
 
 /* delete keymap entries */
 void nf_ct_gre_keymap_destroy(struct nf_conn *ct);
net/netfilter/nf_conntrack_core.c +8 -0
index 8ba5b22a1eef..b521b5ebd664 100644
@@ -568,6 +568,13 @@ static void destroy_gre_conntrack(struct nf_conn *ct)
 #endif
 }
 
+static void warn_on_keymap_list_leak(const struct net *net)
+{
+#ifdef CONFIG_NF_CT_PROTO_GRE
+	WARN_ON_ONCE(!list_empty(&net->ct.nf_ct_proto.gre.keymap_list));
+#endif
+}
+
 void nf_ct_destroy(struct nf_conntrack *nfct)
 {
 	struct nf_conn *ct = (struct nf_conn *)nfct;
@@ -2510,6 +2517,7 @@ void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list)
 	}
 
 	list_for_each_entry(net, net_exit_list, exit_list) {
+		warn_on_keymap_list_leak(net);
 		nf_conntrack_ecache_pernet_fini(net);
 		nf_conntrack_expect_pernet_fini(net);
 		free_percpu(net->ct.stat);
net/netfilter/nf_conntrack_pptp.c +2 -6
index 4c679638df06..3dd8a2eece26 100644
@@ -225,13 +225,9 @@ static int exp_gre(struct nf_conn *ct, __be16 callid, __be16 peer_callid)
 	if (nf_ct_expect_related(exp_reply, 0) != 0)
 		goto out_unexpect_orig;
 
-	/* Add GRE keymap entries */
-	if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_ORIGINAL, &exp_orig->tuple) != 0)
+	if (!nf_ct_gre_keymap_add(ct, &exp_orig->tuple,
+				 &exp_reply->tuple))
 		goto out_unexpect_both;
-	if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_REPLY, &exp_reply->tuple) != 0) {
-		nf_ct_gre_keymap_destroy(ct);
-		goto out_unexpect_both;
-	}
 	ret = 0;
 
 out_put_both:
net/netfilter/nf_conntrack_proto_gre.c +53 -23

94 lines not on this page · raw message

Signature · 1 line
2.53.0