Skip to contentSkip to conversation

ipv4: fib: annotate data-race around nh->nh_saddr

PATCH In review Linkui Xiao 3 participants 6 messages Sep 11 – Sep 15

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipv4: fib: annotate data-race around nh->nh_saddr

From: Linkui Xiao <[email protected]>

fib_select_multipath() compares nexthop_nh->nh_saddr against the flow source address with no lock held, while fib_info_update_nhc_saddr() stores a new value from another CPU as soon as the preferred source address of the egress device changes.

Commit 195374d89368 ("ipv4: fib: annotate races around nh->nh_saddr_genid and nh->nh_saddr") added WRITE_ONCE() on the store side and READ_ONCE() in fib_result_prefsrc() after syzbot reported

	BUG: KCSAN: data-race in fib_select_path / fib_select_path

but it only covered that reader. fib_select_multipath(), reached from fib_select_path(), is a second lockless reader of nh->nh_saddr and was left bare. Annotate it as well so that the value cannot be torn or reloaded while the per-nexthop scores are being computed.

Fixes 195374d89368 ("ipv4: fib: annotate races around nh->nh_saddr_genid and nh->nh_saddr")
---
 net/ipv4/fib_semantics.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/fib_semantics.c +1 -1
index 7a362f2e2c2b..885c6fae5232 100644
@@ -2204,7 +2204,7 @@ void fib_select_multipath(struct fib_result *res, int hash,
 		    (use_neigh && !fib_good_nh(nexthop_nh)))
 			continue;
 
-		if (saddr && nexthop_nh->nh_saddr == saddr)
+		if (saddr && READ_ONCE(nexthop_nh->nh_saddr) == saddr)
 			nh_score += 2;
 		if (hash <= nh_upper_bound)
 			nh_score++;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 12 lines · Linkui Xiao

From: Linkui Xiao <[email protected]>

fib_select_multipath() compares nexthop_nh->nh_saddr against the flow source address with no lock held, while fib_info_update_nhc_saddr() stores a new value from another CPU as soon as the preferred source address of the egress device changes.

Commit 195374d89368 ("ipv4: fib: annotate races around nh->nh_saddr_genid and nh->nh_saddr") added WRITE_ONCE() on the store side and READ_ONCE() in fib_result_prefsrc() after syzbot reported

Quoted 5 lines
        BUG: KCSAN: data-race in fib_select_path / fib_select_path

but it only covered that reader. fib_select_multipath(), reached from fib_select_path(), is a second lockless reader of nh->nh_saddr and was left bare.

32607a332cfe added the reader after 195374d89368.

Quoted 25 lines

Annotate it as well so that the value cannot be torn or reloaded while the per-nexthop scores are being computed.

Fixes 195374d89368 ("ipv4: fib: annotate races around nh->nh_saddr_genid and nh->nh_saddr")
---
 net/ipv4/fib_semantics.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/fib_semantics.c +1 -2
index 7a362f2e2c2b..885c6fae5232 100644
@@ -2204,7 +2204,7 @@ void fib_select_multipath(struct fib_result *res, int hash,
                    (use_neigh && !fib_good_nh(nexthop_nh)))
                        continue;
 
-               if (saddr && nexthop_nh->nh_saddr == saddr)
+               if (saddr && READ_ONCE(nexthop_nh->nh_saddr) == saddr)
                        nh_score += 2;
                if (hash <= nh_upper_bound)
                        nh_score++;
--

2.25.1

#2 lore raw
Quoted 14 lines · Kuniyuki Iwashima
Quoted 12 lines · Linkui Xiao

From: Linkui Xiao <[email protected]>

fib_select_multipath() compares nexthop_nh->nh_saddr against the flow source address with no lock held, while fib_info_update_nhc_saddr() stores a new value from another CPU as soon as the preferred source address of the egress device changes.

Commit 195374d89368 ("ipv4: fib: annotate races around nh->nh_saddr_genid and nh->nh_saddr") added WRITE_ONCE() on the store side and READ_ONCE() in fib_result_prefsrc() after syzbot reported

Quoted 8 lines
Quoted 5 lines
        BUG: KCSAN: data-race in fib_select_path / fib_select_path

but it only covered that reader. fib_select_multipath(), reached from fib_select_path(), is a second lockless reader of nh->nh_saddr and was left bare.

32607a332cfe added the reader after 195374d89368.

Indeed, please put in V2:

