Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yehyeong Lee" <yhlee@isslab.korea.ac.kr>
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH v2] scsi: libiscsi_tcp: hold back_lock across the copy of a Data-In
Date: Fri, 20 Nov 2026 17:04:52 +0000	[thread overview]
Message-ID: <sashiko-outbox-225621@kernel.org> (raw)
In-Reply-To: <20261120163857.3238489-1-yhlee@isslab.korea.ac.kr>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] scsi: libiscsi_tcp: race on datain_task after dropping
  back_lock before done()
- [High] scsi: libiscsi_tcp: missing ISCSI_TASK_RUNNING check in
  iscsi_tcp_hdr_dissect()
--

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

scsi: libiscsi_tcp: hold back_lock across the copy of a Data-In

This commit records the active Data-In task in tcp_conn->in.datain_task,
clears it in iscsi_tcp_cleanup_task(), and holds session->back_lock
across the skb copy in iscsi_tcp_recv_skb() to prevent copying into a
freed scatterlist when error handling fails the command.

> diff --git a/drivers/scsi/libiscsi_tcp.c b/drivers/scsi/libiscsi_tcp.c
> index 20ec3ba45067f..0f0ce902bdc5f 100644
> --- a/drivers/scsi/libiscsi_tcp.c
> +++ b/drivers/scsi/libiscsi_tcp.c
[ ... ]
> @@ -744,6 +752,7 @@ iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
>  				      tcp_task->data_offset,
>  				      tcp_conn->in.datalen);
>  			task->last_xfer = jiffies;
> +			tcp_conn->in.datain_task = task;
>  			rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
>  						   sdb->table.sgl,
>  						   sdb->table.nents,

[Severity: High]
Can iscsi_tcp_hdr_dissect() aim tcp_conn->in.datain_task and
tcp_conn->in.segment at a task that is not in ISCSI_TASK_RUNNING?

In this tree, neither iscsi_itt_to_ctask() nor iscsi_tcp_hdr_dissect()
checks task->state == ISCSI_TASK_RUNNING.  When a new command is queued
in iscsi_queuecommand() -> iscsi_alloc_task(), its preallocated slot has
a valid task->sc and matching session->age while sitting on
conn->cmdqueue in ISCSI_TASK_PENDING.

If a stray Data-In header arrives for that ITT before iscsi_data_xmit()
runs, iscsi_tcp_hdr_dissect() points tcp_conn->in.datain_task and
tcp_conn->in.segment at the pending command's scatterlist; once
iscsi_prep_scsi_cmd_pdu() transitions the task to ISCSI_TASK_RUNNING,
iscsi_tcp_recv_skb() will see a running datain_task and copy the stale
payload into the new command's buffer.  If the pending task on cxgbi is
 instead aborted or failed before cxgbi_conn_alloc_pdu() sets
tcp_task->dd_data, cxgbi_cleanup_task() returns early without calling
iscsi_tcp_cleanup_task(), leaving tcp_conn->in.datain_task dangling and
allowing a subsequent command in that slot to copy into the freed
scatterlist.  Furthermore, a zero-length Data-In with
ISCSI_FLAG_DATA_STATUS calls __iscsi_complete_pdu() directly from
iscsi_tcp_hdr_dissect(), freeing an ISCSI_TASK_PENDING task while it
remains linked on conn->cmdqueue (causing a NULL task->sc dereference in
iscsi_prep_scsi_cmd_pdu()) or overwriting sc->result with DID_OK on an
ISCSI_TASK_ABRT_TMF task.

[ ... ]
> @@ -925,38 +980,36 @@ int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
>  		return 0;
>  	}
>  
> -	if (offloaded) {
> -		segment->total_copied = segment->total_size;
> -		goto segment_done;
> -	}
> -
> -	skb_prepare_seq_read(skb, offset, skb->len, &seq);
> -	while (1) {
> -		unsigned int avail;
> -		const u8 *ptr;
> +	/*
> +	 * Hold back_lock across a Data-In copy so the command cannot be
> +	 * given back while it runs.  _bh: this also runs from a work item.
> +	 */
> +	if (segment->sg) {
> +		struct iscsi_task *task;
>  
> -		avail = skb_seq_read(consumed, &ptr, &seq);
> -		if (avail == 0) {
> -			ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
> -				      consumed);
> -			*status = ISCSI_TCP_SKB_DONE;
> -			goto skb_done;
> +		spin_lock_bh(&session->back_lock);
> +		task = tcp_conn->in.datain_task;
> +		if (!task || task->state != ISCSI_TASK_RUNNING) {
> +			spin_unlock_bh(&session->back_lock);
> +			iscsi_conn_printk(KERN_ERR, conn,
> +					  "Data-In for a command that is gone\n");
> +			*status = ISCSI_TCP_CONN_ERR;
> +			iscsi_conn_failure(conn, ISCSI_ERR_BAD_ITT);
> +			return 0;
>  		}
> -		BUG_ON(segment->copied >= segment->size);
> -
> -		ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
> -			      avail);
> -		rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
> -		BUG_ON(rc == 0);
> -		consumed += rc;
> +		consumed = iscsi_tcp_copy_skb(conn, skb, offset, offloaded,
> +					      &segment_done);
> +		spin_unlock_bh(&session->back_lock);
> +	} else {
> +		consumed = iscsi_tcp_copy_skb(conn, skb, offset, offloaded,
> +					      &segment_done);
> +	}
>  
> -		if (segment->total_copied >= segment->total_size) {
> -			skb_abort_seq_read(&seq);
> -			goto segment_done;
> -		}
> +	if (!segment_done) {
> +		*status = ISCSI_TCP_SKB_DONE;
> +		goto skb_done;
>  	}
>  
> -segment_done:
>  	*status = ISCSI_TCP_SEGMENT_DONE;
>  	ISCSI_DBG_TCP(conn, "segment done\n");
>  	rc = segment->done(tcp_conn, segment);

[Severity: High]
Can dropping session->back_lock before segment->done() let
iscsi_tcp_process_data_in() complete an aborted or reused task in the
same slot?

When segment->done() runs iscsi_tcp_process_data_in() for a Data-In PDU
with ISCSI_FLAG_DATA_STATUS set, it never checks
tcp_conn->in.datain_task; instead, it calls iscsi_complete_pdu() ->
__iscsi_complete_pdu(), which re-acquires session->back_lock and looks up
hdr->itt afresh via iscsi_itt_to_ctask().  Because the error handler was
spinning on session->back_lock while the final skb was being copied, it
acquires session->back_lock as soon as the copy unlocks it:

CPU0                                  CPU1
iscsi_tcp_recv_skb()
  spin_lock_bh(&session->back_lock)
  iscsi_tcp_copy_skb()
                                      iscsi_eh_abort()
                                        fail_scsi_task()
                                          spin_lock_bh(&session->back_lock)
  spin_unlock_bh(&session->back_lock)
                                          sc->result = DID_ABORT << 16
                                          task->state = ISCSI_TASK_ABRT_TMF
                                          spin_unlock_bh(&session->back_lock)
  segment->done()
    iscsi_tcp_process_data_in()
      iscsi_complete_pdu()
        __iscsi_complete_pdu()
          iscsi_itt_to_ctask()
          iscsi_data_in_rsp()
            sc->result = (DID_OK << 16) | ...
            <-- overwrites DID_ABORT
                                        iscsi_put_task(task)
                                          iscsi_free_task()
                                            scsi_done(sc)

If fail_scsi_tasks() (or iscsi_eh_abort() after iscsi_put_task()) frees
the task during that window and iscsi_queuecommand() reuses the slot for
a new command before iscsi_complete_pdu() takes session->back_lock,
__iscsi_complete_pdu() will look up hdr->itt and complete the new
command instead (freeing it while still on conn->cmdqueue if it is in
ISCSI_TASK_PENDING, or completing it prematurely with the old Data-In's
status if it is already running).

Also, when software data digests are enabled,
iscsi_tcp_segment_splice_digest() sets segment->sg = NULL before
receiving the 4-byte digest.  If the digest arrives in a subsequent skb
after the task has been cleaned up, iscsi_tcp_recv_skb() sees
segment->sg == NULL, skips both session->back_lock and the
tcp_conn->in.datain_task check, and runs iscsi_tcp_process_data_in().

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

      reply	other threads:[~2026-11-20 17:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 16:38 [PATCH v2] scsi: libiscsi_tcp: hold back_lock across the copy of a Data-In Yehyeong Lee
2026-11-20 17:04 ` 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-225621@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --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