usb: xhci: Add error handling in xhci_map_urb_for_dma

Message ID 20240228083343.3101303-1-quic_prashk@quicinc.com
State New
Headers
Series usb: xhci: Add error handling in xhci_map_urb_for_dma |

Commit Message

Prashanth K Feb. 28, 2024, 8:33 a.m. UTC
  Currently xhci_map_urb_for_dma() creates a temporary buffer
and copies the SG list to the new linear buffer. But if the
kzalloc_node() fails, then the following sg_pcopy_to_buffer()
can lead to crash since it tries to memcpy to NULL pointer.
So return -EAGAIN if kzalloc returns null pointer.

Cc: <stable@vger.kernel.org> # 5.11
Fixes: 2017a1e58472 ("usb: xhci: Use temporary buffer to consolidate SG")
Signed-off-by: Prashanth K <quic_prashk@quicinc.com>
---
 drivers/usb/host/xhci.c | 3 +++
 1 file changed, 3 insertions(+)
  

Comments

Sergei Shtylyov Feb. 28, 2024, 9:01 a.m. UTC | #1
On 2/28/24 11:33 AM, Prashanth K wrote:

> Currently xhci_map_urb_for_dma() creates a temporary buffer
> and copies the SG list to the new linear buffer. But if the
> kzalloc_node() fails, then the following sg_pcopy_to_buffer()
> can lead to crash since it tries to memcpy to NULL pointer.
> So return -EAGAIN if kzalloc returns null pointer.
> 
> Cc: <stable@vger.kernel.org> # 5.11
> Fixes: 2017a1e58472 ("usb: xhci: Use temporary buffer to consolidate SG")
> Signed-off-by: Prashanth K <quic_prashk@quicinc.com>
> ---
>  drivers/usb/host/xhci.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
> index c057c42c36f4..0597a60bec34 100644
> --- a/drivers/usb/host/xhci.c
> +++ b/drivers/usb/host/xhci.c
> @@ -1218,6 +1218,9 @@ static int xhci_map_temp_buffer(struct usb_hcd *hcd, struct urb *urb)
>  	temp = kzalloc_node(buf_len, GFP_ATOMIC,
>  			    dev_to_node(hcd->self.sysdev));
>  

   I don't think we need an empty line here.

> +	if (!temp)
> +		return -EAGAIN;

   Not -ENOMEM?

> +
>  	if (usb_urb_dir_out(urb))
>  		sg_pcopy_to_buffer(urb->sg, urb->num_sgs,
>  				   temp, buf_len, 0);

MBR, Sergey
  
Prashanth K Feb. 28, 2024, 10:12 a.m. UTC | #2
On 28-02-24 02:31 pm, Sergei Shtylyov wrote:
> On 2/28/24 11:33 AM, Prashanth K wrote:
> 
>> Currently xhci_map_urb_for_dma() creates a temporary buffer
>> and copies the SG list to the new linear buffer. But if the
>> kzalloc_node() fails, then the following sg_pcopy_to_buffer()
>> can lead to crash since it tries to memcpy to NULL pointer.
>> So return -EAGAIN if kzalloc returns null pointer.
>>
>> Cc: <stable@vger.kernel.org> # 5.11
>> Fixes: 2017a1e58472 ("usb: xhci: Use temporary buffer to consolidate SG")
>> Signed-off-by: Prashanth K <quic_prashk@quicinc.com>
>> ---
>>   drivers/usb/host/xhci.c | 3 +++
>>   1 file changed, 3 insertions(+)
>>
>> diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
>> index c057c42c36f4..0597a60bec34 100644
>> --- a/drivers/usb/host/xhci.c
>> +++ b/drivers/usb/host/xhci.c
>> @@ -1218,6 +1218,9 @@ static int xhci_map_temp_buffer(struct usb_hcd *hcd, struct urb *urb)
>>   	temp = kzalloc_node(buf_len, GFP_ATOMIC,
>>   			    dev_to_node(hcd->self.sysdev));
>>   
> 
>     I don't think we need an empty line here.
> 
>> +	if (!temp)
>> +		return -EAGAIN;
> 
>     Not -ENOMEM?
Yea that sounds better, will update in next patch.
Thanks!
  

Patch

diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index c057c42c36f4..0597a60bec34 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -1218,6 +1218,9 @@  static int xhci_map_temp_buffer(struct usb_hcd *hcd, struct urb *urb)
 	temp = kzalloc_node(buf_len, GFP_ATOMIC,
 			    dev_to_node(hcd->self.sysdev));
 
+	if (!temp)
+		return -EAGAIN;
+
 	if (usb_urb_dir_out(urb))
 		sg_pcopy_to_buffer(urb->sg, urb->num_sgs,
 				   temp, buf_len, 0);