netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()
@ 2025-04-14 18:34 Alexey Nepomnyashih
  2025-04-17  0:58 ` Jakub Kicinski
  0 siblings, 1 reply; 9+ messages in thread
From: Alexey Nepomnyashih @ 2025-04-14 18:34 UTC (permalink / raw)
  To: Juergen Gross
  Cc: Alexey Nepomnyashih, Stefano Stabellini, Oleksandr Tyshchenko,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Alexei Starovoitov, Daniel Borkmann, Jesper Dangaard Brouer,
	John Fastabend, xen-devel, netdev, linux-kernel, bpf, lvc-project,
	stable

The function xdp_convert_buff_to_frame() may return NULL if it fails
to correctly convert the XDP buffer into an XDP frame due to memory
constraints, internal errors, or invalid data. Failing to check for NULL
may lead to a NULL pointer dereference if the result is used later in
processing, potentially causing crashes, data corruption, or undefined
behavior.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Cc: stable@vger.kernel.org # v5.9+
Fixes: 6c5aa6fc4def ("xen networking: add basic XDP support for xen-netfront")
Signed-off-by: Alexey Nepomnyashih <sdl@nppct.ru>
---
 drivers/net/xen-netfront.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/net/xen-netfront.c b/drivers/net/xen-netfront.c
index 8425226c09f0..e99561de3cda 100644
--- a/drivers/net/xen-netfront.c
+++ b/drivers/net/xen-netfront.c
@@ -987,6 +987,10 @@ static u32 xennet_run_xdp(struct netfront_queue *queue, struct page *pdata,
 	case XDP_TX:
 		get_page(pdata);
 		xdpf = xdp_convert_buff_to_frame(xdp);
+		if (unlikely(!xdpf)) {
+			trace_xdp_exception(queue->info->netdev, prog, act);
+			break;
+		}
 		err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0);
 		if (unlikely(!err))
 			xdp_return_frame_rx_napi(xdpf);
-- 
2.43.0


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

* Re: [PATCH] xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()
  2025-04-14 18:34 [PATCH] xen-netfront: handle NULL returned by xdp_convert_buff_to_frame() Alexey Nepomnyashih
@ 2025-04-17  0:58 ` Jakub Kicinski
  2025-04-17  7:00   ` Alexey
  0 siblings, 1 reply; 9+ messages in thread
From: Jakub Kicinski @ 2025-04-17  0:58 UTC (permalink / raw)
  To: Alexey Nepomnyashih
  Cc: Juergen Gross, Stefano Stabellini, Oleksandr Tyshchenko,
	David S. Miller, Eric Dumazet, Paolo Abeni, Alexei Starovoitov,
	Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
	xen-devel, netdev, linux-kernel, bpf, lvc-project, stable

On Mon, 14 Apr 2025 18:34:01 +0000 Alexey Nepomnyashih wrote:
>  		get_page(pdata);

Please notice this get_page() here.

>  		xdpf = xdp_convert_buff_to_frame(xdp);
> +		if (unlikely(!xdpf)) {
> +			trace_xdp_exception(queue->info->netdev, prog, act);
> +			break;
> +		}

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

* Re: [PATCH] xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()
  2025-04-17  0:58 ` Jakub Kicinski
@ 2025-04-17  7:00   ` Alexey
  2025-04-17  7:12     ` Jürgen Groß
  0 siblings, 1 reply; 9+ messages in thread
From: Alexey @ 2025-04-17  7:00 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: Juergen Gross, Stefano Stabellini, Oleksandr Tyshchenko,
	David S. Miller, Eric Dumazet, Paolo Abeni, Alexei Starovoitov,
	Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
	xen-devel, netdev, linux-kernel, bpf, lvc-project, stable


On 17.04.2025 03:58, Jakub Kicinski wrote:
> On Mon, 14 Apr 2025 18:34:01 +0000 Alexey Nepomnyashih wrote:
>>   		get_page(pdata);
> Please notice this get_page() here.
>
>>   		xdpf = xdp_convert_buff_to_frame(xdp);
>> +		if (unlikely(!xdpf)) {
>> +			trace_xdp_exception(queue->info->netdev, prog, act);
>> +			break;
>> +		}
Do you mean that it would be better to move the get_page(pdata) call lower,
after checking for NULL in xdpf, so that the reference count is only 
increased
after a successful conversion?

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

