Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yehyeong Lee" <yhlee@isslab.korea.ac.kr>
Cc: target-devel@vger.kernel.org, linux-scsi@vger.kernel.org
Subject: Re: [PATCH v2] scsi: target: iscsi: run the Time2Retain handler in process context
Date: Tue, 17 Nov 2026 12:19:29 +0000	[thread overview]
Message-ID: <sashiko-outbox-218766@kernel.org> (raw)
In-Reply-To: <20261117114920.1955697-1-yhlee@isslab.korea.ac.kr>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] scsi: target: iscsi: use-after-free in iscsit_close_session()
  on timer race
--

commit d4a7bf123031292ded7f0f9fa40305b57dfe90d2
Author: Yehyeong Lee <yhlee@isslab.korea.ac.kr>

scsi: target: iscsi: run the Time2Retain handler in process context

This commit moves the Time2Retain timeout handling from the timer callback
into a work item and coordinates cancellation using a time2retain_fired
atomic flag.

Link: https://lore.kernel.org/all/20261115121943.1446744-1-yhlee@isslab.korea.ac.kr/

> diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
> index 03d618cd76721..645ac9bd156d3 100644
> --- a/drivers/target/iscsi/iscsi_target.c
> +++ b/drivers/target/iscsi/iscsi_target.c
> @@ -4516,6 +4516,15 @@ int iscsit_close_session(struct iscsit_session *sess, bool can_sleep)
>  	sess->sess_ops = NULL;
>  	spin_unlock_bh(&se_tpg->session_lock);
>  
> +	/*
> +	 * No Time2Retain work may outlive the session.  A work item never runs
> +	 * twice at once, so the work itself can only find a queued one.
> +	 */
> +	if (can_sleep)
> +		cancel_work_sync(&sess->time2retain_work);
> +	else
> +		cancel_work(&sess->time2retain_work);
> +
>  	kfree(sess);
>  	return 0;
>  }
> diff --git a/drivers/target/iscsi/iscsi_target_erl0.c b/drivers/target/iscsi/iscsi_target_erl0.c
> index 24db6b07493e6..036ddc1002f6c 100644
> --- a/drivers/target/iscsi/iscsi_target_erl0.c
> +++ b/drivers/target/iscsi/iscsi_target_erl0.c
> @@ -741,15 +741,17 @@ int iscsit_check_post_dataout(
>  	}
>  }
>  
> -void iscsit_handle_time2retain_timeout(struct timer_list *t)
> +void iscsit_time2retain_work(struct work_struct *work)
>  {
> -	struct iscsit_session *sess = timer_container_of(sess, t,
> -							 time2retain_timer);
> +	struct iscsit_session *sess = container_of(work, struct iscsit_session,
> +						   time2retain_work);
>  	struct iscsi_portal_group *tpg = sess->tpg;
>  	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
>  
>  	spin_lock_bh(&se_tpg->session_lock);
> -	if (sess->time2retain_timer_flags & ISCSI_TF_STOP) {
> +	/* A stop after the timer fired takes ->time2retain_fired back. */
> +	if (!atomic_xchg(&sess->time2retain_fired, 0) ||
> +	    (sess->time2retain_timer_flags & ISCSI_TF_STOP)) {
>  		spin_unlock_bh(&se_tpg->session_lock);
>  		return;
>  	}
> @@ -769,6 +771,15 @@ void iscsit_handle_time2retain_timeout(struct timer_list *t)
>  	iscsit_close_session(sess, false);
>  }
>  
> +void iscsit_handle_time2retain_timeout(struct timer_list *t)
> +{
> +	struct iscsit_session *sess = timer_container_of(sess, t,
> +							 time2retain_timer);
> +
> +	atomic_set(&sess->time2retain_fired, 1);
> +	queue_work(system_dfl_wq, &sess->time2retain_work);
> +}

[Severity: High]
Can an already-started iscsit_time2retain_work() from an earlier timer
expiry consume time2retain_fired from a re-armed timer and free sess while
iscsit_handle_time2retain_timeout() is still running?

If the session is resumed and stops the timer after the first work item has
started (before it takes se_tpg->session_lock), and then re-arms the timer
on a subsequent logout, the second timer expiry sets time2retain_fired = 1
before calling queue_work():

CPU0                              CPU1
iscsit_time2retain_work() [1st]
  /* starts before session_lock */
                                  /* timer stopped, then re-armed */
                                  iscsit_handle_time2retain_timeout()
                                    atomic_set(&sess->time2retain_fired, 1)
  spin_lock_bh(&se_tpg->session_lock)
  atomic_xchg(&sess->time2retain_fired, 0)
  sess->time2retain_timer_flags |= ISCSI_TF_EXPIRED
  spin_unlock_bh(&se_tpg->session_lock)
  iscsit_close_session(sess, false)
    iscsit_stop_time2retain_timer(sess)
      /* sees ISCSI_TF_EXPIRED, returns -1 */
    ...
    cancel_work(&sess->time2retain_work)
    kfree(sess)
                                    queue_work(..., &sess->time2retain_work)
                                      <-- use-after-free

Because iscsit_time2retain_work() sets ISCSI_TF_EXPIRED before calling
iscsit_close_session(), iscsit_stop_time2retain_timer() returns early
without calling timer_delete_sync(&sess->time2retain_timer):

drivers/target/iscsi/iscsi_target_erl0.c:iscsit_stop_time2retain_timer() {
	...
	if (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)
		return -1;
	...
	timer_delete_sync(&sess->time2retain_timer);
}

As a result, nothing waits for the timer callback on CPU1 to finish
queue_work() before cancel_work() and kfree(sess) run, queueing work on a
freed session.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117114920.1955697-1-yhlee@isslab.korea.ac.kr?part=1

      reply	other threads:[~2026-11-17 12:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 11:49 [PATCH v2] scsi: target: iscsi: run the Time2Retain handler in process context Yehyeong Lee
2026-11-17 12:19 ` 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-218766@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=target-devel@vger.kernel.org \
    --cc=yhlee@isslab.korea.ac.kr \
    /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