linux-kernel-mentees.lists.linuxfoundation.org archive mirror
 help / color / mirror / Atom feed
* [Linux-kernel-mentees] [PATCH] net: can: prevent potential access of uninitialized value in canfd_rcv()
@ 2020-11-02  3:13 Anant Thazhemadam
  2020-11-02  7:10 ` Marc Kleine-Budde
  0 siblings, 1 reply; 4+ messages in thread
From: Anant Thazhemadam @ 2020-11-02  3:13 UTC (permalink / raw)
  To: Oliver Hartkopp, Marc Kleine-Budde, David S . Miller, Jakub Kicinski
  Cc: Anant Thazhemadam, netdev, linux-kernel, linux-can,
	syzbot+9bcb0c9409066696d3aa, linux-kernel-mentees

In canfd_rcv(), cfd->len is uninitialized when skb->len = 0, and this
uninitialized cfd->len is accessed nonetheless by pr_warn_once().

Fix this uninitialized variable access by checking cfd->len's validity
condition (cfd->len > CANFD_MAX_DLEN) separately after the skb->len's
condition is checked, and appropriately modify the log messages that
are generated as well.
In case either of the required conditions fail, the skb is freed and
NET_RX_DROP is returned, same as before.

Reported-by: syzbot+9bcb0c9409066696d3aa@syzkaller.appspotmail.com
Tested-by: Anant Thazhemadam <anant.thazhemadam@gmail.com>
Signed-off-by: Anant Thazhemadam <anant.thazhemadam@gmail.com>
---
This patch was locally tested using the reproducer and .config file 
generated by syzbot.

 net/can/af_can.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/net/can/af_can.c b/net/can/af_can.c
index ea29a6d97ef5..1b9f2e50f065 100644
--- a/net/can/af_can.c
+++ b/net/can/af_can.c
@@ -694,16 +694,25 @@ static int canfd_rcv(struct sk_buff *skb, struct net_device *dev,
 {
 	struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
 
-	if (unlikely(dev->type != ARPHRD_CAN || skb->len != CANFD_MTU ||
-		     cfd->len > CANFD_MAX_DLEN)) {
-		pr_warn_once("PF_CAN: dropped non conform CAN FD skbuf: dev type %d, len %d, datalen %d\n",
+	if (unlikely(dev->type != ARPHRD_CAN || skb->len != CANFD_MTU)) {
+		pr_warn_once("PF_CAN: dropped non conform CAN FD skbuff: dev type %d, len %d\n",
+			     dev->type, skb->len);
+		goto free_skb;
+	}
+
+	// This check is made separately since cfd->len would be uninitialized if skb->len = 0.
+	else if (unlikely(cfd->len > CANFD_MAX_DLEN)) {
+		pr_warn_once("PF_CAN: dropped non conform CAN FD skbuff: dev type %d, len %d, datalen %d\n",
 			     dev->type, skb->len, cfd->len);
-		kfree_skb(skb);
-		return NET_RX_DROP;
+		goto free_skb;
 	}
 
 	can_receive(skb, dev);
 	return NET_RX_SUCCESS;
+
+free_skb:
+	kfree_skb(skb);
+	return NET_RX_DROP;
 }
 
 /* af_can protocol functions */
-- 
2.25.1

_______________________________________________
Linux-kernel-mentees mailing list
Linux-kernel-mentees@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

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

* Re: [Linux-kernel-mentees] [PATCH] net: can: prevent potential access of uninitialized value in canfd_rcv()
  2020-11-02  3:13 [Linux-kernel-mentees] [PATCH] net: can: prevent potential access of uninitialized value in canfd_rcv() Anant Thazhemadam
@ 2020-11-02  7:10 ` Marc Kleine-Budde
  2020-11-02  7:44   ` Anant Thazhemadam
  0 siblings, 1 reply; 4+ messages in thread
From: Marc Kleine-Budde @ 2020-11-02  7:10 UTC (permalink / raw)
  To: Anant Thazhemadam, Oliver Hartkopp, David S . Miller, Jakub Kicinski
  Cc: syzbot+9bcb0c9409066696d3aa, netdev, linux-kernel-mentees,
	linux-kernel, linux-can


[-- Attachment #1.1.1: Type: text/plain, Size: 2770 bytes --]

On 11/2/20 4:13 AM, Anant Thazhemadam wrote:
> In canfd_rcv(), cfd->len is uninitialized when skb->len = 0, and this
> uninitialized cfd->len is accessed nonetheless by pr_warn_once().
> 
> Fix this uninitialized variable access by checking cfd->len's validity
> condition (cfd->len > CANFD_MAX_DLEN) separately after the skb->len's
> condition is checked, and appropriately modify the log messages that
> are generated as well.
> In case either of the required conditions fail, the skb is freed and
> NET_RX_DROP is returned, same as before.
> 
> Reported-by: syzbot+9bcb0c9409066696d3aa@syzkaller.appspotmail.com
> Tested-by: Anant Thazhemadam <anant.thazhemadam@gmail.com>
> Signed-off-by: Anant Thazhemadam <anant.thazhemadam@gmail.com>
> ---
> This patch was locally tested using the reproducer and .config file 
> generated by syzbot.
> 
>  net/can/af_can.c | 19 ++++++++++++++-----
>  1 file changed, 14 insertions(+), 5 deletions(-)
> 
> diff --git a/net/can/af_can.c b/net/can/af_can.c
> index ea29a6d97ef5..1b9f2e50f065 100644
> --- a/net/can/af_can.c
> +++ b/net/can/af_can.c
> @@ -694,16 +694,25 @@ static int canfd_rcv(struct sk_buff *skb, struct net_device *dev,

Can you create a similar patch for "can_rcv()"?

>  {
>  	struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
>  
> -	if (unlikely(dev->type != ARPHRD_CAN || skb->len != CANFD_MTU ||
> -		     cfd->len > CANFD_MAX_DLEN)) {
> -		pr_warn_once("PF_CAN: dropped non conform CAN FD skbuf: dev type %d, len %d, datalen %d\n",
> +	if (unlikely(dev->type != ARPHRD_CAN || skb->len != CANFD_MTU)) {
> +		pr_warn_once("PF_CAN: dropped non conform CAN FD skbuff: dev type %d, len %d\n",
> +			     dev->type, skb->len);
> +		goto free_skb;
> +	}
> +
> +	// This check is made separately since cfd->len would be uninitialized if skb->len = 0.

Please don't use C++ comment style in the kernel.

> +	else if (unlikely(cfd->len > CANFD_MAX_DLEN)) {

Please move the "else" right after the closing curly bracket: "} else if () {"
or convert it into an "if () {"

> +		pr_warn_once("PF_CAN: dropped non conform CAN FD skbuff: dev type %d, len %d, datalen %d\n",
>  			     dev->type, skb->len, cfd->len);
> -		kfree_skb(skb);
> -		return NET_RX_DROP;
> +		goto free_skb;
>  	}
>  
>  	can_receive(skb, dev);
>  	return NET_RX_SUCCESS;
> +
> +free_skb:
> +	kfree_skb(skb);
> +	return NET_RX_DROP;
>  }
>  
>  /* af_can protocol functions */
> 

regards,
Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |


[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Linux-kernel-mentees mailing list
Linux-kernel-mentees@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

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

* Re: [Linux-kernel-mentees] [PATCH] net: can: prevent potential access of uninitialized value in canfd_rcv()
  2020-11-02  7:10 ` Marc Kleine-Budde
@ 2020-11-02  7:44   ` Anant Thazhemadam
  2020-11-02  8:28     ` Marc Kleine-Budde
  0 siblings, 1 reply; 4+ messages in thread
From: Anant Thazhemadam @ 2020-11-02  7:44 UTC (permalink / raw)
  To: Marc Kleine-Budde, Oliver Hartkopp, David S . Miller, Jakub Kicinski
  Cc: syzbot+9bcb0c9409066696d3aa, netdev, linux-kernel-mentees,
	linux-kernel, linux-can


On 02-11-2020 12:40, Marc Kleine-Budde wrote:
> On 11/2/20 4:13 AM, Anant Thazhemadam wrote:
>> In canfd_rcv(), cfd->len is uninitialized when skb->len = 0, and this
>> uninitialized cfd->len is accessed nonetheless by pr_warn_once().
>>
>> Fix this uninitialized variable access by checking cfd->len's validity
>> condition (cfd->len > CANFD_MAX_DLEN) separately after the skb->len's
>> condition is checked, and appropriately modify the log messages that
>> are generated as well.
>> In case either of the required conditions fail, the skb is freed and
>> NET_RX_DROP is returned, same as before.
>>
>> Reported-by: syzbot+9bcb0c9409066696d3aa@syzkaller.appspotmail.com
>> Tested-by: Anant Thazhemadam <anant.thazhemadam@gmail.com>
>> Signed-off-by: Anant Thazhemadam <anant.thazhemadam@gmail.com>
>> ---
>> This patch was locally tested using the reproducer and .config file 
>> generated by syzbot.
>>
>>  net/can/af_can.c | 19 ++++++++++++++-----
>>  1 file changed, 14 insertions(+), 5 deletions(-)
>>
>> diff --git a/net/can/af_can.c b/net/can/af_can.c
>> index ea29a6d97ef5..1b9f2e50f065 100644
>> --- a/net/can/af_can.c
>> +++ b/net/can/af_can.c
>> @@ -694,16 +694,25 @@ static int canfd_rcv(struct sk_buff *skb, struct net_device *dev,
> Can you create a similar patch for "can_rcv()"?

Yes, I can. Would it be alright if that was part of the v2 itself (since it's similar changes)?
Or would I have to split them up into 2 different patches and send it as a 2-patch series
(since the changes made are in different functions)?

>
>>  {
>>  	struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
>>  
>> -	if (unlikely(dev->type != ARPHRD_CAN || skb->len != CANFD_MTU ||
>> -		     cfd->len > CANFD_MAX_DLEN)) {
>> -		pr_warn_once("PF_CAN: dropped non conform CAN FD skbuf: dev type %d, len %d, datalen %d\n",
>> +	if (unlikely(dev->type != ARPHRD_CAN || skb->len != CANFD_MTU)) {
>> +		pr_warn_once("PF_CAN: dropped non conform CAN FD skbuff: dev type %d, len %d\n",
>> +			     dev->type, skb->len);
>> +		goto free_skb;
>> +	}
>> +
>> +	// This check is made separately since cfd->len would be uninitialized if skb->len = 0.
> Please don't use C++ comment style in the kernel.

Noted. I'll have this fixed in the v2.

>
>> +	else if (unlikely(cfd->len > CANFD_MAX_DLEN)) {
> Please move the "else" right after the closing curly bracket: "} else if () {"
> or convert it into an "if () {"

Noted.

>
>> +		pr_warn_once("PF_CAN: dropped non conform CAN FD skbuff: dev type %d, len %d, datalen %d\n",
>>  			     dev->type, skb->len, cfd->len);
>> -		kfree_skb(skb);
>> -		return NET_RX_DROP;
>> +		goto free_skb;
>>  	}
>>  
>>  	can_receive(skb, dev);
>>  	return NET_RX_SUCCESS;
>> +
>> +free_skb:
>> +	kfree_skb(skb);
>> +	return NET_RX_DROP;
>>  }
>>  
>>  /* af_can protocol functions */
>>
> regards,
> Marc

Thank you for your time.

Thanks,
Anant

_______________________________________________
Linux-kernel-mentees mailing list
Linux-kernel-mentees@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

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

* Re: [Linux-kernel-mentees] [PATCH] net: can: prevent potential access of uninitialized value in canfd_rcv()
  2020-11-02  7:44   ` Anant Thazhemadam
@ 2020-11-02  8:28     ` Marc Kleine-Budde
  0 siblings, 0 replies; 4+ messages in thread
From: Marc Kleine-Budde @ 2020-11-02  8:28 UTC (permalink / raw)
  To: Anant Thazhemadam, Oliver Hartkopp, David S . Miller, Jakub Kicinski
  Cc: syzbot+9bcb0c9409066696d3aa, netdev, linux-kernel-mentees,
	linux-kernel, linux-can


[-- Attachment #1.1.1: Type: text/plain, Size: 2010 bytes --]

On 11/2/20 8:44 AM, Anant Thazhemadam wrote:
> 
> On 02-11-2020 12:40, Marc Kleine-Budde wrote:
>> On 11/2/20 4:13 AM, Anant Thazhemadam wrote:
>>> In canfd_rcv(), cfd->len is uninitialized when skb->len = 0, and this
>>> uninitialized cfd->len is accessed nonetheless by pr_warn_once().
>>>
>>> Fix this uninitialized variable access by checking cfd->len's validity
>>> condition (cfd->len > CANFD_MAX_DLEN) separately after the skb->len's
>>> condition is checked, and appropriately modify the log messages that
>>> are generated as well.
>>> In case either of the required conditions fail, the skb is freed and
>>> NET_RX_DROP is returned, same as before.
>>>
>>> Reported-by: syzbot+9bcb0c9409066696d3aa@syzkaller.appspotmail.com
>>> Tested-by: Anant Thazhemadam <anant.thazhemadam@gmail.com>
>>> Signed-off-by: Anant Thazhemadam <anant.thazhemadam@gmail.com>
>>> ---
>>> This patch was locally tested using the reproducer and .config file 
>>> generated by syzbot.
>>>
>>>  net/can/af_can.c | 19 ++++++++++++++-----
>>>  1 file changed, 14 insertions(+), 5 deletions(-)
>>>
>>> diff --git a/net/can/af_can.c b/net/can/af_can.c
>>> index ea29a6d97ef5..1b9f2e50f065 100644
>>> --- a/net/can/af_can.c
>>> +++ b/net/can/af_can.c
>>> @@ -694,16 +694,25 @@ static int canfd_rcv(struct sk_buff *skb, struct net_device *dev,
>> Can you create a similar patch for "can_rcv()"?
> 
> Yes, I can. Would it be alright if that was part of the v2 itself (since it's similar changes)?
> Or would I have to split them up into 2 different patches and send it as a 2-patch series
> (since the changes made are in different functions)?

Please make it two patches. Please add a "Fixes" line to both patches.

Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |


[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Linux-kernel-mentees mailing list
Linux-kernel-mentees@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

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

end of thread, other threads:[~2020-11-02  8:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-02  3:13 [Linux-kernel-mentees] [PATCH] net: can: prevent potential access of uninitialized value in canfd_rcv() Anant Thazhemadam
2020-11-02  7:10 ` Marc Kleine-Budde
2020-11-02  7:44   ` Anant Thazhemadam
2020-11-02  8:28     ` Marc Kleine-Budde

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).