diff mbox series

um: Add winch to winch_handlers before registering winch IRQ

Message ID 20240307104926.3531358-1-roberto.sassu@huaweicloud.com
State New
Headers show
Series um: Add winch to winch_handlers before registering winch IRQ | expand

Commit Message

Roberto Sassu March 7, 2024, 10:49 a.m. UTC
From: Roberto Sassu <roberto.sassu@huawei.com>

Registering a winch IRQ is racy, an interrupt may occur before the winch is
added to the winch_handlers list.

If that happens, register_winch_irq() adds to that list a winch that is
scheduled to be (or has already been) freed, causing a panic later in
winch_cleanup().

Avoid the race by adding the winch to the winch_handlers list before
registering the IRQ, and rolling back if um_request_irq() fails.

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 arch/um/drivers/line.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

Comments

Roberto Sassu March 7, 2024, 12:43 p.m. UTC | #1
On Thu, 2024-03-07 at 11:49 +0100, Roberto Sassu wrote:
> From: Roberto Sassu <roberto.sassu@huawei.com>
> 
> Registering a winch IRQ is racy, an interrupt may occur before the winch is
> added to the winch_handlers list.
> 
> If that happens, register_winch_irq() adds to that list a winch that is
> scheduled to be (or has already been) freed, causing a panic later in
> winch_cleanup().
> 
> Avoid the race by adding the winch to the winch_handlers list before
> registering the IRQ, and rolling back if um_request_irq() fails.
> 
> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>

Fixes: 42a359e31a0e ("uml: SIGIO support cleanup")

I see that before that commit there was the same ordering (list_add()
before um_request_irq()).

Failure from um_request_irq() should not result in executing
winch_interrupt() which could call list_del() itself. Then, it should
be fine to delete the winch in the error path.

Roberto

> ---
>  arch/um/drivers/line.c | 14 ++++++++------
>  1 file changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/um/drivers/line.c b/arch/um/drivers/line.c
> index ffc5cb92fa36..d82bc3fdb86e 100644
> --- a/arch/um/drivers/line.c
> +++ b/arch/um/drivers/line.c
> @@ -676,24 +676,26 @@ void register_winch_irq(int fd, int tty_fd, int pid, struct tty_port *port,
>  		goto cleanup;
>  	}
>  
> -	*winch = ((struct winch) { .list  	= LIST_HEAD_INIT(winch->list),
> -				   .fd  	= fd,
> +	*winch = ((struct winch) { .fd  	= fd,
>  				   .tty_fd 	= tty_fd,
>  				   .pid  	= pid,
>  				   .port 	= port,
>  				   .stack	= stack });
>  
> +	spin_lock(&winch_handler_lock);
> +	list_add(&winch->list, &winch_handlers);
> +	spin_unlock(&winch_handler_lock);
> +
>  	if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
>  			   IRQF_SHARED, "winch", winch) < 0) {
>  		printk(KERN_ERR "register_winch_irq - failed to register "
>  		       "IRQ\n");
> +		spin_lock(&winch_handler_lock);
> +		list_del(&winch->list);
> +		spin_unlock(&winch_handler_lock);
>  		goto out_free;
>  	}
>  
> -	spin_lock(&winch_handler_lock);
> -	list_add(&winch->list, &winch_handlers);
> -	spin_unlock(&winch_handler_lock);
> -
>  	return;
>  
>   out_free:
Roberto Sassu March 28, 2024, 8:11 a.m. UTC | #2
On 3/7/2024 1:43 PM, Roberto Sassu wrote:
> On Thu, 2024-03-07 at 11:49 +0100, Roberto Sassu wrote:
>> From: Roberto Sassu <roberto.sassu@huawei.com>
>>
>> Registering a winch IRQ is racy, an interrupt may occur before the winch is
>> added to the winch_handlers list.
>>
>> If that happens, register_winch_irq() adds to that list a winch that is
>> scheduled to be (or has already been) freed, causing a panic later in
>> winch_cleanup().
>>
>> Avoid the race by adding the winch to the winch_handlers list before
>> registering the IRQ, and rolling back if um_request_irq() fails.
>>
>> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> 
> Fixes: 42a359e31a0e ("uml: SIGIO support cleanup")
> 
> I see that before that commit there was the same ordering (list_add()
> before um_request_irq()).
> 
> Failure from um_request_irq() should not result in executing
> winch_interrupt() which could call list_del() itself. Then, it should
> be fine to delete the winch in the error path.

Richard, did you have time to look at this?

Thanks

Roberto

> Roberto
> 
>> ---
>>   arch/um/drivers/line.c | 14 ++++++++------
>>   1 file changed, 8 insertions(+), 6 deletions(-)
>>
>> diff --git a/arch/um/drivers/line.c b/arch/um/drivers/line.c
>> index ffc5cb92fa36..d82bc3fdb86e 100644
>> --- a/arch/um/drivers/line.c
>> +++ b/arch/um/drivers/line.c
>> @@ -676,24 +676,26 @@ void register_winch_irq(int fd, int tty_fd, int pid, struct tty_port *port,
>>   		goto cleanup;
>>   	}
>>   
>> -	*winch = ((struct winch) { .list  	= LIST_HEAD_INIT(winch->list),
>> -				   .fd  	= fd,
>> +	*winch = ((struct winch) { .fd  	= fd,
>>   				   .tty_fd 	= tty_fd,
>>   				   .pid  	= pid,
>>   				   .port 	= port,
>>   				   .stack	= stack });
>>   
>> +	spin_lock(&winch_handler_lock);
>> +	list_add(&winch->list, &winch_handlers);
>> +	spin_unlock(&winch_handler_lock);
>> +
>>   	if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
>>   			   IRQF_SHARED, "winch", winch) < 0) {
>>   		printk(KERN_ERR "register_winch_irq - failed to register "
>>   		       "IRQ\n");
>> +		spin_lock(&winch_handler_lock);
>> +		list_del(&winch->list);
>> +		spin_unlock(&winch_handler_lock);
>>   		goto out_free;
>>   	}
>>   
>> -	spin_lock(&winch_handler_lock);
>> -	list_add(&winch->list, &winch_handlers);
>> -	spin_unlock(&winch_handler_lock);
>> -
>>   	return;
>>   
>>    out_free:
Johannes Berg March 28, 2024, 8:25 a.m. UTC | #3
On Thu, 2024-03-07 at 11:49 +0100, Roberto Sassu wrote:
> From: Roberto Sassu <roberto.sassu@huawei.com>
> 
> Registering a winch IRQ is racy, an interrupt may occur before the winch is
> added to the winch_handlers list.
> 
> If that happens, register_winch_irq() adds to that list a winch that is
> scheduled to be (or has already been) freed, causing a panic later in
> winch_cleanup().
> 
> Avoid the race by adding the winch to the winch_handlers list before
> registering the IRQ, and rolling back if um_request_irq() fails.
> 

Reviewed-by: Johannes Berg <johannes@sipsolutions.net>

johannes
Roberto Sassu April 23, 2024, 7:22 a.m. UTC | #4
On Thu, 2024-03-28 at 09:25 +0100, Johannes Berg wrote:
> On Thu, 2024-03-07 at 11:49 +0100, Roberto Sassu wrote:
> > From: Roberto Sassu <roberto.sassu@huawei.com>
> > 
> > Registering a winch IRQ is racy, an interrupt may occur before the winch is
> > added to the winch_handlers list.
> > 
> > If that happens, register_winch_irq() adds to that list a winch that is
> > scheduled to be (or has already been) freed, causing a panic later in
> > winch_cleanup().
> > 
> > Avoid the race by adding the winch to the winch_handlers list before
> > registering the IRQ, and rolling back if um_request_irq() fails.
> > 
> 
> Reviewed-by: Johannes Berg <johannes@sipsolutions.net>

Thank you! Richard, are you going to pick this up?

Thanks

Roberto
Richard Weinberger April 23, 2024, 8:11 a.m. UTC | #5
----- Ursprüngliche Mail -----
> Von: "Roberto Sassu" <roberto.sassu@huaweicloud.com>
> An: "Johannes Berg" <johannes@sipsolutions.net>, "richard" <richard@nod.at>, "anton ivanov"
> <anton.ivanov@cambridgegreys.com>
> CC: "linux-kernel" <linux-kernel@vger.kernel.org>, "linux-um" <linux-um@lists.infradead.org>, "Roberto Sassu"
> <roberto.sassu@huawei.com>
> Gesendet: Dienstag, 23. April 2024 09:22:31
> Betreff: Re: [PATCH] um: Add winch to winch_handlers before registering winch IRQ

> On Thu, 2024-03-28 at 09:25 +0100, Johannes Berg wrote:
>> On Thu, 2024-03-07 at 11:49 +0100, Roberto Sassu wrote:
>> > From: Roberto Sassu <roberto.sassu@huawei.com>
>> > 
>> > Registering a winch IRQ is racy, an interrupt may occur before the winch is
>> > added to the winch_handlers list.
>> > 
>> > If that happens, register_winch_irq() adds to that list a winch that is
>> > scheduled to be (or has already been) freed, causing a panic later in
>> > winch_cleanup().
>> > 
>> > Avoid the race by adding the winch to the winch_handlers list before
>> > registering the IRQ, and rolling back if um_request_irq() fails.
>> > 
>> 
>> Reviewed-by: Johannes Berg <johannes@sipsolutions.net>
> 
> Thank you! Richard, are you going to pick this up?

Yes, it's already in my local queue.

Thanks,
//richard
diff mbox series

Patch

diff --git a/arch/um/drivers/line.c b/arch/um/drivers/line.c
index ffc5cb92fa36..d82bc3fdb86e 100644
--- a/arch/um/drivers/line.c
+++ b/arch/um/drivers/line.c
@@ -676,24 +676,26 @@  void register_winch_irq(int fd, int tty_fd, int pid, struct tty_port *port,
 		goto cleanup;
 	}
 
-	*winch = ((struct winch) { .list  	= LIST_HEAD_INIT(winch->list),
-				   .fd  	= fd,
+	*winch = ((struct winch) { .fd  	= fd,
 				   .tty_fd 	= tty_fd,
 				   .pid  	= pid,
 				   .port 	= port,
 				   .stack	= stack });
 
+	spin_lock(&winch_handler_lock);
+	list_add(&winch->list, &winch_handlers);
+	spin_unlock(&winch_handler_lock);
+
 	if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
 			   IRQF_SHARED, "winch", winch) < 0) {
 		printk(KERN_ERR "register_winch_irq - failed to register "
 		       "IRQ\n");
+		spin_lock(&winch_handler_lock);
+		list_del(&winch->list);
+		spin_unlock(&winch_handler_lock);
 		goto out_free;
 	}
 
-	spin_lock(&winch_handler_lock);
-	list_add(&winch->list, &winch_handlers);
-	spin_unlock(&winch_handler_lock);
-
 	return;
 
  out_free: