From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org> To: tarantool-patches@dev.tarantool.org, kostja.osipov@gmail.com Subject: [Tarantool-patches] [PATCH v2 1/1] iproto: don't destroy a session during disconnect Date: Sat, 16 Nov 2019 01:35:25 +0100 [thread overview] Message-ID: <746ed8a74c07907f85db8ec08e9d1937afc44f08.1573864467.git.v.shpilevoy@tarantool.org> (raw) Binary session disconnect trigger yield could lead to use after free of the session object. That happened because iproto thread sent two requests to TX thread at disconnect: - Close the session and run its on disconnect triggers; - If all requests are handled, destroy the session. When a connection is idle, all requests are handled, so both these requests are sent. If the first one yielded in TX thread, the second one arrived and destroyed the session right under the feet of the first one. This can be solved in two ways - in TX thread, and in iproto thread. Iproto thread solution (which is chosen in the patch): just don't send destroy request until disconnect returns back to iproto thread. TX thread solution (alternative): add a flag which says whether disconnect is processed by TX. When destroy request arrives, it checks the flag. If disconnect is not done, the destroy request waits on a condition variable until it is. The iproto is a bit tricker to implement, but it looks more correct. Closes #4627 --- Branch: https://github.com/tarantool/tarantool/tree/gerold103/gh-4627-session-use-after-free-v2 Issue: https://github.com/tarantool/tarantool/issues/4627 Changes in V2: - Tried to fix from iproto thread side. src/box/iproto.cc | 68 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 18 deletions(-) diff --git a/src/box/iproto.cc b/src/box/iproto.cc index 34c8f469a..fc5bb9026 100644 --- a/src/box/iproto.cc +++ b/src/box/iproto.cc @@ -296,8 +296,13 @@ static const struct cmsg_hop destroy_route[] = { static void tx_process_disconnect(struct cmsg *m); +/** Send destroy message to tx thread. */ +static void +net_finish_disconnect(struct cmsg *m); + static const struct cmsg_hop disconnect_route[] = { - { tx_process_disconnect, NULL } + { tx_process_disconnect, &net_pipe }, + { net_finish_disconnect, NULL } }; /** @@ -432,6 +437,13 @@ struct iproto_connection struct cmsg destroy_msg; /** True if destroy message is sent. Debug-only. */ bool is_destroy_sent; + /** + * True, if disconnect is done in both iproto and TX + * threads, but destroy is not, because there were + * unfinished requests. Then destroy should be sent on the + * last close, when the last request is done. + */ + bool do_destroy_on_close; struct rlist in_stop_list; /** * Kharon is used to implement box.session.push(). @@ -572,6 +584,32 @@ iproto_connection_stop_msg_max_limit(struct iproto_connection *con) rlist_add_tail(&stopped_connections, &con->in_stop_list); } +/** + * Send a destroy message to TX thread in case all requests are + * finished. + */ +static inline bool +iproto_connection_try_to_start_destroy(struct iproto_connection *con) +{ + if (!iproto_connection_is_idle(con)) + return false; + /* + * If the connection has no outstanding requests in the + * input buffer, then no one (e.g. tx thread) is referring + * to it, so it must be destroyed. Firstly queue a msg to + * destroy the session and other resources owned by TX + * thread. When it is done, iproto thread will destroy + * other parts of the connection. + * + * Otherwise, it will be destroyed by the last request on + * this connection that has finished processing. + */ + assert(! con->is_destroy_sent); + con->is_destroy_sent = true; + cpipe_push(&tx_pipe, &con->destroy_msg); + return true; +} + /** * Initiate a connection shutdown. This method may * be invoked many times, and does the internal @@ -597,23 +635,8 @@ iproto_connection_close(struct iproto_connection *con) */ con->p_ibuf->wpos -= con->parse_size; cpipe_push(&tx_pipe, &con->disconnect_msg); - } - /* - * If the connection has no outstanding requests in the - * input buffer, then no one (e.g. tx thread) is referring - * to it, so it must be destroyed at once. Queue a msg to - * run on_disconnect() trigger and destroy the connection. - * - * Otherwise, it will be destroyed by the last request on - * this connection that has finished processing. - * - * The check is mandatory to not destroy a connection - * twice. - */ - if (iproto_connection_is_idle(con)) { - assert(! con->is_destroy_sent); - con->is_destroy_sent = true; - cpipe_push(&tx_pipe, &con->destroy_msg); + } else if (con->do_destroy_on_close) { + iproto_connection_try_to_start_destroy(con); } rlist_del(&con->in_stop_list); } @@ -1049,6 +1072,7 @@ iproto_connection_new(int fd) cmsg_init(&con->destroy_msg, destroy_route); cmsg_init(&con->disconnect_msg, disconnect_route); con->is_destroy_sent = false; + con->do_destroy_on_close = false; con->tx.is_push_pending = false; con->tx.is_push_sent = false; rmean_collect(rmean_net, IPROTO_CONNECTIONS, 1); @@ -1281,6 +1305,14 @@ tx_process_disconnect(struct cmsg *m) } } +static void +net_finish_disconnect(struct cmsg *m) +{ + struct iproto_connection *con = + container_of(m, struct iproto_connection, disconnect_msg); + con->do_destroy_on_close = !iproto_connection_try_to_start_destroy(con); +} + /** * Destroy the session object, as well as output buffers of the * connection. -- 2.21.0 (Apple Git-122.2)
next reply other threads:[~2019-11-16 0:29 UTC|newest] Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-11-16 0:35 Vladislav Shpilevoy [this message] 2019-11-16 13:07 ` Konstantin Osipov 2019-11-17 17:47 ` Vladislav Shpilevoy 2019-11-17 17:49 ` Konstantin Osipov 2019-11-17 20:33 ` Vladislav Shpilevoy
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=746ed8a74c07907f85db8ec08e9d1937afc44f08.1573864467.git.v.shpilevoy@tarantool.org \ --to=v.shpilevoy@tarantool.org \ --cc=kostja.osipov@gmail.com \ --cc=tarantool-patches@dev.tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v2 1/1] iproto: don'\''t destroy a session during disconnect' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox