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)
committerSasha Levin <sashal@kernel.org>
Thu, 26 Aug 2021 12:35:41 +0000 (08:35 -0400)
commitd2ab5491de91a7f70e8474e70378086c29bd7e94
tree51002cab8c47023f950e1fa9a8646e4e5f380207
parent44f454a146c07617b00772f93400aa227f869121
iommu: Check if group is NULL before remove device

[ Upstream commit 5aa95d8834e07907e64937d792c12ffef7fb271f ]

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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/iommu/iommu.c