Skip to content

Commit c211c19

Browse files
kirylbp3tk0v
authored andcommitted
efi/unaccepted: Avoid load_unaligned_zeropad() stepping into unaccepted memory
load_unaligned_zeropad() can lead to unwanted loads across page boundaries. The unwanted loads are typically harmless. But, they might be made to totally unrelated or even unmapped memory. load_unaligned_zeropad() relies on exception fixup (#PF, #GP and now #VE) to recover from these unwanted loads. But, this approach does not work for unaccepted memory. For TDX, a load from unaccepted memory will not lead to a recoverable exception within the guest. The guest will exit to the VMM where the only recourse is to terminate the guest. There are two parts to fix this issue and comprehensively avoid access to unaccepted memory. Together these ensure that an extra "guard" page is accepted in addition to the memory that needs to be used. 1. Implicitly extend the range_contains_unaccepted_memory(start, end) checks up to end+unit_size if 'end' is aligned on a unit_size boundary. 2. Implicitly extend accept_memory(start, end) to end+unit_size if 'end' is aligned on a unit_size boundary. Side note: This leads to something strange. Pages which were accepted at boot, marked by the firmware as accepted and will never _need_ to be accepted might be on unaccepted_pages list This is a cue to ensure that the next page is accepted before 'page' can be used. This is an actual, real-world problem which was discovered during TDX testing. Signed-off-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Reviewed-by: Dave Hansen <[email protected]> Reviewed-by: Ard Biesheuvel <[email protected]> Reviewed-by: Tom Lendacky <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 2053bc5 commit c211c19

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

drivers/firmware/efi/unaccepted_memory.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,34 @@ void accept_memory(phys_addr_t start, phys_addr_t end)
4646
start -= unaccepted->phys_base;
4747
end -= unaccepted->phys_base;
4848

49+
/*
50+
* load_unaligned_zeropad() can lead to unwanted loads across page
51+
* boundaries. The unwanted loads are typically harmless. But, they
52+
* might be made to totally unrelated or even unmapped memory.
53+
* load_unaligned_zeropad() relies on exception fixup (#PF, #GP and now
54+
* #VE) to recover from these unwanted loads.
55+
*
56+
* But, this approach does not work for unaccepted memory. For TDX, a
57+
* load from unaccepted memory will not lead to a recoverable exception
58+
* within the guest. The guest will exit to the VMM where the only
59+
* recourse is to terminate the guest.
60+
*
61+
* There are two parts to fix this issue and comprehensively avoid
62+
* access to unaccepted memory. Together these ensure that an extra
63+
* "guard" page is accepted in addition to the memory that needs to be
64+
* used:
65+
*
66+
* 1. Implicitly extend the range_contains_unaccepted_memory(start, end)
67+
* checks up to end+unit_size if 'end' is aligned on a unit_size
68+
* boundary.
69+
*
70+
* 2. Implicitly extend accept_memory(start, end) to end+unit_size if
71+
* 'end' is aligned on a unit_size boundary. (immediately following
72+
* this comment)
73+
*/
74+
if (!(end % unit_size))
75+
end += unit_size;
76+
4977
/* Make sure not to overrun the bitmap */
5078
if (end > unaccepted->size * unit_size * BITS_PER_BYTE)
5179
end = unaccepted->size * unit_size * BITS_PER_BYTE;
@@ -93,6 +121,13 @@ bool range_contains_unaccepted_memory(phys_addr_t start, phys_addr_t end)
93121
start -= unaccepted->phys_base;
94122
end -= unaccepted->phys_base;
95123

124+
/*
125+
* Also consider the unaccepted state of the *next* page. See fix #1 in
126+
* the comment on load_unaligned_zeropad() in accept_memory().
127+
*/
128+
if (!(end % unit_size))
129+
end += unit_size;
130+
96131
/* Make sure not to overrun the bitmap */
97132
if (end > unaccepted->size * unit_size * BITS_PER_BYTE)
98133
end = unaccepted->size * unit_size * BITS_PER_BYTE;

0 commit comments

Comments
 (0)