Commit 4e3658e
scsi: ufs: core: Fix use-after free in init error and remove paths
BugLink: https://bugs.launchpad.net/bugs/2114239
commit f8fb240 upstream.
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: cb77cb5 ("blk-crypto: rename blk_keyslot_manager to blk_crypto_profile")
Fixes: d76d9d7 ("scsi: ufs: use devm_blk_ksm_init()")
Cc: [email protected]
Signed-off-by: André Draszik <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Reviewed-by: Bean Huo <[email protected]>
Reviewed-by: Manivannan Sadhasivam <[email protected]>
Acked-by: Eric Biggers <[email protected]>
Signed-off-by: Martin K. Petersen <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Noah Wager <[email protected]>
Signed-off-by: Mehmet Basaran <[email protected]>1 parent b7ea57c commit 4e3658e
File tree
4 files changed
+30
-32
lines changed- drivers/ufs
- core
- host
- include/ufs
4 files changed
+30
-32
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10418 | 10418 | | |
10419 | 10419 | | |
10420 | 10420 | | |
10421 | | - | |
10422 | | - | |
10423 | | - | |
10424 | | - | |
10425 | | - | |
10426 | | - | |
10427 | | - | |
10428 | | - | |
10429 | | - | |
10430 | | - | |
10431 | 10421 | | |
10432 | 10422 | | |
10433 | 10423 | | |
| |||
10446 | 10436 | | |
10447 | 10437 | | |
10448 | 10438 | | |
| 10439 | + | |
| 10440 | + | |
| 10441 | + | |
| 10442 | + | |
| 10443 | + | |
| 10444 | + | |
| 10445 | + | |
| 10446 | + | |
| 10447 | + | |
| 10448 | + | |
10449 | 10449 | | |
10450 | 10450 | | |
10451 | 10451 | | |
10452 | 10452 | | |
10453 | 10453 | | |
10454 | 10454 | | |
| 10455 | + | |
| 10456 | + | |
| 10457 | + | |
| 10458 | + | |
10455 | 10459 | | |
10456 | 10460 | | |
10457 | 10461 | | |
| |||
10473 | 10477 | | |
10474 | 10478 | | |
10475 | 10479 | | |
| 10480 | + | |
| 10481 | + | |
| 10482 | + | |
| 10483 | + | |
| 10484 | + | |
| 10485 | + | |
| 10486 | + | |
10476 | 10487 | | |
10477 | 10488 | | |
10478 | 10489 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
517 | 517 | | |
518 | 518 | | |
519 | 519 | | |
520 | | - | |
521 | 520 | | |
522 | 521 | | |
523 | 522 | | |
| |||
562 | 561 | | |
563 | 562 | | |
564 | 563 | | |
565 | | - | |
566 | 564 | | |
567 | 565 | | |
568 | 566 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
469 | 469 | | |
470 | 470 | | |
471 | 471 | | |
472 | | - | |
473 | | - | |
474 | | - | |
475 | | - | |
| 472 | + | |
| 473 | + | |
476 | 474 | | |
477 | 475 | | |
478 | | - | |
479 | | - | |
480 | | - | |
481 | | - | |
| 476 | + | |
| 477 | + | |
482 | 478 | | |
483 | 479 | | |
484 | 480 | | |
485 | 481 | | |
486 | | - | |
| 482 | + | |
487 | 483 | | |
488 | 484 | | |
489 | 485 | | |
| |||
492 | 488 | | |
493 | 489 | | |
494 | 490 | | |
495 | | - | |
| 491 | + | |
496 | 492 | | |
497 | 493 | | |
498 | 494 | | |
499 | 495 | | |
500 | 496 | | |
501 | | - | |
| 497 | + | |
502 | 498 | | |
503 | 499 | | |
504 | 500 | | |
505 | 501 | | |
506 | 502 | | |
507 | 503 | | |
508 | 504 | | |
509 | | - | |
| 505 | + | |
510 | 506 | | |
511 | 507 | | |
512 | 508 | | |
513 | 509 | | |
514 | 510 | | |
515 | 511 | | |
516 | | - | |
| 512 | + | |
517 | 513 | | |
518 | 514 | | |
519 | 515 | | |
520 | 516 | | |
521 | 517 | | |
522 | 518 | | |
523 | | - | |
524 | | - | |
525 | | - | |
526 | | - | |
527 | | - | |
528 | 519 | | |
529 | 520 | | |
530 | 521 | | |
| |||
538 | 529 | | |
539 | 530 | | |
540 | 531 | | |
541 | | - | |
542 | 532 | | |
543 | 533 | | |
544 | 534 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1247 | 1247 | | |
1248 | 1248 | | |
1249 | 1249 | | |
1250 | | - | |
1251 | 1250 | | |
1252 | 1251 | | |
1253 | 1252 | | |
| |||
0 commit comments