mbox series

[RESEND,0/3] fix several bugs about libiscsi

Message ID 20210910010220.24073-1-dinghui@sangfor.com.cn
Headers show
Series fix several bugs about libiscsi | expand

Message

Ding Hui Sept. 10, 2021, 1:02 a.m. UTC
Ding Hui (3):
  scsi: libiscsi: move init ehwait to iscsi_session_setup()
  scsi: libiscsi: fix invalid pointer dereference in
    iscsi_eh_session_reset
  scsi: libiscsi: get ref to conn in iscsi_eh_device/target_reset()

 drivers/scsi/libiscsi.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

Comments

Mike Christie Sept. 10, 2021, 4:25 p.m. UTC | #1
On 9/9/21 8:02 PM, Ding Hui wrote:
> commit ec29d0ac29be ("scsi: iscsi: Fix conn use after free during
> resets") move member ehwait from conn to session, but left init ehwait
> in iscsi_conn_setup().
> 
> Due to one session can be binded by multi conns, the conn after the

A session can only have 1 conn. There is some code that makes it look
like we can do multiple conns or swap the single conn, but it was never
fully implemented/supported upstream.

However, I like the patch. The init should be done in iscsi_session_setup,
so could you fix up the commit, so it's correct?

> first will reinit the session->ehwait, move init ehwait to
> iscsi_session_setup() to fix it.
> 
> Fixes: ec29d0ac29be ("scsi: iscsi: Fix conn use after free during resets")
> Signed-off-by: Ding Hui <dinghui@sangfor.com.cn>
> ---
>  drivers/scsi/libiscsi.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/scsi/libiscsi.c b/drivers/scsi/libiscsi.c
> index 4683c183e9d4..712a45368385 100644
> --- a/drivers/scsi/libiscsi.c
> +++ b/drivers/scsi/libiscsi.c
> @@ -2947,6 +2947,7 @@ iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
>  	session->tmf_state = TMF_INITIAL;
>  	timer_setup(&session->tmf_timer, iscsi_tmf_timedout, 0);
>  	mutex_init(&session->eh_mutex);
> +	init_waitqueue_head(&session->ehwait);
>  
>  	spin_lock_init(&session->frwd_lock);
>  	spin_lock_init(&session->back_lock);
> @@ -3074,8 +3075,6 @@ iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
>  		goto login_task_data_alloc_fail;
>  	conn->login_task->data = conn->data = data;
>  
> -	init_waitqueue_head(&session->ehwait);
> -
>  	return cls_conn;
>  
>  login_task_data_alloc_fail:
>
Mike Christie Sept. 10, 2021, 4:38 p.m. UTC | #2
On 9/9/21 8:02 PM, Ding Hui wrote:
> like commit 5db6dd14b313 ("scsi: libiscsi: Fix NULL pointer dereference in
> iscsi_eh_session_reset"), access conn->persistent_address here is not safe
> too.
> 
> The persistent_address is independent of conn refcount, so maybe
> already freed by iscsi_conn_teardown(), also we put the refcount of conn
> above, the conn pointer may be invalid.

This shouldn't happen like you describe above, because when we wake up
we will see the session->state is ISCSI_STATE_TERMINATE. We will then
not reference the conn in that code below.

However, it looks like we could hit an issue where if a user was resetting
the persistent_address or targetname via iscsi_set_param -> iscsi_switch_str_param
then we could be accessing freed memory. I think we need the frwd_lock when swapping
the strings in iscsi_switch_str_param.


> 
> Signed-off-by: Ding Hui <dinghui@sangfor.com.cn>
> ---
>  drivers/scsi/libiscsi.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/scsi/libiscsi.c b/drivers/scsi/libiscsi.c
> index 712a45368385..69b3b2148328 100644
> --- a/drivers/scsi/libiscsi.c
> +++ b/drivers/scsi/libiscsi.c
> @@ -2531,8 +2531,8 @@ int iscsi_eh_session_reset(struct scsi_cmnd *sc)
>  	spin_lock_bh(&session->frwd_lock);
>  	if (session->state == ISCSI_STATE_LOGGED_IN) {
>  		ISCSI_DBG_EH(session,
> -			     "session reset succeeded for %s,%s\n",
> -			     session->targetname, conn->persistent_address);
> +			     "session reset succeeded for %s\n",
> +			     session->targetname);
>  	} else
>  		goto failed;
>  	spin_unlock_bh(&session->frwd_lock);
>
Ding Hui Sept. 11, 2021, 9:37 a.m. UTC | #3
On 2021/9/11 12:25 上午, Mike Christie wrote:
> On 9/9/21 8:02 PM, Ding Hui wrote:

>> commit ec29d0ac29be ("scsi: iscsi: Fix conn use after free during

>> resets") move member ehwait from conn to session, but left init ehwait

>> in iscsi_conn_setup().

>>

>> Due to one session can be binded by multi conns, the conn after the

> 

> A session can only have 1 conn. There is some code that makes it look

> like we can do multiple conns or swap the single conn, but it was never

> fully implemented/supported upstream.

> 

> However, I like the patch. The init should be done in iscsi_session_setup,

> so could you fix up the commit, so it's correct?

> 


Thanks,I'll update the commit log and send v2 1/3.

>> first will reinit the session->ehwait, move init ehwait to

>> iscsi_session_setup() to fix it.

>>

>> Fixes: ec29d0ac29be ("scsi: iscsi: Fix conn use after free during resets")

>> Signed-off-by: Ding Hui <dinghui@sangfor.com.cn>

>> ---

>>   drivers/scsi/libiscsi.c | 3 +--

>>   1 file changed, 1 insertion(+), 2 deletions(-)

>>

>> diff --git a/drivers/scsi/libiscsi.c b/drivers/scsi/libiscsi.c

>> index 4683c183e9d4..712a45368385 100644

>> --- a/drivers/scsi/libiscsi.c

>> +++ b/drivers/scsi/libiscsi.c

>> @@ -2947,6 +2947,7 @@ iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,

>>   	session->tmf_state = TMF_INITIAL;

>>   	timer_setup(&session->tmf_timer, iscsi_tmf_timedout, 0);

>>   	mutex_init(&session->eh_mutex);

>> +	init_waitqueue_head(&session->ehwait);

>>   

>>   	spin_lock_init(&session->frwd_lock);

>>   	spin_lock_init(&session->back_lock);

>> @@ -3074,8 +3075,6 @@ iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,

>>   		goto login_task_data_alloc_fail;

>>   	conn->login_task->data = conn->data = data;

>>   

>> -	init_waitqueue_head(&session->ehwait);

>> -

>>   	return cls_conn;

>>   

>>   login_task_data_alloc_fail:

>>

> 



-- 
Thanks,
-dinghui
Ding Hui Sept. 11, 2021, 9:52 a.m. UTC | #4
On 2021/9/11 12:38 上午, Mike Christie wrote:
> On 9/9/21 8:02 PM, Ding Hui wrote:

>> like commit 5db6dd14b313 ("scsi: libiscsi: Fix NULL pointer dereference in

>> iscsi_eh_session_reset"), access conn->persistent_address here is not safe

>> too.

>>

>> The persistent_address is independent of conn refcount, so maybe

>> already freed by iscsi_conn_teardown(), also we put the refcount of conn

>> above, the conn pointer may be invalid.

> 

> This shouldn't happen like you describe above, because when we wake up

> we will see the session->state is ISCSI_STATE_TERMINATE. We will then

> not reference the conn in that code below.

> 

> However, it looks like we could hit an issue where if a user was resetting

> the persistent_address or targetname via iscsi_set_param -> iscsi_switch_str_param

> then we could be accessing freed memory. I think we need the frwd_lock when swapping

> the strings in iscsi_switch_str_param.

> 


Thanks for your detailed explanation, I'll drop 2/3 and 3/3 in v2 patch.
I expect that the persistent_address issue be fixed in your future patchset.

Sorry for my ignorance.

> 

>>

>> Signed-off-by: Ding Hui <dinghui@sangfor.com.cn>

>> ---

>>   drivers/scsi/libiscsi.c | 4 ++--

>>   1 file changed, 2 insertions(+), 2 deletions(-)

>>

>> diff --git a/drivers/scsi/libiscsi.c b/drivers/scsi/libiscsi.c

>> index 712a45368385..69b3b2148328 100644

>> --- a/drivers/scsi/libiscsi.c

>> +++ b/drivers/scsi/libiscsi.c

>> @@ -2531,8 +2531,8 @@ int iscsi_eh_session_reset(struct scsi_cmnd *sc)

>>   	spin_lock_bh(&session->frwd_lock);

>>   	if (session->state == ISCSI_STATE_LOGGED_IN) {

>>   		ISCSI_DBG_EH(session,

>> -			     "session reset succeeded for %s,%s\n",

>> -			     session->targetname, conn->persistent_address);

>> +			     "session reset succeeded for %s\n",

>> +			     session->targetname);

>>   	} else

>>   		goto failed;

>>   	spin_unlock_bh(&session->frwd_lock);

>>

> 



-- 
Thanks,
-dinghui