All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Mathias Krause <minipli@grsecurity.net>
Cc: Paolo Bonzini <pbonzini@redhat.com>, kvm@vger.kernel.org
Subject: Re: [kvm-unit-tests PATCH v2 06/16] x86/run_in_user: Change type of code label
Date: Thu, 15 Jun 2023 08:18:45 -0700	[thread overview]
Message-ID: <ZIsr1acxWTbtiWKg@google.com> (raw)
In-Reply-To: <4de05ab1-e4d3-6693-a3c2-3f80236110bf@grsecurity.net>

On Wed, Jun 14, 2023, Mathias Krause wrote:
> On 13.06.23 02:32, Sean Christopherson wrote:
> > On Thu, Apr 13, 2023, Mathias Krause wrote:
> >> Use an array type to refer to the code label 'ret_to_kernel'.
> > 
> > Why?  Taking the address of a label when setting what is effectively the target
> > of a branch seems more intuitive than pointing at an array (that's not an array).
> 
> Well, the flexible array notation is what my understanding of referring
> to a "label" defined in ASM is. I'm probably biased, as that's a pattern
> used a lot in the Linux kernel but trying to look at the individual
> semantics may make it clearer why I prefer 'extern char sym[]' over
> 'extern char sym'.
> 
> The current code refers to the code sequence starting at 'ret_to_kernel'
> by creating an alias of it's first instruction byte. However, we're not
> interested in the first instruction byte at all. We want a symbolic
> handle of the begin of that sequence, which might be an unknown number
> of bytes. But that's also a detail that doesn't matter. We only what to
> know the start. By referring to it as 'extern char' implies that there
> is at least a single byte. (Let's ignore the hair splitting about just
> taking the address vs. actually dereferencing it (which we do not).) By
> looking at another code example, that byte may not actually be there!
> >From x86/vmx_tests.c:
> 
>   extern char vmx_mtf_pdpte_guest_begin;
>   extern char vmx_mtf_pdpte_guest_end;
> 
>   asm("vmx_mtf_pdpte_guest_begin:\n\t"
>       "mov %cr0, %rax\n\t"    /* save CR0 with PG=1                 */
>       "vmcall\n\t"            /* on return from this CR0.PG=0       */
>       "mov %rax, %cr0\n\t"    /* restore CR0.PG=1 to enter PAE mode */
>       "vmcall\n\t"
>       "retq\n\t"
>       "vmx_mtf_pdpte_guest_end:");
> 
> The byte referred to via &vmx_mtf_pdpte_guest_end may not even be mapped
> due to -ftoplevel-reorder possibly putting that asm block at the very
> end of the compilation unit.
> 
> By using 'extern char []' instead this nastiness is avoided by referring
> to an unknown sized byte sequence starting at that symbol (with zero
> being a totally valid length). We don't need to know how many bytes
> follow the label. All we want to know is its address. And that's what an
> array type provides easily.

I think my hangup is that arrays make me think of data, not code.  I wouldn't
object if this were new code, but I dislike changing existing code to something
that's arguably just as flawed.

What if we declare the label as a function?  That's not exactly correct either,
but IMO it's closer to the truth, and let's us document that the kernel trampoline
has a return value, i.e. preserves/outputs RAX.

diff --git a/lib/x86/usermode.c b/lib/x86/usermode.c
index c3ec0ad7..a46a369a 100644
--- a/lib/x86/usermode.c
+++ b/lib/x86/usermode.c
@@ -31,17 +31,18 @@ static void restore_exec_to_jmpbuf_exception_handler(struct ex_regs *regs)
 #endif
 }
 
+uint64_t ret_to_kernel(void);
+
 uint64_t run_in_user(usermode_func func, unsigned int fault_vector,
                uint64_t arg1, uint64_t arg2, uint64_t arg3,
                uint64_t arg4, bool *raised_vector)
 {
-       extern char ret_to_kernel;
        volatile uint64_t rax = 0;
        static unsigned char user_stack[USERMODE_STACK_SIZE];
        handler old_ex;
 
        *raised_vector = 0;
-       set_idt_entry(RET_TO_KERNEL_IRQ, &ret_to_kernel, 3);
+       set_idt_entry(RET_TO_KERNEL_IRQ, ret_to_kernel, 3);
        old_ex = handle_exception(fault_vector,
                                  restore_exec_to_jmpbuf_exception_handler);
 


  reply	other threads:[~2023-06-15 15:18 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-13 18:42 [kvm-unit-tests PATCH v2 00/16] x86: cleanups, fixes and new tests Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 01/16] x86: Drop types.h Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 02/16] x86: Use symbolic names in exception_mnemonic() Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 03/16] x86: Add vendor specific exception vectors Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 04/16] x86/cet: Use symbolic name for #CP Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 05/16] x86/access: Use 'bool' type as defined via libcflat.h Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 06/16] x86/run_in_user: Change type of code label Mathias Krause
2023-06-13  0:32   ` Sean Christopherson
2023-06-14 21:02     ` Mathias Krause
2023-06-15 15:18       ` Sean Christopherson [this message]
2023-06-16  6:38         ` Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 07/16] x86/run_in_user: Preserve exception handler Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 08/16] x86/run_in_user: Relax register constraints of inline asm Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 09/16] x86/run_in_user: Reload SS after successful return Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 10/16] x86/fault_test: Preserve exception handler Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 11/16] x86/emulator64: Relax register constraints for usr_gs_mov() Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 12/16] x86/emulator64: Switch test_sreg() to ASM_TRY() Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 13/16] x86/emulator64: Add non-null selector test Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 14/16] x86/emulator64: Switch test_jmp_noncanonical() to ASM_TRY() Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 15/16] x86/emulator64: Switch test_mmx_movq_mf() " Mathias Krause
2023-04-13 18:42 ` [kvm-unit-tests PATCH v2 16/16] x86/emulator64: Test non-canonical memory access exceptions Mathias Krause
2023-06-13 21:40 ` [kvm-unit-tests PATCH v2 00/16] x86: cleanups, fixes and new tests Sean Christopherson
2023-06-14 21:58   ` Mathias Krause

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZIsr1acxWTbtiWKg@google.com \
    --to=seanjc@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=minipli@grsecurity.net \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.