diff mbox series

rtw88: Fix out-of-bounds write

Message ID 20210711141634.6133-1-len.baker@gmx.com
State New
Headers show
Series rtw88: Fix out-of-bounds write | expand

Commit Message

Len Baker July 11, 2021, 2:16 p.m. UTC
In the rtw_pci_init_rx_ring function the "if (len > TRX_BD_IDX_MASK)"
statement guarantees that len is less than or equal to GENMASK(11, 0) or
in other words that len is less than or equal to 4095. However the
rx_ring->buf has a size of RTK_MAX_RX_DESC_NUM (defined as 512). This
way it is possible an out-of-bounds write in the for statement due to
the i variable can exceed the rx_ring->buff size.

Fix it using the ARRAY_SIZE macro.

Cc: stable@vger.kernel.org
Addresses-Coverity-ID: 1461515 ("Out-of-bounds write")
Fixes: e3037485c68ec ("rtw88: new Realtek 802.11ac driver")
Signed-off-by: Len Baker <len.baker@gmx.com>
---
 drivers/net/wireless/realtek/rtw88/pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--
2.25.1

Comments

Ping-Ke Shih July 12, 2021, 1:43 a.m. UTC | #1
> -----Original Message-----
> From: Len Baker [mailto:len.baker@gmx.com]
> Sent: Sunday, July 11, 2021 10:17 PM
> To: Yan-Hsuan Chuang; Kalle Valo; David S. Miller; Jakub Kicinski
> Cc: Len Baker; Stanislaw Gruszka; Brian Norris; linux-wireless@vger.kernel.org; netdev@vger.kernel.org;
> linux-kernel@vger.kernel.org; stable@vger.kernel.org
> Subject: [PATCH] rtw88: Fix out-of-bounds write
> 
> In the rtw_pci_init_rx_ring function the "if (len > TRX_BD_IDX_MASK)"
> statement guarantees that len is less than or equal to GENMASK(11, 0) or
> in other words that len is less than or equal to 4095. However the
> rx_ring->buf has a size of RTK_MAX_RX_DESC_NUM (defined as 512). This
> way it is possible an out-of-bounds write in the for statement due to
> the i variable can exceed the rx_ring->buff size.
> 
> Fix it using the ARRAY_SIZE macro.
> 
> Cc: stable@vger.kernel.org
> Addresses-Coverity-ID: 1461515 ("Out-of-bounds write")
> Fixes: e3037485c68ec ("rtw88: new Realtek 802.11ac driver")
> Signed-off-by: Len Baker <len.baker@gmx.com>
> ---
>  drivers/net/wireless/realtek/rtw88/pci.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/wireless/realtek/rtw88/pci.c b/drivers/net/wireless/realtek/rtw88/pci.c
> index e7d17ab8f113..b9d8c049e776 100644
> --- a/drivers/net/wireless/realtek/rtw88/pci.c
> +++ b/drivers/net/wireless/realtek/rtw88/pci.c
> @@ -280,7 +280,7 @@ static int rtw_pci_init_rx_ring(struct rtw_dev *rtwdev,

I think "if (len > TRX_BD_IDX_MASK)" you mentioned is

	if (len > TRX_BD_IDX_MASK) {
		rtw_err(rtwdev, "len %d exceeds maximum RX entries\n", len);
		return -EINVAL;
	}

This statement is used to ensure the length doesn't exceed hardware capability.

To prevent the 'len' argument from exceeding the array size of rx_ring->buff, I
suggest to add another checking statement, like

	if (len > ARRAY_SIZE(rx_ring->buf)) {
		rtw_err(rtwdev, "len %d exceeds maximum RX ring buffer\n", len);
		return -EINVAL;
	}

But, I wonder if this a false alarm because 'len' is equal to ARRAY_SIZE(rx_ring->buf)
for now.


>  	}
>  	rx_ring->r.head = head;
> 
> -	for (i = 0; i < len; i++) {
> +	for (i = 0; i < ARRAY_SIZE(rx_ring->buf); i++) {
>  		skb = dev_alloc_skb(buf_sz);
>  		if (!skb) {
>  			allocated = i;
> --
> 2.25.1

--
Ping-Ke
Brian Norris July 12, 2021, 6:38 p.m. UTC | #2
On Sun, Jul 11, 2021 at 6:43 PM Pkshih <pkshih@realtek.com> wrote:
> > -----Original Message-----
> > From: Len Baker [mailto:len.baker@gmx.com]
> >
> > In the rtw_pci_init_rx_ring function the "if (len > TRX_BD_IDX_MASK)"
> > statement guarantees that len is less than or equal to GENMASK(11, 0) or
> > in other words that len is less than or equal to 4095. However the
> > rx_ring->buf has a size of RTK_MAX_RX_DESC_NUM (defined as 512). This
> > way it is possible an out-of-bounds write in the for statement due to
> > the i variable can exceed the rx_ring->buff size.
> >
> > Fix it using the ARRAY_SIZE macro.
> >
> > Cc: stable@vger.kernel.org
> > Addresses-Coverity-ID: 1461515 ("Out-of-bounds write")

Coverity seems to be giving a false warning here. I presume it's
taking the |len| comparison as proof that |len| might be as large as
TRX_BD_IDX_MASK, but as noted below, that's not really true; the |len|
comparison is really just dead code.

> > Fixes: e3037485c68ec ("rtw88: new Realtek 802.11ac driver")
> > Signed-off-by: Len Baker <len.baker@gmx.com>

> To prevent the 'len' argument from exceeding the array size of rx_ring->buff, I
> suggest to add another checking statement, like
>
>         if (len > ARRAY_SIZE(rx_ring->buf)) {
>                 rtw_err(rtwdev, "len %d exceeds maximum RX ring buffer\n", len);
>                 return -EINVAL;
>         }

That seems like a better idea, if we really need to patch anything.

> But, I wonder if this a false alarm because 'len' is equal to ARRAY_SIZE(rx_ring->buf)
> for now.

Or to the point: rtw_pci_init_rx_ring() is only ever called with a
fixed constant -- RTK_MAX_RX_DESC_NUM (i.e., 512) -- so the alleged
overflow cannot happen.

Brian
Len Baker July 16, 2021, 3:08 p.m. UTC | #3
On Mon, Jul 12, 2021 at 11:38:43AM -0700, Brian Norris wrote:
> On Sun, Jul 11, 2021 at 6:43 PM Pkshih <pkshih@realtek.com> wrote:
> > > -----Original Message-----
> > > From: Len Baker [mailto:len.baker@gmx.com]
> > >
> > > In the rtw_pci_init_rx_ring function the "if (len > TRX_BD_IDX_MASK)"
> > > statement guarantees that len is less than or equal to GENMASK(11, 0) or
> > > in other words that len is less than or equal to 4095. However the
> > > rx_ring->buf has a size of RTK_MAX_RX_DESC_NUM (defined as 512). This
> > > way it is possible an out-of-bounds write in the for statement due to
> > > the i variable can exceed the rx_ring->buff size.
> > >
> > > Fix it using the ARRAY_SIZE macro.
> > >
> > > Cc: stable@vger.kernel.org
> > > Addresses-Coverity-ID: 1461515 ("Out-of-bounds write")
>
> Coverity seems to be giving a false warning here. I presume it's
> taking the |len| comparison as proof that |len| might be as large as
> TRX_BD_IDX_MASK, but as noted below, that's not really true; the |len|
> comparison is really just dead code.

I agree.

> > > Fixes: e3037485c68ec ("rtw88: new Realtek 802.11ac driver")
> > > Signed-off-by: Len Baker <len.baker@gmx.com>
>
> > To prevent the 'len' argument from exceeding the array size of rx_ring->buff, I
> > suggest to add another checking statement, like
> >
> >         if (len > ARRAY_SIZE(rx_ring->buf)) {
> >                 rtw_err(rtwdev, "len %d exceeds maximum RX ring buffer\n", len);
> >                 return -EINVAL;
> >         }
>
> That seems like a better idea, if we really need to patch anything.

I think it is reasonable to protect any potencial overflow (for example, if
this function is used in the future with a parameter greater than 512). It
is better to be defensive in this case :)

> > But, I wonder if this a false alarm because 'len' is equal to ARRAY_SIZE(rx_ring->buf)
> > for now.
>
> Or to the point: rtw_pci_init_rx_ring() is only ever called with a
> fixed constant -- RTK_MAX_RX_DESC_NUM (i.e., 512) -- so the alleged
> overflow cannot happen.
>
> Brian

I will send a v2 for review.

Thanks,
Len
diff mbox series

Patch

diff --git a/drivers/net/wireless/realtek/rtw88/pci.c b/drivers/net/wireless/realtek/rtw88/pci.c
index e7d17ab8f113..b9d8c049e776 100644
--- a/drivers/net/wireless/realtek/rtw88/pci.c
+++ b/drivers/net/wireless/realtek/rtw88/pci.c
@@ -280,7 +280,7 @@  static int rtw_pci_init_rx_ring(struct rtw_dev *rtwdev,
 	}
 	rx_ring->r.head = head;

-	for (i = 0; i < len; i++) {
+	for (i = 0; i < ARRAY_SIZE(rx_ring->buf); i++) {
 		skb = dev_alloc_skb(buf_sz);
 		if (!skb) {
 			allocated = i;