Skip to content

Commit 23e6a1c

Browse files
00xctorvalds
authored andcommitted
virt: sev-guest: Do not use host-controlled page order in cleanup path
When issuing an extended guest request (SVM_VMGEXIT_EXT_GUEST_REQUEST), get_ext_report() allocates a buffer to retrieve a certificate blob from the host, keeping track of its size in report_req->certs_len. However, the host may return SNP_GUEST_VMM_ERR_INVALID_LEN, indicating an invalid buffer size, as well as the expected length of such buffer. get_ext_report() subsequently updates report_req->certs_len with the host-controlled value, and cleans up the buffer by computing a page order from such value. This is incorrect, as the host-provided length may not match the page order of the original allocation, potentially resulting in corruption in the page allocator. Fix this by using alloc_pages_exact() instead, and reusing @npages to compute the size passed to free_pages_exact(). For consistency, also use @npages to compute the size when allocating the pages, even though this last change has no functional effect. Fixes: 3e385c0 ("virt: sev-guest: Move SNP Guest Request data pages handling under snp_cmd_mutex") Signed-off-by: Carlos López <clopez@suse.de> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Tested-by: Michael Roth <michael.roth@amd.com> Cc: stable@kernel.org Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e7f24a3 commit 23e6a1c

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

drivers/virt/coco/sev-guest/sev-guest.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques
176176
struct snp_guest_req req = {};
177177
int ret, npages = 0, resp_len;
178178
sockptr_t certs_address;
179-
struct page *page;
180179

181180
if (sockptr_is_null(io->req_data) || sockptr_is_null(io->resp_data))
182181
return -EINVAL;
@@ -211,16 +210,15 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques
211210
* zeros to indicate that certificate data was not provided.
212211
*/
213212
npages = report_req->certs_len >> PAGE_SHIFT;
214-
page = alloc_pages(GFP_KERNEL_ACCOUNT | __GFP_ZERO,
215-
get_order(report_req->certs_len));
216-
if (!page)
213+
req.certs_data = alloc_pages_exact(npages << PAGE_SHIFT,
214+
GFP_KERNEL_ACCOUNT | __GFP_ZERO);
215+
if (!req.certs_data)
217216
return -ENOMEM;
218217

219-
req.certs_data = page_address(page);
220218
ret = set_memory_decrypted((unsigned long)req.certs_data, npages);
221219
if (ret) {
222220
pr_err("failed to mark page shared, ret=%d\n", ret);
223-
__free_pages(page, get_order(report_req->certs_len));
221+
free_pages_exact(req.certs_data, npages << PAGE_SHIFT);
224222
return -EFAULT;
225223
}
226224

@@ -277,7 +275,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques
277275
if (set_memory_encrypted((unsigned long)req.certs_data, npages))
278276
WARN_ONCE(ret, "failed to restore encryption mask (leak it)\n");
279277
else
280-
__free_pages(page, get_order(report_req->certs_len));
278+
free_pages_exact(req.certs_data, npages << PAGE_SHIFT);
281279
}
282280
return ret;
283281
}

0 commit comments

Comments
 (0)