x86: don't use privilege stack areas as a guard

This is causing problems, as if we create a thread in
a system call we will *not* be using the kernel page
tables if CONFIG_KPTI=n, resulting in a crash when
the later call to copy_page_tables() tries to initialize
the PDPT (which is in the same page as the privilege
stack).

Just don't fiddle with this page's permissions; we don't
need it as a guard area anyway since we have a stack
guard placed immediately before it, and this page
is unused if user mode isn't active.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
This commit is contained in:
Andrew Boie 2019-11-21 20:36:27 -08:00 committed by Anas Nashif
commit 9849c7d3e7

View file

@ -112,14 +112,6 @@ static FUNC_NORETURN void drop_to_user(k_thread_entry_t user_entry,
FUNC_NORETURN void arch_user_mode_enter(k_thread_entry_t user_entry,
void *p1, void *p2, void *p3)
{
struct z_x86_thread_stack_header *header =
(struct z_x86_thread_stack_header *)_current->stack_obj;
/* Set up the kernel stack used during privilege elevation */
z_x86_mmu_set_flags(&z_x86_kernel_ptables, &header->privilege_stack,
MMU_PAGE_SIZE, MMU_ENTRY_WRITE, Z_X86_MMU_RW,
true);
/* Initialize per-thread page tables, since that wasn't done when
* the thread was initialized (K_USER was not set at creation time)
*/
@ -179,27 +171,15 @@ void arch_new_thread(struct k_thread *thread, k_thread_stack_t *stack,
char *stack_buf;
char *stack_high;
struct _x86_initial_frame *initial_frame;
#if defined(CONFIG_X86_USERSPACE) || defined(CONFIG_X86_STACK_PROTECTION)
struct z_x86_thread_stack_header *header =
(struct z_x86_thread_stack_header *)stack;
#endif
Z_ASSERT_VALID_PRIO(priority, entry);
stack_buf = Z_THREAD_STACK_BUFFER(stack);
z_new_thread_init(thread, stack_buf, stack_size, priority, options);
#ifdef CONFIG_X86_USERSPACE
/* Set MMU properties for the privilege mode elevation stack.
* If we're not starting in user mode, this functions as a guard
* area.
*/
z_x86_mmu_set_flags(&z_x86_kernel_ptables, &header->privilege_stack,
MMU_PAGE_SIZE,
((options & K_USER) == 0U) ? MMU_ENTRY_READ : MMU_ENTRY_WRITE,
Z_X86_MMU_RW, true);
#endif /* CONFIG_X86_USERSPACE */
#if CONFIG_X86_STACK_PROTECTION
struct z_x86_thread_stack_header *header =
(struct z_x86_thread_stack_header *)stack;
/* Set guard area to read-only to catch stack overflows */
z_x86_mmu_set_flags(&z_x86_kernel_ptables, &header->guard_page,
MMU_PAGE_SIZE, MMU_ENTRY_READ, Z_X86_MMU_RW,