linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Possible race in loop.ko
@ 2017-07-28 15:55 Anton Volkov
  2017-08-01 12:39 ` Ming Lei
  0 siblings, 1 reply; 11+ messages in thread
From: Anton Volkov @ 2017-07-28 15:55 UTC (permalink / raw)
  To: axboe; +Cc: tom.leiming, osandov, linux-kernel, ldv-project, khoroshilov

Hello.
While searching for races in Linux kernel I've come across 
drivers/block/loop.ko module. Here is the question that I came up with 
while analyzing results. Lines are given using the info from Linux v4.12.

In loop_init function additional initialization happens after a 
successful call to misc_register() (loop.c: line 1961). Consider the 
following case:

Thread 1:                 Thread 2:
loop_init()
   misc_register()         loop_control_ioctl
   part_shift = 0          -> loop_add
   if (max_part > 0) {          alloc_disk(1 << part_shift)
     part_shift =
          <greater than 0>
     ...
   }

In this case alloc_disk() will be called with 1 as a parameter although 
part_shift should have been greater than 0. Maybe it would be better to 
move the call to a misc_register() function a bit further down (at least 
so it could be after the part_shift initialization)?

Thank you for your time.

-- Anton Volkov
Linux Verification Center, ISPRAS
web: http://linuxtesting.org
e-mail: avolkov@ispras.ru

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

* Re: Possible race in loop.ko
  2017-07-28 15:55 Possible race in loop.ko Anton Volkov
@ 2017-08-01 12:39 ` Ming Lei
  2017-08-03 15:01   ` [PATCH] loop: fix to a race condition due to the early registration of device Anton Volkov
  0 siblings, 1 reply; 11+ messages in thread
From: Ming Lei @ 2017-08-01 12:39 UTC (permalink / raw)
  To: Anton Volkov
  Cc: Jens Axboe, Omar Sandoval, Linux Kernel Mailing List,
	ldv-project, Alexey Khoroshilov

On Fri, Jul 28, 2017 at 11:55 PM, Anton Volkov <avolkov@ispras.ru> wrote:
> Hello.
> While searching for races in Linux kernel I've come across
> drivers/block/loop.ko module. Here is the question that I came up with while
> analyzing results. Lines are given using the info from Linux v4.12.
>
> In loop_init function additional initialization happens after a successful
> call to misc_register() (loop.c: line 1961). Consider the following case:
>
> Thread 1:                 Thread 2:
> loop_init()
>   misc_register()         loop_control_ioctl
>   part_shift = 0          -> loop_add
>   if (max_part > 0) {          alloc_disk(1 << part_shift)
>     part_shift =
>          <greater than 0>
>     ...
>   }
>
> In this case alloc_disk() will be called with 1 as a parameter although
> part_shift should have been greater than 0. Maybe it would be better to move
> the call to a misc_register() function a bit further down (at least so it
> could be after the part_shift initialization)?

That looks a good idea, could you cook a patch to do it?


-- 
Ming Lei

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

* [PATCH] loop: fix to a race condition due to the early registration of device
  2017-08-01 12:39 ` Ming Lei
@ 2017-08-03 15:01   ` Anton Volkov
  2017-08-07  2:39     ` Ming Lei
  0 siblings, 1 reply; 11+ messages in thread
From: Anton Volkov @ 2017-08-03 15:01 UTC (permalink / raw)
  To: tom.leiming
  Cc: axboe, osandov, linux-kernel, ldv-project, khoroshilov, Anton Volkov

The early device registration made possible a race leading to allocations
of disks with wrong minors.

This patch moves the device registration further down the loop_init
function to make the race infeasible.

Found by Linux Driver Verification project (linuxtesting.org).

Signed-off-by: Anton Volkov <avolkov@ispras.ru>
---
 drivers/block/loop.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index ef83349..2fbd4089 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -1996,10 +1996,6 @@ static int __init loop_init(void)
 	struct loop_device *lo;
 	int err;
 
-	err = misc_register(&loop_misc);
-	if (err < 0)
-		return err;
-
 	part_shift = 0;
 	if (max_part > 0) {
 		part_shift = fls(max_part);
@@ -2017,12 +2013,12 @@ static int __init loop_init(void)
 
 	if ((1UL << part_shift) > DISK_MAX_PARTS) {
 		err = -EINVAL;
-		goto misc_out;
+		goto err_out;
 	}
 
 	if (max_loop > 1UL << (MINORBITS - part_shift)) {
 		err = -EINVAL;
-		goto misc_out;
+		goto err_out;
 	}
 
 	/*
@@ -2041,6 +2037,11 @@ static int __init loop_init(void)
 		range = 1UL << MINORBITS;
 	}
 
+	err = misc_register(&loop_misc);
+	if (err < 0)
+		goto err_out;
+
+
 	if (register_blkdev(LOOP_MAJOR, "loop")) {
 		err = -EIO;
 		goto misc_out;
@@ -2060,6 +2061,7 @@ static int __init loop_init(void)
 
 misc_out:
 	misc_deregister(&loop_misc);
+err_out:
 	return err;
 }
 
-- 
2.7.4

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

* Re: [PATCH] loop: fix to a race condition due to the early registration of device
  2017-08-03 15:01   ` [PATCH] loop: fix to a race condition due to the early registration of device Anton Volkov
@ 2017-08-07  2:39     ` Ming Lei
  2017-08-07 12:37       ` Anton Volkov
  0 siblings, 1 reply; 11+ messages in thread
From: Ming Lei @ 2017-08-07  2:39 UTC (permalink / raw)
  To: Anton Volkov
  Cc: Jens Axboe, Omar Sandoval, Linux Kernel Mailing List,
	ldv-project, Alexey Khoroshilov

On Thu, Aug 3, 2017 at 11:01 PM, Anton Volkov <avolkov@ispras.ru> wrote:
> The early device registration made possible a race leading to allocations
> of disks with wrong minors.
>
> This patch moves the device registration further down the loop_init
> function to make the race infeasible.
>
> Found by Linux Driver Verification project (linuxtesting.org).
>
> Signed-off-by: Anton Volkov <avolkov@ispras.ru>
> ---
>  drivers/block/loop.c | 14 ++++++++------
>  1 file changed, 8 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/block/loop.c b/drivers/block/loop.c
> index ef83349..2fbd4089 100644
> --- a/drivers/block/loop.c
> +++ b/drivers/block/loop.c
> @@ -1996,10 +1996,6 @@ static int __init loop_init(void)
>         struct loop_device *lo;
>         int err;
>
> -       err = misc_register(&loop_misc);
> -       if (err < 0)
> -               return err;
> -
>         part_shift = 0;
>         if (max_part > 0) {
>                 part_shift = fls(max_part);
> @@ -2017,12 +2013,12 @@ static int __init loop_init(void)
>
>         if ((1UL << part_shift) > DISK_MAX_PARTS) {
>                 err = -EINVAL;
> -               goto misc_out;
> +               goto err_out;
>         }
>
>         if (max_loop > 1UL << (MINORBITS - part_shift)) {
>                 err = -EINVAL;
> -               goto misc_out;
> +               goto err_out;
>         }
>
>         /*
> @@ -2041,6 +2037,11 @@ static int __init loop_init(void)
>                 range = 1UL << MINORBITS;
>         }
>
> +       err = misc_register(&loop_misc);
> +       if (err < 0)
> +               goto err_out;
> +
> +
>         if (register_blkdev(LOOP_MAJOR, "loop")) {
>                 err = -EIO;
>                 goto misc_out;
> @@ -2060,6 +2061,7 @@ static int __init loop_init(void)
>
>  misc_out:
>         misc_deregister(&loop_misc);
> +err_out:
>         return err;
>  }
>
> --
> 2.7.4
>

Looks fine:

         Reviewed-by: Ming Lei <ming.lei@redhat.com>

BTW, this patch should have been CCed to linux-block mail list.


Thanks,
Ming Lei

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

* [PATCH] loop: fix to a race condition due to the early registration of device
  2017-08-07  2:39     ` Ming Lei
@ 2017-08-07 12:37       ` Anton Volkov
  2017-08-07 12:54         ` Johannes Thumshirn
                           ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Anton Volkov @ 2017-08-07 12:37 UTC (permalink / raw)
  To: tom.leiming
  Cc: axboe, osandov, linux-kernel, linux-block, ldv-project,
	khoroshilov, Anton Volkov

The early device registration made possible a race leading to allocations
of disks with wrong minors.

This patch moves the device registration further down the loop_init
function to make the race infeasible.

Found by Linux Driver Verification project (linuxtesting.org).

Signed-off-by: Anton Volkov <avolkov@ispras.ru>
Reviewed-by: Ming Lei <ming.lei@redhat.com>
---
 drivers/block/loop.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index ef83349..2fbd4089 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -1996,10 +1996,6 @@ static int __init loop_init(void)
 	struct loop_device *lo;
 	int err;
 
-	err = misc_register(&loop_misc);
-	if (err < 0)
-		return err;
-
 	part_shift = 0;
 	if (max_part > 0) {
 		part_shift = fls(max_part);
@@ -2017,12 +2013,12 @@ static int __init loop_init(void)
 
 	if ((1UL << part_shift) > DISK_MAX_PARTS) {
 		err = -EINVAL;
-		goto misc_out;
+		goto err_out;
 	}
 
 	if (max_loop > 1UL << (MINORBITS - part_shift)) {
 		err = -EINVAL;
-		goto misc_out;
+		goto err_out;
 	}
 
 	/*
@@ -2041,6 +2037,11 @@ static int __init loop_init(void)
 		range = 1UL << MINORBITS;
 	}
 
+	err = misc_register(&loop_misc);
+	if (err < 0)
+		goto err_out;
+
+
 	if (register_blkdev(LOOP_MAJOR, "loop")) {
 		err = -EIO;
 		goto misc_out;
@@ -2060,6 +2061,7 @@ static int __init loop_init(void)
 
 misc_out:
 	misc_deregister(&loop_misc);
+err_out:
 	return err;
 }
 
-- 
2.7.4

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

* Re: [PATCH] loop: fix to a race condition due to the early registration of device
  2017-08-07 12:37       ` Anton Volkov
@ 2017-08-07 12:54         ` Johannes Thumshirn
  2017-08-07 13:09           ` Anton Volkov
  2017-08-08 22:00         ` Omar Sandoval
  2017-08-15 18:51         ` Jens Axboe
  2 siblings, 1 reply; 11+ messages in thread
From: Johannes Thumshirn @ 2017-08-07 12:54 UTC (permalink / raw)
  To: Anton Volkov
  Cc: tom.leiming, axboe, osandov, linux-kernel, linux-block,
	ldv-project, khoroshilov

On Mon, Aug 07, 2017 at 03:37:50PM +0300, Anton Volkov wrote:
> +err_out:
>  	return err;

Any reason you can't just use return err; at the respective callsites?

Thanks,
	Johannes

-- 
Johannes Thumshirn                                          Storage
jthumshirn@suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850

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

* Re: [PATCH] loop: fix to a race condition due to the early registration of device
  2017-08-07 12:54         ` Johannes Thumshirn
@ 2017-08-07 13:09           ` Anton Volkov
  2017-08-07 13:24             ` Johannes Thumshirn
  0 siblings, 1 reply; 11+ messages in thread
From: Anton Volkov @ 2017-08-07 13:09 UTC (permalink / raw)
  To: Johannes Thumshirn
  Cc: tom.leiming, axboe, osandov, linux-kernel, linux-block,
	ldv-project, khoroshilov

This is more of a style-oriented suggestion. This kind of template is 
commonly used in other modules.

Regards,
Anton

On 07.08.2017 15:54, Johannes Thumshirn wrote:
> On Mon, Aug 07, 2017 at 03:37:50PM +0300, Anton Volkov wrote:
>> +err_out:
>>   	return err;
> 
> Any reason you can't just use return err; at the respective callsites?
> 
> Thanks,
> 	Johannes
> 

-- Anton Volkov
Linux Verification Center, ISPRAS
web: http://linuxtesting.org
e-mail: avolkov@ispras.ru

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

* Re: [PATCH] loop: fix to a race condition due to the early registration of device
  2017-08-07 13:09           ` Anton Volkov
@ 2017-08-07 13:24             ` Johannes Thumshirn
  0 siblings, 0 replies; 11+ messages in thread
From: Johannes Thumshirn @ 2017-08-07 13:24 UTC (permalink / raw)
  To: Anton Volkov
  Cc: tom.leiming, axboe, osandov, linux-kernel, linux-block,
	ldv-project, khoroshilov

On Mon, Aug 07, 2017 at 04:09:12PM +0300, Anton Volkov wrote:
> This is more of a style-oriented suggestion. This kind of template is
> commonly used in other modules.

Yes but there is no point in using gotos here (i.e. cleanup to be done), you
an just return directly.

And yes it is a minor nit.

Anyways,

Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de>
-- 
Johannes Thumshirn                                          Storage
jthumshirn@suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850

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

* Re: [PATCH] loop: fix to a race condition due to the early registration of device
  2017-08-07 12:37       ` Anton Volkov
  2017-08-07 12:54         ` Johannes Thumshirn
@ 2017-08-08 22:00         ` Omar Sandoval
  2017-08-10 15:46           ` Anton Volkov
  2017-08-15 18:51         ` Jens Axboe
  2 siblings, 1 reply; 11+ messages in thread
From: Omar Sandoval @ 2017-08-08 22:00 UTC (permalink / raw)
  To: Anton Volkov
  Cc: tom.leiming, axboe, osandov, linux-kernel, linux-block,
	ldv-project, khoroshilov

On Mon, Aug 07, 2017 at 03:37:50PM +0300, Anton Volkov wrote:
> The early device registration made possible a race leading to allocations
> of disks with wrong minors.
> 
> This patch moves the device registration further down the loop_init
> function to make the race infeasible.
> 
> Found by Linux Driver Verification project (linuxtesting.org).
> 
> Signed-off-by: Anton Volkov <avolkov@ispras.ru>
> Reviewed-by: Ming Lei <ming.lei@redhat.com>

Hi, Anton,

Were you able to reproduce this issue or was it purely theoretical? If
the former, it'd be nice if you could add a test case to blktests [1].

1: https://github.com/osandov/blktests

Thanks!
Omar

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

* Re: [PATCH] loop: fix to a race condition due to the early registration of device
  2017-08-08 22:00         ` Omar Sandoval
@ 2017-08-10 15:46           ` Anton Volkov
  0 siblings, 0 replies; 11+ messages in thread
From: Anton Volkov @ 2017-08-10 15:46 UTC (permalink / raw)
  To: Omar Sandoval
  Cc: tom.leiming, axboe, osandov, linux-kernel, linux-block,
	ldv-project, khoroshilov

Hello, Omar.

It was a purely theoretical race that had been considered to be possible 
in real-life.

Regards,
Anton

On 09.08.2017 01:00, Omar Sandoval wrote:
> On Mon, Aug 07, 2017 at 03:37:50PM +0300, Anton Volkov wrote:
>> The early device registration made possible a race leading to allocations
>> of disks with wrong minors.
>>
>> This patch moves the device registration further down the loop_init
>> function to make the race infeasible.
>>
>> Found by Linux Driver Verification project (linuxtesting.org).
>>
>> Signed-off-by: Anton Volkov <avolkov@ispras.ru>
>> Reviewed-by: Ming Lei <ming.lei@redhat.com>
> 
> Hi, Anton,
> 
> Were you able to reproduce this issue or was it purely theoretical? If
> the former, it'd be nice if you could add a test case to blktests [1].
> 
> 1: https://github.com/osandov/blktests
> 
> Thanks!
> Omar
> 

-- Anton Volkov
Linux Verification Center, ISPRAS
web: http://linuxtesting.org
e-mail: avolkov@ispras.ru

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

* Re: [PATCH] loop: fix to a race condition due to the early registration of device
  2017-08-07 12:37       ` Anton Volkov
  2017-08-07 12:54         ` Johannes Thumshirn
  2017-08-08 22:00         ` Omar Sandoval
@ 2017-08-15 18:51         ` Jens Axboe
  2 siblings, 0 replies; 11+ messages in thread
From: Jens Axboe @ 2017-08-15 18:51 UTC (permalink / raw)
  To: Anton Volkov, tom.leiming
  Cc: osandov, linux-kernel, linux-block, ldv-project, khoroshilov

On 08/07/2017 06:37 AM, Anton Volkov wrote:
> The early device registration made possible a race leading to allocations
> of disks with wrong minors.
> 
> This patch moves the device registration further down the loop_init
> function to make the race infeasible.
> 
> Found by Linux Driver Verification project (linuxtesting.org).

Added for 4.14, thanks.

-- 
Jens Axboe

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

end of thread, other threads:[~2017-08-15 18:51 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-28 15:55 Possible race in loop.ko Anton Volkov
2017-08-01 12:39 ` Ming Lei
2017-08-03 15:01   ` [PATCH] loop: fix to a race condition due to the early registration of device Anton Volkov
2017-08-07  2:39     ` Ming Lei
2017-08-07 12:37       ` Anton Volkov
2017-08-07 12:54         ` Johannes Thumshirn
2017-08-07 13:09           ` Anton Volkov
2017-08-07 13:24             ` Johannes Thumshirn
2017-08-08 22:00         ` Omar Sandoval
2017-08-10 15:46           ` Anton Volkov
2017-08-15 18:51         ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).