Skip to content

Commit 270006d

Browse files
Peter Zijlstraopsiff
authored andcommitted
x86/its: Use dynamic thunks for indirect branches
commit 872df34 upstream. ITS mitigation moves the unsafe indirect branches to a safe thunk. This could degrade the prediction accuracy as the source address of indirect branches becomes same for different execution paths. To improve the predictions, and hence the performance, assign a separate thunk for each indirect callsite. This is also a defense-in-depth measure to avoid indirect branches aliasing with each other. As an example, 5000 dynamic thunks would utilize around 16 bits of the address space, thereby gaining entropy. For a BTB that uses 32 bits for indexing, dynamic thunks could provide better prediction accuracy over fixed thunks. Have ITS thunks be variable sized and use EXECMEM_MODULE_TEXT such that they are both more flexible (got to extend them later) and live in 2M TLBs, just like kernel code, avoiding undue TLB pressure. [ pawan: CONFIG_EXECMEM and CONFIG_EXECMEM_ROX are not supported on backport kernel, made changes to use module_alloc() and set_memory_*() for dynamic thunks. ] Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Pawan Gupta <pawan.kumar.gupta@linux.intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Alexandre Chartre <alexandre.chartre@oracle.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> (cherry picked from commit 3b2234cd50a9b4ed664324054901b6f763890104)
1 parent 7a63184 commit 270006d

4 files changed

Lines changed: 151 additions & 3 deletions

File tree

arch/x86/include/asm/alternative.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ static __always_inline int x86_call_depth_emit_accounting(u8 **pprog,
130130
}
131131
#endif
132132

133+
#ifdef CONFIG_MITIGATION_ITS
134+
extern void its_init_mod(struct module *mod);
135+
extern void its_fini_mod(struct module *mod);
136+
extern void its_free_mod(struct module *mod);
137+
#else /* CONFIG_MITIGATION_ITS */
138+
static inline void its_init_mod(struct module *mod) { }
139+
static inline void its_fini_mod(struct module *mod) { }
140+
static inline void its_free_mod(struct module *mod) { }
141+
#endif
142+
133143
#if defined(CONFIG_RETHUNK) && defined(CONFIG_OBJTOOL)
134144
extern bool cpu_wants_rethunk(void);
135145
extern bool cpu_wants_rethunk_at(void *addr);

arch/x86/kernel/alternative.c

Lines changed: 129 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <linux/mmu_context.h>
1919
#include <linux/bsearch.h>
2020
#include <linux/sync_core.h>
21+
#include <linux/moduleloader.h>
22+
#include <linux/cleanup.h>
2123
#include <asm/text-patching.h>
2224
#include <asm/alternative.h>
2325
#include <asm/sections.h>
@@ -31,6 +33,7 @@
3133
#include <asm/paravirt.h>
3234
#include <asm/asm-prototypes.h>
3335
#include <asm/cfi.h>
36+
#include <asm/set_memory.h>
3437

3538
int __read_mostly alternatives_patched;
3639

@@ -124,6 +127,125 @@ const unsigned char * const x86_nops[ASM_NOP_MAX+1] =
124127
#endif
125128
};
126129

130+
#ifdef CONFIG_MITIGATION_ITS
131+
132+
static struct module *its_mod;
133+
static void *its_page;
134+
static unsigned int its_offset;
135+
136+
/* Initialize a thunk with the "jmp *reg; int3" instructions. */
137+
static void *its_init_thunk(void *thunk, int reg)
138+
{
139+
u8 *bytes = thunk;
140+
int i = 0;
141+
142+
if (reg >= 8) {
143+
bytes[i++] = 0x41; /* REX.B prefix */
144+
reg -= 8;
145+
}
146+
bytes[i++] = 0xff;
147+
bytes[i++] = 0xe0 + reg; /* jmp *reg */
148+
bytes[i++] = 0xcc;
149+
150+
return thunk;
151+
}
152+
153+
void its_init_mod(struct module *mod)
154+
{
155+
if (!cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS))
156+
return;
157+
158+
mutex_lock(&text_mutex);
159+
its_mod = mod;
160+
its_page = NULL;
161+
}
162+
163+
void its_fini_mod(struct module *mod)
164+
{
165+
if (!cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS))
166+
return;
167+
168+
WARN_ON_ONCE(its_mod != mod);
169+
170+
its_mod = NULL;
171+
its_page = NULL;
172+
mutex_unlock(&text_mutex);
173+
174+
for (int i = 0; i < mod->its_num_pages; i++) {
175+
void *page = mod->its_page_array[i];
176+
set_memory_rox((unsigned long)page, 1);
177+
}
178+
}
179+
180+
void its_free_mod(struct module *mod)
181+
{
182+
if (!cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS))
183+
return;
184+
185+
for (int i = 0; i < mod->its_num_pages; i++) {
186+
void *page = mod->its_page_array[i];
187+
module_memfree(page);
188+
}
189+
kfree(mod->its_page_array);
190+
}
191+
192+
DEFINE_FREE(its_execmem, void *, if (_T) module_memfree(_T));
193+
194+
static void *its_alloc(void)
195+
{
196+
void *page __free(its_execmem) = module_alloc(PAGE_SIZE);
197+
198+
if (!page)
199+
return NULL;
200+
201+
if (its_mod) {
202+
void *tmp = krealloc(its_mod->its_page_array,
203+
(its_mod->its_num_pages+1) * sizeof(void *),
204+
GFP_KERNEL);
205+
if (!tmp)
206+
return NULL;
207+
208+
its_mod->its_page_array = tmp;
209+
its_mod->its_page_array[its_mod->its_num_pages++] = page;
210+
}
211+
212+
return no_free_ptr(page);
213+
}
214+
215+
static void *its_allocate_thunk(int reg)
216+
{
217+
int size = 3 + (reg / 8);
218+
void *thunk;
219+
220+
if (!its_page || (its_offset + size - 1) >= PAGE_SIZE) {
221+
its_page = its_alloc();
222+
if (!its_page) {
223+
pr_err("ITS page allocation failed\n");
224+
return NULL;
225+
}
226+
memset(its_page, INT3_INSN_OPCODE, PAGE_SIZE);
227+
its_offset = 32;
228+
}
229+
230+
/*
231+
* If the indirect branch instruction will be in the lower half
232+
* of a cacheline, then update the offset to reach the upper half.
233+
*/
234+
if ((its_offset + size - 1) % 64 < 32)
235+
its_offset = ((its_offset - 1) | 0x3F) + 33;
236+
237+
thunk = its_page + its_offset;
238+
its_offset += size;
239+
240+
set_memory_rw((unsigned long)its_page, 1);
241+
thunk = its_init_thunk(thunk, reg);
242+
set_memory_rox((unsigned long)its_page, 1);
243+
244+
return thunk;
245+
}
246+
247+
#endif
248+
127249
/*
128250
* Fill the buffer with a single effective instruction of size @len.
129251
*
@@ -577,9 +699,13 @@ static int emit_call_track_retpoline(void *addr, struct insn *insn, int reg, u8
577699
#ifdef CONFIG_MITIGATION_ITS
578700
static int emit_its_trampoline(void *addr, struct insn *insn, int reg, u8 *bytes)
579701
{
580-
return __emit_trampoline(addr, insn, bytes,
581-
__x86_indirect_its_thunk_array[reg],
582-
__x86_indirect_its_thunk_array[reg]);
702+
u8 *thunk = __x86_indirect_its_thunk_array[reg];
703+
u8 *tmp = its_allocate_thunk(reg);
704+
705+
if (tmp)
706+
thunk = tmp;
707+
708+
return __emit_trampoline(addr, insn, bytes, thunk, thunk);
583709
}
584710

585711
/* Check if an indirect branch is at ITS-unsafe address */

arch/x86/kernel/module.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ int module_finalize(const Elf_Ehdr *hdr,
312312
void *pseg = (void *)para->sh_addr;
313313
apply_paravirt(pseg, pseg + para->sh_size);
314314
}
315+
316+
its_init_mod(me);
317+
315318
if (retpolines || cfi) {
316319
void *rseg = NULL, *cseg = NULL;
317320
unsigned int rsize = 0, csize = 0;
@@ -332,6 +335,9 @@ int module_finalize(const Elf_Ehdr *hdr,
332335
void *rseg = (void *)retpolines->sh_addr;
333336
apply_retpolines(rseg, rseg + retpolines->sh_size);
334337
}
338+
339+
its_fini_mod(me);
340+
335341
if (returns) {
336342
void *rseg = (void *)returns->sh_addr;
337343
apply_returns(rseg, rseg + returns->sh_size);
@@ -379,4 +385,5 @@ int module_finalize(const Elf_Ehdr *hdr,
379385
void module_arch_cleanup(struct module *mod)
380386
{
381387
alternatives_smp_module_del(mod);
388+
its_free_mod(mod);
382389
}

include/linux/module.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,11 @@ struct module {
572572
atomic_t refcnt;
573573
#endif
574574

575+
#ifdef CONFIG_MITIGATION_ITS
576+
int its_num_pages;
577+
void **its_page_array;
578+
#endif
579+
575580
#ifdef CONFIG_CONSTRUCTORS
576581
/* Constructor functions. */
577582
ctor_fn_t *ctors;

0 commit comments

Comments
 (0)