Hi Greg, On Mon, 22 Aug 2022 at 18:42, Jens Wiklander <jens.wiklander@xxxxxxxxxx> wrote: > > commit 573ae4f13f630d6660008f1974c0a8a29c30e18a upstream. > > With special lengths supplied by user space, tee_shm_register() has > an integer overflow when calculating the number of pages covered by a > supplied user space memory region. > > This may cause pin_user_pages_fast() to do a NULL pointer dereference. > > Fix this by adding an an explicit call to access_ok() in > tee_ioctl_shm_register() to catch an invalid user space address early. > > Fixes: 033ddf12bcf5 ("tee: add register user memory") > Cc: stable@xxxxxxxxxxxxxxx # 5.4 > Cc: stable@xxxxxxxxxxxxxxx # 5.10 > Reported-by: Nimish Mishra <neelam.nimish@xxxxxxxxx> > Reported-by: Anirban Chakraborty <ch.anirban00727@xxxxxxxxx> > Reported-by: Debdeep Mukhopadhyay <debdeep.mukhopadhyay@xxxxxxxxx> > Suggested-by: Jerome Forissier <jerome.forissier@xxxxxxxxxx> > Signed-off-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> > [JW: backport to stable 5.4 and 5.10 + update commit message] > Signed-off-by: Jens Wiklander <jens.wiklander@xxxxxxxxxx> > --- > drivers/tee/tee_core.c | 3 +++ > 1 file changed, 3 insertions(+) > The v5.15 backport [1] for this fix has broken the kernel consumers for tee_shm_register(), the trusted keys driver is one of them reported here [2]. We need to fix that up with the following change [3]. Would you like to revert the backport and apply the correct one or should I prepare a fix patch for the following [3]? [1] https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?h=linux-5.15.y&id=c12f0e6126ad223806a365084e86370511654bf1 [2] https://github.com/OP-TEE/optee_os/issues/5624 [3] diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index 3fc426dad2df..d5c4fcd8733d 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -334,6 +334,9 @@ tee_ioctl_shm_register(struct tee_context *ctx, if (data.flags) return -EINVAL; + if (!access_ok((void __user *)data.addr, data.length)) + return ERR_PTR(-EFAULT); + shm = tee_shm_register(ctx, data.addr, data.length, TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED); if (IS_ERR(shm)) diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index bd96ebb82c8e..6fb4400333fb 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -223,9 +223,6 @@ 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); - mutex_lock(&teedev->mutex); shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL); mutex_unlock(&teedev->mutex); -Sumit > diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c > index a7ccd4d2bd10..2db144d2d26f 100644 > --- a/drivers/tee/tee_core.c > +++ b/drivers/tee/tee_core.c > @@ -182,6 +182,9 @@ tee_ioctl_shm_register(struct tee_context *ctx, > if (data.flags) > return -EINVAL; > > + if (!access_ok((void __user *)(unsigned long)data.addr, data.length)) > + return -EFAULT; > + > shm = tee_shm_register(ctx, data.addr, data.length, > TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED); > if (IS_ERR(shm)) > -- > 2.31.1 >