Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH v2 0/2] scsi: target: iscsi: Fix adding a connection to a session that is going away
@ 2026-11-27 13:52 Rongen Wang via B4 Relay
  2026-11-27 13:52 ` [PATCH v2 1/2] scsi: target: iscsi: Refuse to add a connection to a session being closed Rongen Wang via B4 Relay
  2026-11-27 13:52 ` [PATCH v2 2/2] scsi: target: iscsi: Do not let Time2Retain expire a session in use Rongen Wang via B4 Relay
  0 siblings, 2 replies; 4+ messages in thread
From: Rongen Wang via B4 Relay @ 2026-11-27 13:52 UTC (permalink / raw)
  To: Martin K. Petersen; +Cc: linux-scsi, target-devel, stable, Rongen Wang

A Login Request with a non-zero TSIH can complete after the session it
joins has lost its last connection. Patch 1 makes
iscsi_post_login_handler() refuse such a login when the session is
being closed or its Time2Retain timer has expired. Patch 2 keeps the
Time2Retain handler from expiring a session while such a login holds
it; without patch 2 the session that patch 1 refuses to join is never
freed.

Based on mkp/scsi.git 7.4/scsi-fixes (4be084ac36f9). Patch 2 changes
iscsit_time2retain_work() from b30d7199af28 ("scsi: target: iscsi: Run
the Time2Retain handler in process context"), which is in that tree and
marked for stable. Patch 1 also applies to mainline 9437ee8b6cf6.

Tested on that tree under QEMU, iSCSI/TCP, with KASAN and again with
KASAN plus PROVE_LOCKING, DEBUG_ATOMIC_SLEEP and DEBUG_OBJECTS_TIMERS /
DEBUG_OBJECTS_WORK:

- Last connection reset while a TSIH != 0 login is parked (the v1
  case): the login is refused, no report.
- DefaultTime2Retain=2, last connection logged out with CLOSE_CONNECTION
  while a TSIH != 0 login is parked, login finished after the timer ran
  out: the login continues the session, I/O works, and the session is
  freed when that connection is reset or logs out. Without the series,
  and with v1, the connection joins the half closed session and its
  logout gives the KASAN report quoted in patch 1. With patch 1 alone
  the login is refused and the session stays allocated.
- The same with DefaultTime2Retain=1 and the final Login PDU sent
  0.6 s to 1.4 s after the logout, 40 rounds: every login continues its
  session, no session is left allocated, no report. With v1, 14 of the
  16 rounds that ran into the expiry gave KASAN reports; with patch 1
  alone 17 sessions were left allocated.
- MaxConnections=2: adding a connection to a live session, I/O on both,
  logouts, a fresh login, and session continuation after a
  CLOSE_CONNECTION logout still work.
- NodeACL removed while a TSIH != 0 login is parked: no report.

The refusal path in __iscsi_target_login_thread() (login completed by
its first PDU) was not reached by these tests; iSER and cxgbit were not
tested. No W=1 warnings in drivers/target/iscsi/, checkpatch clean.

Reproducers for both cases go to the maintainer privately and are not
posted here.

---
Changes in v2:
- Patch 1: also refuse the login when the Time2Retain timer has
  expired, check under se_tpg->session_lock and sess->conn_lock like
  the session lookups do, and stop the timer before session_lock is
  dropped (Sashiko review)
- Patch 1: take over the session parameters and release
  conn->param_list only after the check (Sashiko review)
- New patch 2: do not let Time2Retain expire a session while a login
  holds a usage count
- Sashiko review of v1: https://sashiko.dev/#/patchset/20261127-b4-lio-iscsi-session-closing-v1-1-cdf2bbbef87a@nitrohex.com
- Link to v1: https://patch.msgid.link/20261127-b4-lio-iscsi-session-closing-v1-1-cdf2bbbef87a@nitrohex.com

---
Rongen Wang (2):
      scsi: target: iscsi: Refuse to add a connection to a session being closed
      scsi: target: iscsi: Do not let Time2Retain expire a session in use

 drivers/target/iscsi/iscsi_target_erl0.c  |  21 ++++++
 drivers/target/iscsi/iscsi_target_login.c | 117 ++++++++++++++++++++++++------
 drivers/target/iscsi/iscsi_target_login.h |   4 +-
 drivers/target/iscsi/iscsi_target_nego.c  |   5 +-
 4 files changed, 121 insertions(+), 26 deletions(-)
---
base-commit: 4be084ac36f95f95e21a1e39207e87326967a73d
change-id: 20261127-b4-lio-iscsi-session-closing-67f4557094ce

Best regards,
--  
Rongen Wang <security@nitrohex.com>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] scsi: target: iscsi: Refuse to add a connection to a session being closed
  2026-11-27 13:52 [PATCH v2 0/2] scsi: target: iscsi: Fix adding a connection to a session that is going away Rongen Wang via B4 Relay
@ 2026-11-27 13:52 ` Rongen Wang via B4 Relay
  2026-11-27 13:52 ` [PATCH v2 2/2] scsi: target: iscsi: Do not let Time2Retain expire a session in use Rongen Wang via B4 Relay
  1 sibling, 0 replies; 4+ messages in thread
From: Rongen Wang via B4 Relay @ 2026-11-27 13:52 UTC (permalink / raw)
  To: Martin K. Petersen; +Cc: linux-scsi, target-devel, stable, Rongen Wang

From: Rongen Wang <security@nitrohex.com>

A Login Request with a non-zero TSIH adds a connection to an existing
session. iscsi_login_non_zero_tsih_s2() looks the session up when the
first Login PDU arrives, checks once that it is not being closed and
takes a session usage count. The login then stays pending for as long
as the initiator delays its next Login PDU.

If the last connection of the session fails in the meantime,
iscsit_close_connection() sets session_fall_back_to_erl0 and calls
iscsit_close_session(), which waits for the usage count held by the
pending login. Nothing stops that login from completing:
iscsi_login_post_auth_non_zero_tsih() only compares nconn + 1 with
MaxConnections, and iscsi_post_login_handler() adds the connection to
sess->sess_conn_list and drops the usage count. iscsit_close_session()
then frees the session while the new connection is in full feature
phase, and the first PDU received on it reads the freed session:

  BUG: KASAN: slab-use-after-free in iscsit_get_rx_pdu+0x2338/0x3330
  Read of size 8 at addr ffff88800b19fde0 by task iscsi_trx/156
  Call Trace:
   iscsit_get_rx_pdu+0x2338/0x3330
   iscsi_target_rx_thread+0x139/0x2f0
   kthread+0x2e8/0x3c0
  Freed by task 154:
   kfree+0x127/0x3b0
   iscsit_close_session+0x3a2/0x570
   iscsit_close_connection+0x134d/0x20f0
   iscsit_take_action_for_connection_exit+0x1b5/0x360
   iscsi_target_rx_thread+0x251/0x2f0

Without KASAN the same sequence ends in a NULL pointer dereference in
iscsit_get_rx_pdu() and, when the NOP-In timer of the new connection
fires, in a panic in softirq context.

If the last connection is instead logged out with reason
CLOSE_CONNECTION, or fails at ErrorRecoveryLevel 2, the session waits
in TARG_SESS_STATE_FAILED and iscsit_close_connection() starts the
Time2Retain timer, which the lookup had no reason to stop. If the timer
expires while the login is pending, iscsit_close_session() finds the
usage count and gives up half way, and the login then adds its
connection to a session that is no longer registered in configfs.
Dropping the usage count completes session_waiting_on_uc_comp with
nobody waiting, so when that connection logs out the session,
iscsit_close_session() does not wait for the usage count held by the
logout and frees the session under it:

  BUG: KASAN: slab-use-after-free in _raw_spin_lock_irq+0x72/0xe0
  Write of size 4 at addr ffff888009aee958 by task iscsi_ttx/160
  Call Trace:
   _raw_spin_lock_irq+0x72/0xe0
   __wait_for_common+0x2b6/0x4a0
   iscsit_logout_post_handler+0x1aa/0x660
   iscsit_response_queue+0xa46/0x19d0
   iscsi_target_tx_thread+0x3c7/0x650
  Freed by task 161:
   kfree+0x127/0x3b0
   iscsit_close_session+0x3a2/0x570
   iscsit_close_connection+0x134d/0x20f0
   iscsit_take_action_for_connection_exit+0x2b7/0x360
   iscsi_target_rx_thread+0x1aa/0x2f0

Before adding the connection, check again what the lookup checked:
session_fall_back_to_erl0, session_logout, session_close and
ISCSI_TF_EXPIRED, under se_tpg->session_lock and sess->conn_lock as
iscsi_check_for_session_reinstatement() and lio_tpg_close_session() do.
iscsit_close_connection() decides under sess->conn_lock whether it
closed the last connection, and the Time2Retain handler sets
ISCSI_TF_EXPIRED under se_tpg->session_lock, so either the new
connection is counted first or the login sees that the session is going
away and is refused. Stop the Time2Retain timer before session_lock is
dropped, so that an expiry that is already pending sees ISCSI_TF_STOP
instead of closing a session that has a connection again. Take over the
session parameters negotiated on the new connection only after the
check, so that a refused login leaves sess->sess_ops alone and
iscsi_target_login_sess_out() records it with the initiator name.

Move the transition to TARG_CONN_STATE_LOGGED_IN after that check. The
RX and TX kthreads were started before the final Login Response was
sent and leave a connection alone while it is still in
TARG_CONN_STATE_IN_LOGIN, so a refused connection can be stopped the
same way the error path of iscsi_target_do_tx_login_io() stops it, and
then released with iscsi_target_login_sess_out(). The initiator has
already received the final Login Response and sees the connection
dropped, as if the session had been closed a moment earlier.

The problem was found and this patch was drafted with the help of AI
assistants (Claude Mythos 5.2 and, for v2, Claude Opus 5.5, Anthropic,
via Claude Code); the result was reviewed and tested as described in
the cover letter.

Fixes: e48354ce078c ("iscsi-target: Add iSCSI fabric support for target v4.1")
Cc: stable@vger.kernel.org
Signed-off-by: Rongen Wang <security@nitrohex.com>
---
 drivers/target/iscsi/iscsi_target_login.c | 117 ++++++++++++++++++++++++------
 drivers/target/iscsi/iscsi_target_login.h |   4 +-
 drivers/target/iscsi/iscsi_target_nego.c  |   5 +-
 3 files changed, 100 insertions(+), 26 deletions(-)

diff --git a/drivers/target/iscsi/iscsi_target_login.c b/drivers/target/iscsi/iscsi_target_login.c
index fda04eb15d..7c465a22d6 100644
--- a/drivers/target/iscsi/iscsi_target_login.c
+++ b/drivers/target/iscsi/iscsi_target_login.c
@@ -644,10 +644,41 @@ out_bitmap:
 	return ret;
 }
 
-void iscsi_post_login_handler(
-	struct iscsi_np *np,
-	struct iscsit_conn *conn,
-	u8 zero_tsih)
+/*
+ * Undo iscsit_start_kthreads() for a connection that is still in
+ * TARG_CONN_STATE_IN_LOGIN.  Both kthreads leave the connection alone in
+ * that state and only wait for kthread_stop().
+ */
+void iscsit_stop_kthreads(struct iscsit_conn *conn)
+{
+	if (conn->rx_thread && conn->rx_thread_active) {
+		send_sig(SIGINT, conn->rx_thread, 1);
+		complete(&conn->rx_login_comp);
+		kthread_stop(conn->rx_thread);
+		conn->rx_thread_active = false;
+	}
+	if (conn->tx_thread && conn->tx_thread_active) {
+		send_sig(SIGINT, conn->tx_thread, 1);
+		kthread_stop(conn->tx_thread);
+		conn->tx_thread_active = false;
+	}
+	spin_lock(&iscsit_global->ts_bitmap_lock);
+	bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
+			      get_order(1));
+	spin_unlock(&iscsit_global->ts_bitmap_lock);
+}
+
+/*
+ * Returns 0 once the connection has been made part of its session.
+ *
+ * Returns -1 if a connection that is being added to an existing session
+ * (TSIH != 0) can not be attached because the session started to shut down
+ * while the login was in progress.  The connection is then still in
+ * TARG_CONN_STATE_IN_LOGIN and the caller has to stop its kthreads and
+ * release it with iscsi_target_login_sess_out().
+ */
+int iscsi_post_login_handler(struct iscsi_np *np, struct iscsit_conn *conn,
+			     u8 zero_tsih)
 {
 	int stop_timer = 0;
 	struct iscsit_session *sess = conn->sess;
@@ -657,23 +688,43 @@ void iscsi_post_login_handler(
 
 	iscsit_inc_conn_usage_count(conn);
 
-	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
-			ISCSI_LOGIN_STATUS_ACCEPT);
-
-	pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
-	conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
-
 	iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
 	/*
 	 * SCSI Initiator -> SCSI Target Port Mapping
 	 */
 	if (!zero_tsih) {
-		iscsi_set_session_parameters(sess->sess_ops,
-				conn->param_list, 0);
-		iscsi_release_param_list(conn->param_list);
-		conn->param_list = NULL;
+		/*
+		 * The session was located from ISID+TSIH when the first Login
+		 * Request arrived, and since then only the session usage count
+		 * kept it alive.  If its last connection went away in the
+		 * meantime, iscsit_close_connection() either committed to
+		 * iscsit_close_session(), or moved the session to
+		 * TARG_SESS_STATE_FAILED and started the Time2Retain timer,
+		 * which may have expired since.  Check again what
+		 * iscsi_login_non_zero_tsih_s2() checked, under the locks
+		 * iscsi_check_for_session_reinstatement() uses, and do not
+		 * attach a new connection to a session that is shutting down.
+		 */
+		spin_lock_bh(&se_tpg->session_lock);
+		spin_lock(&sess->conn_lock);
+		if (atomic_read(&sess->session_fall_back_to_erl0) ||
+		    atomic_read(&sess->session_logout) ||
+		    atomic_read(&sess->session_close) ||
+		    (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
+			spin_unlock(&sess->conn_lock);
+			spin_unlock_bh(&se_tpg->session_lock);
+			pr_err("iSCSI session SID %u is shutting down, refusing to add CID %u\n",
+			       sess->sid, conn->cid);
+			iscsit_dec_conn_usage_count(conn);
+			return -1;
+		}
+
+		iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
+					   ISCSI_LOGIN_STATUS_ACCEPT);
+
+		pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
+		conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
 
-		spin_lock_bh(&sess->conn_lock);
 		atomic_set(&sess->session_continuation, 0);
 		if (sess->session_state == TARG_SESS_STATE_FAILED) {
 			pr_debug("Moving to"
@@ -691,7 +742,21 @@ void iscsi_post_login_handler(
 		pr_debug("Incremented iSCSI Connection count to %d"
 			" from node: %s\n", atomic_read(&sess->nconn),
 			sess->sess_ops->InitiatorName);
-		spin_unlock_bh(&sess->conn_lock);
+		spin_unlock(&sess->conn_lock);
+		/*
+		 * Stop the Time2Retain timer before session_lock is dropped, as
+		 * lio_tpg_close_session() does, so that an expiry that is
+		 * already pending sees ISCSI_TF_STOP instead of closing a
+		 * session that has a connection again.
+		 */
+		if (stop_timer)
+			iscsit_stop_time2retain_timer(sess);
+		spin_unlock_bh(&se_tpg->session_lock);
+
+		iscsi_set_session_parameters(sess->sess_ops, conn->param_list,
+					     0);
+		iscsi_release_param_list(conn->param_list);
+		conn->param_list = NULL;
 
 		iscsi_post_login_start_timers(conn);
 		/*
@@ -707,16 +772,16 @@ void iscsi_post_login_handler(
 		 */
 		complete(&conn->rx_login_comp);
 		iscsit_dec_conn_usage_count(conn);
-
-		if (stop_timer) {
-			spin_lock_bh(&se_tpg->session_lock);
-			iscsit_stop_time2retain_timer(sess);
-			spin_unlock_bh(&se_tpg->session_lock);
-		}
 		iscsit_dec_session_usage_count(sess);
-		return;
+		return 0;
 	}
 
+	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
+				   ISCSI_LOGIN_STATUS_ACCEPT);
+
+	pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
+	conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
+
 	iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
 	iscsi_release_param_list(conn->param_list);
 	conn->param_list = NULL;
@@ -769,6 +834,7 @@ void iscsi_post_login_handler(
 	 */
 	complete(&conn->rx_login_comp);
 	iscsit_dec_conn_usage_count(conn);
+	return 0;
 }
 
 int iscsit_setup_np(
@@ -1321,7 +1387,10 @@ static int __iscsi_target_login_thread(struct iscsi_np *np)
 	if (ret == 1) {
 		tpg_np = conn->tpg_np;
 
-		iscsi_post_login_handler(np, conn, zero_tsih);
+		if (iscsi_post_login_handler(np, conn, zero_tsih) < 0) {
+			iscsit_stop_kthreads(conn);
+			goto new_sess_out;
+		}
 		iscsit_deaccess_np(np, tpg, tpg_np);
 	}
 
diff --git a/drivers/target/iscsi/iscsi_target_login.h b/drivers/target/iscsi/iscsi_target_login.h
index 03c7d695d5..d2c6169adb 100644
--- a/drivers/target/iscsi/iscsi_target_login.h
+++ b/drivers/target/iscsi/iscsi_target_login.h
@@ -20,7 +20,9 @@ extern int iscsit_get_login_rx(struct iscsit_conn *, struct iscsi_login *);
 extern int iscsit_put_login_tx(struct iscsit_conn *, struct iscsi_login *, u32);
 extern void iscsit_free_conn(struct iscsit_conn *);
 extern int iscsit_start_kthreads(struct iscsit_conn *);
-extern void iscsi_post_login_handler(struct iscsi_np *, struct iscsit_conn *, u8);
+void iscsit_stop_kthreads(struct iscsit_conn *conn);
+int iscsi_post_login_handler(struct iscsi_np *np, struct iscsit_conn *conn,
+			     u8 zero_tsih);
 extern void iscsi_target_login_sess_out(struct iscsit_conn *, bool, bool);
 extern int iscsi_target_login_thread(void *);
 
diff --git a/drivers/target/iscsi/iscsi_target_nego.c b/drivers/target/iscsi/iscsi_target_nego.c
index ccd468e26f..23f6dffb79 100644
--- a/drivers/target/iscsi/iscsi_target_nego.c
+++ b/drivers/target/iscsi/iscsi_target_nego.c
@@ -640,7 +640,10 @@ static void iscsi_target_do_login_rx(struct work_struct *work)
 		iscsit_stop_login_timer(conn);
 		cancel_delayed_work(&conn->login_work);
 		iscsi_target_nego_release(conn);
-		iscsi_post_login_handler(np, conn, zero_tsih);
+		if (iscsi_post_login_handler(np, conn, zero_tsih) < 0) {
+			iscsit_stop_kthreads(conn);
+			iscsi_target_login_sess_out(conn, zero_tsih, true);
+		}
 		iscsit_deaccess_np(np, tpg, tpg_np);
 	}
 	return;

-- 
2.34.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] scsi: target: iscsi: Do not let Time2Retain expire a session in use
  2026-11-27 13:52 [PATCH v2 0/2] scsi: target: iscsi: Fix adding a connection to a session that is going away Rongen Wang via B4 Relay
  2026-11-27 13:52 ` [PATCH v2 1/2] scsi: target: iscsi: Refuse to add a connection to a session being closed Rongen Wang via B4 Relay
@ 2026-11-27 13:52 ` Rongen Wang via B4 Relay
  2026-11-27 14:07   ` sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Rongen Wang via B4 Relay @ 2026-11-27 13:52 UTC (permalink / raw)
  To: Martin K. Petersen; +Cc: linux-scsi, target-devel, stable, Rongen Wang

From: Rongen Wang <security@nitrohex.com>

A Login Request with a non-zero TSIH holds a session usage count from
iscsi_login_non_zero_tsih_s2() until the login completes. If the last
connection of the session is logged out with reason CLOSE_CONNECTION
while such a login is pending, iscsit_close_connection() moves the
session to TARG_SESS_STATE_FAILED and starts the Time2Retain timer. The
lookup found the session logged in and had no timer to stop.

When the timer expires, iscsit_time2retain_work() marks the session
ISCSI_TF_EXPIRED and calls iscsit_close_session(sess, false). That
removes the session from configfs and sets session_reinstatement, then
finds the usage count held by the login, clears session_logout again
and tries to restart the timer. As commit b30d7199af28 ("scsi: target:
iscsi: Run the Time2Retain handler in process context") notes, that
restart does nothing because ISCSI_TF_RUNNING is still set, and a later
expiry would stop at session_reinstatement anyway. Every lookup skips
an expired session, so nothing frees it after that. With the previous
patch the login is refused, and the session, its se_session and its
command tag pool stay allocated while the target keeps counting the
session. An initiator can repeat this with new ISIDs.

Handle such a login like one into a session that has already failed,
which stops the timer when it looks the session up: while a usage count
is held, leave the session alone and clear ISCSI_TF_RUNNING. The login
then stops the timer when it adds its connection, or starts it again
from iscsi_target_login_sess_out() when it fails. The other paths that
take a usage count on a session without connections set
session_reinstatement first, which the handler already checks.

The Time2Retain case was pointed out by the Sashiko AI review of v1 of
the previous patch; this patch was drafted with the help of an AI
assistant (Claude Opus 5.5, Anthropic, via Claude Code), and the result
was reviewed and tested as described in the cover letter.

Fixes: e48354ce078c ("iscsi-target: Add iSCSI fabric support for target v4.1")
Cc: stable@vger.kernel.org
Signed-off-by: Rongen Wang <security@nitrohex.com>
---
 drivers/target/iscsi/iscsi_target_erl0.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/drivers/target/iscsi/iscsi_target_erl0.c b/drivers/target/iscsi/iscsi_target_erl0.c
index 036ddc1002..35e2eed117 100644
--- a/drivers/target/iscsi/iscsi_target_erl0.c
+++ b/drivers/target/iscsi/iscsi_target_erl0.c
@@ -747,6 +747,7 @@ void iscsit_time2retain_work(struct work_struct *work)
 						   time2retain_work);
 	struct iscsi_portal_group *tpg = sess->tpg;
 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
+	bool in_use;
 
 	spin_lock_bh(&se_tpg->session_lock);
 	/* A stop after the timer fired takes ->time2retain_fired back. */
@@ -761,6 +762,26 @@ void iscsit_time2retain_work(struct work_struct *work)
 		spin_unlock_bh(&se_tpg->session_lock);
 		return;
 	}
+	/*
+	 * A login that adds a connection to this session holds a usage count
+	 * from iscsi_login_non_zero_tsih_s2() until it completes.  It started
+	 * before the session failed, so there was no timer for it to stop.
+	 * Handle it like a login into a failed session: leave the session
+	 * alone, and let the login stop the timer if it succeeds or start it
+	 * again in iscsi_target_login_sess_out() if it fails.  Expiring the
+	 * session now would make iscsit_close_session() find the usage count
+	 * and give up half way, after which nothing frees the session.
+	 */
+	spin_lock(&sess->session_usage_lock);
+	in_use = sess->session_usage_count != 0;
+	spin_unlock(&sess->session_usage_lock);
+	if (in_use) {
+		pr_debug("Time2Retain timer for SID: %u expired during a login, deferring\n",
+			 sess->sid);
+		sess->time2retain_timer_flags &= ~ISCSI_TF_RUNNING;
+		spin_unlock_bh(&se_tpg->session_lock);
+		return;
+	}
 	sess->time2retain_timer_flags |= ISCSI_TF_EXPIRED;
 
 	pr_err("Time2Retain timer expired for SID: %u, cleaning up"

-- 
2.34.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 2/2] scsi: target: iscsi: Do not let Time2Retain expire a session in use
  2026-11-27 13:52 ` [PATCH v2 2/2] scsi: target: iscsi: Do not let Time2Retain expire a session in use Rongen Wang via B4 Relay
@ 2026-11-27 14:07   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-27 14:07 UTC (permalink / raw)
  To: Rongen Wang; +Cc: linux-scsi, target-devel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] scsi: target: iscsi: Time2Retain timer race in
  iscsi_target_login_sess_out()
--

commit 98654cdef0969ffe654a76bd2db16a5343a38c0a
Author: Rongen Wang <security@nitrohex.com>

scsi: target: iscsi: Do not let Time2Retain expire a session in use

This commit defers Time2Retain session expiry in iscsit_time2retain_work()
when session_usage_count is non-zero, clearing ISCSI_TF_RUNNING so an
in-progress login can either stop or restart the timer when it finishes.

> diff --git a/drivers/target/iscsi/iscsi_target_erl0.c b/drivers/target/iscsi/iscsi_target_erl0.c
> index 036ddc1002f6c..35e2eed11750d 100644
> --- a/drivers/target/iscsi/iscsi_target_erl0.c
> +++ b/drivers/target/iscsi/iscsi_target_erl0.c
> @@ -747,6 +747,7 @@ void iscsit_time2retain_work(struct work_struct *work)
>  						   time2retain_work);
>  	struct iscsi_portal_group *tpg = sess->tpg;
>  	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
> +	bool in_use;
>  
>  	spin_lock_bh(&se_tpg->session_lock);
>  	/* A stop after the timer fired takes ->time2retain_fired back. */
> @@ -761,6 +762,26 @@ void iscsit_time2retain_work(struct work_struct *work)
>  		spin_unlock_bh(&se_tpg->session_lock);
>  		return;
>  	}
> +	/*
> +	 * A login that adds a connection to this session holds a usage count
> +	 * from iscsi_login_non_zero_tsih_s2() until it completes.  It started
> +	 * before the session failed, so there was no timer for it to stop.
> +	 * Handle it like a login into a failed session: leave the session
> +	 * alone, and let the login stop the timer if it succeeds or start it
> +	 * again in iscsi_target_login_sess_out() if it fails.  Expiring the
> +	 * session now would make iscsit_close_session() find the usage count
> +	 * and give up half way, after which nothing frees the session.
> +	 */
> +	spin_lock(&sess->session_usage_lock);
> +	in_use = sess->session_usage_count != 0;
> +	spin_unlock(&sess->session_usage_lock);
> +	if (in_use) {
> +		pr_debug("Time2Retain timer for SID: %u expired during a login, deferring\n",
> +			 sess->sid);
> +		sess->time2retain_timer_flags &= ~ISCSI_TF_RUNNING;
> +		spin_unlock_bh(&se_tpg->session_lock);
> +		return;
> +	}

