Fix some bugs in the CCP driver
Subsystems (3)
cover Fix some bugs in the CCP driver
From: "Tycho Andersen (AMD)" <[email protected]>
This set of fixes came out of using various AI tools on the SNP shutdown series:
https://lore.kernel.org/all/[email protected]/
I'm not quite sure how to cite these tools yet, checkpatch complained a bit about my citations here. Happy to respin if there's a better way. It looks like e.g. Assisted-by in the process of being added:
https://lore.kernel.org/all/[email protected]/
Thanks,
Tycho
Tycho Andersen (AMD) (4):
crypto/ccp: Reverse the cleanup order in psp_dev_destroy() crypto/ccp: Fix snp_filter_reserved_mem_regions() off-by-one crypto/ccp: Check for page allocation failure correctly in TIO crypto/ccp: Initialize data during __sev_snp_init_locked()
drivers/crypto/ccp/psp-dev.c | 8 ++++---- drivers/crypto/ccp/sev-dev.c | 23 +++++++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-)
base-commit: 6c927e5ca9d238f8ae40b453a8382eb9cf4ee855
Signature · 1 line
2.53.0
PATCH 1/4 crypto/ccp: Reverse the cleanup order in psp_dev_destroy()
From: "Tycho Andersen (AMD)" <[email protected]>
Before SNP x86 shutdown [1], all HV_FIXED pages were always leaked on module unload. Now pages can be reclaimed if they are freed before SNP shutdown.
The SFS driver does sfs_dev_destroy() -> snp_free_hv_fixed_pages(), marking the command buffer as free. But this happens after sev_dev_destroy() in psp_dev_destroy(), so the pages are always leaked.
Rearrange psp_dev_destroy() to destroy things in the reverse order from psp_init(), so that any dependencies can be unwound accordingly. This lets SFS free the page and the subsequent SNP shutdown release it.
This was identified with use of Chris Mason's review-prompts:
https://github.com/masoncl/review-prompts
[1]: https://lore.kernel.org/all/[email protected]/
--- drivers/crypto/ccp/psp-dev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/crypto/ccp/psp-dev.c +4 -4
@@ -316,15 +316,15 @@ void psp_dev_destroy(struct sp_device *sp)if (!psp)return ; -sev_dev_destroy (psp); +dbc_dev_destroy (psp); -tee_dev_destroy (psp); +platform_access_dev_destroy (psp);sfs_dev_destroy (psp); -dbc_dev_destroy (psp); +tee_dev_destroy (psp); -platform_access_dev_destroy (psp); +sev_dev_destroy (psp);sp_free_psp_irq (sp, psp);
Signature · 1 line
2.53.0
PATCH 2/4 crypto/ccp: Fix snp_filter_reserved_mem_regions() off-by-one
In reply to Tycho AndersenFrom: "Tycho Andersen (AMD)" <[email protected]>
Sashiko notes:
Quoted 9 lines
regarding the bounds check in
snp_filter_reserved_mem_regions()called viawalk_iomem_res_desc(): does the check
if ((range_list->num_elements* 16 + 8) >PAGE_SIZE) allow an off-by-one heap buffer overflow?If
range_list->num_elementsis 255, 255 * 16 + 8 = 4088, which is <= 4096. Writingrange->base(8 bytes) fills 4088-4095, but writingrange->page_count(4 bytes) would write to 4096-4099, overflowing the kzalloc-allocatedPAGE_SIZEbuffer.
Fix this by accounting for the entry about to be written to, in addition to the entries that are already allocated.
--- drivers/crypto/ccp/sev-dev.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/crypto/ccp/sev-dev.c +4 -3
@@ -1328,10 +1328,11 @@ static int snp_filter_reserved_mem_regions(struct resource *rs, void *arg)size_t size;/* -* Ensure the list of HV_FIXED pages that will be passed to firmware -* do not exceed the page-sized argument buffer. +* Ensure the list of HV_FIXED pages passed to the firmware including +* the one about to be written to do not exceed the page-sized argument +* buffer. */ -if ((range_list->num_elements *sizeof (struct sev_data_range ) + +if (((range_list->num_elements +1 ) *sizeof (struct sev_data_range ) +sizeof (struct sev_data_range_list )) >PAGE_SIZE )return -E2BIG ;
Signature · 1 line
2.53.0
PATCH 3/4 crypto/ccp: Check for page allocation failure correctly in TIO
In reply to Tycho AndersenFrom: "Tycho Andersen (AMD)" <[email protected]>
Sashiko notes:
Quoted 7 lines
if
__snp_alloc_firmware_pages()returns NULL under memory pressure, is it safe to pass it directly topage_address()?On architectures without
HASHED_PAGE_VIRTUAL,page_address(NULL)might compute a deterministic but invalid, non-zero virtual address. The subsequent if (tio_status) check would then evaluate to true, andsev_tsm_init_locked()would dereference the invalid pointer.
Indeed, page_address(NULL) will return non-NULL garbage here. Fix this by checking the page allocation itself for NULL, not the resulting virtual address.
--- drivers/crypto/ccp/sev-dev.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/crypto/ccp/sev-dev.c +8 -4
@@ -1488,6 +1488,8 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) &snp_panic_notifier);if (data.tio_en) { +struct page *page; +/* * This executes with the sev_cmd_mutex held so down the stack * snp_reclaim_pages(locked=false) might be needed (which is extremely
@@ -1495,12 +1497,14 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)* Instead of exporting __snp_alloc_firmware_pages(), allocate a page * for this one call here. */ -void *tio_status =page_address (__snp_alloc_firmware_pages ( -GFP_KERNEL_ACCOUNT | __GFP_ZERO,0 , true)); + page =__snp_alloc_firmware_pages (GFP_KERNEL_ACCOUNT | __GFP_ZERO, +0 , true); +if (page) { +void *tio_status =page_address (page); -if (tio_status) {sev_tsm_init_locked (sev, tio_status); -__snp_free_firmware_pages (virt_to_page (tio_status),0 , true); + +__snp_free_firmware_pages (page,0 , true); } }
Signature · 1 line
2.53.0
PATCH 4/4 crypto/ccp: Initialize data during __sev_snp_init_locked()
In reply to Tycho AndersenFrom: "Tycho Andersen (AMD)" <[email protected]>
Sashiko notes:
Quoted 4 lines
is the stack variable data left uninitialized when taking the else branch? Since data.tio_en is later evaluated unconditionally, could stack garbage cause it to evaluate to true, leading to erroneous attempts to allocate pages and initialize SEV-TIO on unsupported hardware?
If the firmware is too old to support SEV_INIT_EX, data is left uninitialized but used in the debug logging about whether TIO is enabled or not.
--- drivers/crypto/ccp/sev-dev.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/crypto/ccp/sev-dev.c +1 -3
@@ -1356,7 +1356,7 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) { struct sev_data_range_list *snp_range_list __free(kfree) = NULL;struct psp_device *psp = psp_master; -struct sev_data_snp_init_ex data; +struct sev_data_snp_init_ex data = {};struct sev_device *sev;void *arg = &data;int cmd, rc =0 ;
@@ -1420,8 +1420,6 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)*/ snp_add_hv_fixed_pages (sev, snp_range_list); -memset (&data,0 ,sizeof (data)); -if (max_snp_asid) { data.ciphertext_hiding_en =1 ; data.max_snp_asid = max_snp_asid;
Signature · 1 line
2.53.0
Quoted 21 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
This set of fixes came out of using various AI tools on the SNP shutdown series:
https://lore.kernel.org/all/[email protected]/I'm not quite sure how to cite these tools yet, checkpatch complained a bit about my citations here. Happy to respin if there's a better way. It looks like e.g. Assisted-by in the process of being added:
https://lore.kernel.org/all/[email protected]/Thanks,
Tycho
Tycho Andersen (AMD) (4):
crypto/ccp: Reverse the cleanup order inpsp_dev_destroy()crypto/ccp: Fixsnp_filter_reserved_mem_regions()off-by-onecrypto/ccp: Check for page allocation failure correctly in TIOcrypto/ccp: Initialize data during__sev_snp_init_locked()
Quoted 3 lines
drivers/crypto/ccp/psp-dev.c | 8 ++++---- drivers/crypto/ccp/sev-dev.c | 23 +++++++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-)
For the series:
Quoted 3 lines
base-commit:
6c927e5ca9d238f8ae40b453a8382eb9cf4ee855
Herbert Xu
maintainerQuoted 21 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
This set of fixes came out of using various AI tools on the SNP shutdown series:
https://lore.kernel.org/all/[email protected]/I'm not quite sure how to cite these tools yet, checkpatch complained a bit about my citations here. Happy to respin if there's a better way. It looks like e.g. Assisted-by in the process of being added:
https://lore.kernel.org/all/[email protected]/Thanks,
Tycho
Tycho Andersen (AMD) (4):
crypto/ccp: Reverse the cleanup order inpsp_dev_destroy()crypto/ccp: Fixsnp_filter_reserved_mem_regions()off-by-onecrypto/ccp: Check for page allocation failure correctly in TIOcrypto/ccp: Initialize data during__sev_snp_init_locked()
Quoted 8 lines
drivers/crypto/ccp/psp-dev.c | 8 ++++---- drivers/crypto/ccp/sev-dev.c | 23 +++++++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-)base-commit:
6c927e5ca9d238f8ae40b453a8382eb9cf4ee855Signature · 1 line
2.53.0
All applied. Thanks.
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt