* [PATCH] virt: tdx-guest: Fix the decrypted failure memory free
@ 2024-06-13 11:19 Li RongQing
2024-06-13 16:07 ` Dave Hansen
2024-06-13 16:10 ` Edgecombe, Rick P
0 siblings, 2 replies; 5+ messages in thread
From: Li RongQing @ 2024-06-13 11:19 UTC (permalink / raw)
To: kirill.shutemov, dave.hansen, x86, linux-coco; +Cc: Li RongQing
In CoCo VMs 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.
When set_memory_decrypted() fails, the memory should be encrypted
via set_memory_encrypted(); if encrypting the memory fails, leak it
Signed-off-by: Li RongQing <lirongqing@baidu.com>
---
drivers/virt/coco/tdx-guest/tdx-guest.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/virt/coco/tdx-guest/tdx-guest.c b/drivers/virt/coco/tdx-guest/tdx-guest.c
index 1253bf7..63271fc 100644
--- a/drivers/virt/coco/tdx-guest/tdx-guest.c
+++ b/drivers/virt/coco/tdx-guest/tdx-guest.c
@@ -125,7 +125,8 @@ static void *alloc_quote_buf(void)
return NULL;
if (set_memory_decrypted((unsigned long)addr, count)) {
- free_pages_exact(addr, len);
+ if (!set_memory_encrypted((unsigned long)addr, count))
+ free_pages_exact(addr, len);
return NULL;
}
--
2.9.4
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] virt: tdx-guest: Fix the decrypted failure memory free
2024-06-13 11:19 [PATCH] virt: tdx-guest: Fix the decrypted failure memory free Li RongQing
@ 2024-06-13 16:07 ` Dave Hansen
2024-06-13 16:13 ` Edgecombe, Rick P
2024-06-13 16:10 ` Edgecombe, Rick P
1 sibling, 1 reply; 5+ messages in thread
From: Dave Hansen @ 2024-06-13 16:07 UTC (permalink / raw)
To: Li RongQing, kirill.shutemov, dave.hansen, x86, linux-coco,
Edgecombe, Rick P, LKML
On 6/13/24 04:19, Li RongQing wrote:
> When set_memory_decrypted() fails, the memory should be encrypted
> via set_memory_encrypted(); if encrypting the memory fails, leak it
Please, always cc LKML on this stuff.
Second, Rick was looking in this area, but I'm not sure we ever applied
his patches. The idea was to never leak memory silently in these
failures. Doesn't this silently leak memory?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] virt: tdx-guest: Fix the decrypted failure memory free
2024-06-13 11:19 [PATCH] virt: tdx-guest: Fix the decrypted failure memory free Li RongQing
2024-06-13 16:07 ` Dave Hansen
@ 2024-06-13 16:10 ` Edgecombe, Rick P
2024-06-14 4:48 ` [外部邮件] " Li,Rongqing
1 sibling, 1 reply; 5+ messages in thread
From: Edgecombe, Rick P @ 2024-06-13 16:10 UTC (permalink / raw)
To: kirill.shutemov@linux.intel.com, linux-coco@lists.linux.dev,
lirongqing@baidu.com, dave.hansen@linux.intel.com, x86@kernel.org
Thanks.
On Thu, 2024-06-13 at 19:19 +0800, Li RongQing wrote:
> In CoCo VMs 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.
>
> When set_memory_decrypted() fails, the memory should be encrypted
> via set_memory_encrypted(); if encrypting the memory fails, leak it
>
> Signed-off-by: Li RongQing <lirongqing@baidu.com>
> ---
> drivers/virt/coco/tdx-guest/tdx-guest.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/virt/coco/tdx-guest/tdx-guest.c b/drivers/virt/coco/tdx-
> guest/tdx-guest.c
> index 1253bf7..63271fc 100644
> --- a/drivers/virt/coco/tdx-guest/tdx-guest.c
> +++ b/drivers/virt/coco/tdx-guest/tdx-guest.c
> @@ -125,7 +125,8 @@ static void *alloc_quote_buf(void)
> return NULL;
>
> if (set_memory_decrypted((unsigned long)addr, count)) {
> - free_pages_exact(addr, len);
> + if (!set_memory_encrypted((unsigned long)addr, count))
> + free_pages_exact(addr, len);
In the other cases the general consensus was to not optimize for this failure.
As in, don't try to re-encrypt the pages first, just give up and leak them on
the first failure. So the fix could just be to remove the free_pages_exact()
call. I think we should stick with that pattern.
> return NULL;
> }
On another note, it looks like that this one popped up after we made the
previous sweep to fix the pattern. Maybe it was in-flight at the time, but since
it even popped up in TDX specific code, it makes me wonder again about how we
can keep on top of the problem.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] virt: tdx-guest: Fix the decrypted failure memory free
2024-06-13 16:07 ` Dave Hansen
@ 2024-06-13 16:13 ` Edgecombe, Rick P
0 siblings, 0 replies; 5+ messages in thread
From: Edgecombe, Rick P @ 2024-06-13 16:13 UTC (permalink / raw)
To: kirill.shutemov@linux.intel.com, linux-coco@lists.linux.dev,
Hansen, Dave, lirongqing@baidu.com, linux-kernel@vger.kernel.org,
dave.hansen@linux.intel.com, x86@kernel.org
On Thu, 2024-06-13 at 09:07 -0700, Dave Hansen wrote:
> Second, Rick was looking in this area, but I'm not sure we ever applied
> his patches. The idea was to never leak memory silently in these
> failures. Doesn't this silently leak memory?
They did get applied actually. After a fair amount of discussion the solution
was to always leak the pages, and rely on the WARN that happens in set_memory()
to make noise about it.
It looks like this instance popped up after the sweep through the code was done.
(at least in my local branch with the patches for the fixes, this code was not
merged yet)
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: [外部邮件] Re: [PATCH] virt: tdx-guest: Fix the decrypted failure memory free
2024-06-13 16:10 ` Edgecombe, Rick P
@ 2024-06-14 4:48 ` Li,Rongqing
0 siblings, 0 replies; 5+ messages in thread
From: Li,Rongqing @ 2024-06-14 4:48 UTC (permalink / raw)
To: Edgecombe, Rick P, kirill.shutemov@linux.intel.com,
linux-coco@lists.linux.dev, dave.hansen@linux.intel.com,
x86@kernel.org
> In the other cases the general consensus was to not optimize for this failure.
> As in, don't try to re-encrypt the pages first, just give up and leak them on the
> first failure. So the fix could just be to remove the free_pages_exact() call. I
> think we should stick with that pattern.
>
I see, I will send v2
Thanks
-Li
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-06-14 5:19 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-13 11:19 [PATCH] virt: tdx-guest: Fix the decrypted failure memory free Li RongQing
2024-06-13 16:07 ` Dave Hansen
2024-06-13 16:13 ` Edgecombe, Rick P
2024-06-13 16:10 ` Edgecombe, Rick P
2024-06-14 4:48 ` [外部邮件] " Li,Rongqing
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).