[Severity: Medium]
Can this still leak the session when a failing login in
iscsi_target_login_sess_out() races with Time2Retain expiration?

drivers/target/iscsi/iscsi_target_login.c:iscsi_target_login_sess_out() {
    ...
	if (!zero_tsih && conn->sess) {
		spin_lock_bh(&conn->sess->conn_lock);
		if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
			struct se_portal_group *se_tpg =
					&conn->tpg->tpg_se_tpg;

			atomic_set(&conn->sess->session_continuation, 0);
			spin_unlock_bh(&conn->sess->conn_lock);
			spin_lock_bh(&se_tpg->session_lock);
			iscsit_start_time2retain_handler(conn->sess);
			spin_unlock_bh(&se_tpg->session_lock);
		} else
			spin_unlock_bh(&conn->sess->conn_lock);
		iscsit_dec_session_usage_count(conn->sess);
	}
    ...
}

Because iscsi_target_login_sess_out() restarts the timer and drops
se_tpg->session_lock before it decrements session_usage_count, both sides
can defer to each other after the timer fires in softirq:

CPU0                                     CPU1 (kworker)
iscsit_handle_time2retain_timeout()
  atomic_set(&sess->time2retain_fired, 1)
  schedule_work(&sess->time2retain_work)
iscsi_target_login_sess_out()
  spin_lock_bh(&se_tpg->session_lock)
  iscsit_start_time2retain_handler(sess)
    /* ISCSI_TF_RUNNING is still set, returns early */
  spin_unlock_bh(&se_tpg->session_lock)
                                         iscsit_time2retain_work()
                                           spin_lock_bh(&se_tpg->session_lock)
                                           /* session_usage_count != 0 */
                                           sess->time2retain_timer_flags &=
                                                   ~ISCSI_TF_RUNNING
                                           spin_unlock_bh(&se_tpg->session_lock)
                                           return;
  iscsit_dec_session_usage_count(sess)
  /* session left failed with no timer running <-- leak */

The same thing happens if iscsit_time2retain_work() runs first and clears
ISCSI_TF_RUNNING, and the timer restarted by iscsi_target_login_sess_out()
fires again (for example when DefaultTime2Retain is 0) before
iscsit_dec_session_usage_count() runs, or if the last connection fails
right after iscsi_target_login_sess_out() checks session_state and its
timer expires before session_usage_count is decremented.  In each case
the session stays in TARG_SESS_STATE_FAILED with no connections and no
active Time2Retain timer, leaking struct iscsit_session, struct se_session
and the command tag pool.

Decrementing session_usage_count before restarting the Time2Retain timer
(or while holding se_tpg->session_lock across both the decrement and
iscsit_start_time2retain_handler()) would close this window.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127-b4-lio-iscsi-session-closing-v2-0-a3ac8e88cbad@nitrohex.com?part=2

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-27 14:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 13:52 [PATCH v2 0/2] scsi: target: iscsi: Fix adding a connection to a session that is going away Rongen Wang via B4 Relay
2026-11-27 13:52 ` [PATCH v2 1/2] scsi: target: iscsi: Refuse to add a connection to a session being closed Rongen Wang via B4 Relay
2026-11-27 13:52 ` [PATCH v2 2/2] scsi: target: iscsi: Do not let Time2Retain expire a session in use Rongen Wang via B4 Relay
2026-11-27 14:07   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox