mirror-linux/drivers/ufs/host
André Draszik f8fb2403dd scsi: ufs: core: Fix use-after free in init error and remove paths
devm_blk_crypto_profile_init() registers a cleanup handler to run when
the associated (platform-) device is being released. For UFS, the
crypto private data and pointers are stored as part of the ufs_hba's
data structure 'struct ufs_hba::crypto_profile'. This structure is
allocated as part of the underlying ufshcd and therefore Scsi_host
allocation.

During driver release or during error handling in ufshcd_pltfrm_init(),
this structure is released as part of ufshcd_dealloc_host() before the
(platform-) device associated with the crypto call above is released.
Once this device is released, the crypto cleanup code will run, using
the just-released 'struct ufs_hba::crypto_profile'. This causes a
use-after-free situation:

  Call trace:
   kfree+0x60/0x2d8 (P)
   kvfree+0x44/0x60
   blk_crypto_profile_destroy_callback+0x28/0x70
   devm_action_release+0x1c/0x30
   release_nodes+0x6c/0x108
   devres_release_all+0x98/0x100
   device_unbind_cleanup+0x20/0x70
   really_probe+0x218/0x2d0

In other words, the initialisation code flow is:

  platform-device probe
    ufshcd_pltfrm_init()
      ufshcd_alloc_host()
        scsi_host_alloc()
          allocation of struct ufs_hba
          creation of scsi-host devices
    devm_blk_crypto_profile_init()
      devm registration of cleanup handler using platform-device

and during error handling of ufshcd_pltfrm_init() or during driver
removal:

  ufshcd_dealloc_host()
    scsi_host_put()
      put_device(scsi-host)
        release of struct ufs_hba
  put_device(platform-device)
    crypto cleanup handler

To fix this use-after free, change ufshcd_alloc_host() to register a
devres action to automatically cleanup the underlying SCSI device on
ufshcd destruction, without requiring explicit calls to
ufshcd_dealloc_host(). This way:

    * the crypto profile and all other ufs_hba-owned resources are
      destroyed before SCSI (as they've been registered after)
    * a memleak is plugged in tc-dwc-g210-pci.c remove() as a
      side-effect
    * EXPORT_SYMBOL_GPL(ufshcd_dealloc_host) can be removed fully as
      it's not needed anymore
    * no future drivers using ufshcd_alloc_host() could ever forget
      adding the cleanup

Fixes: cb77cb5abe ("blk-crypto: rename blk_keyslot_manager to blk_crypto_profile")
Fixes: d76d9d7d10 ("scsi: ufs: use devm_blk_ksm_init()")
Cc: stable@vger.kernel.org
Signed-off-by: André Draszik <andre.draszik@linaro.org>
Link: https://lore.kernel.org/r/20250124-ufshcd-fix-v4-1-c5d0144aae59@linaro.org
Reviewed-by: Bean Huo <beanhuo@micron.com>
Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Acked-by: Eric Biggers <ebiggers@kernel.org>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2025-02-03 17:20:01 -05:00
..
Kconfig scsi: ufs: ufs-mediatek: Add dependency for RESET_CONTROLLER 2023-07-05 21:40:53 -04:00
Makefile scsi: ufs: ufs-qcom: Switch to the new ICE API 2023-06-16 12:04:31 -04:00
cdns-pltfrm.c Merge patch series "scsi: ufs: Bug fixes for ufs core and platform drivers" 2024-11-20 22:04:15 -05:00
tc-dwc-g210-pci.c scsi: ufs: Replace deprecated PCI functions 2024-11-06 20:45:32 -05:00
tc-dwc-g210-pltfrm.c Merge patch series "scsi: ufs: Bug fixes for ufs core and platform drivers" 2024-11-20 22:04:15 -05:00
tc-dwc-g210.c scsi: ufs: Fix kernel-doc headers 2023-07-31 15:17:50 -04:00
tc-dwc-g210.h
ti-j721e-ufs.c scsi: ufs: Switch back to struct platform_driver::remove() 2024-11-20 21:46:27 -05:00
ufs-exynos.c Merge branch '6.13/scsi-queue' into 6.13/scsi-fixes 2024-12-02 12:36:18 -05:00
ufs-exynos.h scsi: ufs: exynos: Remove superfluous function parameter 2024-11-02 21:13:00 -04:00
ufs-hisi.c Merge patch series "scsi: ufs: Bug fixes for ufs core and platform drivers" 2024-11-20 22:04:15 -05:00
ufs-hisi.h
ufs-mediatek-sip.h scsi: ufs: mediatek: Support rtff in PM flow 2024-03-25 21:03:00 -04:00
ufs-mediatek-trace.h
ufs-mediatek.c Merge patch series "scsi: ufs: Bug fixes for ufs core and platform drivers" 2024-11-20 22:04:15 -05:00
ufs-mediatek.h scsi: ufs: core: Maximum RTT supported by the host driver 2024-05-30 20:34:38 -04:00
ufs-qcom.c Merge branch '6.13/scsi-fixes' into 6.14/scsi-staging 2025-01-10 15:20:30 -05:00
ufs-qcom.h scsi: ufs: qcom: Power down the controller/device during system suspend for SM8550/SM8650 SoCs 2025-01-02 13:00:16 -05:00
ufs-renesas.c Merge patch series "scsi: ufs: Bug fixes for ufs core and platform drivers" 2024-11-20 22:04:15 -05:00
ufs-sprd.c Merge patch series "scsi: ufs: Bug fixes for ufs core and platform drivers" 2024-11-20 22:04:15 -05:00
ufs-sprd.h
ufshcd-dwc.c scsi: ufs: Fix kernel-doc headers 2023-07-31 15:17:50 -04:00
ufshcd-dwc.h
ufshcd-pci.c scsi: ufs: core: Fix use-after free in init error and remove paths 2025-02-03 17:20:01 -05:00
ufshcd-pltfrm.c scsi: ufs: core: Fix use-after free in init error and remove paths 2025-02-03 17:20:01 -05:00
ufshcd-pltfrm.h scsi: ufs: pltfrm: Disable runtime PM during removal of glue drivers 2024-11-20 22:03:05 -05:00
ufshci-dwc.h