ipv4: fib: annotate data-race around nh->nh_saddr
Subsystems (3)
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.
--- net/ipv4/fib_semantics.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/fib_semantics.c +1 -1
@@ -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
Quoted 12 lines · Linkui Xiao
From: Linkui Xiao <[email protected]>
fib_select_multipath()comparesnexthop_nh->nh_saddragainst the flow source address with no lock held, whilefib_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 aroundnh->nh_saddr_genidandnh->nh_saddr") addedWRITE_ONCE()on the store side andREAD_ONCE()infib_result_prefsrc()after syzbot reported
Quoted 5 lines
BUG: KCSAN: data-race in fib_select_path / fib_select_pathbut it only covered that reader.
fib_select_multipath(), reached fromfib_select_path(), is a second lockless reader ofnh->nh_saddrand 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")Signed-off-by Linkui Xiao <[email protected]>--- net/ipv4/fib_semantics.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/ipv4/fib_semantics.c +1
-2@@ -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
Quoted 14 lines · Kuniyuki Iwashima
Quoted 12 lines · Linkui Xiao
From: Linkui Xiao <[email protected]>
fib_select_multipath()comparesnexthop_nh->nh_saddragainst the flow source address with no lock held, whilefib_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 aroundnh->nh_saddr_genidandnh->nh_saddr") addedWRITE_ONCE()on the store side andREAD_ONCE()infib_result_prefsrc()after syzbot reported
Quoted 8 lines
Quoted 5 lines
BUG: KCSAN: data-race in fib_select_path / fib_select_pathbut it only covered that reader.
fib_select_multipath(), reached fromfib_select_path(), is a second lockless reader ofnh->nh_saddrand was left bare.
32607a332cfeadded the reader after195374d89368.
Indeed, please put in V2:
source address")
pw-bot: cr
Quoted 22 lines · Eric Dumazet
Quoted 20 lines · Kuniyuki Iwashima
Quoted 17 lines · Linkui Xiao
From: Linkui Xiao <[email protected]>
fib_select_multipath()comparesnexthop_nh->nh_saddragainst the flow source address with no lock held, whilefib_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 aroundnh->nh_saddr_genidandnh->nh_saddr") addedWRITE_ONCE()on the store side andREAD_ONCE()infib_result_prefsrc()after syzbot reportedBUG: KCSAN: data-race in fib_select_path / fib_select_pathbut it only covered that reader.
fib_select_multipath(), reached fromfib_select_path(), is a second lockless reader ofnh->nh_saddrand was left bare.
Quoted 7 lines
Quoted 2 lines
32607a332cfeadded the reader after195374d89368.Indeed, please put in V2:
Fixes 32607a332cfe ("ipv4: prefer multipath nexthop that matchessource address")
Adding Willem
It seems that this code also lacks a check against genid?
net/ipv4/fib_semantics.c +0 -0
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++;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()comparesnexthop_nh->nh_saddragainst the flow source address with no lock held, whilefib_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 aroundnh->nh_saddr_genidandnh->nh_saddr") addedWRITE_ONCE()on the store side andREAD_ONCE()infib_result_prefsrc()after syzbot reportedQuoted 5 lines
BUG: KCSAN: data-race in fib_select_path / fib_select_pathbut it only covered that reader.
fib_select_multipath(), reached fromfib_select_path(), is a second lockless reader ofnh->nh_saddrand was left bare.
32607a332cfeadded the reader after195374d89368.
Quoted 8 lines
Quoted 4 lines
Indeed, please put in V2:
Fixes 32607a332cfe ("ipv4: prefer multipath nexthop that matchessource 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
-0100644
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++;
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()comparesnexthop_nh->nh_saddragainst the flow source address with no lock held, whilefib_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 aroundnh->nh_saddr_genidandnh->nh_saddr") addedWRITE_ONCE()on the store side andREAD_ONCE()infib_result_prefsrc()after syzbot reportedQuoted 5 lines
BUG: KCSAN: data-race in fib_select_path / fib_select_pathbut it only covered that reader.
fib_select_multipath(), reached fromfib_select_path(), is a second lockless reader ofnh->nh_saddrand was left bare.
32607a332cfeadded the reader after195374d89368.Quoted 8 lines
Quoted 4 lines
Indeed, please put in V2:
Fixes 32607a332cfe ("ipv4: prefer multipath nexthop that matchessource 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_saddris only meaningful whennh_saddr_genidmatchesdev_addr_genid, andfib_select_multipath()currently skips that validation. I'll fold the genid check into V2 along with theREAD_ONCEannotations, 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 XiaoQuoted 36 lines
net/ipv4/fib_semantics.c +0
-0100644
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++;