drm/nouveau/mmu: fix use-after-free bug in nvkm_vmm_pfn_map

Message ID 20221029074654.203153-1-zyytlz.wz@163.com
State New
Headers
Series drm/nouveau/mmu: fix use-after-free bug in nvkm_vmm_pfn_map |

Commit Message

Zheng Wang Oct. 29, 2022, 7:46 a.m. UTC
  If it failed in kzalloc, vma will be freed in nvkm_vmm_node_merge.
The later use of vma will casue use after free.

Reported-by: Zheng Wang <hackerzheng666@gmail.com>
Reported-by: Zhuorao Yang <alex000young@gmail.com>

Fix it by returning to upper caller as soon as error occurs.

Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
---
 drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
  

Comments

Zheng Hacker Nov. 2, 2022, 10:35 a.m. UTC | #1
Friendly Ping :)
  
Lyude Paul March 7, 2023, 12:04 a.m. UTC | #2
Reviewed-by: Lyude Paul <lyude@redhat.com>

Will push upstream in a moment

On Sat, 2022-10-29 at 15:46 +0800, Zheng Wang wrote:
> If it failed in kzalloc, vma will be freed in nvkm_vmm_node_merge.
> The later use of vma will casue use after free.
> 
> Reported-by: Zheng Wang <hackerzheng666@gmail.com>
> Reported-by: Zhuorao Yang <alex000young@gmail.com>
> 
> Fix it by returning to upper caller as soon as error occurs.
> 
> Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
> ---
>  drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c
> index ae793f400ba1..04befd28f80b 100644
> --- a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c
> +++ b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c
> @@ -1272,8 +1272,7 @@ nvkm_vmm_pfn_map(struct nvkm_vmm *vmm, u8 shift, u64 addr, u64 size, u64 *pfn)
>  						       page -
>  						       vmm->func->page, map);
>  			if (WARN_ON(!tmp)) {
> -				ret = -ENOMEM;
> -				goto next;
> +				return -ENOMEM;
>  			}
>  
>  			if ((tmp->mapped = map))
  
Lyude Paul March 7, 2023, 12:11 a.m. UTC | #3
Actually - could you resend this with dri-devel@lists.freedesktop.org added to
the cc list just to make patchwork happy?

On Sat, 2022-10-29 at 15:46 +0800, Zheng Wang wrote:
> If it failed in kzalloc, vma will be freed in nvkm_vmm_node_merge.
> The later use of vma will casue use after free.
> 
> Reported-by: Zheng Wang <hackerzheng666@gmail.com>
> Reported-by: Zhuorao Yang <alex000young@gmail.com>
> 
> Fix it by returning to upper caller as soon as error occurs.
> 
> Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
> ---
>  drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c
> index ae793f400ba1..04befd28f80b 100644
> --- a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c
> +++ b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c
> @@ -1272,8 +1272,7 @@ nvkm_vmm_pfn_map(struct nvkm_vmm *vmm, u8 shift, u64 addr, u64 size, u64 *pfn)
>  						       page -
>  						       vmm->func->page, map);
>  			if (WARN_ON(!tmp)) {
> -				ret = -ENOMEM;
> -				goto next;
> +				return -ENOMEM;
>  			}
>  
>  			if ((tmp->mapped = map))
  
Zheng Hacker March 7, 2023, 4:24 a.m. UTC | #4
Hi,

This bug has been proved to be a false positive. So there is no need
to make the patch.

Thanks,
Zheng

Lyude Paul <lyude@redhat.com> 于2023年3月7日周二 08:11写道:
>
> Actually - could you resend this with dri-devel@lists.freedesktop.org added to
> the cc list just to make patchwork happy?
>
> On Sat, 2022-10-29 at 15:46 +0800, Zheng Wang wrote:
> > If it failed in kzalloc, vma will be freed in nvkm_vmm_node_merge.
> > The later use of vma will casue use after free.
> >
> > Reported-by: Zheng Wang <hackerzheng666@gmail.com>
> > Reported-by: Zhuorao Yang <alex000young@gmail.com>
> >
> > Fix it by returning to upper caller as soon as error occurs.
> >
> > Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
> > ---
> >  drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c | 3 +--
> >  1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c
> > index ae793f400ba1..04befd28f80b 100644
> > --- a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c
> > +++ b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c
> > @@ -1272,8 +1272,7 @@ nvkm_vmm_pfn_map(struct nvkm_vmm *vmm, u8 shift, u64 addr, u64 size, u64 *pfn)
> >                                                      page -
> >                                                      vmm->func->page, map);
> >                       if (WARN_ON(!tmp)) {
> > -                             ret = -ENOMEM;
> > -                             goto next;
> > +                             return -ENOMEM;
> >                       }
> >
> >                       if ((tmp->mapped = map))
>
> --
> Cheers,
>  Lyude Paul (she/her)
>  Software Engineer at Red Hat
>
  

Patch

diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c
index ae793f400ba1..04befd28f80b 100644
--- a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c
+++ b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c
@@ -1272,8 +1272,7 @@  nvkm_vmm_pfn_map(struct nvkm_vmm *vmm, u8 shift, u64 addr, u64 size, u64 *pfn)
 						       page -
 						       vmm->func->page, map);
 			if (WARN_ON(!tmp)) {
-				ret = -ENOMEM;
-				goto next;
+				return -ENOMEM;
 			}
 
 			if ((tmp->mapped = map))