Skip to content

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

Vitalii Dodonov linux-scsi stable target-devel 1 version New

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest1 patch 0 repliesNew

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/1scsi: target: iscsi: fix use-after-free in iscsit_stop_session()drivers/target/iscsi/iscsi_target.c+47 -210 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/1] scsi: target: iscsi: fix use-after-free in iscsit_stop_session()

The iSCSI target panics with a general protection fault inside iscsit_stop_session() when a session that has more than one connection is torn down while another thread closes one of those connections.

iscsit_stop_session() walks sess->sess_conn_list with list_for_each_entry_safe() and drops sess->conn_lock inside the loop. A connection that is being shut down concurrently runs iscsit_close_connection(), which removes itself from the list with list_del() under the same lock and leaves LIST_POISON1 in conn->conn_list.next. Pinning the next connection with iscsit_inc_conn_usage_count() keeps the object alive, but not the list links, so the following iteration computes conn_tmp = list_next_entry() from the poisoned pointer and passes it to iscsit_inc_conn_usage_count(), which takes a spinlock at *(conn_tmp + offsetof(conn_usage_lock)) and faults on 0xdead000000000004.

Crash, 7.3.0-11-generic, LIO target with MaxConnections=8, Microsoft iSCSI initiator holding 8 MC/S connections, graceful reboot of the initiator while I/O is in flight:

  Oops: general protection fault, probably for non-canonical address 0xdead000000000004: 0000 [#1] SMP PTI
  CPU: 0 UID: 0 PID: 9028 Comm: kworker/0:2 Not tainted 7.3.0-11-generic #12-Ubuntu PREEMPT(lazy)
  Workqueue: events iscsi_target_do_login_rx [iscsi_target_mod]
  RIP: 0010:_raw_spin_lock_bh+0x18/0x50
  RDI: dead000000000004   RBX: deacfffffffffcb0   R15: deacfffffffffcb0
  Call Trace:
   ? iscsit_inc_conn_usage_count+0x1b/0x40 [iscsi_target_mod]
   iscsit_stop_session+0x7d/0x1b0 [iscsi_target_mod]
   iscsi_check_for_session_reinstatement+0x1e1/0x290 [iscsi_target_mod]
   iscsi_target_handle_csg_one+0x135/0x160 [iscsi_target_mod]
   iscsi_target_do_login+0x32/0xe0 [iscsi_target_mod]
   iscsi_target_do_login_rx+0x2d2/0x420 [iscsi_target_mod]
   process_one_work+0x1dc/0x3f0
   ...
  Kernel panic - not syncing: Fatal exception in interrupt

The same fault is reachable from the logout path:
iscsit_stop_session <- iscsit_logout_post_handler <- iscsit_response_queue <- iscsi_target_tx_thread, with the identical 0xdead000000000004.

Where it was first seen (field report). A Proxmox VE host (7.0.14-9-pve, later 7.0.14-28-pve) exported zvols through LIO to Windows 11 clients using the Microsoft iSCSI initiator with MC/S. The host had been stable for 95 days with MaxConnections=1; after raising it to 2 there were four panics in one night, all with the same general protection fault at 0xdead000000000004 and the logout stack above, and the journal of one of them contains "Received logout request on connection that is not in logged in state". The panic also reproduced on 7.0.14-28-pve, so it is not tied to a single build. The module carrying this fix has been running on that host for 9 days with MaxConnections=4 and 72 active iSCSI connections, without a single panic.

Reproducer (any MC/S-capable initiator; open-iscsi does not implement MC/S, so a Microsoft iSCSI initiator was used):

1. LIO target with MaxConnections=8, no CHAP, generate_node_acls=1. The value lives in configfs and is not exported by targetcli:

echo 8 > /sys/kernel/config/target/iscsi/<iqn>/tpgt_1/param/MaxConnections

With MaxConnections=1 the extra connection is refused with ISDSC_TOO_MANY_CONNECTIONS and nothing happens, so the target has to be configured first.

2. Initiator: log in, for example on Windows

Connect-IscsiTarget -NodeAddress <iqn> -TargetPortalAddress <ip>

and then add the remaining connections to the *same* session, one call per connection:

iscsicli SessionList # take the "Session Id"
iscsicli ListInitiators # -> ROOT\ISCSIPRT\0000_0
iscsicli QAddConnection <SessionId> ROOT\ISCSIPRT\0000_0 <portal-ip>

Repeat until the session holds MaxConnections connections. AddIScsiConnectionW() does the same thing programmatically, and the "MCS..." button in the iSCSI Initiator GUI (target -> Properties -> Sessions) as well.

3. Bring the LUN online, mount a filesystem on it and start writing to it.

4. Reboot the initiator gracefully while the I/O is still in flight; repeat. The stock module crashed on the 2nd-4th cycle (2 panics in ~20 minutes).

Results: stock module 2 crashes in 8 cycles, patched module 0 in 118 cycles (the target stayed up for the whole run).

Patch 1/1 takes a snapshot of the connections, with a reference on each, while conn_lock is held, and then works only on that snapshot; the order in which the connections are reinstated is unchanged.

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

 drivers/target/iscsi/iscsi_target.c | 68 ++++++++++++++++++++---------
 1 file changed, 47 insertions(+), 21 deletions(-)