Fixes 32607a332cfe ("ipv4: prefer multipath nexthop that matches

source address")

pw-bot: cr

#3 lore raw
Quoted 22 lines · Eric Dumazet
Quoted 20 lines · Kuniyuki Iwashima
Quoted 17 lines · Linkui Xiao

From: Linkui Xiao <[email protected]>

fib_select_multipath() compares nexthop_nh->nh_saddr against the flow source address with no lock held, while fib_info_update_nhc_saddr() stores a new value from another CPU as soon as the preferred source address of the egress device changes.

Commit 195374d89368 ("ipv4: fib: annotate races around nh->nh_saddr_genid and nh->nh_saddr") added WRITE_ONCE() on the store side and READ_ONCE() in fib_result_prefsrc() after syzbot reported

        BUG: KCSAN: data-race in fib_select_path / fib_select_path

but it only covered that reader. fib_select_multipath(), reached from fib_select_path(), is a second lockless reader of nh->nh_saddr and was left bare.

Quoted 7 lines
Quoted 2 lines

32607a332cfe added the reader after 195374d89368.

Indeed, please put in V2:

Fixes 32607a332cfe ("ipv4: prefer multipath nexthop that matches

source address")

Adding Willem

It seems that this code also lacks a check against genid?

net/ipv4/fib_semantics.c +0 -0
index 50e96f86ca59ab164f764f5d2185d158c14a4a6a..5a8bbcb65daaada1c3ae96339cabc2e801653dac

100644

net/ipv4/fib_semantics.c +0 -0
@@ -2185,6 +2185,7 @@ void fib_select_multipath(struct fib_result

*res, int hash,

        bool use_neigh;
        int score = -1;
        __be32 saddr;

+ int genid;

        if (unlikely(res->fi->nh)) {
                nexthop_path_fib_result(res, hash);

@@ -2193,6 +2194,7 @@ void fib_select_multipath(struct fib_result *res, int hash,

        use_neigh = READ_ONCE(net->ipv4.sysctl_fib_multipath_use_neigh);
        saddr = fl4 ? fl4->saddr : 0;

+ genid = saddr ? atomic_read(&net->ipv4.dev_addr_genid) : 0;

        change_nexthops(fi) {
                int nh_upper_bound, nh_score = 0;
@@ -2205,7 +2207,9 @@ void fib_select_multipath(struct fib_result
*res, int hash,
                    (use_neigh && !fib_good_nh(nexthop_nh)))
                        continue;

- if (saddr && nexthop_nh->nh_saddr == saddr)
+ if (saddr &&
+ READ_ONCE(nexthop_nh->nh_saddr_genid) == genid &&
+ READ_ONCE(nexthop_nh->nh_saddr) == saddr)

                        nh_score += 2;
                if (hash <= nh_upper_bound)
                        nh_score++;
#4 lore raw
Quoted 26 lines · Eric Dumazet
Quoted 25 lines · Eric Dumazet
Quoted 22 lines · Kuniyuki Iwashima
Quoted 12 lines · Linkui Xiao

From: Linkui Xiao <[email protected]>

fib_select_multipath() compares nexthop_nh->nh_saddr against the flow source address with no lock held, while fib_info_update_nhc_saddr() stores a new value from another CPU as soon as the preferred source address of the egress device changes.

Commit 195374d89368 ("ipv4: fib: annotate races around nh->nh_saddr_genid and nh->nh_saddr") added WRITE_ONCE() on the store side and READ_ONCE() in fib_result_prefsrc() after syzbot reported

Quoted 5 lines
         BUG: KCSAN: data-race in fib_select_path / fib_select_path

but it only covered that reader. fib_select_multipath(), reached from fib_select_path(), is a second lockless reader of nh->nh_saddr and was left bare.

32607a332cfe added the reader after 195374d89368.

Quoted 8 lines
Quoted 4 lines

Indeed, please put in V2:

Fixes 32607a332cfe ("ipv4: prefer multipath nexthop that matches

source address")

Adding Willem

It seems that this code also lacks a check against genid?

Thanks Eric and Kuniyuki for the correction. I'll send a V2 with Fixes: 32607a332cfe.

Kuniyuki, good catch on the missing genid check. nh_saddr is only meaningful when nh_saddr_genid matches dev_addr_genid, and fib_select_multipath() currently skips that validation. I'll fold the genid check into V2 along with the READ_ONCE annotations, unless you'd prefer to send it as a separate patch. Let me know.

Best regards,
Linkui Xiao

Quoted 36 lines
net/ipv4/fib_semantics.c +0 -0
index 50e96f86ca59ab164f764f5d2185d158c14a4a6a..5a8bbcb65daaada1c3ae96339cabc2e801653dac

100644

net/ipv4/fib_semantics.c +1 -0
@@ -2185,6 +2185,7 @@ void fib_select_multipath(struct fib_result
 *res, int hash,
         bool use_neigh;
         int score = -1;
         __be32 saddr;
+       int genid;
 
         if (unlikely(res->fi->nh)) {
                 nexthop_path_fib_result(res, hash);
net/ipv4/fib_semantics.c +1 -0
@@ -2193,6 +2194,7 @@ void fib_select_multipath(struct fib_result
 *res, int hash,
 
         use_neigh = READ_ONCE(net->ipv4.sysctl_fib_multipath_use_neigh);
         saddr = fl4 ? fl4->saddr : 0;
+       genid = saddr ? atomic_read(&net->ipv4.dev_addr_genid) : 0;
 
         change_nexthops(fi) {
                 int nh_upper_bound, nh_score = 0;
net/ipv4/fib_semantics.c +3 -1
@@ -2205,7 +2207,9 @@ void fib_select_multipath(struct fib_result
 *res, int hash,
                     (use_neigh && !fib_good_nh(nexthop_nh)))
                         continue;
 
-               if (saddr && nexthop_nh->nh_saddr == saddr)
+               if (saddr &&
+                   READ_ONCE(nexthop_nh->nh_saddr_genid) == genid &&
+                   READ_ONCE(nexthop_nh->nh_saddr) == saddr)
                         nh_score += 2;
                 if (hash <= nh_upper_bound)
                         nh_score++;
#5 lore raw
Quoted 38 lines · Linkui Xiao
Quoted 26 lines · Eric Dumazet
Quoted 25 lines · Eric Dumazet
Quoted 22 lines · Kuniyuki Iwashima
Quoted 12 lines · Linkui Xiao

From: Linkui Xiao <[email protected]>

fib_select_multipath() compares nexthop_nh->nh_saddr against the flow source address with no lock held, while fib_info_update_nhc_saddr() stores a new value from another CPU as soon as the preferred source address of the egress device changes.

Commit 195374d89368 ("ipv4: fib: annotate races around nh->nh_saddr_genid and nh->nh_saddr") added WRITE_ONCE() on the store side and READ_ONCE() in fib_result_prefsrc() after syzbot reported

Quoted 5 lines
         BUG: KCSAN: data-race in fib_select_path / fib_select_path

but it only covered that reader. fib_select_multipath(), reached from fib_select_path(), is a second lockless reader of nh->nh_saddr and was left bare.

32607a332cfe added the reader after 195374d89368.

Quoted 8 lines
Quoted 4 lines

Indeed, please put in V2:

Fixes 32607a332cfe ("ipv4: prefer multipath nexthop that matches

source address")

Adding Willem

It seems that this code also lacks a check against genid?

Quoted 9 lines

Thanks Eric and Kuniyuki for the correction. I'll send a V2 with Fixes:
32607a332cfe.

Kuniyuki, good catch on the missing genid check. nh_saddr is only meaningful when nh_saddr_genid matches dev_addr_genid, and fib_select_multipath() currently skips that validation. I'll fold the genid check into V2 along with the READ_ONCE annotations, unless you'd prefer to send it as a separate patch. Let me know.

I (Eric) was the one who mentioned the genid thing :)

Send a V2 with both bugs fixed.
Thanks

Quoted 39 lines

Best regards,
Linkui Xiao

Quoted 36 lines
net/ipv4/fib_semantics.c +0 -0
index 50e96f86ca59ab164f764f5d2185d158c14a4a6a..5a8bbcb65daaada1c3ae96339cabc2e801653dac

100644

net/ipv4/fib_semantics.c +1 -0
@@ -2185,6 +2185,7 @@ void fib_select_multipath(struct fib_result
 *res, int hash,
         bool use_neigh;
         int score = -1;
         __be32 saddr;
+       int genid;
 
         if (unlikely(res->fi->nh)) {
                 nexthop_path_fib_result(res, hash);
net/ipv4/fib_semantics.c +1 -0
@@ -2193,6 +2194,7 @@ void fib_select_multipath(struct fib_result
 *res, int hash,
 
         use_neigh = READ_ONCE(net->ipv4.sysctl_fib_multipath_use_neigh);
         saddr = fl4 ? fl4->saddr : 0;
+       genid = saddr ? atomic_read(&net->ipv4.dev_addr_genid) : 0;
 
         change_nexthops(fi) {
                 int nh_upper_bound, nh_score = 0;
net/ipv4/fib_semantics.c +3 -1
@@ -2205,7 +2207,9 @@ void fib_select_multipath(struct fib_result
 *res, int hash,
                     (use_neigh && !fib_good_nh(nexthop_nh)))
                         continue;
 
-               if (saddr && nexthop_nh->nh_saddr == saddr)
+               if (saddr &&
+                   READ_ONCE(nexthop_nh->nh_saddr_genid) == genid &&
+                   READ_ONCE(nexthop_nh->nh_saddr) == saddr)
                         nh_score += 2;
                 if (hash <= nh_upper_bound)
                         nh_score++;