From: Showrya M N <[email protected]>

[ Upstream commit 3ea3a256ed81f95ab0f3281a0e234b01a9cae605 ]

In case of an ib_fast_reg_mr allocation failure during iSER setup, the
machine hits a panic because iscsi_conn->dd_data is initialized
unconditionally, even when no memory is allocated (dd_size == 0).  This
leads invalid pointer dereference during connection teardown.

Fix by setting iscsi_conn->dd_data only if memory is actually allocated.

Panic trace:
------------
 iser: iser_create_fastreg_desc: Failed to allocate ib_fast_reg_mr err=-12
 iser: iser_alloc_rx_descriptors: failed allocating rx descriptors / data 
buffers
 BUG: unable to handle page fault for address: fffffffffffffff8
 RIP: 0010:swake_up_locked.part.5+0xa/0x40
 Call Trace:
  complete+0x31/0x40
  iscsi_iser_conn_stop+0x88/0xb0 [ib_iser]
  iscsi_stop_conn+0x66/0xc0 [scsi_transport_iscsi]
  iscsi_if_stop_conn+0x14a/0x150 [scsi_transport_iscsi]
  iscsi_if_rx+0x1135/0x1834 [scsi_transport_iscsi]
  ? netlink_lookup+0x12f/0x1b0
  ? netlink_deliver_tap+0x2c/0x200
  netlink_unicast+0x1ab/0x280
  netlink_sendmsg+0x257/0x4f0
  ? _copy_from_user+0x29/0x60
  sock_sendmsg+0x5f/0x70

Signed-off-by: Showrya M N <[email protected]>
Signed-off-by: Potnuri Bharat Teja <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Reviewed-by: Chris Leech <[email protected]>
Signed-off-by: Martin K. Petersen <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
---

LLM Generated explanations, may be completely bogus:

Now I see the pattern clearly! When `iscsi_alloc_conn` is called with
`dd_size = 0`:
- It allocates only `sizeof(*conn)` (no extra space)
- It sets `conn->dd_data = NULL` (because dd_size is 0, the condition
  `if (dd_size)` is false)

But in the original code in `iscsi_conn_setup()`, it was unconditionally
setting:
```c
conn->dd_data = cls_conn->dd_data + sizeof(*conn);
```

This would set `conn->dd_data` to point past the allocated memory when
`dd_size = 0`, leading to the panic when the iSER driver tries to
dereference it in `iscsi_iser_conn_stop()`.

**Backport Status: YES**

This commit should definitely be backported to stable kernel trees for
the following reasons:

1. **Critical Bug Fix**: This fixes a kernel panic/crash that occurs
   during iSCSI/iSER connection teardown when memory allocation fails.
   The panic trace shows a page fault at address `fffffffffffffff8`,
   which is clearly an invalid pointer dereference.

2. **Real-World Impact**: The bug triggers when `ib_fast_reg_mr`
   allocation fails during iSER setup, which can happen under memory
   pressure. This leads to a system crash during error recovery, turning
   a recoverable allocation failure into a fatal kernel panic.

3. **Small and Contained Fix**: The fix is minimal - just adding a
   single conditional check:
  ```c
  if (dd_size)
  conn->dd_data = cls_conn->dd_data + sizeof(*conn);
  ```
  This only affects the initialization path when `dd_size = 0`.

4. **No Architectural Changes**: This is a straightforward bug fix that
   doesn't introduce new features or change any architecture. It simply
   prevents setting an invalid pointer when no driver-specific data is
   allocated.

5. **Clear Root Cause**: The issue is well-understood: when drivers like
   iSER pass `dd_size = 0` to `iscsi_conn_setup()`, the code was
   unconditionally calculating a pointer past the allocated memory.
   During teardown, accessing this invalid pointer causes the panic.

6. **Minimal Risk**: The change only affects the case where `dd_size =
   0`, which is specifically the iSER driver's usage pattern. Other
   drivers that pass non-zero `dd_size` values are unaffected.

7. **Follows Stable Rules**: This meets all stable kernel criteria:
   - Fixes a real bug that users can hit
   - Fix is already upstream (has proper sign-offs and review)
   - Small change (2 lines)
   - Obviously correct
   - Doesn't add new features

The commit prevents a kernel panic during error handling in the
iSCSI/iSER subsystem, making it an important stability fix that should
be backported to all maintained stable kernels that include the iSER
driver.

 drivers/scsi/libiscsi.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/libiscsi.c b/drivers/scsi/libiscsi.c
index 392d57e054db..c9f410c50978 100644
--- a/drivers/scsi/libiscsi.c
+++ b/drivers/scsi/libiscsi.c
@@ -3185,7 +3185,8 @@ iscsi_conn_setup(struct iscsi_cls_session *cls_session, 
int dd_size,
                return NULL;
        conn = cls_conn->dd_data;
 
-       conn->dd_data = cls_conn->dd_data + sizeof(*conn);
+       if (dd_size)
+               conn->dd_data = cls_conn->dd_data + sizeof(*conn);
        conn->session = session;
        conn->cls_conn = cls_conn;
        conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
-- 
2.39.5

-- 
You received this message because you are subscribed to the Google Groups 
"open-iscsi" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/open-iscsi/20250805130945.471732-56-sashal%40kernel.org.

Reply via email to