x86/sev-es: Do not use copy_from_kernel_nofault in early #VC handler
PATCH 1/1 x86/sev-es: Do not use copy_from_kernel_nofault in early #VC handler
In the early #VC handler before start_kernel, boot_cpu_data.x86_virt_bits is not yet initialized. copy_from_kernel_nofault references this variable, so it cannot be called. Instead, simply use memcpy.
Usage of this uninitialized variable is currently causing boot failures in the latest version of ubuntu2204 in the gcp project, but in general reading the uninitialized variable is undefined behavior. If the variable happens to be 0, UB also happens due to a bit shift by 64.
--- arch/x86/boot/compressed/sev.c | 4 ++-- arch/x86/kernel/sev-shared.c | 4 ++-- arch/x86/kernel/sev.c | 22 ++++++++++++++-------- 3 files changed, 18 insertions(+), 12 deletions(-)
arch/x86/boot/compressed/sev.c +2 -2
@@ -73,7 +73,7 @@ static inline void sev_es_wr_ghcb_msr(u64 val)boot_wrmsr (MSR_AMD64_SEV_ES_GHCB , &m); } -static enum es_result vc_decode_insn (struct es_em_ctxt *ctxt) +static enum es_result vc_decode_insn (struct es_em_ctxt *ctxt,bool is_early) {char buffer[MAX_INSN_SIZE ];int ret;
@@ -290,7 +290,7 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)sev_es_terminate (SEV_TERM_SET_GEN ,GHCB_SEV_ES_GEN_REQ );vc_ghcb_invalidate (boot_ghcb); - result =vc_init_em_ctxt (&ctxt, regs, exit_code); + result =vc_init_em_ctxt (&ctxt, regs, exit_code, true);if (result !=ES_OK ) goto finish;
arch/x86/kernel/sev-shared.c +2 -2
@@ -176,7 +176,7 @@ static bool vc_decoding_needed(unsigned long exit_code)static enum es_result vc_init_em_ctxt (struct es_em_ctxt *ctxt,struct pt_regs *regs, -unsigned long exit_code) +unsigned long exit_code,bool is_early) {enum es_result ret =ES_OK ;
@@ -184,7 +184,7 @@ static enum es_result vc_init_em_ctxt(struct es_em_ctxt *ctxt, ctxt->regs = regs;if (vc_decoding_needed (exit_code)) - ret =vc_decode_insn (ctxt); + ret =vc_decode_insn (ctxt, is_early);return ret; }
arch/x86/kernel/sev.c +14 -8
@@ -270,9 +270,15 @@ static __always_inline void sev_es_wr_ghcb_msr(u64 val) }static int vc_fetch_insn_kernel (struct es_em_ctxt *ctxt, -unsigned char *buffer) +unsigned char *buffer,bool is_early) { -return copy_from_kernel_nofault (buffer, (unsigned char *)ctxt->regs->ip,MAX_INSN_SIZE ); +if (is_early) { +memcpy (buffer, (unsigned char *)ctxt->regs->ip,MAX_INSN_SIZE ); +return 0 ; + }else { +return copy_from_kernel_nofault (buffer, (unsigned char *)ctxt->regs->ip, +MAX_INSN_SIZE ); + } }static enum es_result __vc_decode_user_insn (struct es_em_ctxt *ctxt)
@@ -304,12 +310,12 @@ static enum es_result __vc_decode_user_insn(struct es_em_ctxt *ctxt)return ES_DECODE_FAILED ; } -static enum es_result __vc_decode_kern_insn (struct es_em_ctxt *ctxt) +static enum es_result __vc_decode_kern_insn (struct es_em_ctxt *ctxt,bool is_early) {char buffer[MAX_INSN_SIZE ];int res, ret; - res =vc_fetch_insn_kernel (ctxt, buffer); + res =vc_fetch_insn_kernel (ctxt, buffer, is_early);if (res) { ctxt->fi.vector =X86_TRAP_PF ; ctxt->fi.error_code =X86_PF_INSTR ;
@@ -324,12 +330,12 @@ static enum es_result __vc_decode_kern_insn(struct es_em_ctxt *ctxt)return ES_OK ; } -static enum es_result vc_decode_insn (struct es_em_ctxt *ctxt) +static enum es_result vc_decode_insn (struct es_em_ctxt *ctxt,bool is_early) {if (user_mode (ctxt->regs))return __vc_decode_user_insn (ctxt);else -return __vc_decode_kern_insn (ctxt); +return __vc_decode_kern_insn (ctxt, is_early); } static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
@@ -1829,7 +1835,7 @@ static bool vc_raw_handle_exception(struct pt_regs *regs, unsigned long error_co ghcb =__sev_get_ghcb (&state);vc_ghcb_invalidate (ghcb); - result =vc_init_em_ctxt (&ctxt, regs, error_code); + result =vc_init_em_ctxt (&ctxt, regs, error_code, false);if (result ==ES_OK ) result =vc_handle_exitcode (&ctxt, ghcb, error_code);
@@ -1969,7 +1975,7 @@ bool __init handle_vc_boot_ghcb(struct pt_regs *regs)vc_ghcb_invalidate (boot_ghcb); - result =vc_init_em_ctxt (&ctxt, regs, exit_code); + result =vc_init_em_ctxt (&ctxt, regs, exit_code, true);if (result ==ES_OK ) result =vc_handle_exitcode (&ctxt, boot_ghcb, exit_code);
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
arch/x86/kernel/sev.c static __always_inline void sev_es_wr_ghcb_msr(u64 val) · line 282
Quoted 13 lines · Adam Dunlap
static int vc_fetch_insn_kernel(struct
es_em_ctxt*ctxt,arch/x86/kernel/sev.c +8
-2- unsigned char *buffer) + unsigned char *buffer, bool is_early) { -return copy_from_kernel_nofault (buffer, (unsigned char *)ctxt->regs->ip,MAX_INSN_SIZE ); +if (is_early) { +memcpy (buffer, (unsigned char *)ctxt->regs->ip,MAX_INSN_SIZE ); +return 0 ; + }else { +return copy_from_kernel_nofault (buffer, (unsigned char *)ctxt->regs->ip, +MAX_INSN_SIZE ); + } }
This isn't the normal way we do these kinds of things.
If we go with this solution, they next guy who tries copy_from_kernel_nofault() will hit the same issue, and start plumbing their own 'is_early' through _their_ little chunk of arch/x86.
Usually, we'll add some gunk in arch/x86/boot/compressed/misc.h to override the troublesome implementation. In this case, it would make a lot of sense to somehow avoid touching boot_cpu_data.x86_virt_bits in the first place.
Quoted 4 lines
Usually, we'll add some gunk in
arch/x86/boot/compressed/misc.hto override the troublesome implementation. In this case, it would make a lot of sense to somehow avoid touching boot_cpu_data.x86_virt_bits in the first place.
Thanks for the comment. I realize this patch is doing something a bit misleading here. In this case, "early" does not refer to the compressed kernel, but actually the regular kernel but in the stage with this early #VC handler vc_boot_ghcb (instead of the usual vc_raw_handle_exception). This #VC handler triggers for the first time on a cpuid instruction in secondary_startup_64, but boot_cpu_data.x86_virt_bits is not initialized until setup_arch inside of start_kernel, which is at the end of secondary_startup_64.
The compressed kernel already has its own implementation of vc_decode_insn which simply calls memcpy instead of copy_from_kernel_nofault, so the point of this patch is to do the same thing for the early stages of the post-decompression kernel.
So unless I'm misunderstanding, adding a version of copy_from_kernel_nofault to the compressed directory wouldn't solve the problem since the problem does not occur during the compressed stage.
Quoted 11 lines · Adam Dunlap
Quoted 4 lines
Usually, we'll add some gunk in
arch/x86/boot/compressed/misc.hto override the troublesome implementation. In this case, it would make a lot of sense to somehow avoid touching boot_cpu_data.x86_virt_bits in the first place.Thanks for the comment. I realize this patch is doing something a bit misleading here. In this case, "early" does not refer to the compressed kernel, but actually the regular kernel but in the stage with this early #VC handler
vc_boot_ghcb(instead of the usualvc_raw_handle_exception). This #VC handler triggers for the first time on a cpuid instruction insecondary_startup_64, but boot_cpu_data.x86_virt_bits is not initialized untilsetup_archinside ofstart_kernel, which is at the end ofsecondary_startup_64.
How about something like the attached patch?
It avoids passing around 'is_early' everywhere, which I'm sure we'll get wrong at some point. If we get it wrong, we lose *ALL* the checking that copy_from_kernel*() does in addition to the canonical checks.
The attached patch at least preserves the userspace address checks.
This also makes me wonder how much other code is called via the early exception handlers that's subtly broken. I scanned a function or two deep and the instruction decoding was the most guilty looking thing. But a closer look would be appreciated.
Also, what's the root cause here? What's causing the early exception? It is some silly CPUID leaf? Should we be more careful to just avoid these exceptions?
arch/x86/mm/maccess.c +15 -5
@@ -7,14 +7,24 @@bool copy_from_kernel_nofault_allowed (const void *unsafe_src,size_t size) {unsigned long vaddr = (unsigned long )unsafe_src; +bool ret;/* -* Range covering the highest possible canonical userspace address -* as well as non-canonical address range. For the canonical range -* we also need to include the userspace guard page. +* Do not allow userspace addresses. This disallows +* normal userspace and the userspace guard page: */ -return vaddr >=TASK_SIZE_MAX +PAGE_SIZE && -__is_canonical_address (vaddr, boot_cpu_data.x86_virt_bits); +if (vaddr <TASK_SIZE_MAX +PAGE_SIZE ) +return false; + +/* +* Allow everything during early boot before 'x86_virt_bits' +* is initialized. Needed for instruction decoding in early +* exception handlers. +*/ +if (!boot_cpu_data.x86_virt_bits) +return true; + +return __is_canonical_address (vaddr, boot_cpu_data.x86_virt_bits); }#else bool copy_from_kernel_nofault_allowed (const void *unsafe_src,size_t size)
Quoted 1 line · Dave Hansen
How about something like the attached patch?
I think that's a much better idea, but unfortunately we can't rely on boot_cpu_data being 0'd out. While it is a static global variable that C says should be 0'd, the early interrupt happens before .bss is cleared (Note if it happens to be 0, then the __is_canonical_address check succeeds anyway -- the boot failure only happens when that variable happens to be other random values). If there's another check we could do, I'd agree that'd end up being a much better patch. For example, maybe we could add a field to cpuinfo_x86 is_valid that is manually (i.e. not part of the regular .bss clearing) set to false and is only set to true after early_identify_cpu is finished. Or the simplest thing would
be to just manually set x86_virt_bits to 0 somewhere super early.
Quoted 1 line
But a closer look would be appreciated.
Turning on CONFIG_UBSAN catches the UB that happens when x86_virt_bits is 0 since that causes a bit shift by 64. Unfortunately, the ubsan reporting mechanism also doesn't play well with being called so early into boot, so the messages end up corrupted. But the fact that it only reports one error means that (at least in our tests), it only happens once. Of course, there could be other interrupts or other configurations where other problems happen.
Quoted 3 lines
Also, what's the root cause here? What's causing the early exception? It is some silly CPUID leaf? Should we be more careful to just avoid these exceptions?
Yes, it is a CPUID instruction in secondary_startup_64 with the comment /* Check if nx is implemented */. It appears to be pretty important. Potentially we could paravirtualize the CPUID direclty (i.e. mess with the GHCB and make the vmgexit) instead of taking the #VC, but I'm not sure that's a great idea.
There's a couple of other CPUIDs that are called in early_identify_cpu between when x86_virt_bits is set to 48 and when it is set to its real value (IIUC, it may be set to 57 if 5 level paging is enabled), which could potentially cause spurious failures in those later calls.
Quoted 13 lines · Adam Dunlap
Quoted 1 line · Dave Hansen
How about something like the attached patch?
I think that's a much better idea, but unfortunately we can't rely on
boot_cpu_databeing 0'd out. While it is a static global variable that C says should be 0'd, the early interrupt happens before .bss is cleared (Note if it happens to be 0, then the__is_canonical_addresscheck succeeds anyway -- the boot failure only happens when that variable happens to be other random values). If there's another check we could do, I'd agree that'd end up being a much better patch. For example, maybe we could add a field tocpuinfo_x86is_validthat is manually (i.e. not part of the regular .bss clearing) set to false and is only set to true afterearly_identify_cpuis finished. Or the
simplest thing would be to just manually setx86_virt_bitsto 0 somewhere super early.
Gah, what a mess. So the guilty CPUID in question is this:
Quoted 7 lines
/* Setup and Load IDT */
call early_setup_idt
/* Check if nx is implemented */
movl $0x80000001, %eax
cpuid
movl %edx,%ediwhich is _barely_ after we have an IDT with which to generate exceptions. What happens before this? This isn't the first CPUID invocation. Does this one just happen to #VC and all the others before don't?
In any case, the most straightforward way out of this mess is to just move boot_cpu_data out of .bss and explicitly initialize it along with some documentation explaining the situation.
Quoted 8 lines
Quoted 3 lines
Also, what's the root cause here? What's causing the early exception? It is some silly CPUID leaf? Should we be more careful to just avoid these exceptions?
Yes, it is a CPUID instruction in
secondary_startup_64with the comment /* Check if nx is implemented */. It appears to be pretty important. Potentially we could paravirtualize the CPUID direclty (i.e. mess with the GHCB and make the vmgexit) instead of taking the #VC, but I'm not sure that's a great idea.
What TDX does here is actually pretty nice. It doesn't generate a #VE for these.
But seriously, is it even *possible* to spin up a SEV-SNP VM what doesn't have NX?
Quoted 4 lines
There's a couple of other CPUIDs that are called in
early_identify_cpubetween whenx86_virt_bitsis set to 48 and when it is set to its real value (IIUC, it may be set to 57 if 5 level paging is enabled), which could potentially cause spurious failures in those later calls.
That should be easy enough to fix.
These things where we initialize a value and then write over it are always fragile. Let's just make one function that does it right and does it once.
Totally untested patch attached.
arch/x86/kernel/cpu/common.c +22 -15
@@ -1099,17 +1099,32 @@ void get_cpu_cap(struct cpuinfo_x86 *c)void get_cpu_address_sizes (struct cpuinfo_x86 *c) {u32 eax, ebx, ecx, edx; +bool vp_bits_from_cpuid = true; -if (c->extended_cpuid_level >=0x80000008 ) { +if (!cpu_has (c,X86_FEATURE_CPUID ) || + (c->extended_cpuid_level <0x80000008 )) + vp_bits_from_cpuid = false; + +if (vp_bits_from_cpuid) {cpuid (0x80000008 , &eax, &ebx, &ecx, &edx); c->x86_virt_bits = (eax >>8 ) &0xff ; c->x86_phys_bits = eax &0xff ; + }else { +if (IS_ENABLED (CONFIG_X86_64 )) { + c->x86_clflush_size =64 ; + c->x86_phys_bits =36 ; + c->x86_virt_bits =48 ; + }else { + c->x86_clflush_size =32 ; + c->x86_virt_bits =32 ; + c->x86_phys_bits =32 ; + +if (cpu_has (c,X86_FEATURE_PAE ) || +cpu_has (c,X86_FEATURE_PSE36 )) + c->x86_phys_bits =36 ; + } } -#ifdef CONFIG_X86_32 - else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36)) - c->x86_phys_bits = 36; -#endif c->x86_cache_bits = c->x86_phys_bits; }
@@ -1539,15 +1554,6 @@ static void __init cpu_parse_early_param(void)*/ static void __init early_identify_cpu (struct cpuinfo_x86 *c) { -#ifdef CONFIG_X86_64 - c->x86_clflush_size =64 ; - c->x86_phys_bits =36 ; - c->x86_virt_bits =48 ; -#else - c->x86_clflush_size =32 ; - c->x86_phys_bits =32 ; - c->x86_virt_bits =32 ; -#endif c->x86_cache_alignment = c->x86_clflush_size;memset (&c->x86_capability,0 ,sizeof (c->x86_capability));
@@ -1561,7 +1567,6 @@ static void __init early_identify_cpu(struct cpuinfo_x86 *c)cpu_detect (c);get_cpu_vendor (c);get_cpu_cap (c); -get_cpu_address_sizes (c);setup_force_cpu_cap (X86_FEATURE_CPUID );cpu_parse_early_param ();
@@ -1577,6 +1582,8 @@ static void __init early_identify_cpu(struct cpuinfo_x86 *c)setup_clear_cpu_cap (X86_FEATURE_CPUID ); } +get_cpu_address_sizes (c); +setup_force_cpu_cap (X86_FEATURE_ALWAYS );cpu_set_bug_bits (c);
Quoted 3 lines · Dave Hansen
What happens before this? This isn't the first CPUID invocation. Does this one just happen to #VC and all the others before don't?
I hadn't noticed this before, but there is an even earlier interrupt handler vc_no_ghcb
which only supports cpuid. Potentially this version could work until boot_cpu_data is
set up, but wasn't able to get it working myself.
Quoted 3 lines
In any case, the most straightforward way out of this mess is to just move
boot_cpu_dataout of .bss and explicitly initialize it along with some documentation explaining the situation.
That seems totally reasonable. I tried applying the two patches that you sent plus
boot_cpu_data.x86_virt_bits = 0; in early_setup_idt(), and that fixes the problems
that we can see. Do you want me to send out a new patch with these changes together?
Quoted 5 lines · Adam Dunlap
Quoted 3 lines
In any case, the most straightforward way out of this mess is to just move
boot_cpu_dataout of .bss and explicitly initialize it along with some documentation explaining the situation.That seems totally reasonable. I tried applying the two patches that you sent plus boot_cpu_data.x86_virt_bits = 0; in
early_setup_idt(),
Let's just initialize boot_cpu_data to zero and get it out of .bss entirely.
Quoted 2 lines
and that fixes the problems that we can see. Do you want me to send out a new patch with these changes together?
Well, ideally broken out in a couple of patches, but yeah, please resend also with some revised changelogs based on what you've learned.
Quoted 25 lines · Dave Hansen
Quoted 13 lines · Adam Dunlap
Quoted 1 line · Dave Hansen
How about something like the attached patch?
I think that's a much better idea, but unfortunately we can't rely on
boot_cpu_databeing 0'd out. While it is a static global variable that C says should be 0'd, the early interrupt happens before .bss is cleared (Note if it happens to be 0, then the__is_canonical_addresscheck succeeds anyway -- the boot failure only happens when that variable happens to be other random values). If there's another check we could do, I'd agree that'd end up being a much better patch. For example, maybe we could add a field tocpuinfo_x86is_validthat is manually (i.e. not part of the regular .bss clearing) set to false and is only set to true afterearly_identify_cpuis finished. Or the
simplest thing would be to just manually setx86_virt_bitsto 0 somewhere super early.Gah, what a mess. So the guilty CPUID in question is this:
Quoted 7 lines
/* Setup and Load IDT */ call early_setup_idt /* Check if nx is implemented */ movl $0x80000001, %eax cpuid movl %edx,%edi
Quoted 8 lines
which is _barely_ after we have an IDT with which to generate exceptions. What happens before this? This isn't the first CPUID invocation. Does this one just happen to #VC and all the others before don't?
In any case, the most straightforward way out of this mess is to just move
boot_cpu_dataout of .bss and explicitly initialize it along with some documentation explaining the situation.
Agreed, putting this in the .data section will ensure it is zero from the start.
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines
Also, what's the root cause here? What's causing the early exception? It is some silly CPUID leaf? Should we be more careful to just avoid these exceptions?
Yes, it is a CPUID instruction in
secondary_startup_64with the comment /* Check if nx is implemented */. It appears to be pretty important. Potentially we could paravirtualize the CPUID direclty (i.e. mess with the GHCB and make the vmgexit) instead of taking the #VC, but I'm not sure that's a great idea.
Quoted 5 lines
What TDX does here is actually pretty nice. It doesn't generate a #VE for these.
But seriously, is it even *possible* to spin up a SEV-SNP VM what doesn't have NX?
It is a common path, so while an SEV guest would have NX support, you would first have to determine that it is an SEV guest. That would take issuing a CPUID instruction in order to determine if a particular MSR can be read...
Ultimately, we could probably pass the encryption mask from the decompressor to the kernel and avoid some of the checks during early boot of the kernel proper. Is it possible to boot an x86 kernel without going through the decompressor?
Thanks,
Tom
Quoted 13 lines
Quoted 5 lines
There's a couple of other CPUIDs that are called in
early_identify_cpubetween whenx86_virt_bitsis set to 48 and when it is set to its real value (IIUC, it may be set to 57 if 5 level paging is enabled), which could potentially cause spurious failures in those later calls.That should be easy enough to fix.
These things where we initialize a value and then write over it are always fragile. Let's just make one function that does it right and does it once.
Totally untested patch attached.
Quoted 8 lines · Tom Lendacky
Quoted 2 lines · Dave Hansen
But seriously, is it even *possible* to spin up a SEV-SNP VM what doesn't have NX?
It is a common path, so while an SEV guest would have NX support, you would first have to determine that it is an SEV guest. That would take issuing a CPUID instruction in order to determine if a particular MSR can be read...
I was thinking more along the lines of telling folks that if they want to turn SEV-SNP support on, they also have to give up on running on a !NX system. That would be a _bit_ nicer than just refusing to boot on all !NX systems.
Quoted 4 lines
Ultimately, we could probably pass the encryption mask from the decompressor to the kernel and avoid some of the checks during early boot of the kernel proper. Is it possible to boot an x86 kernel without going through the decompressor?
I think it's possible, but it's very unusual.
Peter Zijlstra
maintainerQuoted 10 lines · Dave Hansen
Quoted 8 lines · Tom Lendacky
Quoted 2 lines · Dave Hansen
But seriously, is it even *possible* to spin up a SEV-SNP VM what doesn't have NX?
It is a common path, so while an SEV guest would have NX support, you would first have to determine that it is an SEV guest. That would take issuing a CPUID instruction in order to determine if a particular MSR can be read...
Quoted 4 lines
I was thinking more along the lines of telling folks that if they want to turn SEV-SNP support on, they also have to give up on running on a !NX system. That would be a _bit_ nicer than just refusing to boot on all !NX systems.
I think there were a very limited amount of !NX P4 class x86_64 systems. I vote in favour of dropping them into the museum bucket, they get to run an old kernel.
Andrew and me looked at this a few months ago, but I forgot all details again :/