* [PATCH] netlink: fix potential race issue in netlink_native_seq_show()
@ 2024-01-17 8:37 nai lin
2024-01-17 10:28 ` Eric Dumazet
0 siblings, 1 reply; 3+ messages in thread
From: nai lin @ 2024-01-17 8:37 UTC (permalink / raw)
To: netdev
Cc: nai lin, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Liam R. Howlett, Anjali Kulkarni,
Li RongQing, David Howells, Kuniyuki Iwashima, linux-kernel
Access to the nlk group should be protected by netlink_lock_table() like
commit <f773608026ee> ("netlink: access nlk groups safely in netlink bind
and getname"), otherwise there will be potential race conditions.
Signed-off-by: nai lin <ayano2023th@gmail.com>
---
net/netlink/af_netlink.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index 4ed8ffd58ff3..61ad81fb80f5 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -2693,6 +2693,7 @@ static int netlink_native_seq_show(struct seq_file *seq, void *v)
struct sock *s = v;
struct netlink_sock *nlk = nlk_sk(s);
+ netlink_lock_table();
seq_printf(seq, "%pK %-3d %-10u %08x %-8d %-8d %-5d %-8d %-8u %-8lu\n",
s,
s->sk_protocol,
@@ -2705,7 +2706,7 @@ static int netlink_native_seq_show(struct seq_file *seq, void *v)
atomic_read(&s->sk_drops),
sock_i_ino(s)
);
-
+ netlink_unlock_table();
}
return 0;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] netlink: fix potential race issue in netlink_native_seq_show() 2024-01-17 8:37 [PATCH] netlink: fix potential race issue in netlink_native_seq_show() nai lin @ 2024-01-17 10:28 ` Eric Dumazet 2024-01-18 2:15 ` nai lin 0 siblings, 1 reply; 3+ messages in thread From: Eric Dumazet @ 2024-01-17 10:28 UTC (permalink / raw) To: nai lin Cc: netdev, David S. Miller, Jakub Kicinski, Paolo Abeni, Simon Horman, Liam R. Howlett, Anjali Kulkarni, Li RongQing, David Howells, Kuniyuki Iwashima, linux-kernel On Wed, Jan 17, 2024 at 9:38 AM nai lin <ayano2023th@gmail.com> wrote: > > Access to the nlk group should be protected by netlink_lock_table() like > commit <f773608026ee> ("netlink: access nlk groups safely in netlink bind > and getname"), otherwise there will be potential race conditions. > > Signed-off-by: nai lin <ayano2023th@gmail.com> OK, I think you forgot to include this tag I suggested earlier to you. Fixes: e341694e3eb5 ("netlink: Convert netlink_lookup() to use RCU protected hash table") > --- > net/netlink/af_netlink.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c > index 4ed8ffd58ff3..61ad81fb80f5 100644 > --- a/net/netlink/af_netlink.c > +++ b/net/netlink/af_netlink.c > @@ -2693,6 +2693,7 @@ static int netlink_native_seq_show(struct seq_file *seq, void *v) > struct sock *s = v; > struct netlink_sock *nlk = nlk_sk(s); > > + netlink_lock_table(); netlink_lock_table() is heavy weight, appropriate for contexts where we might sleep. We could instead use a helper to acquire the lock for a very small period. diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c index 4ed8ffd58ff375f3fa9f262e6f3b4d1a1aaf2731..c50ca0f5adfb9691e6df37b4ac518b95c2d7908f 100644 --- a/net/netlink/af_netlink.c +++ b/net/netlink/af_netlink.c @@ -1136,6 +1136,18 @@ static int netlink_connect(struct socket *sock, struct sockaddr *addr, return err; } +static u32 netlink_get_groups_mask(const struct netlink_sock *nlk) +{ + unsigned long flags; + u32 res; + + read_lock_irqsave(&nl_table_lock, flags); + res = nlk->groups ? nlk->groups[0] : 0; + read_unlock_irqrestore(&nl_table_lock, flags); + + return res; +} + static int netlink_getname(struct socket *sock, struct sockaddr *addr, int peer) { @@ -1153,9 +1165,7 @@ static int netlink_getname(struct socket *sock, struct sockaddr *addr, } else { /* Paired with WRITE_ONCE() in netlink_insert() */ nladdr->nl_pid = READ_ONCE(nlk->portid); - netlink_lock_table(); - nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0; - netlink_unlock_table(); + nladdr->nl_groups = netlink_get_groups_mask(nlk); } return sizeof(*nladdr); } @@ -2697,7 +2707,7 @@ static int netlink_native_seq_show(struct seq_file *seq, void *v) s, s->sk_protocol, nlk->portid, - nlk->groups ? (u32)nlk->groups[0] : 0, + netlink_get_groups_mask(nlk), sk_rmem_alloc_get(s), sk_wmem_alloc_get(s), READ_ONCE(nlk->cb_running), ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] netlink: fix potential race issue in netlink_native_seq_show() 2024-01-17 10:28 ` Eric Dumazet @ 2024-01-18 2:15 ` nai lin 0 siblings, 0 replies; 3+ messages in thread From: nai lin @ 2024-01-18 2:15 UTC (permalink / raw) To: Eric Dumazet Cc: netdev, David S. Miller, Jakub Kicinski, Paolo Abeni, Simon Horman, Liam R. Howlett, Anjali Kulkarni, Li RongQing, David Howells, Kuniyuki Iwashima, linux-kernel On Wed, Jan 17, 2024 at 11:28:24AM +0100, Eric Dumazet wrote: > On Wed, Jan 17, 2024 at 9:38 AM nai lin <ayano2023th@gmail.com> wrote: > > > > Access to the nlk group should be protected by netlink_lock_table() like > > commit <f773608026ee> ("netlink: access nlk groups safely in netlink bind > > and getname"), otherwise there will be potential race conditions. > > > > Signed-off-by: nai lin <ayano2023th@gmail.com> > > OK, I think you forgot to include this tag I suggested earlier to you. > > Fixes: e341694e3eb5 ("netlink: Convert netlink_lookup() to use RCU > protected hash table") > Sorry I forgot to include fix tag, thank you for pointing out my problem and thank you very much for your work. > > --- > > net/netlink/af_netlink.c | 3 ++- > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c > > index 4ed8ffd58ff3..61ad81fb80f5 100644 > > --- a/net/netlink/af_netlink.c > > +++ b/net/netlink/af_netlink.c > > @@ -2693,6 +2693,7 @@ static int netlink_native_seq_show(struct seq_file *seq, void *v) > > struct sock *s = v; > > struct netlink_sock *nlk = nlk_sk(s); > > > > + netlink_lock_table(); > > netlink_lock_table() is heavy weight, appropriate for contexts where > we might sleep. > > We could instead use a helper to acquire the lock for a very small period. > > diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c > index 4ed8ffd58ff375f3fa9f262e6f3b4d1a1aaf2731..c50ca0f5adfb9691e6df37b4ac518b95c2d7908f > 100644 > --- a/net/netlink/af_netlink.c > +++ b/net/netlink/af_netlink.c > @@ -1136,6 +1136,18 @@ static int netlink_connect(struct socket *sock, > struct sockaddr *addr, > return err; > } > > +static u32 netlink_get_groups_mask(const struct netlink_sock *nlk) > +{ > + unsigned long flags; > + u32 res; > + > + read_lock_irqsave(&nl_table_lock, flags); > + res = nlk->groups ? nlk->groups[0] : 0; > + read_unlock_irqrestore(&nl_table_lock, flags); > + > + return res; > +} > + > static int netlink_getname(struct socket *sock, struct sockaddr *addr, > int peer) > { > @@ -1153,9 +1165,7 @@ static int netlink_getname(struct socket *sock, > struct sockaddr *addr, > } else { > /* Paired with WRITE_ONCE() in netlink_insert() */ > nladdr->nl_pid = READ_ONCE(nlk->portid); > - netlink_lock_table(); > - nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0; > - netlink_unlock_table(); > + nladdr->nl_groups = netlink_get_groups_mask(nlk); > } > return sizeof(*nladdr); > } > @@ -2697,7 +2707,7 @@ static int netlink_native_seq_show(struct > seq_file *seq, void *v) > s, > s->sk_protocol, > nlk->portid, > - nlk->groups ? (u32)nlk->groups[0] : 0, > + netlink_get_groups_mask(nlk), > sk_rmem_alloc_get(s), > sk_wmem_alloc_get(s), > READ_ONCE(nlk->cb_running), ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-01-18 2:15 UTC | newest] Thread overview: 3+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-01-17 8:37 [PATCH] netlink: fix potential race issue in netlink_native_seq_show() nai lin 2024-01-17 10:28 ` Eric Dumazet 2024-01-18 2:15 ` nai lin
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox