diff mbox series

[v2] scsi: sg: Avoid race in error handling & drop bogus warn

Message ID 20240401100317.5395-1-Alexander@wetzel-home.de (mailing list archive)
State Handled Elsewhere
Headers show
Series [v2] scsi: sg: Avoid race in error handling & drop bogus warn | expand

Commit Message

Alexander Wetzel April 1, 2024, 10:03 a.m. UTC
commit 27f58c04a8f4 ("scsi: sg: Avoid sg device teardown race")
introduced an incorrect WARN_ON_ONCE() and missed a sequence where
sg_device_destroy() was used after scsi_device_put().

sg_device_destroy() is accessing the parent scsi_device request_queue which
will already be set to NULL when the preceding call to scsi_device_put()
removed the last reference to the parent scsi_device.

Drop the incorrect WARN_ON_ONCE() - allowing more than one concurrent
access to the sg device - and make sure sg_device_destroy() is not used
after scsi_device_put() in the error handling.

Link: https://lore.kernel.org/all/5375B275-D137-4D5F-BE25-6AF8ACAE41EF@linux.ibm.com
Fixes: 27f58c04a8f4 ("scsi: sg: Avoid sg device teardown race")
Cc: stable@vger.kernel.org
Signed-off-by: Alexander Wetzel <Alexander@wetzel-home.de>
---

Changes compared to V1: fixed commit message

The WARN_ON_ONCE() was kind of stupid to add:
We get add reference for each sg_open(). So opening a second session and
then closing either one will trigger the warning... Nothing to warn
about here.

Alexander
---
 drivers/scsi/sg.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Bart Van Assche April 1, 2024, 5:09 p.m. UTC | #1
On 4/1/24 03:03, Alexander Wetzel wrote:
> commit 27f58c04a8f4 ("scsi: sg: Avoid sg device teardown race")
> introduced an incorrect WARN_ON_ONCE() and missed a sequence where
> sg_device_destroy() was used after scsi_device_put().

Isn't that too negative? I think that the WARN_ON_ONCE() mentioned above
has proven to be useful: it helped to catch a bug.

> sg_device_destroy() is accessing the parent scsi_device request_queue which
> will already be set to NULL when the preceding call to scsi_device_put()
> removed the last reference to the parent scsi_device.
> 
> Drop the incorrect WARN_ON_ONCE() - allowing more than one concurrent
> access to the sg device - and make sure sg_device_destroy() is not used
> after scsi_device_put() in the error handling.
> 
> Link: https://lore.kernel.org/all/5375B275-D137-4D5F-BE25-6AF8ACAE41EF@linux.ibm.com
> Fixes: 27f58c04a8f4 ("scsi: sg: Avoid sg device teardown race")

The "goto sg_put" removed by this patch was introduced by commit
cc833acbee9d ("sg: O_EXCL and other lock handling"). Since the latter
commit is older than the one mentioned above, shouldn't the Fixes tag
refer to the latter commit?

> diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
> index 386981c6976a..833c9277419b 100644
> --- a/drivers/scsi/sg.c
> +++ b/drivers/scsi/sg.c
> @@ -372,8 +372,9 @@ sg_open(struct inode *inode, struct file *filp)
>   error_out:
>   	scsi_autopm_put_device(sdp->device);
>   sdp_put:
> +	kref_put(&sdp->d_ref, sg_device_destroy);
>   	scsi_device_put(sdp->device);
> -	goto sg_put;
> +	return retval;
>   }

Please add a comment above "return retval" that explains which code will
drop the sg reference.

Thanks,

Bart.
Alexander Wetzel April 1, 2024, 7:01 p.m. UTC | #2
On 01.04.24 19:09, Bart Van Assche wrote:
> On 4/1/24 03:03, Alexander Wetzel wrote:
>> commit 27f58c04a8f4 ("scsi: sg: Avoid sg device teardown race")
>> introduced an incorrect WARN_ON_ONCE() and missed a sequence where
>> sg_device_destroy() was used after scsi_device_put().
> 
> Isn't that too negative? I think that the WARN_ON_ONCE() mentioned above
> has proven to be useful: it helped to catch a bug.
> 

It helped to find the other issue. But the WARN_ON_ONCE() here is still 
plain wrong. Any only explained by my lack of understanding of the code 
and stupid assumptions I should have checked a bit more.

The warning always triggers when we have more than one user of the sg 
device (and then free one of them).
While trying to understand the issue I tripped over the other wrong 
sequence. Which is probably very seldom really executed...

That said I can of course update the wording when you have a better 
suggestion. But I only have some variations of "Ups... sorry. I thought 
that was a good idea. Turns out it's not"


>> sg_device_destroy() is accessing the parent scsi_device request_queue 
>> which
>> will already be set to NULL when the preceding call to scsi_device_put()
>> removed the last reference to the parent scsi_device.
>>
>> Drop the incorrect WARN_ON_ONCE() - allowing more than one concurrent
>> access to the sg device - and make sure sg_device_destroy() is not used
>> after scsi_device_put() in the error handling.
>>
>> Link: 
>> https://lore.kernel.org/all/5375B275-D137-4D5F-BE25-6AF8ACAE41EF@linux.ibm.com
>> Fixes: 27f58c04a8f4 ("scsi: sg: Avoid sg device teardown race")
> 
> The "goto sg_put" removed by this patch was introduced by commit
> cc833acbee9d ("sg: O_EXCL and other lock handling"). Since the latter
> commit is older than the one mentioned above, shouldn't the Fixes tag
> refer to the latter commit?
> 

The order was not wrong till commit db59133e9279 ("scsi: sg: fix 
blktrace debugfs entries leakage"), the one my original patch tried to 
fix. Prior to that one sg_device_destroy() was not using the scsi device 
request_queue.

I guess I (or one maintainer) could add that commit here again, too...

My reasoning here is, that this patch here fixes what my first patch got 
wrong.
Which is already heading into the stable trees. And I would prefer to 
not have any kernel release with commit 27f58c04a8f4 ("scsi: sg: Avoid 
sg device teardown race")  without this fix, too.


>> diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
>> index 386981c6976a..833c9277419b 100644
>> --- a/drivers/scsi/sg.c
>> +++ b/drivers/scsi/sg.c
>> @@ -372,8 +372,9 @@ sg_open(struct inode *inode, struct file *filp)
>>   error_out:
>>       scsi_autopm_put_device(sdp->device);
>>   sdp_put:
>> +    kref_put(&sdp->d_ref, sg_device_destroy);
>>       scsi_device_put(sdp->device);
>> -    goto sg_put;
>> +    return retval;
>>   }
> 
> Please add a comment above "return retval" that explains which code will
> drop the sg reference.
> 

Hm, don't get that. That kref_put() is the one dropping the reference.
The matching kref_get() is in sg_add_sfp(). Which is called a few lines 
prior to the code here (line 350).

The patch is literally only swapping the order of scsi_device_put() and 
kref_put().

Which *again* causes a use-after free. So I'll send out v3 immediately 
and if any of the thinks discussed here require a v4 we'll do that.

Alexander
diff mbox series

Patch

diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index 386981c6976a..833c9277419b 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -372,8 +372,9 @@  sg_open(struct inode *inode, struct file *filp)
 error_out:
 	scsi_autopm_put_device(sdp->device);
 sdp_put:
+	kref_put(&sdp->d_ref, sg_device_destroy);
 	scsi_device_put(sdp->device);
-	goto sg_put;
+	return retval;
 }
 
 /* Release resources associated with a successful sg_open()
@@ -2233,7 +2234,6 @@  sg_remove_sfp_usercontext(struct work_struct *work)
 			"sg_remove_sfp: sfp=0x%p\n", sfp));
 	kfree(sfp);
 
-	WARN_ON_ONCE(kref_read(&sdp->d_ref) != 1);
 	kref_put(&sdp->d_ref, sg_device_destroy);
 	scsi_device_put(device);
 	module_put(THIS_MODULE);