mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] scsi: iscsi_tcp: Don't read sk_user_data from the recv work
@ 2026-11-10 22:24 Praneeth Balasubramanian
  2026-11-16 18:12 ` Mike Christie
  0 siblings, 1 reply; 3+ messages in thread
From: Praneeth Balasubramanian @ 2026-11-10 22:24 UTC (permalink / raw)
  To: Lee Duncan, Chris Leech, Mike Christie
  Cc: Martin K . Petersen, James E . J . Bottomley, open-iscsi,
	linux-scsi, linux-kernel, Praneeth Balasubramanian, stable

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. At the end of iscsi_sw_tcp_recv_data(),
iscsi_sw_sk_state_check() reads the connection from sk->sk_user_data.
Its comment says it must be called with sk_callback_lock, which the work
does not hold.

On teardown, iscsi_sw_tcp_release_conn() first calls
iscsi_sw_tcp_conn_restore_callbacks(), which sets sk->sk_user_data to
NULL, and only then flushes recvwork in iscsi_suspend_rx(). If queued
work runs in between and the socket is in TCP_CLOSE or TCP_CLOSE_WAIT,
iscsi_sw_sk_state_check() 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

Both callers already have the connection. iscsi_sw_tcp_recv_data() gets
it as an argument, and iscsi_sw_tcp_state_change() reads it under
sk_callback_lock. Pass it in instead of reading sk_user_data again. The
connection stays valid until iscsi_suspend_rx() has flushed the work.

The unlocked 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")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneeth@ucsb.edu>
---

Notes:
    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.
    
    recv_from_iscsi_q is off by default, and setting up the session needs
    root on the initiator, so I am sending this as a regular fix rather than
    a security report.
    
    Testing: two QEMU VMs on next-20261023, an LIO target and an open-iscsi
    initiator booted with iscsi_tcp.recv_from_iscsi_q=1, using CHAP and
    continuous reads. The initiator kernel had a 20 ms msleep() added at the
    start of iscsi_sw_tcp_recv_data_work(). Closing the TCP connection from
    the target side produced the oops above. I have only seen it with the
    added delay. 
    
    The patch applies to mainline 9ba46df34488 and to next-20261109.

 drivers/scsi/iscsi_tcp.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/scsi/iscsi_tcp.c b/drivers/scsi/iscsi_tcp.c
index 9260b1c9b0e0..88b1f255ae9b 100644
--- a/drivers/scsi/iscsi_tcp.c
+++ b/drivers/scsi/iscsi_tcp.c
@@ -104,18 +104,20 @@ static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
 
 /**
  * iscsi_sw_sk_state_check - check socket state
+ * @conn: iscsi connection the socket belongs to
  * @sk: socket
  *
  * If the socket is in CLOSE or CLOSE_WAIT we should
  * not close the connection if there is still some
  * data pending.
  *
- * Must be called with sk_callback_lock.
+ * The caller passes @conn rather than having it read from
+ * sk->sk_user_data. The recv work runs without sk_callback_lock,
+ * and connection teardown may already have cleared sk_user_data.
  */
-static inline int iscsi_sw_sk_state_check(struct sock *sk)
+static inline int iscsi_sw_sk_state_check(struct iscsi_conn *conn,
+					  struct sock *sk)
 {
-	struct iscsi_conn *conn = sk->sk_user_data;
-
 	if ((sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) &&
 	    (conn->session->state != ISCSI_STATE_LOGGING_OUT) &&
 	    !atomic_read(&sk->sk_rmem_alloc)) {
@@ -148,7 +150,7 @@ static void iscsi_sw_tcp_recv_data(struct iscsi_conn *conn)
 	 * unmap it now. */
 	iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
 
-	iscsi_sw_sk_state_check(sk);
+	iscsi_sw_sk_state_check(conn, sk);
 }
 
 static void iscsi_sw_tcp_recv_data_work(struct work_struct *work)
@@ -202,7 +204,7 @@ static void iscsi_sw_tcp_state_change(struct sock *sk)
 		return;
 	}
 
-	iscsi_sw_sk_state_check(sk);
+	iscsi_sw_sk_state_check(conn, sk);
 
 	tcp_conn = conn->dd_data;
 	tcp_sw_conn = tcp_conn->dd_data;
-- 
2.25.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] scsi: iscsi_tcp: Don't read sk_user_data from the recv work
  2026-11-10 22:24 [PATCH] scsi: iscsi_tcp: Don't read sk_user_data from the recv work Praneeth Balasubramanian
@ 2026-11-16 18:12 ` Mike Christie
  2026-11-25 19:38   ` [PATCH v2] scsi: iscsi_tcp: Take sk_callback_lock in " Praneeth Balasubramanian
  0 siblings, 1 reply; 3+ messages in thread
From: Mike Christie @ 2026-11-16 18:12 UTC (permalink / raw)
  To: Praneeth Balasubramanian, Lee Duncan, Chris Leech
  Cc: Martin K . Petersen, James E . J . Bottomley, open-iscsi,
	linux-scsi, linux-kernel, Praneeth Balasubramanian, stable

On 11/10/26 4:24 PM, Praneeth Balasubramanian wrote:
> 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. At the end of iscsi_sw_tcp_recv_data(), iscsi_sw_sk_state_check() reads the connection
> 
> 
> 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. At the end of iscsi_sw_tcp_recv_data(),
> iscsi_sw_sk_state_check() reads the connection from sk->sk_user_data.
> Its comment says it must be called with sk_callback_lock, which the work
> does not hold.
> 
> On teardown, iscsi_sw_tcp_release_conn() first calls
> iscsi_sw_tcp_conn_restore_callbacks(), which sets sk->sk_user_data to
> NULL, and only then flushes recvwork in iscsi_suspend_rx(). If queued
> work runs in between and the socket is in TCP_CLOSE or TCP_CLOSE_WAIT,
> iscsi_sw_sk_state_check() 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
> 
> Both callers already have the connection. iscsi_sw_tcp_recv_data() gets
> it as an argument, and iscsi_sw_tcp_state_change() reads it under
> sk_callback_lock. Pass it in instead of reading sk_user_data again. The
> connection stays valid until iscsi_suspend_rx() has flushed the work.
> 
> The unlocked 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")
> Cc: stable@vger.kernel.org
> Signed-off-by: Praneeth Balasubramanian <praneeth@ucsb.edu>
> ---
> 
> Notes:
>     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.
>     
>     recv_from_iscsi_q is off by default, and setting up the session needs
>     root on the initiator, so I am sending this as a regular fix rather than
>     a security report.
>     
>     Testing: two QEMU VMs on next-20261023, an LIO target and an open-iscsi
>     initiator booted with iscsi_tcp.recv_from_iscsi_q=1, using CHAP and
>     continuous reads. The initiator kernel had a 20 ms msleep() added at the
>     start of iscsi_sw_tcp_recv_data_work(). Closing the TCP connection from
>     the target side produced the oops above. I have only seen it with the
>     added delay. 
>     
>     The patch applies to mainline 9ba46df34488 and to next-20261109.
> 
>  drivers/scsi/iscsi_tcp.c | 14 ++++++++------
>  1 file changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/scsi/iscsi_tcp.c b/drivers/scsi/iscsi_tcp.c
> index 9260b1c9b0e0..88b1f255ae9b 100644
> --- a/drivers/scsi/iscsi_tcp.c
> +++ b/drivers/scsi/iscsi_tcp.c
> @@ -104,18 +104,20 @@ static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
>  
>  /**
>   * iscsi_sw_sk_state_check - check socket state
> + * @conn: iscsi connection the socket belongs to
>   * @sk: socket
>   *
>   * If the socket is in CLOSE or CLOSE_WAIT we should
>   * not close the connection if there is still some
>   * data pending.
>   *
> - * Must be called with sk_callback_lock.
> + * The caller passes @conn rather than having it read from
> + * sk->sk_user_data. The recv work runs without sk_callback_lock,
> + * and connection teardown may already have cleared sk_user_data.
>   */
> -static inline int iscsi_sw_sk_state_check(struct sock *sk)
> +static inline int iscsi_sw_sk_state_check(struct iscsi_conn *conn,
> +					  struct sock *sk)
>  {
> -	struct iscsi_conn *conn = sk->sk_user_data;
> -
>  	if ((sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) &&
>  	    (conn->session->state != ISCSI_STATE_LOGGING_OUT) &&
>  	    !atomic_read(&sk->sk_rmem_alloc)) {
> @@ -148,7 +150,7 @@ static void iscsi_sw_tcp_recv_data(struct iscsi_conn *conn)
>  	 * unmap it now. */
>  	iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
>  
> -	iscsi_sw_sk_state_check(sk);
> +	iscsi_sw_sk_state_check(conn, sk);
>  }
>  
>  static void iscsi_sw_tcp_recv_data_work(struct work_struct *work)
> @@ -202,7 +204,7 @@ static void iscsi_sw_tcp_state_change(struct sock *sk)
>  		return;
>  	}
>  
> -	iscsi_sw_sk_state_check(sk);
> +	iscsi_sw_sk_state_check(conn, sk);

I think after we have done iscsi_sw_tcp_conn_restore_callbacks it's
a bug to be accessing sk_user_data from inside iscsi_tcp and expect
that it's non-NULL.

We don't want to allow that to happen so we should take the 
sk_callback_lock when callling iscsi_sw_tcp_recv_data from
iscsi_sw_tcp_recv_data_work like is done from iscsi_sw_tcp_data_ready.
Or, call iscsi_suspend_rx before iscsi_sw_tcp_conn_restore_callbacks
and then add some code so after iscsi_suspend_rx has returned we
know that racing iscsi_conn_queue_recv calls are handled.




 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2] scsi: iscsi_tcp: Take sk_callback_lock in the recv work
  2026-11-16 18:12 ` Mike Christie
@ 2026-11-25 19:38   ` Praneeth Balasubramanian
  0 siblings, 0 replies; 3+ messages in thread
From: Praneeth Balasubramanian @ 2026-11-25 19:38 UTC (permalink / raw)
  To: Lee Duncan, Chris Leech, Mike Christie
  Cc: Martin K . Petersen, James E . J . Bottomley, open-iscsi,
	linux-scsi, linux-kernel, Praneeth Balasubramanian, stable

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-25 19:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-10 22:24 [PATCH] scsi: iscsi_tcp: Don't read sk_user_data from the recv work Praneeth Balasubramanian
2026-11-16 18:12 ` Mike Christie
2026-11-25 19:38   ` [PATCH v2] scsi: iscsi_tcp: Take sk_callback_lock in " Praneeth Balasubramanian

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®