Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Andrew Stellman" <bugs@stellman-greene.com>
Cc: target-devel@vger.kernel.org, linux-scsi@vger.kernel.org
Subject: Re: [PATCH scsi-fixes] scsi: target: iscsi: Fix UAF on an immediate data digest error
Date: Sat, 28 Nov 2026 19:38:30 +0000	[thread overview]
Message-ID: <sashiko-outbox-239001@kernel.org> (raw)
In-Reply-To: <20261128191722.30110-1-bugs@stellman-greene.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] scsi: target: iscsi: UAF on teardown of retained WRITE_PENDING
  reject cmd
- [High] scsi: target: iscsi: stale ITT on retained reject cmd breaks
  Data-Out/UAF
--

commit 8f8dd4e72853eebb81ea598563061a3153ff3d89
Author: Andrew Stellman <astellman@stellman-greene.com>

scsi: target: iscsi: Fix UAF on an immediate data digest error

This commit stops queuing ISTATE_REMOVE on the immediate queue when
immediate data fails its data digest check at ErrorRecoveryLevel >= 1 in
iscsit_get_immediate_data() and cxgbit_get_immediate_data(), so that the
command is not freed before the queued Reject PDU is transmitted.

> diff --git a/drivers/target/iscsi/cxgbit/cxgbit_target.c b/drivers/target/iscsi/cxgbit/cxgbit_target.c
> index 3698f2eb097e3..445bf5439964c 100644
> --- a/drivers/target/iscsi/cxgbit/cxgbit_target.c
> +++ b/drivers/target/iscsi/cxgbit/cxgbit_target.c
> @@ -946,8 +946,8 @@ cxgbit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
>  	} else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
>  		/*
>  		 * Immediate Data failed DataCRC and ERL>=1,
> -		 * silently drop this PDU and let the initiator
> -		 * plug the CmdSN gap.
> +		 * reject this PDU and let the initiator plug the CmdSN gap.
> +		 * The queued Reject retains the command.
>  		 *
>  		 * FIXME: Send Unsolicited NOPIN with reserved
>  		 * TTT here to help the initiator figure out
> @@ -955,8 +955,6 @@ cxgbit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
>  		 * intelligent enough to determine the missing
>  		 * CmdSN and issue a retry to plug the sequence.
>  		 */
> -		cmd->i_state = ISTATE_REMOVE;
> -		iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
>  	} else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
>  		return -1;
>  
> diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
> index 2b4956f46c132..8e1dec7e96510 100644
> --- a/drivers/target/iscsi/iscsi_target.c
> +++ b/drivers/target/iscsi/iscsi_target.c
> @@ -1358,8 +1358,8 @@ iscsit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
>  	} else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
>  		/*
>  		 * Immediate Data failed DataCRC and ERL>=1,
> -		 * silently drop this PDU and let the initiator
> -		 * plug the CmdSN gap.
> +		 * reject this PDU and let the initiator plug the CmdSN gap.
> +		 * The queued Reject retains the command.
>  		 *
>  		 * FIXME: Send Unsolicited NOPIN with reserved
>  		 * TTT here to help the initiator figure out
> @@ -1367,8 +1367,6 @@ iscsit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
>  		 * intelligent enough to determine the missing
>  		 * CmdSN and issue a retry to plug the sequence.
>  		 */
> -		cmd->i_state = ISTATE_REMOVE;
> -		iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
>  	} else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
>  		return -1;

[Severity: High]
Can retaining cmd on conn->conn_cmd_list in both iscsit_get_immediate_data()
and cxgbit_get_immediate_data() cause a refcount underflow and
use-after-free if the connection closes before ExpStatSN acknowledges the
Reject?

Before immediate data is read, iscsit_setup_scsi_cmd() takes a second
reference via target_get_sess_cmd() (cmd_kref = 2), and
iscsit_process_scsi_cmd() -> transport_generic_new_cmd() adds cmd->se_cmd to
se_dev->queues[cpuid].state_list and sets cmd->se_cmd.t_state =
TRANSPORT_WRITE_PENDING.  When the immediate data CRC fails,
iscsit_reject_cmd() -> iscsit_add_reject_from_cmd() calls
target_put_sess_cmd(), dropping cmd_kref to 1 while leaving t_state as
TRANSPORT_WRITE_PENDING.

