History

mptcp: fix race on unaccepted mptcp sockets

When the listener socket owning the relevant request is closed, it frees the unaccepted subflows and that causes later deletion of the paired MPTCP sockets. The mptcp socket's worker can run in the time interval between such delete operations. When that happens, any access to msk->first will cause an UaF access, as the subflow cleanup did not cleared such field in the mptcp socket. Address the issue explicitly traversing the listener socket accept queue at close time and performing the needed cleanup on the pending msk. Note that the locking is a bit tricky, as we need to acquire the msk socket lock, while still owning the subflow socket one.

Paolo Abeni committed 4 years ago

Commit 6aeed901 parent f745a3e3 files · +59 −0Browse files

Unified · Split

3 files
  1. net/mptcp/protocol.c +5 −0
  2. net/mptcp/protocol.h +2 −0
  3. net/mptcp/subflow.c +52 −0
net/mptcp/protocol.c+5−0
@@ -2331,6 +2331,11 @@ static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, · 2330 unmodified lines
2331 kfree_rcu(subflow, rcu);2331 kfree_rcu(subflow, rcu);
2332 } else {2332 } else {
2333 /* otherwise tcp will dispose of the ssk and subflow ctx */2333 /* otherwise tcp will dispose of the ssk and subflow ctx */
2334 if (ssk->sk_state == TCP_LISTEN) {
2335 tcp_set_state(ssk, TCP_CLOSE);
2336 mptcp_subflow_queue_clean(ssk);
2337 inet_csk_listen_stop(ssk);
2338 }
2334 __tcp_close(ssk, 0);2339 __tcp_close(ssk, 0);
2335 2340
2336 /* close acquired an extra ref */2341 /* close acquired an extra ref */
net/mptcp/protocol.h+2−0
@@ -306,6 +306,7 @@ struct mptcp_sock { · 305 unmodified lines
306 306
307 u32 setsockopt_seq;307 u32 setsockopt_seq;
308 char ca_name[TCP_CA_NAME_MAX];308 char ca_name[TCP_CA_NAME_MAX];
309 struct mptcp_sock *dl_next;
309};310};
310 311
311#define mptcp_data_lock(sk) spin_lock_bh(&(sk)->sk_lock.slock)312#define mptcp_data_lock(sk) spin_lock_bh(&(sk)->sk_lock.slock)
@@ -608,6 +609,7 @@ void mptcp_close_ssk(struct sock *sk, struct sock *ssk, · 296 unmodified lines
608 struct mptcp_subflow_context *subflow);609 struct mptcp_subflow_context *subflow);
609void mptcp_subflow_send_ack(struct sock *ssk);610void mptcp_subflow_send_ack(struct sock *ssk);
610void mptcp_subflow_reset(struct sock *ssk);611void mptcp_subflow_reset(struct sock *ssk);
612void mptcp_subflow_queue_clean(struct sock *ssk);
611void mptcp_sock_graft(struct sock *sk, struct socket *parent);613void mptcp_sock_graft(struct sock *sk, struct socket *parent);
612struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk);614struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk);
613 615
net/mptcp/subflow.c+52−0
@@ -1723,6 +1723,58 @@ static void subflow_state_change(struct sock *sk) · 1722 unmodified lines
1723 }1723 }
1724}1724}
1725 1725
1726void mptcp_subflow_queue_clean(struct sock *listener_ssk)
1727{
1728 struct request_sock_queue *queue = &inet_csk(listener_ssk)->icsk_accept_queue;
1729 struct mptcp_sock *msk, *next, *head = NULL;
1730 struct request_sock *req;
1731
1732 /* build a list of all unaccepted mptcp sockets */
1733 spin_lock_bh(&queue->rskq_lock);
1734 for (req = queue->rskq_accept_head; req; req = req->dl_next) {
1735 struct mptcp_subflow_context *subflow;
1736 struct sock *ssk = req->sk;
1737 struct mptcp_sock *msk;
1738
1739 if (!sk_is_mptcp(ssk))
1740 continue;
1741
1742 subflow = mptcp_subflow_ctx(ssk);
1743 if (!subflow || !subflow->conn)
1744 continue;
1745
1746 /* skip if already in list */
1747 msk = mptcp_sk(subflow->conn);
1748 if (msk->dl_next || msk == head)
1749 continue;
1750
1751 msk->dl_next = head;
1752 head = msk;
1753 }
1754 spin_unlock_bh(&queue->rskq_lock);
1755 if (!head)
1756 return;
1757
1758 /* can't acquire the msk socket lock under the subflow one,
1759 * or will cause ABBA deadlock
1760 */
1761 release_sock(listener_ssk);
1762
1763 for (msk = head; msk; msk = next) {
1764 struct sock *sk = (struct sock *)msk;
1765 bool slow;
1766
1767 slow = lock_sock_fast_nested(sk);
1768 next = msk->dl_next;
1769 msk->first = NULL;
1770 msk->dl_next = NULL;
1771 unlock_sock_fast(sk, slow);
1772 }
1773
1774 /* we are still under the listener msk socket lock */
1775 lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING);
1776}
1777
1726static int subflow_ulp_init(struct sock *sk)1778static int subflow_ulp_init(struct sock *sk)
1727{1779{
1728 struct inet_connection_sock *icsk = inet_csk(sk);1780 struct inet_connection_sock *icsk = inet_csk(sk);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t