* Re: [PATCH] xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()
  2025-04-17  7:00   ` Alexey
@ 2025-04-17  7:12     ` Jürgen Groß
  2025-04-17  8:45       ` Alexey
  0 siblings, 1 reply; 9+ messages in thread
From: Jürgen Groß @ 2025-04-17  7:12 UTC (permalink / raw)
  To: Alexey, Jakub Kicinski
  Cc: Stefano Stabellini, Oleksandr Tyshchenko, David S. Miller,
	Eric Dumazet, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend, xen-devel, netdev,
	linux-kernel, bpf, lvc-project, stable


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

On 17.04.25 09:00, Alexey wrote:
> 
> On 17.04.2025 03:58, Jakub Kicinski wrote:
>> On Mon, 14 Apr 2025 18:34:01 +0000 Alexey Nepomnyashih wrote:
>>>           get_page(pdata);
>> Please notice this get_page() here.
>>
>>>           xdpf = xdp_convert_buff_to_frame(xdp);
>>> +        if (unlikely(!xdpf)) {
>>> +            trace_xdp_exception(queue->info->netdev, prog, act);
>>> +            break;
>>> +        }
> Do you mean that it would be better to move the get_page(pdata) call lower,
> after checking for NULL in xdpf, so that the reference count is only increased
> after a successful conversion?

I think the error handling here is generally broken (or at least very
questionable).

I suspect that in case of at least some errors the get_page() is leaking
even without this new patch.

In case I'm wrong a comment reasoning why there is no leak should be
added.


Juergen

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3743 bytes --]

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

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

* Re: [PATCH] xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()
  2025-04-17  7:12     ` Jürgen Groß
@ 2025-04-17  8:45       ` Alexey
  2025-04-17  8:51         ` Juergen Gross
  0 siblings, 1 reply; 9+ messages in thread
From: Alexey @ 2025-04-17  8:45 UTC (permalink / raw)
  To: Jürgen Groß, Jakub Kicinski
  Cc: Stefano Stabellini, Oleksandr Tyshchenko, David S. Miller,
	Eric Dumazet, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend, xen-devel, netdev,
	linux-kernel, bpf, lvc-project, stable


On 17.04.2025 10:12, Jürgen Groß wrote:
> On 17.04.25 09:00, Alexey wrote:
>>
>> On 17.04.2025 03:58, Jakub Kicinski wrote:
>>> On Mon, 14 Apr 2025 18:34:01 +0000 Alexey Nepomnyashih wrote:
>>>>           get_page(pdata);
>>> Please notice this get_page() here.
>>>
>>>>           xdpf = xdp_convert_buff_to_frame(xdp);
>>>> +        if (unlikely(!xdpf)) {
>>>> +            trace_xdp_exception(queue->info->netdev, prog, act);
>>>> +            break;
>>>> +        }
>> Do you mean that it would be better to move the get_page(pdata) call 
>> lower,
>> after checking for NULL in xdpf, so that the reference count is only 
>> increased
>> after a successful conversion?
>
> I think the error handling here is generally broken (or at least very
> questionable).
>
> I suspect that in case of at least some errors the get_page() is leaking
> even without this new patch.
>
> In case I'm wrong a comment reasoning why there is no leak should be
> added.
>
>
> Juergen

I think pdata is freed in xdp_return_frame_rx_napi() -> __xdp_return()


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

* Re: [PATCH] xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()
  2025-04-17  8:45       ` Alexey
@ 2025-04-17  8:51         ` Juergen Gross
       [not found]           ` <8264519a-d58a-486e-b3c5-dba400658513@nppct.ru>
  0 siblings, 1 reply; 9+ messages in thread
From: Juergen Gross @ 2025-04-17  8:51 UTC (permalink / raw)
  To: Alexey, Jakub Kicinski
  Cc: Stefano Stabellini, Oleksandr Tyshchenko, David S. Miller,
	Eric Dumazet, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend, xen-devel, netdev,
	linux-kernel, bpf, lvc-project, stable


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

On 17.04.25 10:45, Alexey wrote:
> 
> On 17.04.2025 10:12, Jürgen Groß wrote:
>> On 17.04.25 09:00, Alexey wrote:
>>>
>>> On 17.04.2025 03:58, Jakub Kicinski wrote:
>>>> On Mon, 14 Apr 2025 18:34:01 +0000 Alexey Nepomnyashih wrote:
>>>>>           get_page(pdata);
>>>> Please notice this get_page() here.
>>>>
>>>>>           xdpf = xdp_convert_buff_to_frame(xdp);
>>>>> +        if (unlikely(!xdpf)) {
>>>>> +            trace_xdp_exception(queue->info->netdev, prog, act);
>>>>> +            break;
>>>>> +        }
>>> Do you mean that it would be better to move the get_page(pdata) call lower,
>>> after checking for NULL in xdpf, so that the reference count is only increased
>>> after a successful conversion?
>>
>> I think the error handling here is generally broken (or at least very
>> questionable).
>>
>> I suspect that in case of at least some errors the get_page() is leaking
>> even without this new patch.
>>
>> In case I'm wrong a comment reasoning why there is no leak should be
>> added.
>>
>>
>> Juergen
> 
> I think pdata is freed in xdp_return_frame_rx_napi() -> __xdp_return()

Agreed. But what if xennet_xdp_xmit() returns an error < 0?

In this case xdp_return_frame_rx_napi() won't be called.


Juergen

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3743 bytes --]

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

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

* Re: [PATCH] xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()
       [not found]           ` <8264519a-d58a-486e-b3c5-dba400658513@nppct.ru>
@ 2025-04-17 10:23             ` Jürgen Groß
  2025-04-17 11:19               ` Alexey
  0 siblings, 1 reply; 9+ messages in thread
From: Jürgen Groß @ 2025-04-17 10:23 UTC (permalink / raw)
  To: Alexey, Jakub Kicinski
  Cc: Stefano Stabellini, Oleksandr Tyshchenko, David S. Miller,
	Eric Dumazet, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend, xen-devel, netdev,
	linux-kernel, bpf, lvc-project, stable


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

On 17.04.25 12:06, Alexey wrote:
> 
> On 17.04.2025 11:51, Juergen Gross wrote:
>> On 17.04.25 10:45, Alexey wrote:
>>>
>>> On 17.04.2025 10:12, Jürgen Groß wrote:
>>>> On 17.04.25 09:00, Alexey wrote:
>>>>>
>>>>> On 17.04.2025 03:58, Jakub Kicinski wrote:
>>>>>> On Mon, 14 Apr 2025 18:34:01 +0000 Alexey Nepomnyashih wrote:
>>>>>>>           get_page(pdata);
>>>>>> Please notice this get_page() here.
>>>>>>
>>>>>>>           xdpf = xdp_convert_buff_to_frame(xdp);
>>>>>>> +        if (unlikely(!xdpf)) {
>>>>>>> + trace_xdp_exception(queue->info->netdev, prog, act);
>>>>>>> +            break;
>>>>>>> +        }
>>>>> Do you mean that it would be better to move the get_page(pdata) call lower,
>>>>> after checking for NULL in xdpf, so that the reference count is only increased
>>>>> after a successful conversion?
>>>>
>>>> I think the error handling here is generally broken (or at least very
>>>> questionable).
>>>>
>>>> I suspect that in case of at least some errors the get_page() is leaking
>>>> even without this new patch.
>>>>
>>>> In case I'm wrong a comment reasoning why there is no leak should be
>>>> added.
>>>>
>>>>
>>>> Juergen
>>>
>>> I think pdata is freed in xdp_return_frame_rx_napi() -> __xdp_return()
>>
>> Agreed. But what if xennet_xdp_xmit() returns an error < 0?
>>
>> In this case xdp_return_frame_rx_napi() won't be called.
>>
>>
>> Juergen
> 
> Agreed. There is no explicit freed pdata in the calling function
> xennet_get_responses(). Without this, the page referenced by pdata
> could be leaked.
> 
> I suggest:
 >
 >  	case XDP_TX:
 > -		get_page(pdata);
 >  		xdpf = xdp_convert_buff_to_frame(xdp);
 > +		if (unlikely(!xdpf)) {
 > +			trace_xdp_exception(queue->info->netdev, prog, act);
 > +			break;
 > +		}
 > +		get_page(pdata);
 >  		err = xennet_xdp_xmit(queue->info->netdev, 1, &xdpf, 0);
 >  		if (unlikely(!err))
 >  			xdp_return_frame_rx_napi(xdpf);
 > -		else if (unlikely(err < 0))
 > +		else if (unlikely(err < 0)) {
 >  			trace_xdp_exception(queue->info->netdev, prog, act);
 > +			xdp_return_frame_rx_napi(xdpf);
 > +		}

Could you please merge the two if () blocks, as they share the
call of xdp_return_frame_rx_napi() now? Something like:

if (unlikely(err <= 0)) {
	if (err < 0)
		trace_xdp_exception(queue->info->netdev, prog, act);
	xdp_return_frame_rx_napi(xdpf);
}

 >  		break;
 >  	case XDP_REDIRECT:
 >  		get_page(pdata);
 >  		err = xdp_do_redirect(queue->info->netdev, xdp, prog);
 >  		*need_xdp_flush = true;
 > -		if (unlikely(err))
 > +		if (unlikely(err)) {
 >  			trace_xdp_exception(queue->info->netdev, prog, act);
 > +			__xdp_return(page_address(pdata), &xdp->mem, true, xdp);
 > +		}
 >  		break;


Juergen

P.S.: please don't use HTML in emails

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3743 bytes --]

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

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

* Re: [PATCH] xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()
  2025-04-17 10:23             ` Jürgen Groß
@ 2025-04-17 11:19               ` Alexey
  2025-04-17 13:22                 ` Jürgen Groß
  0 siblings, 1 reply; 9+ messages in thread
From: Alexey @ 2025-04-17 11:19 UTC (permalink / raw)
  To: Jürgen Groß, Jakub Kicinski
  Cc: Stefano Stabellini, Oleksandr Tyshchenko, David S. Miller,
	Eric Dumazet, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend, xen-devel, netdev,
	linux-kernel, bpf, lvc-project, stable


On 17.04.2025 13:23, Jürgen Groß wrote:
> On 17.04.25 12:06, Alexey wrote:
>>
>> On 17.04.2025 11:51, Juergen Gross wrote:
>>> On 17.04.25 10:45, Alexey wrote:
>>>>
>>>> On 17.04.2025 10:12, Jürgen Groß wrote:
>>>>> On 17.04.25 09:00, Alexey wrote:
>>>>>>
>>>>>> On 17.04.2025 03:58, Jakub Kicinski wrote:
>>>>>>> On Mon, 14 Apr 2025 18:34:01 +0000 Alexey Nepomnyashih wrote:
>>>>>>>>           get_page(pdata);
>>>>>>> Please notice this get_page() here.
>>>>>>>
>>>>>>>>           xdpf = xdp_convert_buff_to_frame(xdp);
>>>>>>>> +        if (unlikely(!xdpf)) {
>>>>>>>> + trace_xdp_exception(queue->info->netdev, prog, act);
>>>>>>>> +            break;
>>>>>>>> +        }
>>>>>> Do you mean that it would be better to move the get_page(pdata) 
>>>>>> call lower,
>>>>>> after checking for NULL in xdpf, so that the reference count is 
>>>>>> only increased
>>>>>> after a successful conversion?
>>>>>
>>>>> I think the error handling here is generally broken (or at least very
>>>>> questionable).
>>>>>
>>>>> I suspect that in case of at least some errors the get_page() is 
>>>>> leaking
>>>>> even without this new patch.
>>>>>
>>>>> In case I'm wrong a comment reasoning why there is no leak should be
>>>>> added.
>>>>>
>>>>>
>>>>> Juergen
>>>>
>>>> I think pdata is freed in xdp_return_frame_rx_napi() -> __xdp_return()
>>>
>>> Agreed. But what if xennet_xdp_xmit() returns an error < 0?
>>>
>>> In this case xdp_return_frame_rx_napi() won't be called.
>>>
>>>
>>> Juergen
>>
>> Agreed. There is no explicit freed pdata in the calling function
>> xennet_get_responses(). Without this, the page referenced by pdata
>> could be leaked.
>>
>> I suggest:
>
> Could you please merge the two if () blocks, as they share the
> call of xdp_return_frame_rx_napi() now? Something like:
>
> if (unlikely(err <= 0)) {
>     if (err < 0)
>         trace_xdp_exception(queue->info->netdev, prog, act);
>     xdp_return_frame_rx_napi(xdpf);
> }
>
> Juergen
>
> P.S.: please don't use HTML in emails

I can't do this because xennet_xdp_xmit() can return a value > 0



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

* Re: [PATCH] xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()
  2025-04-17 11:19               ` Alexey
@ 2025-04-17 13:22                 ` Jürgen Groß
  0 siblings, 0 replies; 9+ messages in thread
From: Jürgen Groß @ 2025-04-17 13:22 UTC (permalink / raw)
  To: Alexey, Jakub Kicinski
  Cc: Stefano Stabellini, Oleksandr Tyshchenko, David S. Miller,
	Eric Dumazet, Paolo Abeni, Alexei Starovoitov, Daniel Borkmann,
	Jesper Dangaard Brouer, John Fastabend, xen-devel, netdev,
	linux-kernel, bpf, lvc-project, stable


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

On 17.04.25 13:19, Alexey wrote:
> 
> On 17.04.2025 13:23, Jürgen Groß wrote:
>> On 17.04.25 12:06, Alexey wrote:
>>>
>>> On 17.04.2025 11:51, Juergen Gross wrote:
>>>> On 17.04.25 10:45, Alexey wrote:
>>>>>
>>>>> On 17.04.2025 10:12, Jürgen Groß wrote:
>>>>>> On 17.04.25 09:00, Alexey wrote:
>>>>>>>
>>>>>>> On 17.04.2025 03:58, Jakub Kicinski wrote:
>>>>>>>> On Mon, 14 Apr 2025 18:34:01 +0000 Alexey Nepomnyashih wrote:
>>>>>>>>>           get_page(pdata);
>>>>>>>> Please notice this get_page() here.
>>>>>>>>
>>>>>>>>>           xdpf = xdp_convert_buff_to_frame(xdp);
>>>>>>>>> +        if (unlikely(!xdpf)) {
>>>>>>>>> + trace_xdp_exception(queue->info->netdev, prog, act);
>>>>>>>>> +            break;
>>>>>>>>> +        }
>>>>>>> Do you mean that it would be better to move the get_page(pdata) call lower,
>>>>>>> after checking for NULL in xdpf, so that the reference count is only 
>>>>>>> increased
>>>>>>> after a successful conversion?
>>>>>>
>>>>>> I think the error handling here is generally broken (or at least very
>>>>>> questionable).
>>>>>>
>>>>>> I suspect that in case of at least some errors the get_page() is leaking
>>>>>> even without this new patch.
>>>>>>
>>>>>> In case I'm wrong a comment reasoning why there is no leak should be
>>>>>> added.
>>>>>>
>>>>>>
>>>>>> Juergen
>>>>>
>>>>> I think pdata is freed in xdp_return_frame_rx_napi() -> __xdp_return()
>>>>
>>>> Agreed. But what if xennet_xdp_xmit() returns an error < 0?
>>>>
>>>> In this case xdp_return_frame_rx_napi() won't be called.
>>>>
>>>>
>>>> Juergen
>>>
>>> Agreed. There is no explicit freed pdata in the calling function
>>> xennet_get_responses(). Without this, the page referenced by pdata
>>> could be leaked.
>>>
>>> I suggest:
>>
>> Could you please merge the two if () blocks, as they share the
>> call of xdp_return_frame_rx_napi() now? Something like:
>>
>> if (unlikely(err <= 0)) {
>>     if (err < 0)
>>         trace_xdp_exception(queue->info->netdev, prog, act);
>>     xdp_return_frame_rx_napi(xdpf);
>> }
>>
>> Juergen
>>
>> P.S.: please don't use HTML in emails
> 
> I can't do this because xennet_xdp_xmit() can return a value > 0

Yes, this is what the "if (unlikely(err <= 0))" is for.


Juergen

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3743 bytes --]

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

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

end of thread, other threads:[~2025-04-17 13:22 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-14 18:34 [PATCH] xen-netfront: handle NULL returned by xdp_convert_buff_to_frame() Alexey Nepomnyashih
2025-04-17  0:58 ` Jakub Kicinski
2025-04-17  7:00   ` Alexey
2025-04-17  7:12     ` Jürgen Groß
2025-04-17  8:45       ` Alexey
2025-04-17  8:51         ` Juergen Gross
     [not found]           ` <8264519a-d58a-486e-b3c5-dba400658513@nppct.ru>
2025-04-17 10:23             ` Jürgen Groß
2025-04-17 11:19               ` Alexey
2025-04-17 13:22                 ` Jürgen Groß

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