All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Rongen Wang" <security@nitrohex.com>
Cc: linux-scsi@vger.kernel.org, target-devel@vger.kernel.org
Subject: Re: [PATCH v2 2/2] scsi: target: iscsi: Do not let Time2Retain expire a session in use
Date: Fri, 27 Nov 2026 14:07:38 +0000	[thread overview]
Message-ID: <sashiko-outbox-237532@kernel.org> (raw)
In-Reply-To: <20261127-b4-lio-iscsi-session-closing-v2-2-a3ac8e88cbad@nitrohex.com>

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

      reply	other threads:[~2026-11-27 14:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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
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
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 13:52   ` Rongen Wang
2026-11-27 14:07   ` sashiko-bot [this message]

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=sashiko-outbox-237532@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=security@nitrohex.com \
    --cc=target-devel@vger.kernel.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.