Skip to content

Commit 6c8f7e7

Browse files
tgrafdavem330
authored andcommitted
netlink: hold nl_sock_hash_lock during diag dump
Although RCU protection would be possible during diag dump, doing so allows for concurrent table mutations which can render the in-table offset between individual Netlink messages invalid and thus cause legitimate sockets to be skipped in the dump. Since the diag dump is relatively low volume and consistency is more important than performance, the table mutex is held during dump. Reported-by: Andrey Wagin <avagin@gmail.com> Signed-off-by: Thomas Graf <tgraf@suug.ch> Fixes: e341694 ("netlink: Convert netlink_lookup() to use RCU protected hash table") Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 9ea88a1 commit 6c8f7e7

3 files changed

Lines changed: 5 additions & 0 deletions

File tree

‎net/netlink/af_netlink.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ static atomic_t nl_table_users = ATOMIC_INIT(0);
104104

105105
/* Protects netlink socket hash table mutations */
106106
DEFINE_MUTEX(nl_sk_hash_lock);
107+
EXPORT_SYMBOL_GPL(nl_sk_hash_lock);
107108

108109
static int lockdep_nl_sk_hash_is_held(void)
109110
{

‎net/netlink/af_netlink.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,6 @@ struct netlink_table {
7373

7474
extern struct netlink_table *nl_table;
7575
extern rwlock_t nl_table_lock;
76+
extern struct mutex nl_sk_hash_lock;
7677

7778
#endif

‎net/netlink/diag.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
170170

171171
req = nlmsg_data(cb->nlh);
172172

173+
mutex_lock(&nl_sk_hash_lock);
173174
read_lock(&nl_table_lock);
174175

175176
if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
@@ -183,13 +184,15 @@ static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
183184
} else {
184185
if (req->sdiag_protocol >= MAX_LINKS) {
185186
read_unlock(&nl_table_lock);
187+
mutex_unlock(&nl_sk_hash_lock);
186188
return -ENOENT;
187189
}
188190

189191
__netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
190192
}
191193

192194
read_unlock(&nl_table_lock);
195+
mutex_unlock(&nl_sk_hash_lock);
193196

194197
return skb->len;
195198
}

0 commit comments

Comments
 (0)