tipc: call start and done ops directly in __tipc_nl_compat_dumpit()
Subsystems (3)
PATCH 1/1 tipc: call start and done ops directly in __tipc_nl_compat_dumpit()
__tipc_nl_compat_dumpit() uses a netlink_callback on stack, so the only way to align it with other ->dumpit() call path is calling tipc_dump_start() and tipc_dump_done() directly inside it. Otherwise ->dumpit() would always get NULL from cb->args[0].
But, tipc_dump_start() uses sock_net(cb->skb->sk) to retrieve net pointer, the cb->skb here doesn't set ->sk, the net pointer is saved in msg->net instead, so introduce a helper function __tipc_dump_start() to pass in msg->net.
--- net/tipc/netlink_compat.c | 2 ++ net/tipc/socket.c | 8 ++++++-- net/tipc/socket.h | 1 + 3 files changed, 9 insertions(+), 2 deletions(-)
net/tipc/netlink_compat.c +2 -0
@@ -185,6 +185,7 @@ static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,return -ENOMEM ; buf->sk = msg->dst_sk; +__tipc_dump_start (&cb, msg->net); do { int rem;
@@ -216,6 +217,7 @@ static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd, err =0 ; err_out: +tipc_dump_done (&cb);kfree_skb (buf);if (err == -EMSGSIZE ) {
net/tipc/socket.c +6 -2
@@ -3264,9 +3264,14 @@ int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,EXPORT_SYMBOL (tipc_nl_sk_walk);int tipc_dump_start (struct netlink_callback *cb) +{ +return __tipc_dump_start (cb,sock_net (cb->skb->sk)); +} +EXPORT_SYMBOL (tipc_dump_start); + +int __tipc_dump_start (struct netlink_callback *cb,struct net *net) {struct rhashtable_iter *iter = (void *)cb->args[0 ]; -struct net *net =sock_net (cb->skb->sk);struct tipc_net *tn =tipc_net (net);if (!iter) {
@@ -3280,7 +3285,6 @@ int tipc_dump_start(struct netlink_callback *cb)rhashtable_walk_enter (&tn->sk_rht, iter);return 0 ; } -EXPORT_SYMBOL (tipc_dump_start);int tipc_dump_done (struct netlink_callback *cb) {
net/tipc/socket.h +1 -0
@@ -69,5 +69,6 @@ int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,struct netlink_callback *cb, struct tipc_sock *tsk));int tipc_dump_start (struct netlink_callback *cb); +int __tipc_dump_start (struct netlink_callback *cb,struct net *net);int tipc_dump_done (struct netlink_callback *cb);#endif
Signature · 1 line
2.14.4
PATCH 1/1 tipc: orphan sock in tipc_release()
Before we unlock the sock in tipc_release(), we have to detach sk->sk_socket from sk, otherwise a parallel tipc_sk_fill_sock_diag() could stil read it after we free this socket.
--- net/tipc/socket.c | 1 + 1 file changed, 1 insertion(+)
net/tipc/socket.c +1 -0
@@ -576,6 +576,7 @@ static int tipc_release(struct socket *sock)sk_stop_timer (sk, &sk->sk_timer);tipc_sk_remove (tsk); +sock_orphan (sk);/* Reject any messages that accumulated in backlog queue */ release_sock (sk);tipc_dest_list_purge (&tsk->cong_links);
Signature · 1 line
2.14.4
Re: [Patch net] tipc: orphan sock in tipc_release()
Quoted 10 lines · Cong Wang
Before we unlock the sock in
tipc_release(), we have to detachsk->sk_socketfrom sk, otherwise a paralleltipc_sk_fill_sock_diag()could stil read it after we free this socket.Fixes c30b70deb5f4 ("tipc: implement socket diagnostics for AF_TIPC")Reported-and-tested-by [email protected]Cc Jon Maloy <[email protected]>Cc Ying Xue <[email protected]>Signed-off-by Cong Wang <[email protected]>
Quoted 10 lines
--- net/tipc/socket.c | 1 + 1 file changed, 1 insertion(+)net/tipc/socket.c +0
-0
net/tipc/socket.c static int tipc_release(struct socket *sock) · line 582
Quoted 7 lines
net/tipc/socket.c +1
-0@@ -576,6 +576,7 @@ static int tipc_release(struct socket *sock)sk_stop_timer (sk, &sk->sk_timer);tipc_sk_remove (tsk); +sock_orphan (sk);/* Reject any messages that accumulated in backlog queue */ release_sock (sk);tipc_dest_list_purge (&tsk->cong_links);
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
David Miller
maintainerRe: [Patch net] tipc: orphan sock in tipc_release()
In reply to Cong WangFrom: Cong Wang <[email protected]>
Date: Mon, 3 Sep 2018 19:12:41 -0700
Quoted 10 lines
Before we unlock the sock in
tipc_release(), we have to detachsk->sk_socketfrom sk, otherwise a paralleltipc_sk_fill_sock_diag()could stil read it after we free this socket.Fixes c30b70deb5f4 ("tipc: implement socket diagnostics for AF_TIPC")Reported-and-tested-by [email protected]Cc Jon Maloy <[email protected]>Cc Ying Xue <[email protected]>Signed-off-by Cong Wang <[email protected]>
Applied and queued up for -stable, thanks.
Quoted 5 lines · Cong Wang
__tipc_nl_compat_dumpit()uses anetlink_callbackon stack, so the only way to align it with other ->dumpit() call path is callingtipc_dump_start()andtipc_dump_done()directly inside it. Otherwise ->dumpit() would always get NULL from cb->args[0].
Thank you for your fix Cong!
Your solution is fine with me.
When we align __tipc_nl_compat_dumpit() with ->dumpit() functions defined in tipc_genl_v2_ops[], cb->args[0] is used to save a rhashtable_iter object allocated in tipc_dump_start(), and the object will be retrieved with cb->args[0] in tipc_dump_done() and will be freed.
But unfortunately cb->args[0] has been used to other purposes in tipc_nl_bearer_dump(), tipc_nl_node_dump_link(), tipc_nl_name_table_dump(), tipc_nl_node_dump() and tipc_nl_net_dump(). It means cb->args[0] saved in __tipc_dump_start() will be overwritten in these ->dumpit() functions. As a consequence, not only the rhashtable_iter object allocated in tipc_dump_start() cannot be properly released in tipc_dump_done(), but also more kernel panics might be triggered in tipc_dump_done().
Quoted 77 lines
But,
tipc_dump_start()usessock_net(cb->skb->sk)to retrieve net pointer, thecb->skbhere doesn't set ->sk, the net pointer is saved inmsg->netinstead, so introduce a helper function__tipc_dump_start()to pass inmsg->net.Fixes 9a07efa9aea2 ("tipc: switch to rhashtable iterator")Reported-by [email protected]Cc Jon Maloy <[email protected]>Cc Ying Xue <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/tipc/netlink_compat.c | 2 ++ net/tipc/socket.c | 8 ++++++-- net/tipc/socket.h | 1 + 3 files changed, 9 insertions(+), 2 deletions(-)net/tipc/netlink_compat.c +2
-0@@ -185,6 +185,7 @@ static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,return -ENOMEM ; buf->sk = msg->dst_sk; +__tipc_dump_start (&cb, msg->net); do { int rem;@@ -216,6 +217,7 @@ static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd, err =0 ; err_out: +tipc_dump_done (&cb);kfree_skb (buf);if (err == -EMSGSIZE ) {net/tipc/socket.c +6
-2@@ -3264,9 +3264,14 @@ int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,EXPORT_SYMBOL (tipc_nl_sk_walk);int tipc_dump_start (struct netlink_callback *cb) +{ +return __tipc_dump_start (cb,sock_net (cb->skb->sk)); +} +EXPORT_SYMBOL (tipc_dump_start); + +int __tipc_dump_start (struct netlink_callback *cb,struct net *net) {struct rhashtable_iter *iter = (void *)cb->args[0 ]; -struct net *net =sock_net (cb->skb->sk);struct tipc_net *tn =tipc_net (net);if (!iter) {@@ -3280,7 +3285,6 @@ int tipc_dump_start(struct netlink_callback *cb)rhashtable_walk_enter (&tn->sk_rht, iter);return 0 ; } -EXPORT_SYMBOL (tipc_dump_start);int tipc_dump_done (struct netlink_callback *cb) {net/tipc/socket.h +1
-0@@ -69,5 +69,6 @@ int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,struct netlink_callback *cb, struct tipc_sock *tsk));int tipc_dump_start (struct netlink_callback *cb); +int __tipc_dump_start (struct netlink_callback *cb,struct net *net);int tipc_dump_done (struct netlink_callback *cb);#endif
Quoted 18 lines · Ying Xue
Quoted 5 lines · Cong Wang
__tipc_nl_compat_dumpit()uses anetlink_callbackon stack, so the only way to align it with other ->dumpit() call path is callingtipc_dump_start()andtipc_dump_done()directly inside it. Otherwise ->dumpit() would always get NULL from cb->args[0].Thank you for your fix Cong!
Your solution is fine with me.
When we align
__tipc_nl_compat_dumpit()with ->dumpit() functions defined in tipc_genl_v2_ops[], cb->args[0] is used to save arhashtable_iterobject allocated intipc_dump_start(), and the object will be retrieved with cb->args[0] intipc_dump_done()and will be freed.
Quoted 8 lines
But unfortunately cb->args[0] has been used to other purposes in
tipc_nl_bearer_dump(),tipc_nl_node_dump_link(),tipc_nl_name_table_dump(),tipc_nl_node_dump()andtipc_nl_net_dump(). It means cb->args[0] saved in__tipc_dump_start()will be overwritten in these ->dumpit() functions. As a consequence, not only therhashtable_iterobject allocated intipc_dump_start()cannot be properly released intipc_dump_done(), but also more kernel panics might be triggered intipc_dump_done().
Ah, good catch!
The max utilization of cb->args is tipc_nl_name_table_dump():
net/tipc/name_table.c: cb->args[0] = last_type; net/tipc/name_table.c: cb->args[1] = last_lower; net/tipc/name_table.c: cb->args[2] = last_key; net/tipc/name_table.c: cb->args[3] = done;
Looks like I should just use cb->args[4] for rhashtable iterator, as we still have some room in cb->args[].