mptcp: token refactor follow-up
Subsystems (2)
Paolo Abeni
cover mptcp: token refactor follow-up
First patch clean-up a bit the token container refactor 2nd adapt the tests cases, 3rd one fixes a long standing bug which become apparent with token refactor and the last one helps debugging with kasan
patch 3 will target -net tree hopefully soon (adapted to the current -net token apis). will need a little rebasing of export branch after that.
@Christph: this series should replace any pending export branch fixes shared on pastebin/mails/whatever (excluding the -net patch submitted little time ago)
Paolo Abeni (4):
Squash-to: "mptcp: refactor token container."
Squash-to: "mptcp: introduce token KUNIT self-tests"
mptcp: don't leak msk in token container [DO-NOT-MERGE] mptcp: use kmalloc on kasan build
net/ipv4/af_inet.c | 3 +++ net/mptcp/protocol.c | 21 ++++++++++++----- net/mptcp/protocol.h | 9 ++++++-- net/mptcp/subflow.c | 8 ++++--- net/mptcp/token.c | 51 ++++++++++++++++++++++-------------------- net/mptcp/token_test.c | 11 ++++----- 6 files changed, 64 insertions(+), 39 deletions(-)
Signature · 1 line
2.21.3
Paolo Abeni
PATCH 1/4 Squash-to: "mptcp: refactor token container."
Track the 'token avail' status via the token node 'pprev' field, and use sk_hashed helper for msk socket.
Explicitly clear the token before each syscall potentially re-creating it.
Move the token accept in the 'own_req' branch: mptcp_token_accept() modifies the req socket and we can't do that if we don't own the req socket.
Always insert at bucket head, to avoid unneeded traversal.
--- net/mptcp/protocol.c | 6 +++--- net/mptcp/protocol.h | 9 ++++++-- net/mptcp/subflow.c | 7 +++--- net/mptcp/token.c | 51 +++++++++++++++++++++++--------------------- 4 files changed, 41 insertions(+), 32 deletions(-)
net/mptcp/protocol.c +3 -3
@@ -1445,8 +1445,6 @@ struct sock *mptcp_sk_clone(const struct sock *sk, msk->token = subflow_req->token; msk->subflow =NULL ; -mptcp_token_accept (subflow_req, msk); - msk->write_seq = subflow_req->idsn +1 ;atomic64_set (&msk->snd_una, msk->write_seq);if (mp_opt->mp_capable) {
@@ -1532,7 +1530,7 @@ static void mptcp_destroy(struct sock *sk) {struct mptcp_sock *msk =mptcp_sk (sk); -mptcp_token_destroy (msk->token); +mptcp_token_destroy (msk);if (msk->cached_ext)__skb_ext_put (msk->cached_ext);
@@ -1811,6 +1809,7 @@ static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr, goto do_connect; } +mptcp_token_destroy (msk); ssock =__mptcp_socket_create (msk,TCP_SYN_SENT );if (IS_ERR (ssock)) { err =PTR_ERR (ssock);
@@ -1887,6 +1886,7 @@ static int mptcp_listen(struct socket *sock, int backlog)pr_debug ("msk=%p" , msk);lock_sock (sock->sk); +mptcp_token_destroy (msk); ssock =__mptcp_socket_create (msk,TCP_LISTEN );if (IS_ERR (ssock)) { err =PTR_ERR (ssock);
net/mptcp/protocol.h +7 -2
@@ -383,13 +383,18 @@ void mptcp_data_acked(struct sock *sk);void mptcp_subflow_eof (struct sock *sk);void __init mptcp_token_init (void ); +static inline void mptcp_token_init_request (struct request_sock *req) +{ +mptcp_subflow_rsk (req)->token_node.pprev =NULL ; +} +int mptcp_token_new_request (struct request_sock *req); -void mptcp_token_destroy_request (u32 token); +void mptcp_token_destroy_request (struct request_sock *req);int mptcp_token_new_connect (struct sock *sk);void mptcp_token_accept (struct mptcp_subflow_request_sock *r,struct mptcp_sock *msk);struct mptcp_sock *mptcp_token_get_sock (u32 token); -void mptcp_token_destroy (u32 token); +void mptcp_token_destroy (struct mptcp_sock *msk);void mptcp_crypto_key_sha (u64 key,u32 *token,u64 *idsn);static inline void mptcp_crypto_key_gen_sha (u64 *key,u32 *token,u64 *idsn)
net/mptcp/subflow.c +4 -3
@@ -34,7 +34,7 @@ static int subflow_rebuild_header(struct sock *sk)struct mptcp_subflow_context *subflow =mptcp_subflow_ctx (sk);int local_id; -if (subflow->request_mptcp && !subflow->token) { +if (subflow->request_mptcp &&sk_unhashed (subflow->conn)) {pr_debug ("subflow=%p" , sk);if (mptcp_token_new_connect (sk)) { subflow->mp_capable =0 ;
@@ -69,8 +69,7 @@ static void subflow_req_destructor(struct request_sock *req)pr_debug ("subflow_req=%p" , subflow_req); -if (subflow_req->mp_capable) -mptcp_token_destroy_request (subflow_req->token); +mptcp_token_destroy_request (req); tcp_request_sock_ops.destructor (req); }
@@ -133,6 +132,7 @@ static void subflow_init_req(struct request_sock *req, subflow_req->mp_capable =0 ; subflow_req->mp_join =0 ; +mptcp_token_init_request (req);#ifdef CONFIG_TCP_MD5SIG /* no MPTCP if MD5SIG is enabled on this socket or we may run out of
@@ -509,6 +509,7 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,*/ new_msk->sk_destruct = mptcp_sock_destruct;mptcp_pm_new_connection (mptcp_sk (new_msk),1 ); +mptcp_token_accept (subflow_req,mptcp_sk (new_msk)); ctx->conn = new_msk; new_msk =NULL ;
net/mptcp/token.c +27 -24
96 lines not on this page · raw message
Signature · 1 line
2.21.3
Paolo Abeni
PATCH 2/4 Squash-to: "mptcp: introduce token KUNIT self-tests"
In reply to Paolo AbeniUpdate tests to APIs change
--- net/mptcp/token_test.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
net/mptcp/token_test.c +6 -5
@@ -9,6 +9,7 @@ static struct mptcp_subflow_request_sock *build_req_sock(struct kunit *test) req =kunit_kzalloc (test,sizeof (struct mptcp_subflow_request_sock ),GFP_USER );KUNIT_EXPECT_NOT_ERR_OR_NULL (test, req); +mptcp_token_init_request ((struct request_sock *)req);return req; }
@@ -23,7 +24,7 @@ static void mptcp_token_test_req_basic(struct kunit *test)KUNIT_EXPECT_PTR_EQ (test, null_msk,mptcp_token_get_sock (req->token));/* cleanup */ -mptcp_token_destroy_request (req->token); +mptcp_token_destroy_request ((struct request_sock *)req); }static struct inet_connection_sock *build_icsk (struct kunit *test)
@@ -75,7 +76,7 @@ static void mptcp_token_test_msk_basic(struct kunit *test)KUNIT_EXPECT_PTR_EQ (test, msk,mptcp_token_get_sock (ctx->token));KUNIT_EXPECT_EQ (test,2 , (int )refcount_read (&sk->sk_refcnt)); -mptcp_token_destroy (ctx->token); +mptcp_token_destroy (msk);KUNIT_EXPECT_PTR_EQ (test, null_msk,mptcp_token_get_sock (ctx->token)); }
@@ -91,11 +92,11 @@ static void mptcp_token_test_accept(struct kunit *test)KUNIT_EXPECT_PTR_EQ (test, msk,mptcp_token_get_sock (msk->token));/* this is now a no-op */ -mptcp_token_destroy_request (req->token); +mptcp_token_destroy_request ((struct request_sock *)req);KUNIT_EXPECT_PTR_EQ (test, msk,mptcp_token_get_sock (msk->token));/* cleanup */ -mptcp_token_destroy (msk->token); +mptcp_token_destroy (msk); }static void mptcp_token_test_destroyed (struct kunit *test)
@@ -117,7 +118,7 @@ static void mptcp_token_test_destroyed(struct kunit *test)KUNIT_EXPECT_PTR_EQ (test, null_msk,mptcp_token_get_sock (msk->token));/* cleanup */ -mptcp_token_destroy (msk->token); +mptcp_token_destroy (msk); }static struct kunit_case mptcp_token_test_cases[] = {
Signature · 1 line
2.21.3
Paolo Abeni
PATCH 3/4 mptcp: don't leak msk in token container
In reply to Paolo AbeniWhen the left-over msk is freed by subflow_syn_recv_sock(), we don't invoke the proto->destroy() method, to the socket is not removed from the token container, leading to later UaF.
Address the issue explicitly removing the token even in the above error path.
---
note:
- the net patch will be slightly different, to cope with the token API changes introduced by the token refactor
--- net/mptcp/subflow.c | 1 + 1 file changed, 1 insertion(+)
net/mptcp/subflow.c +1 -0
@@ -393,6 +393,7 @@ static void mptcp_sock_destruct(struct sock *sk)sock_orphan (sk); } +mptcp_token_destroy (mptcp_sk (sk));inet_sock_destruct (sk); }
Signature · 1 line
2.21.3
Paolo Abeni
PATCH 4/4 mptcp: use kmalloc on kasan build
In reply to Paolo AbeniHelps detection UaF, which apparently kasan misses with kmem_cache allocator.
We also need to always set the SOCK_RCU_FREE flag, to preserved the current code leveraging SLAB_TYPESAFE_BY_RCU. This latter change will make unreachable some existing errors path, but I don't see other options.
--- net/ipv4/af_inet.c | 3 +++ net/mptcp/protocol.c | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-)
net/ipv4/af_inet.c +3 -0
@@ -316,7 +316,10 @@ static int inet_create(struct net *net, struct socket *sock, int protocol, answer_flags = answer->flags;rcu_read_unlock (); +#if !IS_ENABLED (CONFIG_KASAN ) +/* with kasan we use kmalloc */ WARN_ON (!answer_prot->slab); +#endif err = -ENOBUFS ; sk =sk_alloc (net,PF_INET ,GFP_KERNEL , answer_prot, kern);
net/mptcp/protocol.c +13 -2
@@ -1262,6 +1262,9 @@ static int __mptcp_init_sock(struct sock *sk)/* re-use the csk retrans timer for MPTCP-level retrans */ timer_setup (&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer,0 ); +#if IS_ENABLED (CONFIG_KASAN ) +sock_set_flag (sk,SOCK_RCU_FREE ); +#endif return 0 ; }
@@ -1455,7 +1458,9 @@ struct sock *mptcp_sk_clone(const struct sock *sk, msk->ack_seq = ack_seq; } +#if !IS_ENABLED (CONFIG_KASAN )sock_reset_flag (nsk,SOCK_RCU_FREE ); +#endif /* will be fully established after successful MPC subflow creation */ inet_sk_state_store (nsk,TCP_SYN_RECV );bh_unlock_sock (nsk);
@@ -2076,6 +2081,12 @@ static struct inet_protosw mptcp_protosw = { .flags = INET_PROTOSW_ICSK, }; +#if IS_ENABLED (CONFIG_KASAN ) +#define MPTCP_USE_SLAB 0 +#else +#define MPTCP_USE_SLAB 1 +#endif +void __init mptcp_proto_init (void ) { mptcp_prot.h.hashinfo = tcp_prot.h.hashinfo;
@@ -2087,7 +2098,7 @@ void __init mptcp_proto_init(void)mptcp_pm_init ();mptcp_token_init (); -if (proto_register (&mptcp_prot,1 ) !=0 ) +if (proto_register (&mptcp_prot,MPTCP_USE_SLAB ) !=0 )panic ("Failed to register MPTCP proto.\n" );inet_register_protosw (&mptcp_protosw);
@@ -2149,7 +2160,7 @@ int __init mptcp_proto_v6_init(void) mptcp_v6_prot.destroy = mptcp_v6_destroy; mptcp_v6_prot.obj_size =sizeof (struct mptcp6_sock ); - err =proto_register (&mptcp_v6_prot,1 ); + err =proto_register (&mptcp_v6_prot,MPTCP_USE_SLAB );if (err)return err;
Signature · 1 line
2.21.3
Matthieu Baerts
Hi Paolo, Christoph,
Quoted 11 lines · Paolo Abeni
First patch clean-up a bit the token container refactor 2nd adapt the tests cases, 3rd one fixes a long standing bug which become apparent with token refactor and the last one helps debugging with kasan
patch 3 will target -net tree hopefully soon (adapted to the current -net token apis). will need a little rebasing of export branch after that.
@Christph: this series should replace any pending export branch fixes shared on pastebin/mails/whatever (excluding the -net patch submitted little time ago)
Thank you for the patches and the reviews/test (on IRC)!
- d439e86abf12: "squashed" patch 1/4 in "mptcp: refactor token container."
- 0cf95b148037..c6c48c061cc1: result
- 80e87cdeeaaa: "squashed" patch 2/4 in "mptcp: introduce token KUNIT self-tests"
- c6c48c061cc1..b194cc9477a5: result
The other patches have been added at the end: b194cc9477a5..12401818bff5
"export" branch has just been updated, tests seem still OK!
Cheers,
Matt
Signature · 5 lines
Matthieu Baerts | R&D Engineer matthieu.baerts(a)tessares.net Tessares SA | Hybrid Access Solutions www.tessares.net 1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium