Skip to contentSkip to conversation

usb: gadget: f_fs: Fix use after free issue as part of queue failure

PATCH Superseded Sriharsha Allenki 3 participants 6 messages Mar 26, 2020 USB

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: f_fs: Fix use after free issue as part of queue failure

In AIO case, the request is freed up if ep_queue fails. However, io_data->req still has the reference to this freed request. In the case of this failure if there is aio_cancel call on this io_data it will lead to an invalid dequeue operation and a potential use after free issue.
Fix this by setting the io_data->req to NULL when the request is freed as part of queue failure.

---
 drivers/usb/gadget/function/f_fs.c | 1 +
 1 file changed, 1 insertion(+)
drivers/usb/gadget/function/f_fs.c +1 -0
index 571917677d35..767f30b86645 100644
@@ -1120,6 +1120,7 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
 
 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
 		if (unlikely(ret)) {
+			io_data->req = NULL;
 			usb_ep_free_request(ep->ep, req);
 			goto error_lock;
 		}
Signature · 1 line
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
maintainer
#1 lore raw
Quoted 27 lines · Sriharsha Allenki

In AIO case, the request is freed up if ep_queue fails. However, io_data->req still has the reference to this freed request. In the case of this failure if there is aio_cancel call on this io_data it will lead to an invalid dequeue operation and a potential use after free issue.
Fix this by setting the io_data->req to NULL when the request is freed as part of queue failure.

---
 drivers/usb/gadget/function/f_fs.c | 1 +
 1 file changed, 1 insertion(+)
drivers/usb/gadget/function/f_fs.c +1 -0
index 571917677d35..767f30b86645 100644
@@ -1120,6 +1120,7 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
 
 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
 		if (unlikely(ret)) {
+			io_data->req = NULL;
 			usb_ep_free_request(ep->ep, req);
 			goto error_lock;
 		}
Signature · 1 line
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

What commit does this fix? Should it go to stable kernels, and if so, how far back?

thanks,

greg k-h

#2 lore raw

Hi Greg,

Quoted 20 lines · Greg KH
Quoted 19 lines · Sriharsha Allenki

In AIO case, the request is freed up if ep_queue fails. However, io_data->req still has the reference to this freed request. In the case of this failure if there is aio_cancel call on this io_data it will lead to an invalid dequeue operation and a potential use after free issue.
Fix this by setting the io_data->req to NULL when the request is freed as part of queue failure.

---
 drivers/usb/gadget/function/f_fs.c | 1 +
 1 file changed, 1 insertion(+)
drivers/usb/gadget/function/f_fs.c +0 -0
index 571917677d35..767f30b86645 100644
@@ -1120,6 +1120,7 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
 
Quoted 10 lines
Quoted 8 lines
drivers/usb/gadget/function/f_fs.c +1 -0
 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
 		if (unlikely(ret)) {
+			io_data->req = NULL;
 			usb_ep_free_request(ep->ep, req);
 			goto error_lock;
 		}
Signature · 1 line
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

What commit does this fix? Should it go to stable kernels, and if so, how far back?

The commit 2e4c7553cd6f ("usb: gadget: f_fs: add aio support") introduced the problem. It is good to have in stable kernels and it needs to go all theway back to linux-3.15.y.

Hope this information helps.

Quoted 4 lines

thanks,

greg k-h

Regards,
Sriharsha

maintainer
#3 lore raw
Quoted 33 lines · Sriharsha Allenki

Hi Greg,

Quoted 20 lines · Greg KH
Quoted 19 lines · Sriharsha Allenki

In AIO case, the request is freed up if ep_queue fails. However, io_data->req still has the reference to this freed request. In the case of this failure if there is aio_cancel call on this io_data it will lead to an invalid dequeue operation and a potential use after free issue.
Fix this by setting the io_data->req to NULL when the request is freed as part of queue failure.

---
 drivers/usb/gadget/function/f_fs.c | 1 +
 1 file changed, 1 insertion(+)
drivers/usb/gadget/function/f_fs.c +0 -0
index 571917677d35..767f30b86645 100644
@@ -1120,6 +1120,7 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
 
Quoted 10 lines
Quoted 8 lines
drivers/usb/gadget/function/f_fs.c +1 -0
 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
 		if (unlikely(ret)) {
+			io_data->req = NULL;
 			usb_ep_free_request(ep->ep, req);
 			goto error_lock;
 		}
Signature · 1 line
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

What commit does this fix? Should it go to stable kernels, and if so, how far back?

Quoted 5 lines

The commit 2e4c7553cd6f ("usb: gadget: f_fs: add aio support") introduced the problem. It is good to have in stable kernels and it needs to go all theway back to linux-3.15.y.

Hope this information helps.

Great, please resend this with the proper Fixes: and cc: stable... tag on it so that it gets picked up properly.

thanks,

greg k-h

#4 lore raw

Hi Greg,

Quoted 34 lines · Greg KH
Quoted 33 lines · Sriharsha Allenki

Hi Greg,

Quoted 30 lines · Greg KH
Quoted 27 lines · Sriharsha Allenki

In AIO case, the request is freed up if ep_queue fails. However, io_data->req still has the reference to this freed request. In the case of this failure if there is aio_cancel call on this io_data it will lead to an invalid dequeue operation and a potential use after free issue.
Fix this by setting the io_data->req to NULL when the request is freed as part of queue failure.

---
 drivers/usb/gadget/function/f_fs.c | 1 +
 1 file changed, 1 insertion(+)
drivers/usb/gadget/function/f_fs.c +1 -0
index 571917677d35..767f30b86645 100644
@@ -1120,6 +1120,7 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
 
 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
 		if (unlikely(ret)) {
+			io_data->req = NULL;
 			usb_ep_free_request(ep->ep, req);
 			goto error_lock;
 		}
Signature · 1 line
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

What commit does this fix? Should it go to stable kernels, and if so, how far back?

Quoted 7 lines
Quoted 5 lines

The commit 2e4c7553cd6f ("usb: gadget: f_fs: add aio support") introduced the problem. It is good to have in stable kernels and it needs to go all theway back to linux-3.15.y.

Hope this information helps.

Great, please resend this with the proper Fixes: and cc: stable... tag on it so that it gets picked up properly.

Thanks Greg for review. Sent out v2 patch with the updated tags.

Quoted 3 lines

thanks,

greg k-h

Regards,
Sriharsha

Reviewed-by on v1 1/1
#5 lore raw
In reply to Sriharsha Allenki
Quoted 9 lines · Sriharsha Allenki

In AIO case, the request is freed up if ep_queue fails. However, io_data->req still has the reference to this freed request. In the case of this failure if there is aio_cancel call on this io_data it will lead to an invalid dequeue operation and a potential use after free issue.
Fix this by setting the io_data->req to NULL when the request is freed as part of queue failure.

Peter

Quoted 18 lines
---
 drivers/usb/gadget/function/f_fs.c | 1 +
 1 file changed, 1 insertion(+)
drivers/usb/gadget/function/f_fs.c +1 -0
index 571917677d35..767f30b86645 100644
@@ -1120,6 +1120,7 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
 
 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
 		if (unlikely(ret)) {
+			io_data->req = NULL;
 			usb_ep_free_request(ep->ep, req);
 			goto error_lock;
 		}
Signature · 1 line
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Signature · 2 lines
Thanks,
Peter Chen