If the connection is torn down before the Reject's StatSN is acknowledged,
iscsit_release_commands_from_conn() sees t_state == TRANSPORT_WRITE_PENDING
and calls target_complete_cmd(&cmd->se_cmd, SAM_STAT_TASK_ABORTED), which
schedules target_complete_ok_work, and then calls iscsit_free_cmd(cmd, true)
in its second loop:

iscsit_release_commands_from_conn()   kworker (target_completion_wq)
  target_complete_cmd(&cmd->se_cmd, ...)
    schedule_work(&cmd->work)
                                      target_complete_ok_work()
                                        lio_queue_status()
                                          iscsit_add_cmd_to_response_queue()
                                        transport_cmd_check_stop_to_fabric()
                                          lio_check_stop_free()
                                            target_put_sess_cmd()
                                              /* cmd_kref 1 -> 0, frees cmd */
  iscsit_free_cmd(cmd, true)
    transport_generic_free_cmd()
      target_put_sess_cmd()  <-- refcount underflow / use-after-free

Even if target_wait_free_cmd() sets CMD_T_STOP before
target_complete_ok_work finishes, target_complete_ok_work still queues a new
iscsi_queue_req onto conn->response_queue_list via lio_queue_status() after
the first __iscsit_free_cmd(cmd, true) has flushed the response queue.
Because cmd_kref was already 1, transport_generic_free_cmd() drops it to 0,
frees cmd, and returns rc = 1, so iscsit_free_cmd() skips the second
__iscsit_free_cmd(cmd, true) and leaves a dangling qr->cmd on
conn->response_queue_list that iscsit_free_queue_reqs_for_conn() later
dereferences.  In addition, because cmd->se_cmd remains on state_list, an
ABORT_TASK or LUN_RESET TMF can also find and abort it while cmd_kref is 1.

[Severity: High]
Does leaving the rejected command on conn->conn_cmd_list with its original
cmd->init_task_tag also allow subsequent Data-Out or SNACK PDUs for that ITT
to match the rejected command?

When the rejected WRITE command had F=0 (cmd->unsolicited_data == 1),
iscsit_get_immediate_data() and cxgbit_get_immediate_data() skip
iscsit_set_unsolicited_dataout(cmd), leaving cmd->seq_end_offset == 0 while
ICF_GOT_LAST_DATAOUT is unset.  If pipelined Unsolicited Data-Out PDUs from
the same first burst arrive, iscsit_find_cmd_from_itt_or_dump() matches the
retained cmd instead of dumping their payload:

drivers/target/iscsi/iscsi_target_erl0.c:iscsit_dataout_check_unsolicited_sequence() {
	if ((be32_to_cpu(hdr->offset) < cmd->seq_start_offset) ||
	   ((be32_to_cpu(hdr->offset) + payload_length) > cmd->seq_end_offset)) {
...
		return DATAOUT_CANNOT_RECOVER;
	}
}

Because seq_end_offset is 0, iscsit_check_dataout_hdr() returns -1 without
draining payload_length bytes from the socket, and iscsit_handle_data_out()
returns 0, leaving the unconsumed Data-Out payload in the TCP stream to be
parsed as the next iSCSI PDU header (or dropping the connection in cxgbit).

Furthermore, if a follow-up PDU for that ITT matches the retained cmd and
triggers iscsit_reject_cmd() (for example, a Data-Out PDU with offset +
payload_length > cmd->se_cmd.data_length in __iscsit_check_dataout_hdr()),
iscsit_add_reject_from_cmd() sees cmd->se_cmd.se_tfo != NULL and calls
target_put_sess_cmd(&cmd->se_cmd) a second time, dropping cmd_kref from 1 to
0 and freeing cmd while it is still linked on conn->conn_cmd_list and
conn->response_queue_list.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128191722.30110-1-bugs@stellman-greene.com?part=1

      reply	other threads:[~2026-11-28 19:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 19:17 [PATCH scsi-fixes] scsi: target: iscsi: Fix UAF on an immediate data digest error Andrew Stellman
2026-11-28 19:38 ` 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-239001@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bugs@stellman-greene.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