um: Calculate stub data address relative to stub code

Instead of using the current stack pointer, we can also use the current
instruction to calculate where the stub data is. With this the stub data
only needs to be aligned to a full page boundary.

Changing this has the advantage that we do not have a hole in the memory
space above the stub data (which would need to be explicitly cleared).

Another motivation to do this is that with the planned addition of a
SECCOMP based userspace the stack pointer may not be fully trustworthy.

Signed-off-by: Benjamin Berg <benjamin.berg@intel.com>
Link: https://patch.msgid.link/20240919124511.282088-7-benjamin@sipsolutions.net
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
This commit is contained in:
Benjamin Berg 2024-09-19 14:45:07 +02:00 committed by Johannes Berg
parent 77eb31b600
commit 91f0a0c5cc
3 changed files with 14 additions and 10 deletions

View File

@ -325,10 +325,8 @@ int __init linux_main(int argc, char **argv)
add_arg(DEFAULT_COMMAND_LINE_CONSOLE); add_arg(DEFAULT_COMMAND_LINE_CONSOLE);
host_task_size = os_get_top_address(); host_task_size = os_get_top_address();
/* reserve a few pages for the stubs (taking care of data alignment) */ /* reserve a few pages for the stubs */
/* align the data portion */ stub_start = host_task_size - STUB_DATA_PAGES * PAGE_SIZE;
BUILD_BUG_ON(!is_power_of_2(STUB_DATA_PAGES));
stub_start = (host_task_size - 1) & ~(STUB_DATA_PAGES * PAGE_SIZE - 1);
/* another page for the code portion */ /* another page for the code portion */
stub_start -= PAGE_SIZE; stub_start -= PAGE_SIZE;
host_task_size = stub_start; host_task_size = stub_start;

View File

@ -112,10 +112,14 @@ static __always_inline void *get_stub_data(void)
unsigned long ret; unsigned long ret;
asm volatile ( asm volatile (
"movl %%esp,%0 ;" "call _here_%=;"
"andl %1,%0" "_here_%=:"
"popl %0;"
"andl %1, %0 ;"
"addl %2, %0 ;"
: "=a" (ret) : "=a" (ret)
: "g" (~(STUB_DATA_PAGES * UM_KERN_PAGE_SIZE - 1))); : "g" (~(UM_KERN_PAGE_SIZE - 1)),
"g" (UM_KERN_PAGE_SIZE));
return (void *)ret; return (void *)ret;
} }

View File

@ -117,10 +117,12 @@ static __always_inline void *get_stub_data(void)
unsigned long ret; unsigned long ret;
asm volatile ( asm volatile (
"movq %%rsp,%0 ;" "lea 0(%%rip), %0;"
"andq %1,%0" "andq %1, %0 ;"
"addq %2, %0 ;"
: "=a" (ret) : "=a" (ret)
: "g" (~(STUB_DATA_PAGES * UM_KERN_PAGE_SIZE - 1))); : "g" (~(UM_KERN_PAGE_SIZE - 1)),
"g" (UM_KERN_PAGE_SIZE));
return (void *)ret; return (void *)ret;
} }