Skip to content

Commit 1904fb9

Browse files
committed
netlink: terminate outstanding dump on socket close
Netlink supports iterative dumping of data. It provides the families the following ops: - start - (optional) kicks off the dumping process - dump - actual dump helper, keeps getting called until it returns 0 - done - (optional) pairs with .start, can be used for cleanup The whole process is asynchronous and the repeated calls to .dump don't actually happen in a tight loop, but rather are triggered in response to recvmsg() on the socket. This gives the user full control over the dump, but also means that the user can close the socket without getting to the end of the dump. To make sure .start is always paired with .done we check if there is an ongoing dump before freeing the socket, and if so call .done. The complication is that sockets can get freed from BH and .done is allowed to sleep. So we use a workqueue to defer the call, when needed. Unfortunately this does not work correctly. What we defer is not the cleanup but rather releasing a reference on the socket. We have no guarantee that we own the last reference, if someone else holds the socket they may release it in BH and we're back to square one. The whole dance, however, appears to be unnecessary. Only the user can interact with dumps, so we can clean up when socket is closed. And close always happens in process context. Some async code may still access the socket after close, queue notification skbs to it etc. but no dumps can start, end or otherwise make progress. Delete the workqueue and flush the dump state directly from the release handler. Note that further cleanup is possible in -next, for instance we now always call .done before releasing the main module reference, so dump doesn't have to take a reference of its own. Reported-by: syzkaller <[email protected]> Fixes: ed5d778 ("netlink: Do not schedule work from sk_destruct") Reviewed-by: Kuniyuki Iwashima <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent bfc64d9 commit 1904fb9

File tree

2 files changed

+8
-25
lines changed

2 files changed

+8
-25
lines changed

net/netlink/af_netlink.c

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,6 @@ static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
393393

394394
static void netlink_sock_destruct(struct sock *sk)
395395
{
396-
struct netlink_sock *nlk = nlk_sk(sk);
397-
398-
if (nlk->cb_running) {
399-
if (nlk->cb.done)
400-
nlk->cb.done(&nlk->cb);
401-
module_put(nlk->cb.module);
402-
kfree_skb(nlk->cb.skb);
403-
}
404-
405396
skb_queue_purge(&sk->sk_receive_queue);
406397

407398
if (!sock_flag(sk, SOCK_DEAD)) {
@@ -414,14 +405,6 @@ static void netlink_sock_destruct(struct sock *sk)
414405
WARN_ON(nlk_sk(sk)->groups);
415406
}
416407

417-
static void netlink_sock_destruct_work(struct work_struct *work)
418-
{
419-
struct netlink_sock *nlk = container_of(work, struct netlink_sock,
420-
work);
421-
422-
sk_free(&nlk->sk);
423-
}
424-
425408
/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
426409
* SMP. Look, when several writers sleep and reader wakes them up, all but one
427410
* immediately hit write lock and grab all the cpus. Exclusive sleep solves
@@ -731,12 +714,6 @@ static void deferred_put_nlk_sk(struct rcu_head *head)
731714
if (!refcount_dec_and_test(&sk->sk_refcnt))
732715
return;
733716

734-
if (nlk->cb_running && nlk->cb.done) {
735-
INIT_WORK(&nlk->work, netlink_sock_destruct_work);
736-
schedule_work(&nlk->work);
737-
return;
738-
}
739-
740717
sk_free(sk);
741718
}
742719

@@ -788,6 +765,14 @@ static int netlink_release(struct socket *sock)
788765
NETLINK_URELEASE, &n);
789766
}
790767

768+
/* Terminate any outstanding dump */
769+
if (nlk->cb_running) {
770+
if (nlk->cb.done)
771+
nlk->cb.done(&nlk->cb);
772+
module_put(nlk->cb.module);
773+
kfree_skb(nlk->cb.skb);
774+
}
775+
791776
module_put(nlk->module);
792777

793778
if (netlink_is_kernel(sk)) {

net/netlink/af_netlink.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <linux/rhashtable.h>
66
#include <linux/atomic.h>
7-
#include <linux/workqueue.h>
87
#include <net/sock.h>
98

109
/* flags */
@@ -50,7 +49,6 @@ struct netlink_sock {
5049

5150
struct rhash_head node;
5251
struct rcu_head rcu;
53-
struct work_struct work;
5452
};
5553

5654
static inline struct netlink_sock *nlk_sk(struct sock *sk)

0 commit comments

Comments
 (0)