diff mbox series

drm/msm: Fix use-after-free in msm_gem with carveout

Message ID 20201126082817.1242995-1-iskren.chernev@gmail.com (mailing list archive)
State Superseded
Headers show
Series drm/msm: Fix use-after-free in msm_gem with carveout | expand

Commit Message

Iskren Chernev Nov. 26, 2020, 8:28 a.m. UTC
When using gem with vram carveout the page allocation is managed via
drm_mm. The necessary drm_mm_node is allocated in add_vma, but it freed
before the drm_mm_node has been deallocated leading to use-after-free on
every single vram allocation.

Currently put_iova is called before free_object.

	put_iova -> del_vma -> kfree(vma) // vma holds drm_mm_node

	free_object -> put_pages -> put_pages_vram
				 -> drm_mm_remove_node

It looks like del_vma does nothing else other than freeing the vma
object and removing it from it's list, so delaying the deletion should
be harmless.

This patch splits put_iova in put_iova_spaces and put_iova_vmas, so the
vma can be freed after the mm_node has been deallocated with the mm.

Signed-off-by: Iskren Chernev <iskren.chernev@gmail.com>
---
 drivers/gpu/drm/msm/msm_gem.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)


base-commit: 6147c83fd749d19a0d3ccc2f64d12138ab010b47

Comments

kernel test robot Nov. 26, 2020, 11:28 a.m. UTC | #1
Hi Iskren,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on next-20201125]
[also build test ERROR on 6147c83fd749d19a0d3ccc2f64d12138ab010b47]
[cannot apply to drm-intel/for-linux-next drm-tip/drm-tip linus/master robclark/msm-next v5.10-rc5 v5.10-rc4 v5.10-rc3 v5.10-rc5]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Iskren-Chernev/drm-msm-Fix-use-after-free-in-msm_gem-with-carveout/20201126-163426
base:    9d3e48f20e1159a7bb2ff5de96594b6375157fe0
config: arm-defconfig (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/0day-ci/linux/commit/b08a39143f118f378b9fbb9ccfa03999dc419b1c
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Iskren-Chernev/drm-msm-Fix-use-after-free-in-msm_gem-with-carveout/20201126-163426
        git checkout b08a39143f118f378b9fbb9ccfa03999dc419b1c
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arm 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   In file included from arch/arm/include/asm/bug.h:60,
                    from include/linux/bug.h:5,
                    from include/linux/thread_info.h:12,
                    from include/asm-generic/current.h:5,
                    from ./arch/arm/include/generated/asm/current.h:1,
                    from include/linux/sched.h:12,
                    from include/linux/ratelimit.h:6,
                    from include/linux/dev_printk.h:16,
                    from include/linux/device.h:15,
                    from include/linux/dma-mapping.h:7,
                    from include/linux/dma-map-ops.h:9,
                    from drivers/gpu/drm/msm/msm_gem.c:7:
   drivers/gpu/drm/msm/msm_gem.c: In function 'put_iova_vmas':
>> drivers/gpu/drm/msm/msm_gem.c:380:35: error: 'struct msm_gem_object' has no member named 'lock'
     380 |  WARN_ON(!mutex_is_locked(&msm_obj->lock));
         |                                   ^~
   include/asm-generic/bug.h:119:25: note: in definition of macro 'WARN_ON'
     119 |  int __ret_warn_on = !!(condition);    \
         |                         ^~~~~~~~~
   drivers/gpu/drm/msm/msm_gem.c: In function 'msm_gem_free_object':
>> drivers/gpu/drm/msm/msm_gem.c:983:2: error: implicit declaration of function 'put_iova_vma'; did you mean 'put_iova_vmas'? [-Werror=implicit-function-declaration]
     983 |  put_iova_vma(obj);
         |  ^~~~~~~~~~~~
         |  put_iova_vmas
   cc1: some warnings being treated as errors

vim +380 drivers/gpu/drm/msm/msm_gem.c

   372	
   373	/* Called with msm_obj locked */
   374	static void
   375	put_iova_vmas(struct drm_gem_object *obj)
   376	{
   377		struct msm_gem_object *msm_obj = to_msm_bo(obj);
   378		struct msm_gem_vma *vma, *tmp;
   379	
 > 380		WARN_ON(!mutex_is_locked(&msm_obj->lock));
   381	
   382		list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) {
   383			del_vma(vma);
   384		}
   385	}
   386	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/drivers/gpu/drm/msm/msm_gem.c b/drivers/gpu/drm/msm/msm_gem.c
index 15715a156620f..b83247202ea5d 100644
--- a/drivers/gpu/drm/msm/msm_gem.c
+++ b/drivers/gpu/drm/msm/msm_gem.c
@@ -355,18 +355,31 @@  static void del_vma(struct msm_gem_vma *vma)
 
 /* Called with msm_obj locked */
 static void
-put_iova(struct drm_gem_object *obj)
+put_iova_spaces(struct drm_gem_object *obj)
 {
 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
-	struct msm_gem_vma *vma, *tmp;
+	struct msm_gem_vma *vma;
 
 	WARN_ON(!msm_gem_is_locked(obj));
 
-	list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) {
+	list_for_each_entry(vma, &msm_obj->vmas, list) {
 		if (vma->aspace) {
 			msm_gem_purge_vma(vma->aspace, vma);
 			msm_gem_close_vma(vma->aspace, vma);
 		}
+	}
+}
+
+/* Called with msm_obj locked */
+static void
+put_iova_vmas(struct drm_gem_object *obj)
+{
+	struct msm_gem_object *msm_obj = to_msm_bo(obj);
+	struct msm_gem_vma *vma, *tmp;
+
+	WARN_ON(!mutex_is_locked(&msm_obj->lock));
+
+	list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) {
 		del_vma(vma);
 	}
 }
@@ -688,12 +701,14 @@  void msm_gem_purge(struct drm_gem_object *obj)
 	WARN_ON(!is_purgeable(msm_obj));
 	WARN_ON(obj->import_attach);
 
-	put_iova(obj);
+	put_iova_spaces(obj);
 
 	msm_gem_vunmap(obj);
 
 	put_pages(obj);
 
+	put_iova_vmas(obj);
+
 	msm_obj->madv = __MSM_MADV_PURGED;
 
 	drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
@@ -942,7 +957,7 @@  void msm_gem_free_object(struct drm_gem_object *obj)
 
 	msm_gem_lock(obj);
 
-	put_iova(obj);
+	put_iova_spaces(obj);
 
 	if (obj->import_attach) {
 		WARN_ON(msm_obj->vaddr);
@@ -965,6 +980,8 @@  void msm_gem_free_object(struct drm_gem_object *obj)
 		msm_gem_unlock(obj);
 	}
 
+	put_iova_vma(obj);
+
 	drm_gem_object_release(obj);
 
 	kfree(msm_obj);