Subject: AW: Where is this patch buried?

From: "Schmid, Carsten" <[email-masked]>
Date: Mon, 8 Feb 2021 09:24:11 +0000

>> Hi Greg,
>>
>> in kernel 4.14 i have seen a NULL pointer deref in
>> [65064.613465] RIP: 0010:ip_route_output_key_hash_rcu+0x755/0x850
>> (i have a core dump and detailed analysis)
>>
>> That looks like this patch could have prevented it:
>>
>> https://www.spinics.net/lists/stable-commits/msg133055.html
>>
>> this one was queued for 4.14, but i can't find it in git tree?
>> Any idea who/what buried this one?
>>
>> In 4.19 it is present.
>>
>> Because our customer uses 4.14 (going to 4.14.212 in a few days) i kindly want to
>> ask why this patch hasn't entered 4.14.
>
> Because it breaks the build?  Try it yourself and see what happens :)

yep. rt_add_uncached_list is implemented _after_ the call :-(

>
> I will gladly take a working backport if you can submit it.
>
Please find it attached - i needed to move rt_add_uncached_list before
the rt_cache_route, nothing else changed.
This is for 4.14 only, as other kernels do have this patch.

I can't reproduce the crash at will, but at least i could compile and flash it on my target.
And the target comes up .. hopefully the testing in other/newer kernels
done by the developers of the patch is also valid for 4.14.

> thanks,
> greg k-h

Thanks,
Carsten
-----------------
Mentor Graphics (Deutschland) GmbH, Arnulfstrasse 201, 80634 München Registergericht München HRB 106955, Geschäftsführer: Thomas Heurung, Frank Thürauf
From 667f814207fc0aa9b3e620c733d1754ec522c447 Mon Sep 17 00:00:00 2001
From: Wei Wang <[email-masked]>
Date: Wed, 16 Oct 2019 12:03:15 -0700
Subject: [PATCH] ipv4: fix race condition between route lookup and
 invalidation

[ upstream commit 5018c59607a511cdee743b629c76206d9c9e6d7b ]

Jesse and Ido reported the following race condition:
<CPU A, t0> - Received packet A is forwarded and cached dst entry is
taken from the nexthop ('nhc->nhc_rth_input'). Calls skb_dst_set()

<t1> - Given Jesse has busy routers ("ingesting full BGP routing tables
from multiple ISPs"), route is added / deleted and rt_cache_flush() is
called

<CPU B, t2> - Received packet B tries to use the same cached dst entry
from t0, but rt_cache_valid() is no longer true and it is replaced in
rt_cache_route() by the newer one. This calls dst_dev_put() on the
original dst entry which assigns the blackhole netdev to 'dst->dev'

<CPU A, t3> - dst_input(skb) is called on packet A and it is dropped due
to 'dst->dev' being the blackhole netdev

There are 2 issues in the v4 routing code:
1. A per-netns counter is used to do the validation of the route. That
means whenever a route is changed in the netns, users of all routes in
the netns needs to redo lookup. v6 has an implementation of only
updating fn_sernum for routes that are affected.
2. When rt_cache_valid() returns false, rt_cache_route() is called to
throw away the current cache, and create a new one. This seems
unnecessary because as long as this route does not change, the route
cache does not need to be recreated.

To fully solve the above 2 issues, it probably needs quite some code
changes and requires careful testing, and does not suite for net branch.

So this patch only tries to add the deleted cached rt into the uncached
list, so user could still be able to use it to receive packets until
it's done.

Fixes: 95c47f9cf5e0 ("ipv4: call dst_dev_put() properly")
Signed-off-by: Wei Wang <[email-masked]>
Reported-by: Ido Schimmel <[email-masked]>
Reported-by: Jesse Hathaway <[email-masked]>
Tested-by: Jesse Hathaway <[email-masked]>
Acked-by: Martin KaFai Lau <[email-masked]>
Cc: David Ahern <[email-masked]>
Reviewed-by: Ido Schimmel <[email-masked]>
Signed-off-by: David S. Miller <[email-masked]>
Signed-off-by: Carsten Schmid <[email-masked]>
---
 net/ipv4/route.c | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)

diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index c25b3be78e02..7eb8f3048c86 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -1432,6 +1432,24 @@ static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,
 	return ret;
 }
 
+struct uncached_list {
+	spinlock_t		lock;
+	struct list_head	head;
+};
+
+static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt_uncached_list);
+
+static void rt_add_uncached_list(struct rtable *rt)
+{
+	struct uncached_list *ul = raw_cpu_ptr(&rt_uncached_list);
+
+	rt->rt_uncached_list = ul;
+
+	spin_lock_bh(&ul->lock);
+	list_add_tail(&rt->rt_uncached, &ul->head);
+	spin_unlock_bh(&ul->lock);
+}
+
 static bool rt_cache_route(struct fib_nh *nh, struct rtable *rt)
 {
 	struct rtable *orig, *prev, **p;
@@ -1451,7 +1469,7 @@ static bool rt_cache_route(struct fib_nh *nh, struct rtable *rt)
 	prev = cmpxchg(p, orig, rt);
 	if (prev == orig) {
 		if (orig) {
-			dst_dev_put(&orig->dst);
+			rt_add_uncached_list(orig);
 			dst_release(&orig->dst);
 		}
 	} else {
@@ -1462,24 +1480,6 @@ static bool rt_cache_route(struct fib_nh *nh, struct rtable *rt)
 	return ret;
 }
 
-struct uncached_list {
-	spinlock_t		lock;
-	struct list_head	head;
-};
-
-static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt_uncached_list);
-
-static void rt_add_uncached_list(struct rtable *rt)
-{
-	struct uncached_list *ul = raw_cpu_ptr(&rt_uncached_list);
-
-	rt->rt_uncached_list = ul;
-
-	spin_lock_bh(&ul->lock);
-	list_add_tail(&rt->rt_uncached, &ul->head);
-	spin_unlock_bh(&ul->lock);
-}
-
 static void ipv4_dst_destroy(struct dst_entry *dst)
 {
 	struct dst_metrics *p = (struct dst_metrics *)DST_METRICS_PTR(dst);
-- 
2.17.1