Skip to content

Commit 87f0df7

Browse files
rpedgecohansendc
authored andcommitted
x86/shstk: Move arch detail comment out of core mm
The comment around VM_SHADOW_STACK in mm.h refers to a lot of x86 specific details that don't belong in a cross arch file. Remove these out of core mm, and just leave the non-arch details. Since the comment includes some useful details that would be good to retain in the source somewhere, put the arch specifics parts in arch/x86/shstk.c near alloc_shstk(), where memory of this type is allocated. Include a reference to the existence of the x86 details near the VM_SHADOW_STACK definition mm.h. Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Mark Brown <broonie@kernel.org> Link: https://lore.kernel.org/all/20230706233248.445713-1-rick.p.edgecombe%40intel.com
1 parent 67840ad commit 87f0df7

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

arch/x86/kernel/shstk.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,31 @@ static int create_rstor_token(unsigned long ssp, unsigned long *token_addr)
7272
return 0;
7373
}
7474

75+
/*
76+
* VM_SHADOW_STACK will have a guard page. This helps userspace protect
77+
* itself from attacks. The reasoning is as follows:
78+
*
79+
* The shadow stack pointer(SSP) is moved by CALL, RET, and INCSSPQ. The
80+
* INCSSP instruction can increment the shadow stack pointer. It is the
81+
* shadow stack analog of an instruction like:
82+
*
83+
* addq $0x80, %rsp
84+
*
85+
* However, there is one important difference between an ADD on %rsp
86+
* and INCSSP. In addition to modifying SSP, INCSSP also reads from the
87+
* memory of the first and last elements that were "popped". It can be
88+
* thought of as acting like this:
89+
*
90+
* READ_ONCE(ssp); // read+discard top element on stack
91+
* ssp += nr_to_pop * 8; // move the shadow stack
92+
* READ_ONCE(ssp-8); // read+discard last popped stack element
93+
*
94+
* The maximum distance INCSSP can move the SSP is 2040 bytes, before
95+
* it would read the memory. Therefore a single page gap will be enough
96+
* to prevent any operation from shifting the SSP to an adjacent stack,
97+
* since it would have to land in the gap at least once, causing a
98+
* fault.
99+
*/
75100
static unsigned long alloc_shstk(unsigned long addr, unsigned long size,
76101
unsigned long token_offset, bool set_res_tok)
77102
{

include/linux/mm.h

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -343,33 +343,13 @@ extern unsigned int kobjsize(const void *objp);
343343

344344
#ifdef CONFIG_X86_USER_SHADOW_STACK
345345
/*
346-
* This flag should not be set with VM_SHARED because of lack of support
347-
* core mm. It will also get a guard page. This helps userspace protect
348-
* itself from attacks. The reasoning is as follows:
346+
* VM_SHADOW_STACK should not be set with VM_SHARED because of lack of
347+
* support core mm.
349348
*
350-
* The shadow stack pointer(SSP) is moved by CALL, RET, and INCSSPQ. The
351-
* INCSSP instruction can increment the shadow stack pointer. It is the
352-
* shadow stack analog of an instruction like:
353-
*
354-
* addq $0x80, %rsp
355-
*
356-
* However, there is one important difference between an ADD on %rsp
357-
* and INCSSP. In addition to modifying SSP, INCSSP also reads from the
358-
* memory of the first and last elements that were "popped". It can be
359-
* thought of as acting like this:
360-
*
361-
* READ_ONCE(ssp); // read+discard top element on stack
362-
* ssp += nr_to_pop * 8; // move the shadow stack
363-
* READ_ONCE(ssp-8); // read+discard last popped stack element
364-
*
365-
* The maximum distance INCSSP can move the SSP is 2040 bytes, before
366-
* it would read the memory. Therefore a single page gap will be enough
367-
* to prevent any operation from shifting the SSP to an adjacent stack,
368-
* since it would have to land in the gap at least once, causing a
369-
* fault.
370-
*
371-
* Prevent using INCSSP to move the SSP between shadow stacks by
372-
* having a PAGE_SIZE guard gap.
349+
* These VMAs will get a single end guard page. This helps userspace protect
350+
* itself from attacks. A single page is enough for current shadow stack archs
351+
* (x86). See the comments near alloc_shstk() in arch/x86/kernel/shstk.c
352+
* for more details on the guard size.
373353
*/
374354
# define VM_SHADOW_STACK VM_HIGH_ARCH_5
375355
#else

0 commit comments

Comments
 (0)