Skip to contentSkip to conversation

smb: smbdirect: fix listener backlog leak and teardown races

PATCH Applied Stefan Metzmacher 3 participants 6 messages Oct 9

Subsystems (4)
lore
Share & tools
lore

Stefan Metzmacher

author reviewer
#0 lore raw

v4 cover smb: smbdirect: fix listener backlog leak and teardown races

Hi Namjae,

These fix a set of problems in the shared smbdirect module (fs/smb/smbdirect/), reached through the listener/accept path, which in practice is driven by ksmbd for incoming SMB Direct connections.

The main one is a pre-authentication remote denial of service: the listener keeps every accepted connection on a fixed-size backlog (listen.pending, limit 10 for ksmbd). A connection is only removed from that backlog on the success path (promotion to listen.ready after a completed negotiate, or dequeue by accept()). A connection that is accepted at the RDMA level but then fails before it is accepted by the upper layer - negotiate timeout, peer disconnect, or an invalid negotiate request - is never removed, so its backlog slot is leaked. After about ten such events the listener rejects every new connection with -EBUSY and SMB Direct stays unusable until the service is restarted. An unauthenticated peer can trigger this with ~10 aborted connections.

This was reported by Lee Seong Hyeon <[email protected]>:

https://lore.kernel.org/linux-cifs/CANTrAmxL5sWU8Sn29JAGZvSq5PBB2OA+VKA0MHsZJMesagtfzA@mail.gmail.com/

The series:

- smbdirect_socket_destroy_sync() no longer waits for RDMA_CM_EVENT_DISCONNECTED after rdma_disconnect(). That wait could take very long (until the rdma cm gives up, e.g. a peer that just vanished) or never complete if the event already happened and the status was overwritten. rdma_destroy_id() in smbdirect_socket_destroy() is enough to stop further events; the rest of the disconnect protocol is handled by the rdma core asynchronously. This also makes releasing orphaned sockets (next patch) non-blocking.

- A failed, not-yet-accepted child of a listener now moves itself to a new listen.orphaned list at the end of its cleanup work and queues listen.purge_orphaned_work, which releases it, so it no longer leaks its backlog slot (nor its struct sock / RDMA resources) for the lifetime of the listener. sc->accept.listener is only changed under listener->listen.lock, and whoever clears it owns the release; the listener is freed via kfree_rcu() so a child can dereference it under rcu_read_lock(). This is the DoS fix.

- smbdirect_socket_accept() no longer hands out a socket that failed after it was put on the ready list (e.g. the peer disconnected in the meantime); it checks first_error / the status under listen.lock, orphans such a socket and tries the next one. And it also no longer returns -EAGAIN if a non zero timeout was passed.

I tested the reproducer and the problem is fixed
and I run various xfstests.

I think these are important and should go into 7.3

Changes since v3:
(https://lore.kernel.org/r/[email protected]/)

- "smb: smbdirect: release failed pending sockets of a listener":
install the real event handler and the expected event at the very start of smbdirect_accept_connect_request(), before anything that can fail. On an early error the caller only schedules nsc's deferred cleanup and returns 0, so the rdma_cm core keeps the cm_id - it must then already carry smbdirect_accept_rdma_event_handler(), which turns any unexpected event (e.g. RDMA_CM_EVENT_DEVICE_REMOVAL) into a clean smbdirect_socket_schedule_cleanup(). Previously the placeholder smbdirect_socket_rdma_event_handler() left by smbdirect_socket_init_accepting() stayed installed on that path and returned -ESTALE, making the rdma_cm core destroy the cm_id behind the back of the pending teardown.

- smbdirect_socket_accept always goes back to smbdirect_socket_wait_for_accept when no valid new socket was found, so that we don't return -EAGAIN unless a zero timeout was given.

Changes since v2:
(https://lore.kernel.org/r/[email protected]/)

- "smb: smbdirect: release failed pending sockets of a listener":
reworked the connect-request error handling after more Sashiko review. smbdirect_accept_connect_request() no longer tears down any RDMA state or clears the cm_id on failure; it only returns a not-yet-posted recv_io to the pool and returns the error. smbdirect_listen_connect_request() now sets nsc->accept.listener and publishes nsc on the pending list *before* it calls smbdirect_accept_connect_request(), and on a non-zero return it schedules nsc's teardown with smbdirect_socket_schedule_cleanup() and always returns 0, so the rdma_cm core keeps the connection id and nsc owns it (and destroys it in its own deferred teardown). The teardown must be deferred because nsc's id_priv->handler_mutex is held by the rdma_cm core across the CONNECT_REQUEST handler; that same lock (and the listener's) also guarantees nsc can't go away under us. This closes the race Sashiko reported, where a connection that established between rdma_accept() and setting the listener could be left stranded on the pending list.

Changes since v1:
(https://lore.kernel.org/r/[email protected])

- "smb: smbdirect: release failed pending sockets of a listener":
fix a use-after-free and a missed-orphan race in smbdirect_listen_connect_request(), spotted by Sashiko. The nsc->first_error check and the orphaning are now done under listen.lock, together with setting nsc->accept.listener, so a concurrent smbdirect_socket_cleanup_work()/purge_orphaned_work() can neither free nsc while we still look at it, nor miss the orphaning and leak it on the pending list.

- "smb: smbdirect: don't hand out already failed sockets in smbdirect_socket_accept()": bound the accept() wait across the "skip a failed socket and try the next one" retries, also spotted by Sashiko. smbdirect_socket_wait_for_accept() now returns the remaining timeout and smbdirect_socket_accept() carries it over, so a stream of failed connections can no longer reset the caller's timeout. ksmbd passes MAX_SCHEDULE_TIMEOUT, so it is unaffected in practice.

- Added Reported-by:/Closes: for the Sashiko reviews.

Stefan Metzmacher (3):
smb: smbdirect: don't wait for RDMA_CM_EVENT_DISCONNECTED in
smbdirect_socket_destroy_sync()
smb: smbdirect: release failed pending sockets of a listener
smb: smbdirect: don't hand out already failed sockets in
smbdirect_socket_accept()

 fs/smb/smbdirect/accept.c     | 225 ++++++++++++++++++++++++----------
 fs/smb/smbdirect/connection.c |  12 +-
 fs/smb/smbdirect/internal.h   |   2 +
 fs/smb/smbdirect/listen.c     | 137 +++++++++++++++++++--
 fs/smb/smbdirect/socket.c     | 113 +++++++++++++++--
 fs/smb/smbdirect/socket.h     |  42 +++++++
 6 files changed, 438 insertions(+), 93 deletions(-)
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH v4 1/3 smb: smbdirect: don't wait for RDMA_CM_EVENT_DISCONNECTED in smbdirect_socket_destroy_sync()

smbdirect_socket_destroy_sync() waited for RDMA_CM_EVENT_DISCONNECTED after smbdirect_socket_cleanup_work() called rdma_disconnect(). That can take very long, e.g. if the peer just disappeared, until the rdma cm gives up, or forever if the event already happened, but the status was overwritten afterwards.

This is not needed: smbdirect_socket_destroy() drains the qp under rdma_lock_handler(), destroys it and calls rdma_destroy_id(), which only waits for a currently running event handler and makes sure no further events are delivered. The rest of the disconnect protocol (DREQ/DREP and timewait for IB, abrupt close for iWarp) is handled by the rdma core asynchronously. drivers/nvme/host/rdma.c also just calls rdma_disconnect() and ib_drain_qp() before rdma_destroy_id().

So smbdirect_socket_destroy() now also accepts SMBDIRECT_SOCKET_DISCONNECTING and changes the status to SMBDIRECT_SOCKET_DISCONNECTED itself under rdma_lock_handler().

We could still get RDMA_CM_EVENT_DISCONNECTED in the small windows between rdma_unlock_handler() and rdma_destroy_id(), but in that case smbdirect_connection_rdma_event_handler() is basically a no-op.

Fixes 422a2436697d ("smb: smbdirect: introduce smbdirect_socket_destroy[_sync]()")
Cc Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
Assisted-by Claude:claude-opus-5-5
---
 fs/smb/smbdirect/connection.c | 12 ++++----
 fs/smb/smbdirect/socket.c     | 56 ++++++++++++++++++++++++++++-------
 2 files changed, 52 insertions(+), 16 deletions(-)
fs/smb/smbdirect/connection.c +6 -6
index afd31fa12a36..f48c857bda5a 100644
@@ -83,9 +83,9 @@ static int smbdirect_connection_rdma_event_handler(struct rdma_cm_id *id,
 		 * smbdirect_socket_schedule_cleanup[_status]() =>
 		 * smbdirect_socket_cleanup_work().
 		 *
-		 * As otherwise we'd set SMBDIRECT_SOCKET_DISCONNECTING,
-		 * but never ever get RDMA_CM_EVENT_DISCONNECTED and
-		 * never reach SMBDIRECT_SOCKET_DISCONNECTED.
+		 * As otherwise we'd set SMBDIRECT_SOCKET_DISCONNECTING
+		 * and call rdma_disconnect(), but never ever get
+		 * RDMA_CM_EVENT_DISCONNECTED.
 		 */
 		if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
 			smbdirect_socket_schedule_cleanup_status(sc,
@@ -113,9 +113,9 @@ static int smbdirect_connection_rdma_event_handler(struct rdma_cm_id *id,
 		 * smbdirect_socket_schedule_cleanup_status() =>
 		 * smbdirect_socket_cleanup_work().
 		 *
-		 * As otherwise we'd set SMBDIRECT_SOCKET_DISCONNECTING,
-		 * but never ever get RDMA_CM_EVENT_DISCONNECTED and
-		 * never reach SMBDIRECT_SOCKET_DISCONNECTED.
+		 * As otherwise we'd set SMBDIRECT_SOCKET_DISCONNECTING
+		 * and call rdma_disconnect(), but never ever get
+		 * RDMA_CM_EVENT_DISCONNECTED.
 		 *
 		 * This is also a normal disconnect so
 		 * SMBDIRECT_LOG_INFO should be good enough
fs/smb/smbdirect/socket.c +46 -10

76 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v4 1/3] smb: smbdirect: don't wait for RDMA_CM_EVENT_DISCONNECTED in smbdirect_socket_destroy_sync()

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines

smbdirect_socket_destroy_sync() waited for RDMA_CM_EVENT_DISCONNECTED after smbdirect_socket_cleanup_work() called rdma_disconnect(). That can take very long, e.g. if the peer just disappeared, until the rdma cm gives up, or forever if the event already happened, but the status was overwritten afterwards.

This is not needed: smbdirect_socket_destroy() drains the qp under rdma_lock_handler(), destroys it and calls rdma_destroy_id(), which only waits for a currently running event handler and makes sure no further events are delivered. The rest of the disconnect protocol (DREQ/DREP and timewait for IB, abrupt close for iWarp) is handled by the rdma core asynchronously. drivers/nvme/host/rdma.c also just calls rdma_disconnect() and ib_drain_qp() before rdma_destroy_id().

Quoted 7 lines

So smbdirect_socket_destroy() now also accepts

[ ... ]
Assisted-by Claude:claude-opus-5-5

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#3 lore raw

PATCH v4 2/3 smb: smbdirect: release failed pending sockets of a listener

In reply to Stefan Metzmacher

A child of a listener only left the listen.pending or listen.ready list via smbdirect_socket_accept() or when the listener was destroyed. So every connection that failed before it was accepted (negotiate timeout, peer disconnect, invalid negotiate request, ...) stayed on the list and counted against the backlog until the listener was destroyed. After about backlog (10 for ksmbd) such connections smbdirect_listen_connect_request() rejected every new connection with -EBUSY, so an unauthenticated peer was able to permanently stop ksmbd from accepting SMB-Direct connections.

Now a failed child moves itself to a new listen.orphaned list at the end of smbdirect_socket_cleanup_work(), after the disconnect was started, and queues listen.purge_orphaned_work, which releases it with smbdirect_socket_release(), without holding the listener's rdma_lock_handler() lock, in the same way smbdirect_socket_destroy() does for the remaining children. Orphaned sockets still count against the backlog until they are released.

sc->accept.listener is now only changed under listener->listen.lock and whoever clears it is responsible for releasing the socket:
smbdirect_socket_accept(), purge_orphaned_work or the destroy of the listener.

In order to make that work:

- a struct smbdirect_socket that is a listener is now freed via kfree_rcu(), so that a child can dereference sc->accept.listener under rcu_read_lock(), even if the listener is released concurrently.

- smbdirect_listen_connect_request() adds nsc to the listen.pending list and sets nsc->accept.listener before it calls smbdirect_accept_connect_request(), and always returns 0 so the rdma_cm core keeps the connection id and nsc owns it. On a non-zero return it schedules nsc's teardown with smbdirect_socket_schedule_cleanup(), which orphans it off the listener so that purge_orphaned_work releases it; nsc destroys its own connection id with rdma_destroy_id() in that teardown. The teardown has to be deferred because the rdma_cm core holds nsc's id_priv->handler_mutex across the handler, so nsc->rdma.cm_id must not be destroyed synchronously from here. That same handler_mutex (and the listener's) also guarantees nsc can't go away under us, so setting the listener before the call is safe and lets a failure orphan nsc.

- smbdirect_accept_negotiate_recv_work() no longer moves a socket to the ready list if it already failed or no longer belongs to the listener. Every accepting socket has a listener, so if sc->accept.listener is already cleared, it no longer sends a negotiate response, as the socket is about to be released.

- smbdirect_socket_destroy() of the listener disables purge_orphaned_work and also releases the orphaned sockets.

---
 fs/smb/smbdirect/accept.c   | 141 +++++++++++++++++++++++-------------
 fs/smb/smbdirect/internal.h |   2 +
 fs/smb/smbdirect/listen.c   | 137 ++++++++++++++++++++++++++++++++---
 fs/smb/smbdirect/socket.c   |  57 ++++++++++++++-
 fs/smb/smbdirect/socket.h   |  42 +++++++++++
 5 files changed, 314 insertions(+), 65 deletions(-)
fs/smb/smbdirect/accept.c +89 -52
index 1b30ca8476c1..977144442974 100644
@@ -19,15 +19,33 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc,
 				     const struct rdma_conn_param *param)
 {
 	struct smbdirect_socket_parameters *sp = &sc->parameters;
-	struct smbdirect_recv_io *recv_io;
+	struct smbdirect_recv_io *recv_io = NULL;
 	u8 peer_initiator_depth;
 	u8 peer_responder_resources;
 	struct rdma_conn_param conn_param;
 	__be32 ird_ord_hdr[2];
 	int ret;
 
-	if (SMBDIRECT_CHECK_STATUS_WARN(sc, SMBDIRECT_SOCKET_CREATED))
-		return -EINVAL;
+	/*
+	 * Install the real event handler (and the event we expect next)
+	 * before anything that can fail. On an early error the caller
+	 * (smbdirect_listen_connect_request()) only schedules our deferred
+	 * cleanup and returns 0 to the rdma_cm core, which keeps the cm_id, so
+	 * the cm_id must already carry smbdirect_accept_rdma_event_handler():
+	 * it turns any unexpected event (e.g. RDMA_CM_EVENT_DEVICE_REMOVAL)
+	 * into a clean smbdirect_socket_schedule_cleanup(). Otherwise the
+	 * placeholder smbdirect_socket_rdma_event_handler() installed by
+	 * smbdirect_socket_init_accepting() stays in place and returns -ESTALE,
+	 * which makes the rdma_cm core destroy the cm_id behind the back of our
+	 * pending teardown.
+	 */
+	sc->rdma.expected_event = RDMA_CM_EVENT_ESTABLISHED;
+	sc->rdma.cm_id->event_handler = smbdirect_accept_rdma_event_handler;
+
+	if (SMBDIRECT_CHECK_STATUS_WARN(sc, SMBDIRECT_SOCKET_CREATED)) {
+		ret = -EINVAL;
+		goto cleanup;
+	}
 
 	/*
 	 * First set what the we as server are able to support
@@ -48,7 +66,7 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc,
 		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
 			"smbdirect_accept_init_params() failed %1pe\n",
 			SMBDIRECT_DEBUG_ERR_PTR(ret));
-		goto init_params_failed;
+		goto cleanup;
 	}
 
 	ret = smbdirect_connection_create_qp(sc);
@@ -56,7 +74,7 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc,
 		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
 			"smbdirect_connection_create_qp() failed %1pe\n",
 			SMBDIRECT_DEBUG_ERR_PTR(ret));
-		goto create_qp_failed;
+		goto cleanup;
 	}
 
 	ret = smbdirect_connection_create_mem_pools(sc);
@@ -64,7 +82,7 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc,
 		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
 			"smbdirect_connection_create_mem_pools() failed %1pe\n",
 			SMBDIRECT_DEBUG_ERR_PTR(ret));
-		goto create_mem_failed;
+		goto cleanup;
 	}
 
 	recv_io = smbdirect_connection_get_recv_io(sc);
@@ -73,7 +91,7 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc,
 		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
 			"smbdirect_connection_get_recv_io() failed %1pe\n",
 			SMBDIRECT_DEBUG_ERR_PTR(ret));
-		goto get_recv_io_failed;
+		goto cleanup;
 	}
 	recv_io->cqe.done = smbdirect_accept_negotiate_recv_done;
 
@@ -87,7 +105,7 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc,
 		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
 			"smbdirect_connection_post_recv_io() failed %1pe\n",
 			SMBDIRECT_DEBUG_ERR_PTR(ret));
-		goto post_recv_io_failed;
+		goto cleanup;
 	}
 	/*
 	 * From here recv_io is known to the RDMA QP and needs ib_drain_qp and
@@ -123,14 +141,12 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc,
 	/* explicitly set above */
 	WARN_ON_ONCE(sc->status != SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED);
 	sc->status = SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING;
-	sc->rdma.expected_event = RDMA_CM_EVENT_ESTABLISHED;
-	sc->rdma.cm_id->event_handler = smbdirect_accept_rdma_event_handler;
 	ret = rdma_accept(sc->rdma.cm_id, &conn_param);
 	if (ret) {
 		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
 			"rdma_accept() failed %1pe\n",
 			SMBDIRECT_DEBUG_ERR_PTR(ret));
-		goto rdma_accept_failed;
+		goto cleanup;
 	}
 
 	/*
@@ -144,31 +160,25 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc,
 
 	return 0;
 
-rdma_accept_failed:
+cleanup:
 	/*
-	 * The recv_io posted above is now owned by the QP (recv_io was set to
-	 * NULL after a successful post).  smbdirect_connection_destroy_qp()
-	 * calls ib_drain_qp(), whose completion
-	 * (smbdirect_accept_negotiate_recv_done) returns the recv_io to the
-	 * free list via smbdirect_connection_put_recv_io().  It therefore MUST
-	 * run BEFORE smbdirect_connection_destroy_mem_pools(): otherwise the
-	 * posted recv_io is still outstanding when kmem_cache_destroy() runs
-	 * ("Slab cache still has objects") and is later freed into an
-	 * already-destroyed mempool (mempool_free_bulk NULL-ptr-deref).
+	 * No RDMA state is torn down here, and in particular sc is not
+	 * released: the caller (smbdirect_listen_connect_request()) owns that
+	 * decision and schedules sc's teardown via
+	 * smbdirect_socket_schedule_cleanup() on a non-zero return. All of the
+	 * RDMA state - the QP, the memory pools and the cm_id itself - is then
+	 * torn down asynchronously by smbdirect_socket_cleanup_work() ->
+	 * smbdirect_socket_release() -> smbdirect_socket_destroy(), which
+	 * drains the QP (returning any posted recv_io to the pool) before
+	 * destroying the memory pools and destroys the cm_id with
+	 * rdma_destroy_id(), all in the correct order.
+	 *
+	 * The only resource not reachable by that teardown is a recv_io that
+	 * was taken from the pool but not yet posted to the QP (recv_io is set
+	 * to NULL after a successful post), so hand it back here.
 	 */
-	smbdirect_connection_destroy_qp(sc);
-	smbdirect_connection_destroy_mem_pools(sc);
-	return ret;
-post_recv_io_failed:
-	/* post failed: recv_io was not accepted by the QP, still in hand */
 	if (recv_io)
 		smbdirect_connection_put_recv_io(recv_io);
-get_recv_io_failed:
-	smbdirect_connection_destroy_mem_pools(sc);
-create_mem_failed:
-	smbdirect_connection_destroy_qp(sc);
-create_qp_failed:
-init_params_failed:
 	return ret;
 }
 
@@ -302,6 +312,7 @@ static void smbdirect_accept_negotiate_recv_work(struct work_struct *work)
 	struct smbdirect_socket *sc =
 		container_of(work, struct smbdirect_socket, connect.work);
 	struct smbdirect_socket_parameters *sp = &sc->parameters;
+	struct smbdirect_socket *lsc;
 	struct smbdirect_recv_io *recv_io;
 	struct smbdirect_negotiate_req *nreq;
 	unsigned long flags;
@@ -464,29 +475,55 @@ static void smbdirect_accept_negotiate_recv_work(struct work_struct *work)
 	 */
 	sp->max_fragmented_send_size = max_fragmented_size;
 
-	if (sc->accept.listener) {
-		struct smbdirect_socket *lsc = sc->accept.listener;
-		unsigned long flags;
+	/*
+	 * Every accepting socket was created by
+	 * smbdirect_listen_connect_request() and has a
+	 * listener. If it's already cleared, the listener
+	 * (or its purge_orphaned_work) is responsible for
+	 * releasing us, so we must not send a negotiate
+	 * response.
+	 *
+	 * The memory of the listener is freed via kfree_rcu(),
+	 * see smbdirect_listen_orphan_socket().
+	 */
+	rcu_read_lock();
+	lsc = READ_ONCE(sc->accept.listener);
+	if (!lsc) {
+		rcu_read_unlock();
+		return;
+	}
 
-		spin_lock_irqsave(&lsc->listen.lock, flags);
-		list_del(&sc->accept.list);
-		list_add_tail(&sc->accept.list, &lsc->listen.ready);
+	spin_lock_irqsave(&lsc->listen.lock, flags);
+	/*
+	 * The listener (or its purge_orphaned_work)
+	 * may have cleared sc->accept.listener in the
+	 * meantime and is responsible for releasing us.
+	 *
+	 * If we already failed, we're either
+	 * already on the orphaned list or
+	 * smbdirect_socket_cleanup_work() will
+	 * move us there.
+	 *
+	 * In both cases we must not move us
+	 * to the ready list.
+	 */
+	if (sc->accept.listener == lsc && !READ_ONCE(sc->first_error)) {
+		list_move_tail(&sc->accept.list, &lsc->listen.ready);
 		wake_up(&lsc->listen.wait_queue);
-		spin_unlock_irqrestore(&lsc->listen.lock, flags);
-
-		/*
-		 * smbdirect_socket_accept() will call
-		 * smbdirect_accept_negotiate_finish(nsc, 0);
-		 *
-		 * So that we don't send the negotiation
-		 * response that grants credits to the peer
-		 * before the socket is accepted by the
-		 * application.
-		 */
-		return;
 	}
+	spin_unlock_irqrestore(&lsc->listen.lock, flags);
+	rcu_read_unlock();
 
-	ntstatus = le32_to_cpu(STATUS_SUCCESS);
+	/*
+	 * smbdirect_socket_accept() will call
+	 * smbdirect_accept_negotiate_finish(nsc, 0);
+	 *
+	 * So that we don't send the negotiation
+	 * response that grants credits to the peer
+	 * before the socket is accepted by the
+	 * application.
+	 */
+	return;
 
 not_supported:
 	smbdirect_accept_negotiate_finish(sc, ntstatus);
@@ -839,7 +876,7 @@ struct smbdirect_socket *smbdirect_socket_accept(struct smbdirect_socket *lsc,
 				       struct smbdirect_socket,
 				       accept.list);
 	if (nsc) {
-		nsc->accept.listener = NULL;
+		WRITE_ONCE(nsc->accept.listener, NULL);
 		list_del_init_careful(&nsc->accept.list);
 		arg->is_empty = list_empty_careful(&lsc->listen.ready);
 	}
fs/smb/smbdirect/internal.h +2 -0
index e9959e6dc13a..69d0c9f899de 100644
@@ -74,6 +74,8 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc,
 
 void smbdirect_socket_destroy_sync(struct smbdirect_socket *sc);
 
+void smbdirect_listen_orphan_socket(struct smbdirect_socket *sc);
+
 int smbdirect_socket_wait_for_credits(struct smbdirect_socket *sc,
 				      enum smbdirect_socket_status expected_status,
 				      int unexpected_errno,
fs/smb/smbdirect/listen.c +126 -11

180 lines not on this page · raw message

fs/smb/smbdirect/socket.c +55 -2

99 lines not on this page · raw message

fs/smb/smbdirect/socket.h +42 -0

67 lines not on this page · raw message

Signature · 1 line
2.43.0
author reviewer
#4 lore raw

PATCH v4 3/3 smb: smbdirect: don't hand out already failed sockets in smbdirect_socket_accept()

In reply to Stefan Metzmacher

A socket on the ready list is in SMBDIRECT_SOCKET_NEGOTIATE_RUNNING, but it may fail before smbdirect_socket_accept() picks it up, e.g. RDMA_CM_EVENT_DISCONNECTED already moved it to SMBDIRECT_SOCKET_DISCONNECTED.

smbdirect_socket_accept() unconditionally overwrote the status with SMBDIRECT_SOCKET_CONNECTED and handed out the already disconnected socket. smbdirect_socket_cleanup_work() then called rdma_disconnect() on it again, and before the recent commit smbdirect_socket_destroy_sync() waited forever for an RDMA_CM_EVENT_DISCONNECTED that already happened.

Now we check first_error and change the status to SMBDIRECT_SOCKET_CONNECTED only via cmpxchg() from SMBDIRECT_SOCKET_NEGOTIATE_RUNNING, while we still hold listen.lock. If the socket already failed, we move it to the orphaned list and queue purge_orphaned_work, like smbdirect_listen_orphan_socket() does, and try the next one. If we only found failed sockets, we wait for the next one.

Fixes dc691b91ad16 ("smb: smbdirect: introduce smbdirect_socket_{listen,accept}()")
Cc Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
Assisted-by Claude:claude-opus-5-5
---
 fs/smb/smbdirect/accept.c | 84 +++++++++++++++++++++++++++++++++------
 1 file changed, 72 insertions(+), 12 deletions(-)
fs/smb/smbdirect/accept.c +72 -12
index 977144442974..e7e5eb75a5ae 100644
@@ -836,7 +836,11 @@ static long smbdirect_socket_wait_for_accept(struct smbdirect_socket *lsc, long
 	if (ret < 0)
 		return ret;
 
-	return 0;
+	/*
+	 * Return the remaining timeout, so the caller can carry it
+	 * over to the next smbdirect_socket_wait_for_accept() call.
+	 */
+	return ret;
 }
 
 struct smbdirect_socket *smbdirect_socket_accept(struct smbdirect_socket *lsc,
@@ -846,6 +850,7 @@ struct smbdirect_socket *smbdirect_socket_accept(struct smbdirect_socket *lsc,
 	struct smbdirect_socket *nsc;
 	unsigned long flags;
 
+again:
 	if (lsc->status != SMBDIRECT_SOCKET_LISTENING) {
 		arg->err = -EINVAL;
 		return NULL;
@@ -857,7 +862,7 @@ struct smbdirect_socket *smbdirect_socket_accept(struct smbdirect_socket *lsc,
 	}
 
 	if (list_empty_careful(&lsc->listen.ready)) {
-		int ret;
+		long ret;
 
 		if (timeo == 0) {
 			arg->err = -EAGAIN;
@@ -865,16 +870,64 @@ struct smbdirect_socket *smbdirect_socket_accept(struct smbdirect_socket *lsc,
 		}
 
 		ret = smbdirect_socket_wait_for_accept(lsc, timeo);
-		if (ret) {
+		if (ret < 0) {
 			arg->err = ret;
 			return NULL;
 		}
+		/*
+		 * Carry the remaining timeout over, so that a stream of
+		 * failed connections that we orphan and skip (goto again)
+		 * can't reset the caller's timeout and wait forever.
+		 */
+		timeo = ret;
 	}
 
 	spin_lock_irqsave(&lsc->listen.lock, flags);
-	nsc = list_first_entry_or_null(&lsc->listen.ready,
-				       struct smbdirect_socket,
-				       accept.list);
+	/*
+	 * Recheck under the lock.
+	 */
+	if (lsc->status != SMBDIRECT_SOCKET_LISTENING) {
+		arg->err = -EINVAL;
+		spin_unlock_irqrestore(&lsc->listen.lock, flags);
+		return NULL;
+	}
+	if (lsc->first_error) {
+		arg->err = lsc->first_error;
+		spin_unlock_irqrestore(&lsc->listen.lock, flags);
+		return NULL;
+	}
+
+	while ((nsc = list_first_entry_or_null(&lsc->listen.ready,
+					       struct smbdirect_socket,
+					       accept.list))) {
+		/*
+		 * nsc may have failed after it was moved
+		 * to the ready list, e.g. RDMA_CM_EVENT_DISCONNECTED
+		 * already moved it to SMBDIRECT_SOCKET_DISCONNECTED.
+		 * We must not overwrite that with
+		 * SMBDIRECT_SOCKET_CONNECTED and hand
+		 * out an already disconnected socket.
+		 *
+		 * Doing this under listen.lock means nsc
+		 * still belongs to us, so we can just move
+		 * a failed socket to the orphaned list,
+		 * like smbdirect_listen_orphan_socket() does,
+		 * and try the next one.
+		 */
+		if (!READ_ONCE(nsc->first_error) &&
+		    cmpxchg(&nsc->status,
+			    SMBDIRECT_SOCKET_NEGOTIATE_RUNNING,
+			    SMBDIRECT_SOCKET_CONNECTED) ==
+		    SMBDIRECT_SOCKET_NEGOTIATE_RUNNING)
+			break;
+
+		smbdirect_log_rdma_event(nsc, SMBDIRECT_LOG_INFO,
+			"orphaning failed socket status=%s first_error=%1pe\n",
+			smbdirect_socket_status_string(nsc->status),
+			SMBDIRECT_DEBUG_ERR_PTR(nsc->first_error));
+		list_move_tail(&nsc->accept.list, &lsc->listen.orphaned);
+		queue_work(lsc->workqueues.cleanup, &lsc->listen.purge_orphaned_work);
+	}
 	if (nsc) {
 		WRITE_ONCE(nsc->accept.listener, NULL);
 		list_del_init_careful(&nsc->accept.list);
@@ -882,8 +935,13 @@ struct smbdirect_socket *smbdirect_socket_accept(struct smbdirect_socket *lsc,
 	}
 	spin_unlock_irqrestore(&lsc->listen.lock, flags);
 	if (!nsc) {
-		arg->err = -EAGAIN;
-		return NULL;
+		/*
+		 * If we only found failed sockets or no socket,
+		 * we wait for the next one.
+		 *
+		 * A possible -EAGAIN/-ETIMEOUT is handled above.
+		 */
+		goto again;
 	}
 
 	/*
@@ -892,12 +950,14 @@ struct smbdirect_socket *smbdirect_socket_accept(struct smbdirect_socket *lsc,
 	 * so it didn't grant any credits to us.
 	 *
 	 * The caller expects a connected socket
-	 * now as there are no credits anyway.
+	 * now as there are no credits anyway,
+	 * above we already changed to SMBDIRECT_SOCKET_CONNECTED
+	 * under the lsc->listen.lock and with cmpxchg.
 	 *
-	 * Then we send the negotiation response in
-	 * order to grant credits to the peer.
+	 * Now we send the negotiation response in
+	 * order to grant credits to the peer,
+	 * as the socket is now visible to the application layer.
 	 */
-	nsc->status = SMBDIRECT_SOCKET_CONNECTED;
 	smbdirect_accept_negotiate_finish(nsc, 0);
 
 	return nsc;
Signature · 1 line
2.43.0
maintainer Applied
#5 lore raw
In reply to Stefan Metzmacher
Quoted 118 lines · Stefan Metzmacher

Hi Namjae,

These fix a set of problems in the shared smbdirect module (fs/smb/smbdirect/), reached through the listener/accept path, which in practice is driven by ksmbd for incoming SMB Direct connections.

The main one is a pre-authentication remote denial of service: the listener keeps every accepted connection on a fixed-size backlog (listen.pending, limit 10 for ksmbd). A connection is only removed from that backlog on the success path (promotion to listen.ready after a completed negotiate, or dequeue by accept()). A connection that is accepted at the RDMA level but then fails before it is accepted by the upper layer - negotiate timeout, peer disconnect, or an invalid negotiate request - is never removed, so its backlog slot is leaked. After about ten such events the listener rejects every new connection with -EBUSY and SMB Direct stays unusable until the service is restarted. An unauthenticated peer can trigger this with ~10 aborted connections.

This was reported by Lee Seong Hyeon <[email protected]>:

https://lore.kernel.org/linux-cifs/CANTrAmxL5sWU8Sn29JAGZvSq5PBB2OA+VKA0MHsZJMesagtfzA@mail.gmail.com/

The series:

- smbdirect_socket_destroy_sync() no longer waits for RDMA_CM_EVENT_DISCONNECTED after rdma_disconnect(). That wait could take very long (until the rdma cm gives up, e.g. a peer that just vanished) or never complete if the event already happened and the status was overwritten. rdma_destroy_id() in smbdirect_socket_destroy() is enough to stop further events; the rest of the disconnect protocol is handled by the rdma core asynchronously. This also makes releasing orphaned sockets (next patch) non-blocking.

- A failed, not-yet-accepted child of a listener now moves itself to a new listen.orphaned list at the end of its cleanup work and queues listen.purge_orphaned_work, which releases it, so it no longer leaks its backlog slot (nor its struct sock / RDMA resources) for the lifetime of the listener. sc->accept.listener is only changed under listener->listen.lock, and whoever clears it owns the release; the listener is freed via kfree_rcu() so a child can dereference it under rcu_read_lock(). This is the DoS fix.

- smbdirect_socket_accept() no longer hands out a socket that failed after it was put on the ready list (e.g. the peer disconnected in the meantime); it checks first_error / the status under listen.lock, orphans such a socket and tries the next one. And it also no longer returns -EAGAIN if a non zero timeout was passed.

I tested the reproducer and the problem is fixed
and I run various xfstests.

I think these are important and should go into 7.3

Changes since v3:
(https://lore.kernel.org/r/[email protected]/)

- "smb: smbdirect: release failed pending sockets of a listener":
install the real event handler and the expected event at the very start of smbdirect_accept_connect_request(), before anything that can fail. On an early error the caller only schedules nsc's deferred cleanup and returns 0, so the rdma_cm core keeps the cm_id - it must then already carry smbdirect_accept_rdma_event_handler(), which turns any unexpected event (e.g. RDMA_CM_EVENT_DEVICE_REMOVAL) into a clean smbdirect_socket_schedule_cleanup(). Previously the placeholder smbdirect_socket_rdma_event_handler() left by smbdirect_socket_init_accepting() stayed installed on that path and returned -ESTALE, making the rdma_cm core destroy the cm_id behind the back of the pending teardown.

- smbdirect_socket_accept always goes back to smbdirect_socket_wait_for_accept when no valid new socket was found, so that we don't return -EAGAIN unless a zero timeout was given.

Changes since v2:
(https://lore.kernel.org/r/[email protected]/)

- "smb: smbdirect: release failed pending sockets of a listener":
reworked the connect-request error handling after more Sashiko review. smbdirect_accept_connect_request() no longer tears down any RDMA state or clears the cm_id on failure; it only returns a not-yet-posted recv_io to the pool and returns the error. smbdirect_listen_connect_request() now sets nsc->accept.listener and publishes nsc on the pending list *before* it calls smbdirect_accept_connect_request(), and on a non-zero return it schedules nsc's teardown with smbdirect_socket_schedule_cleanup() and always returns 0, so the rdma_cm core keeps the connection id and nsc owns it (and destroys it in its own deferred teardown). The teardown must be deferred because nsc's id_priv->handler_mutex is held by the rdma_cm core across the CONNECT_REQUEST handler; that same lock (and the listener's) also guarantees nsc can't go away under us. This closes the race Sashiko reported, where a connection that established between rdma_accept() and setting the listener could be left stranded on the pending list.

Changes since v1:
(https://lore.kernel.org/r/[email protected])

- "smb: smbdirect: release failed pending sockets of a listener":
fix a use-after-free and a missed-orphan race in smbdirect_listen_connect_request(), spotted by Sashiko. The nsc->first_error check and the orphaning are now done under listen.lock, together with setting nsc->accept.listener, so a concurrent smbdirect_socket_cleanup_work()/purge_orphaned_work() can neither free nsc while we still look at it, nor miss the orphaning and leak it on the pending list.

- "smb: smbdirect: don't hand out already failed sockets in smbdirect_socket_accept()": bound the accept() wait across the "skip a failed socket and try the next one" retries, also spotted by Sashiko. smbdirect_socket_wait_for_accept() now returns the remaining timeout and smbdirect_socket_accept() carries it over, so a stream of failed connections can no longer reset the caller's timeout. ksmbd passes MAX_SCHEDULE_TIMEOUT, so it is unaffected in practice.

Quoted 8 lines

- Added Reported-by:/Closes: for the Sashiko reviews.

Stefan Metzmacher (3):
smb: smbdirect: don't wait for RDMA_CM_EVENT_DISCONNECTED in
smbdirect_socket_destroy_sync()
smb: smbdirect: release failed pending sockets of a listener
smb: smbdirect: don't hand out already failed sockets in
smbdirect_socket_accept()

Applied them to #ksmbd-for-next.
Thanks!