Re: [PATCH 5.10 540/545] tee: add overflow check in register_shm_helper()

Jens Wiklander
2022-08-22T13:04:05Z

Hi,

On Mon, Aug 22, 2022 at 1:15 PM Pavel Machek <pavel@denx.de> wrote:
>
> Hi!
>
> > From: Jens Wiklander <jens.wiklander@linaro.org>
> >
> > commit 573ae4f13f630d6660008f1974c0a8a29c30e18a upstream.
> >
> > With special lengths supplied by user space, register_shm_helper() has
> > an integer overflow when calculating the number of pages covered by a
> > supplied user space memory region.
> >
> > This causes internal_get_user_pages_fast() a helper function of
> > pin_user_pages_fast() to do a NULL pointer dereference:
>
> Maybe this needs fixing, but this fix adds a memory leak or two. Note
> the goto err, that needs to be done.

Thanks for bringing this up. I believe the best option is to take the
backport I just did for 5.4, it should apply cleanly on the 5.10
stable kernel too.

Greg, can you cherry-pick the 5.4 backport patch, or would you rather
have an explicit backport for this stable kernel?

Cheers,
Jens

>
> Best regards,
> Pavel
>
> Signed-off-by: Pavel Machek <pavel@denx.de>
>
> > +++ b/drivers/tee/tee_shm.c
> > @@ -222,6 +222,9 @@ struct tee_shm *tee_shm_register(struct
> > goto err;
> > }
> >
> > + if (!access_ok((void __user *)addr, length))
> > + return ERR_PTR(-EFAULT);
> > +
> > mutex_lock(&teedev->mutex);
> > shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
> > mutex_unlock(&teedev->mutex);
> >
>
>
> diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c
> index 6e662fb131d5..283fa50676a2 100644
> --- a/drivers/tee/tee_shm.c
> +++ b/drivers/tee/tee_shm.c
> @@ -222,8 +222,10 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
> goto err;
> }
>
> - if (!access_ok((void __user *)addr, length))
> - return ERR_PTR(-EFAULT);
> + if (!access_ok((void __user *)addr, length)) {
> + ret = ERR_PTR(-EFAULT);
> + goto err;
> + }
>
> mutex_lock(&teedev->mutex);
> shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
>
> --
> DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
> HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany