From: Peter Chen <peter.chen@nxp.com>
To: Jia-Ju Bai <baijiaju1990@gmail.com>
Cc: "gregkh@linuxfoundation.org" <gregkh@linuxfoundation.org>,
"balbi@kernel.org" <balbi@kernel.org>,
"pawell@cadence.com" <pawell@cadence.com>,
"rogerq@ti.com" <rogerq@ti.com>,
"colin.king@canonical.com" <colin.king@canonical.com>,
"yuehaibing@huawei.com" <yuehaibing@huawei.com>,
"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] usb: cdns3: fix possible buffer overflow caused by bad DMA value
Date: Mon, 1 Jun 2020 04:10:24 +0000 [thread overview]
Message-ID: <20200601041048.GB13752@b29397-desktop> (raw)
In-Reply-To: <20200530032400.12743-1-baijiaju1990@gmail.com>
On 20-05-30 11:24:00, Jia-Ju Bai wrote:
> In cdns3_ep0_setup_phase():
> struct usb_ctrlrequest *ctrl = priv_dev->setup_buf;
>
> Because priv_dev->setup_buf (allocated in cdns3_gadget_start) is stored
> in DMA memory, and thus ctrl is a DMA value.
>
> cdns3_ep0_setup_phase()
> cdns3_ep0_standard_request(priv_dev, ctrl)
> cdns3_req_ep0_get_status(priv_dev, ctrl)
> index = cdns3_ep_addr_to_index(ctrl->wIndex);
> priv_ep = priv_dev->eps[index];
>
> cdns3_ep0_setup_phase()
> cdns3_ep0_standard_request(priv_dev, ctrl)
> cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 0)
> cdns3_ep0_feature_handle_endpoint(priv_dev, ctrl, set)
> index = cdns3_ep_addr_to_index(ctrl->wIndex);
> priv_ep = priv_dev->eps[index];
>
> In these cases, ctrl->wIndex can be be modified at anytime by malicious
> hardware, and thus a buffer overflow can occur when the code
> "priv_dev->eps[index]" is executed.
>
Did you see the setup buffer is overwritten before the setup handling is
finished?
> To fix these possible bugs, index is checked before being used.
I think the better fix is to use one additional buffer for struct
usb_ctrlrequest, and copy the dma_buf to it after setup packet
has received, then use the value stored in this buffer for later
operation, it could avoid quitting the code which is useful in fact.
Peter
>
> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> ---
> drivers/usb/cdns3/ep0.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/drivers/usb/cdns3/ep0.c b/drivers/usb/cdns3/ep0.c
> index e71240b386b4..0a80c7ade613 100644
> --- a/drivers/usb/cdns3/ep0.c
> +++ b/drivers/usb/cdns3/ep0.c
> @@ -265,6 +265,8 @@ static int cdns3_req_ep0_get_status(struct cdns3_device *priv_dev,
> return cdns3_ep0_delegate_req(priv_dev, ctrl);
> case USB_RECIP_ENDPOINT:
> index = cdns3_ep_addr_to_index(ctrl->wIndex);
> + if (index >= CDNS3_ENDPOINTS_MAX_COUNT)
> + return -EINVAL;
> priv_ep = priv_dev->eps[index];
>
> /* check if endpoint is stalled or stall is pending */
> @@ -388,6 +390,9 @@ static int cdns3_ep0_feature_handle_endpoint(struct cdns3_device *priv_dev,
> return 0;
>
> index = cdns3_ep_addr_to_index(ctrl->wIndex);
> + if (index >= CDNS3_ENDPOINTS_MAX_COUNT)
> + return -EINVAL;
> +
> priv_ep = priv_dev->eps[index];
>
> cdns3_select_ep(priv_dev, ctrl->wIndex);
> --
> 2.17.1
>
--
Thanks,
Peter Chen
next prev parent reply other threads:[~2020-06-01 4:10 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-30 3:24 [PATCH] usb: cdns3: fix possible buffer overflow caused by bad DMA value Jia-Ju Bai
2020-06-01 4:10 ` Peter Chen [this message]
2020-07-01 6:52 ` Felipe Balbi
2020-07-01 8:31 ` Peter Chen
-- strict thread matches above, loose matches on Subject: below --
2020-05-30 9:24 Markus Elfring
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200601041048.GB13752@b29397-desktop \
--to=peter.chen@nxp.com \
--cc=baijiaju1990@gmail.com \
--cc=balbi@kernel.org \
--cc=colin.king@canonical.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=pawell@cadence.com \
--cc=rogerq@ti.com \
--cc=yuehaibing@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox