Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Vitalii Dodonov" <dreamer.two@gmail.com>, linux-scsi@vger.kernel.org
Cc: target-devel@vger.kernel.org
Subject: Re: [PATCH 1/1] scsi: target: iscsi: fix use-after-free in iscsit_stop_session()
Date: Sat, 21 Nov 2026 16:50:05 +0000	[thread overview]
Message-ID: <sashiko-outbox-227178@kernel.org> (raw)
In-Reply-To: <179527913740.32224.16728687439026782592@gmail.com>

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] scsi: target: iscsi: session leak when closing failed session
  with nconn == 0 [1]
--

commit 29536f098b4fb525d53737f9024a60f1ba499f18
Author: Vitalii Dodonov <dreamer.two@gmail.com>

scsi: target: iscsi: fix use-after-free in iscsit_stop_session()

This commit fixes a use-after-free in iscsit_stop_session() by taking a
reference-counted snapshot of sess->sess_conn_list under sess->conn_lock
before dropping the lock to reinstate connections.

> diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
> index 03d618cd76721..bd56dc76fbca8 100644
> --- a/drivers/target/iscsi/iscsi_target.c
> +++ b/drivers/target/iscsi/iscsi_target.c
> @@ -4669,36 +4669,62 @@ void iscsit_stop_session(
>  	int session_sleep,
>  	int connection_sleep)
>  {
> -	u16 conn_count = atomic_read(&sess->nconn);
> -	struct iscsit_conn *conn, *conn_tmp = NULL;
> -	int is_last;
> +	struct iscsit_conn *conn, **conns = NULL;
> +	unsigned int i, n, conn_count = 0;
> +
> +	/*
> +	 * sess_conn_list may only be walked while holding sess->conn_lock: a
> +	 * connection that is being shut down concurrently runs
> +	 * iscsit_close_connection(), which removes itself from that list with
> +	 * list_del() under the same lock and leaves LIST_POISON1 in
> +	 * conn->conn_list.next.  Pinning the connection itself is not enough,
> +	 * because the list links are poisoned as well, so take a snapshot of
> +	 * the connections (with a reference on each) and work on that snapshot
> +	 * only.
> +	 *
> +	 * The sleeping variant is called from process context only --
> +	 * iscsi_target_tx_thread(), the login workqueue (session
> +	 * reinstatement), configfs, iscsit_release_sessions_for_tpg() -- with
> +	 * no locks held, and this function already blocks below in
> +	 * iscsit_cause_connection_reinstatement(conn, 1) and in
> +	 * wait_for_completion(&sess->session_wait_comp), so the GFP_KERNEL
> +	 * allocation does not add a new constraint.
> +	 */
> +	if (connection_sleep) {
> +		conn_count = atomic_read(&sess->nconn);

[Severity: High]
This problem wasn't introduced by this patch, but can closing or
reinstating a session in TARG_SESS_STATE_FAILED when sess->nconn is
already 0 leak the session [1]?

When the last connection closes without falling back to ERL=0 (for
example on ISCSI_LOGOUT_REASON_CLOSE_CONNECTION),
iscsit_close_connection() moves the session to TARG_SESS_STATE_FAILED
with sess->nconn == 0 and starts the Time2Retain timer.  Commit
57c46e9f33da ("scsi: target: fix hang when multiple threads try to
destroy the same iscsi session") removed the direct
iscsit_close_session() calls from iscsi_check_for_session_reinstatement(),
lio_tpg_close_session(), and iscsit_release_sessions_for_tpg(), relying
on iscsit_stop_session() to tear down the remaining connections so the
last connection calls iscsit_close_session().

If one of those paths runs while sess->nconn is already 0, it stops the
Time2Retain timer and either returns early on TARG_SESS_STATE_FAILED:

drivers/target/iscsi/iscsi_target_login.c:iscsi_check_for_session_reinstatement() {
    ...
	spin_lock_bh(&sess->conn_lock);
	if (sess->session_state == TARG_SESS_STATE_FAILED) {
		spin_unlock_bh(&sess->conn_lock);
		iscsit_dec_session_usage_count(sess);
		return 0;
	}
	spin_unlock_bh(&sess->conn_lock);

	iscsit_stop_session(sess, 1, 1);
    ...
}

or calls iscsit_stop_session() with an empty sess->sess_conn_list.  With
no connection left to run iscsit_close_connection(),
iscsit_close_session() never runs, leaking struct iscsit_session and
struct se_session and leaving core_tpg_del_initiator_node_acl() hung in
wait_for_completion(&acl->acl_free_comp).

[1] https://sashiko.dev/#/bug/linux-305f6726-98b0-4cd9-9238-5325539f4459

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179527913740.32224.2426414360492240680@gmail.com?part=1

      reply	other threads:[~2026-11-21 16:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21 16:38 [PATCH 0/1] scsi: target: iscsi: fix use-after-free in iscsit_stop_session() Vitalii Dodonov
2026-11-21 16:38 ` [PATCH 1/1] " Vitalii Dodonov
2026-11-21 16:50   ` 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-227178@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dreamer.two@gmail.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox