iommu: Check if group is NULL before remove device
authorFrank Wunderlich <frank-w@public-files.de>
Sat, 31 Jul 2021 07:47:37 +0000 (09:47 +0200)
committerJoerg Roedel <jroedel@suse.de>
Mon, 2 Aug 2021 14:22:00 +0000 (16:22 +0200)
commit5aa95d8834e07907e64937d792c12ffef7fb271f
treee6dbfb30dfa120b3783d2d49fcad406f744830df
parent0fbea680540108b09db7b26d9f4d24236d58a6ad
iommu: Check if group is NULL before remove device

If probe_device is failing, iommu_group is not initialized because
iommu_group_add_device is not reached, so freeing it will result
in NULL pointer access.

iommu_bus_init
  ->bus_iommu_probe
      ->probe_iommu_group in for each:/* return -22 in fail case */
          ->iommu_probe_device
              ->__iommu_probe_device       /* return -22 here.*/
                  -> ops->probe_device          /* return -22 here.*/
                  -> iommu_group_get_for_dev
                        -> ops->device_group
                        -> iommu_group_add_device //good case
  ->remove_iommu_group  //in fail case, it will remove group
     ->iommu_release_device
         ->iommu_group_remove_device // here we don't have group

In my case ops->probe_device (mtk_iommu_probe_device from
mtk_iommu_v1.c) is due to failing fwspec->ops mismatch.

Fixes: d72e31c93746 ("iommu: IOMMU Groups")
Signed-off-by: Frank Wunderlich <frank-w@public-files.de>
Link: https://lore.kernel.org/r/20210731074737.4573-1-linux@fw-web.de
Signed-off-by: Joerg Roedel <jroedel@suse.de>
drivers/iommu/iommu.c