public inbox for linux-scsi@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/2] iscsi_tcp: do not bind sockets that already have extra callbacks
@ 2024-05-23 22:21 Khazhismel Kumykov
  2024-05-23 22:21 ` [PATCH v2 2/2] libiscsi: disallow binding an already-bound connection Khazhismel Kumykov
  0 siblings, 1 reply; 3+ messages in thread
From: Khazhismel Kumykov @ 2024-05-23 22:21 UTC (permalink / raw)
  To: Lee Duncan, Chris Leech, Mike Christie
  Cc: James E.J. Bottomley, Martin K. Petersen, open-iscsi, linux-scsi,
	linux-kernel, Khazhismel Kumykov

This attempts to avoid a situation where a misbehaving iscsi daemon
passes a socket for a different iSCSI connection to BIND_CONN - which
would result in infinite recursion and stack overflow. This will
also prevent passing *other* sockets which had sk_user_data overridden,
but that wouldn't have been safe anyways - since we throw away that
pointer anyways. This does not cover all hypothetical scenarios where we
pass bad sockets to BIND_CONN.

This also papers over a different bug - we allow a daemon to call
BIND_CONN twice for the same connection - which would result in, at the
least, failing to uninitialize/teardown the previous socket, which will
be addressed separately.

Signed-off-by: Khazhismel Kumykov <khazhy@google.com>
---
 drivers/scsi/iscsi_tcp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/scsi/iscsi_tcp.c b/drivers/scsi/iscsi_tcp.c
index 60688f18fac6..deb9252e02e6 100644
--- a/drivers/scsi/iscsi_tcp.c
+++ b/drivers/scsi/iscsi_tcp.c
@@ -725,7 +725,7 @@ iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
 	}
 
 	err = -EINVAL;
-	if (!sk_is_tcp(sock->sk))
+	if (!sk_is_tcp(sock->sk) || sock->sk->sk_user_data)
 		goto free_socket;
 
 	err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
-- 
2.45.1.288.g0e0cd299f1-goog


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

* [PATCH v2 2/2] libiscsi: disallow binding an already-bound connection
  2024-05-23 22:21 [PATCH v2 1/2] iscsi_tcp: do not bind sockets that already have extra callbacks Khazhismel Kumykov
@ 2024-05-23 22:21 ` Khazhismel Kumykov
  2024-05-24 16:14   ` Mike Christie
  0 siblings, 1 reply; 3+ messages in thread
From: Khazhismel Kumykov @ 2024-05-23 22:21 UTC (permalink / raw)
  To: Lee Duncan, Chris Leech, Mike Christie
  Cc: James E.J. Bottomley, Martin K. Petersen, open-iscsi, linux-scsi,
	linux-kernel, Khazhismel Kumykov

This fixes issue where misbehaving userspace initiator could bind the
same connection multiple times, which would leak the old connection
socket without cleaning it up.

For iscsi_tcp, it calls iscsi_suspend_tx directly in stop_conn. Update
this to iscsi_conn_unbind, which matches the lifecycle of other drivers,
and clears the CONN_FLAG_BOUND.

Suggested-by: Mike Christie <michael.christie@oracle.com>
Signed-off-by: Khazhismel Kumykov <khazhy@google.com>
---
 drivers/scsi/iscsi_tcp.c | 2 +-
 drivers/scsi/libiscsi.c  | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/iscsi_tcp.c b/drivers/scsi/iscsi_tcp.c
index deb9252e02e6..1d93404515ae 100644
--- a/drivers/scsi/iscsi_tcp.c
+++ b/drivers/scsi/iscsi_tcp.c
@@ -696,7 +696,7 @@ static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
 	wake_up_interruptible(sk_sleep(sock->sk));
 
 	/* stop xmit side */
-	iscsi_suspend_tx(conn);
+	iscsi_conn_unbind(cls_conn, true);
 
 	/* stop recv side and release socket */
 	iscsi_sw_tcp_release_conn(conn);
diff --git a/drivers/scsi/libiscsi.c b/drivers/scsi/libiscsi.c
index 0fda8905eabd..0fb98eb53584 100644
--- a/drivers/scsi/libiscsi.c
+++ b/drivers/scsi/libiscsi.c
@@ -3453,6 +3453,12 @@ int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
 	struct iscsi_conn *conn = cls_conn->dd_data;
 
 	spin_lock_bh(&session->frwd_lock);
+	if (test_bit(ISCSI_CONN_FLAG_BOUND, &conn->flags)) {
+		spin_unlock_bh(&session->frwd_lock);
+		return -EBUSY;
+	}
+
+
 	if (is_leading)
 		session->leadconn = conn;
 
-- 
2.45.1.288.g0e0cd299f1-goog


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

* Re: [PATCH v2 2/2] libiscsi: disallow binding an already-bound connection
  2024-05-23 22:21 ` [PATCH v2 2/2] libiscsi: disallow binding an already-bound connection Khazhismel Kumykov
@ 2024-05-24 16:14   ` Mike Christie
  0 siblings, 0 replies; 3+ messages in thread
From: Mike Christie @ 2024-05-24 16:14 UTC (permalink / raw)
  To: Khazhismel Kumykov, Lee Duncan, Chris Leech
  Cc: James E.J. Bottomley, Martin K. Petersen, open-iscsi, linux-scsi,
	linux-kernel, Khazhismel Kumykov

On 5/23/24 5:21 PM, Khazhismel Kumykov wrote:
> This fixes issue where misbehaving userspace initiator could bind the
> same connection multiple times, which would leak the old connection
> socket without cleaning it up.
> 
> For iscsi_tcp, it calls iscsi_suspend_tx directly in stop_conn. Update
> this to iscsi_conn_unbind, which matches the lifecycle of other drivers,
> and clears the CONN_FLAG_BOUND.
> 
> Suggested-by: Mike Christie <michael.christie@oracle.com>
> Signed-off-by: Khazhismel Kumykov <khazhy@google.com>
> ---
>  drivers/scsi/iscsi_tcp.c | 2 +-
>  drivers/scsi/libiscsi.c  | 6 ++++++
>  2 files changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/scsi/iscsi_tcp.c b/drivers/scsi/iscsi_tcp.c
> index deb9252e02e6..1d93404515ae 100644
> --- a/drivers/scsi/iscsi_tcp.c
> +++ b/drivers/scsi/iscsi_tcp.c
> @@ -696,7 +696,7 @@ static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
>  	wake_up_interruptible(sk_sleep(sock->sk));
>  
>  	/* stop xmit side */
> -	iscsi_suspend_tx(conn);
> +	iscsi_conn_unbind(cls_conn, true);
>  
>  	/* stop recv side and release socket */
>  	iscsi_sw_tcp_release_conn(conn);
> diff --git a/drivers/scsi/libiscsi.c b/drivers/scsi/libiscsi.c
> index 0fda8905eabd..0fb98eb53584 100644
> --- a/drivers/scsi/libiscsi.c
> +++ b/drivers/scsi/libiscsi.c
> @@ -3453,6 +3453,12 @@ int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
>  	struct iscsi_conn *conn = cls_conn->dd_data;
>  
>  	spin_lock_bh(&session->frwd_lock);
> +	if (test_bit(ISCSI_CONN_FLAG_BOUND, &conn->flags)) {
> +		spin_unlock_bh(&session->frwd_lock);
> +		return -EBUSY;
> +	}
> +

If some of the driver's bind_conn callout fails then it could leave the
ISCSI_CONN_FLAG_BOUND bit set, and this will fail on a retry.

It's ok to call iscsi_conn_bind at the end of the bind_conn callout
like how iscsi_tcp and cxgbi* do. So for iscsi_iser.c, be_iscsi.c,
bnx2i_iscsi.c, and qedi_iscsi.c you need to move their iscsi_conn_bind
to the end of their bind_conn callout.

You could also keep the the existing flow but add unwind/goto error
handling to those drivers, but I think just moving the iscsi_conn_bind
call to the end of the function is easier and it syncs all the drivers
behavior which is nice. 

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

end of thread, other threads:[~2024-05-24 16:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-23 22:21 [PATCH v2 1/2] iscsi_tcp: do not bind sockets that already have extra callbacks Khazhismel Kumykov
2024-05-23 22:21 ` [PATCH v2 2/2] libiscsi: disallow binding an already-bound connection Khazhismel Kumykov
2024-05-24 16:14   ` Mike Christie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox