Skip to content

Commit

Permalink
dma-direct: Leak pages on dma_set_decrypted() failure
Browse files Browse the repository at this point in the history
On TDX it is possible for the untrusted host to cause
set_memory_encrypted() or set_memory_decrypted() to fail such that an
error is returned and the resulting memory is shared. Callers need to
take care to handle these errors to avoid returning decrypted (shared)
memory to the page allocator, which could lead to functional or security
issues.

DMA could free decrypted/shared pages if dma_set_decrypted() fails. This
should be a rare case. Just leak the pages in this case instead of
freeing them.

Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
  • Loading branch information
rpedgeco authored and Christoph Hellwig committed Feb 28, 2024
1 parent 02e7656 commit b9fa169
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions kernel/dma/direct.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void *dma_direct_alloc(struct device *dev, size_t size,
} else {
ret = page_address(page);
if (dma_set_decrypted(dev, ret, size))
goto out_free_pages;
goto out_leak_pages;
}

memset(ret, 0, size);
Expand All @@ -307,6 +307,8 @@ void *dma_direct_alloc(struct device *dev, size_t size,
out_free_pages:
__dma_direct_free_pages(dev, page, size);
return NULL;
out_leak_pages:
return NULL;
}

void dma_direct_free(struct device *dev, size_t size,
Expand Down Expand Up @@ -367,12 +369,11 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,

ret = page_address(page);
if (dma_set_decrypted(dev, ret, size))
goto out_free_pages;
goto out_leak_pages;
memset(ret, 0, size);
*dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
return page;
out_free_pages:
__dma_direct_free_pages(dev, page, size);
out_leak_pages:
return NULL;
}

Expand Down

0 comments on commit b9fa169

Please sign in to comment.