Re: [PATCH] IB/mthca: Fix possible uninitialized-variable access in mthca_SYS_EN()

From: Leon Romanovsky
Date: Wed Aug 11 2021 - 08:49:13 EST


On Wed, Aug 11, 2021 at 05:34:15AM -0700, Tuo Li wrote:
> The variable out is declared without initialization, and its address is
> passed to mthca_cmd_imm():
> ret = mthca_cmd_imm(dev, 0, &out, 0, 0, CMD_SYS_EN, CMD_TIME_CLASS_D);
>
> In this function, mthca_cmd_wait() or mthca_cmd_poll() will be called with
> the argument out_param, which is the address of the varialbe out. In these
> two called functions, mthca_cmd_post() will be called with *out_param,
> whose value comes from the uninitialized variable out.
> err = mthca_cmd_post(dev, in_param, out_param ? *out_param : 0, ...)
>
> In mthca_cmd_post(), mthca_cmd_post_dbell() or mthca_cmd_post_hcr() will
> be called, in which the value from the uninitialized varialble out may be
> used:
> __raw_writel((__force u32) cpu_to_be32(out_param >> 32), ptr + offs[3]);
>
> To fix this possible uninitialized-variable access, initialized out to 0
> at the begining of mthca_SYS_EN().
>
> Reported-by: TOTE Robot <oslab@xxxxxxxxxxxxxxx>
> Signed-off-by: Tuo Li <islituo@xxxxxxxxx>
> ---
> drivers/infiniband/hw/mthca/mthca_cmd.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)

Analysis is right, but I don't think that "out" value is important in
this flow.

Thanks

>
> diff --git a/drivers/infiniband/hw/mthca/mthca_cmd.c b/drivers/infiniband/hw/mthca/mthca_cmd.c
> index bdf5ed38de22..86584982e496 100644
> --- a/drivers/infiniband/hw/mthca/mthca_cmd.c
> +++ b/drivers/infiniband/hw/mthca/mthca_cmd.c
> @@ -635,7 +635,7 @@ void mthca_free_mailbox(struct mthca_dev *dev, struct mthca_mailbox *mailbox)
>
> int mthca_SYS_EN(struct mthca_dev *dev)
> {
> - u64 out;
> + u64 out = 0;
> int ret;
>
> ret = mthca_cmd_imm(dev, 0, &out, 0, 0, CMD_SYS_EN, CMD_TIME_CLASS_D);
> --
> 2.25.1
>