Re: [PATCH] net: atlantic: Avoid out-of-bounds indexing
From: Jakub Kicinski
Date: Sat Mar 05 2022 - 00:24:54 EST
On Fri, 4 Mar 2022 13:08:12 +0800 Kai-Heng Feng wrote:
> UBSAN warnings are observed on atlantic driver:
> [ 294.432996] UBSAN: array-index-out-of-bounds in /build/linux-Qow4fL/linux-5.15.0/drivers/net/ethernet/aquantia/atlantic/aq_nic.c:484:48
> [ 294.433695] index 8 is out of range for type 'aq_vec_s *[8]'
>
> The index is assigned right before breaking out the loop, so there's no actual
> deferencing happening.
I think you're underselling it. This codes does not compute the address
of the ring, it reads the ring pointer from an array. The description
reads like it was doing:
ring = &self->ring[i];
which would indeed be valid. What the code actually does is not.
Please repost with the commit message improved and a Fixes: tag(s)
pointing to commit(s) where the buggy code was introduced.
> So only use the index inside the loop to fix the issue.
>
> BugLink: https://bugs.launchpad.net/bugs/1958770
> Tested-by: Mario Limonciello <mario.limonciello@xxxxxxx>
> Signed-off-by: Kai-Heng Feng <kai.heng.feng@xxxxxxxxxxxxx>
> ---
> .../net/ethernet/aquantia/atlantic/aq_vec.c | 24 +++++++++----------
> 1 file changed, 12 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/net/ethernet/aquantia/atlantic/aq_vec.c b/drivers/net/ethernet/aquantia/atlantic/aq_vec.c
> index f4774cf051c97..6ab1f3212d246 100644
> --- a/drivers/net/ethernet/aquantia/atlantic/aq_vec.c
> +++ b/drivers/net/ethernet/aquantia/atlantic/aq_vec.c
> @@ -43,8 +43,8 @@ static int aq_vec_poll(struct napi_struct *napi, int budget)
> if (!self) {
> err = -EINVAL;
> } else {
> - for (i = 0U, ring = self->ring[0];
> - self->tx_rings > i; ++i, ring = self->ring[i]) {
> + for (i = 0U; self->tx_rings > i; ++i) {
> + ring = self->ring[i];
> u64_stats_update_begin(&ring[AQ_VEC_RX_ID].stats.rx.syncp);
> ring[AQ_VEC_RX_ID].stats.rx.polls++;
> u64_stats_update_end(&ring[AQ_VEC_RX_ID].stats.rx.syncp);