netlink: fix potential race issue in netlink_native_seq_show()
Subsystems (2)
PATCH 1/1 netlink: fix potential race issue in netlink_native_seq_show()
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.
--- net/netlink/af_netlink.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/netlink/af_netlink.c +2 -1
@@ -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 ; }
Signature · 1 line
2.25.1
Quoted 6 lines · nai lin
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 <[email protected]>
OK, I think you forgot to include this tag I suggested earlier to you.
protected hash table")
net/netlink/af_netlink.c static int netlink_native_seq_show(struct seq_file *seq, void *v) · line 2696
Quoted 13 lines
--- net/netlink/af_netlink.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)net/netlink/af_netlink.c +1
-0@@ -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.
net/netlink/af_netlink.c +0 -0
100644
net/netlink/af_netlink.c +0 -0
@@ -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),Quoted 6 lines · Eric Dumazet
Quoted 5 lines · nai lin
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.
Quoted 7 lines
Quoted 1 line
Signed-off-by nai lin <[email protected]>OK, I think you forgot to include this tag I suggested earlier to you.
Fixes e341694e3eb5 ("netlink: Convert netlink_lookup() to use RCUprotected hash table")
Sorry I forgot to include fix tag, thank you for pointing out my problem and thank you very much for your work.
Quoted 66 lines
net/netlink/af_netlink.c static int netlink_native_seq_show(struct seq_file *seq, void *v) · line 2696
Quoted 13 lines
--- net/netlink/af_netlink.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)net/netlink/af_netlink.c +1
-0@@ -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.
net/netlink/af_netlink.c +0
-0100644
net/netlink/af_netlink.c +12
-0@@ -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){
net/netlink/af_netlink.c +1
-3@@ -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);}
net/netlink/af_netlink.c +1
-1@@ -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),