mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Praneeth Balasubramanian <praneethb2002@gmail.com>
To: Lee Duncan <lduncan@suse.com>, Chris Leech <cleech@redhat.com>,
	Mike Christie <michael.christie@oracle.com>
Cc: "Martin K . Petersen" <mkp@kernel.org>,
	"James E . J . Bottomley" <James.Bottomley@HansenPartnership.com>,
	open-iscsi@googlegroups.com, linux-scsi@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Praneeth Balasubramanian <praneeth@ucsb.edu>,
	stable@vger.kernel.org
Subject: [PATCH v2] scsi: iscsi_tcp: Take sk_callback_lock in the recv work
Date: Wed, 25 Nov 2026 11:38:26 -0800	[thread overview]
Message-ID: <20261125193826.3466197-1-praneethb2002@gmail.com> (raw)
In-Reply-To: <4fc02402-4ab3-42d0-8dbf-d0a36a125e43@oracle.com>

From: Praneeth Balasubramanian <praneeth@ucsb.edu>

With recv_from_iscsi_q=1, iscsi_sw_tcp_data_ready() queues
conn->recvwork, and iscsi_sw_tcp_recv_data_work() runs the receive path
under lock_sock() only. The data_ready path runs it under
read_lock_bh(&sk->sk_callback_lock) and returns early when sk_user_data
is NULL. The work does neither, and that causes two problems.

First, on teardown, iscsi_sw_tcp_release_conn() calls
iscsi_sw_tcp_conn_restore_callbacks(), which clears sk->sk_user_data,
and only then flushes recvwork in iscsi_suspend_rx(). Work that runs in
between processes a socket whose callbacks were already restored. If
the socket is in TCP_CLOSE or TCP_CLOSE_WAIT, iscsi_sw_sk_state_check()
then dereferences NULL->session. With a delay added at the start of the
work to widen this window, a target-side connection close gives:

  Oops: general protection fault, probably for non-canonical address ...
  KASAN: null-ptr-deref in range [0x0000000000000010-0x0000000000000017]
  Workqueue: iscsi_q_3 iscsi_sw_tcp_recv_data_work
  RIP: 0010:iscsi_sw_tcp_recv_data+0x219/0x420

Second, the receive path was written to run with BHs disabled. For
example, __iscsi_complete_pdu() takes session->frwd_lock with
spin_lock(), and iscsi_check_transport_timeouts() takes the same lock
from a timer. From the work, BHs are enabled. With lockdep, an
unmodified kernel booted with recv_from_iscsi_q=1 reports as soon as
reads run:

  WARNING: inconsistent lock state
  inconsistent {IN-SOFTIRQ-W} -> {SOFTIRQ-ON-W} usage.
  kworker/u10:2/55 [HC0[0]:SC0[0]:HE1:SE1] takes:
  (&session->frwd_lock){+.?.}-{3:3}, at: __iscsi_complete_pdu+0x4f7/0x2420
  {IN-SOFTIRQ-W} state was registered at:
    _raw_spin_lock+0x2e/0x40
    iscsi_check_transport_timeouts+0x4c/0x5d0
    call_timer_fn+0x19f/0x630
  ...
   *** DEADLOCK ***

Take sk_callback_lock with read_lock_bh() in the recv work, as
iscsi_sw_tcp_data_ready() does. The receive path then runs with BHs
disabled, as it does from softirq, and is skipped once sk_user_data has
been cleared.

The unlocked sk_user_data read was found with an LLM-based tool that
checks callers against the locking rules stated in kernel comments.

Fixes: f1d269765ee2 ("scsi: iscsi: Run recv path from workqueue")
Suggested-by: Mike Christie <michael.christie@oracle.com>
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneeth@ucsb.edu>
---

Notes:
    v2:
    - Take sk_callback_lock in the recv work and skip the receive path once
      sk_user_data is NULL, as Mike Christie suggested. v1 passed conn
      into iscsi_sw_sk_state_check() instead. That avoided the NULL
      dereference but still let the work run after the callbacks were
      restored.
    - I went with taking the lock rather than calling iscsi_suspend_rx()
      before restoring the callbacks. Taking it with read_lock_bh() also
      fixes the frwd_lock report above, which reordering the teardown alone
      would not.
    v1 and review: https://lore.kernel.org/all/4fc02402-4ab3-42d0-8dbf-d0a36a125e43@oracle.com/
    
    This was found by an LLM-based static analysis (GPT-5.6) that checks
    kernel code against the rules stated in its comments, as part of
    research at UCSB SecLab. The patch and changelog were drafted with
    Claude Opus 5.5. I reviewed the patch and take responsibility for it.
    
    Testing: two QEMU VMs on next-20261023 with KASAN and lockdep, an LIO
    target and an open-iscsi initiator booted with
    iscsi_tcp.recv_from_iscsi_q=1, using CHAP and continuous reads.
    
    - The teardown race: both initiator kernels had a 20 ms msleep() added
      at the start of iscsi_sw_tcp_recv_data_work(), and the TCP connection
      was closed from the target side. The unpatched kernel oopsed as
      above. The patched kernel ran three times with no oops and no lockdep
      report. In one run, a test-only print showed the work reaching the
      new check after the callbacks were restored, with the socket in
      TCP_CLOSE, and skipping the receive path. The race needs the delay to
      hit reliably.
    - The frwd_lock report: it appeared in all 10 unpatched runs in this
      mode, including runs without the delay, and in none of the patched
      runs.
    
    The patch applies to mainline 9ba46df34488 and to next-20261116.

 drivers/scsi/iscsi_tcp.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/iscsi_tcp.c b/drivers/scsi/iscsi_tcp.c
index 9260b1c9b0e0..6d802b84f7ad 100644
--- a/drivers/scsi/iscsi_tcp.c
+++ b/drivers/scsi/iscsi_tcp.c
@@ -160,7 +160,15 @@ static void iscsi_sw_tcp_recv_data_work(struct work_struct *work)
 	struct sock *sk = tcp_sw_conn->sock->sk;
 
 	lock_sock(sk);
-	iscsi_sw_tcp_recv_data(conn);
+	/*
+	 * Teardown clears sk_user_data under sk_callback_lock before it
+	 * flushes this work. Take the lock like iscsi_sw_tcp_data_ready()
+	 * so the recv path never runs after the callbacks are restored.
+	 */
+	read_lock_bh(&sk->sk_callback_lock);
+	if (sk->sk_user_data)
+		iscsi_sw_tcp_recv_data(conn);
+	read_unlock_bh(&sk->sk_callback_lock);
 	release_sock(sk);
 }
 
-- 
2.25.1


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

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10 22:24 [PATCH] scsi: iscsi_tcp: Don't read sk_user_data from " Praneeth Balasubramanian
2026-11-16 18:12 ` Mike Christie
2026-11-25 19:38   ` Praneeth Balasubramanian [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=20261125193826.3466197-1-praneethb2002@gmail.com \
    --to=praneethb2002@gmail.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=cleech@redhat.com \
    --cc=lduncan@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=michael.christie@oracle.com \
    --cc=mkp@kernel.org \
    --cc=open-iscsi@googlegroups.com \
    --cc=praneeth@ucsb.edu \
    --cc=stable@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

all inboxes | Powered by JetHome®