Skip to content

Commit dfbdcda

Browse files
committed
gcc-plugins: arm-ssp: Prepare for THREAD_INFO_IN_TASK support
We will be enabling THREAD_INFO_IN_TASK support for ARM, which means that we can no longer load the stack canary value by masking the stack pointer and taking the copy that lives in thread_info. Instead, we will be able to load it from the task_struct directly, by using the TPIDRURO register which will hold the current task pointer when THREAD_INFO_IN_TASK is in effect. This is much more straight-forward, and allows us to declutter this code a bit while at it. Note that this means that ARMv6 (non-v6K) SMP systems can no longer use this feature, but those are quite rare to begin with, so this is a reasonable trade off. Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Ard Biesheuvel <[email protected]> Reviewed-by: Linus Walleij <[email protected]> Tested-by: Amit Daniel Kachhap <[email protected]>
1 parent 6880fa6 commit dfbdcda

File tree

7 files changed

+8
-39
lines changed

7 files changed

+8
-39
lines changed

arch/arm/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1600,7 +1600,7 @@ config XEN
16001600

16011601
config STACKPROTECTOR_PER_TASK
16021602
bool "Use a unique stack canary value for each task"
1603-
depends on GCC_PLUGINS && STACKPROTECTOR && SMP && !XIP_DEFLATED_DATA
1603+
depends on GCC_PLUGINS && STACKPROTECTOR && THREAD_INFO_IN_TASK && !XIP_DEFLATED_DATA
16041604
select GCC_PLUGIN_ARM_SSP_PER_TASK
16051605
default y
16061606
help

arch/arm/Makefile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,8 @@ ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y)
273273
prepare: stack_protector_prepare
274274
stack_protector_prepare: prepare0
275275
$(eval SSP_PLUGIN_CFLAGS := \
276-
-fplugin-arg-arm_ssp_per_task_plugin-tso=$(shell \
277-
awk '{if ($$2 == "THREAD_SZ_ORDER") print $$3;}'\
278-
include/generated/asm-offsets.h) \
279276
-fplugin-arg-arm_ssp_per_task_plugin-offset=$(shell \
280-
awk '{if ($$2 == "TI_STACK_CANARY") print $$3;}'\
277+
awk '{if ($$2 == "TSK_STACK_CANARY") print $$3;}'\
281278
include/generated/asm-offsets.h))
282279
$(eval KBUILD_CFLAGS += $(SSP_PLUGIN_CFLAGS))
283280
$(eval GCC_PLUGINS_CFLAGS += $(SSP_PLUGIN_CFLAGS))

arch/arm/include/asm/stackprotector.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ static __always_inline void boot_init_stack_canary(void)
3939
current->stack_canary = canary;
4040
#ifndef CONFIG_STACKPROTECTOR_PER_TASK
4141
__stack_chk_guard = current->stack_canary;
42-
#else
43-
current_thread_info()->stack_canary = current->stack_canary;
4442
#endif
4543
}
4644

arch/arm/include/asm/thread_info.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ struct thread_info {
5555
struct task_struct *task; /* main task structure */
5656
__u32 cpu; /* cpu */
5757
__u32 cpu_domain; /* cpu domain */
58-
#ifdef CONFIG_STACKPROTECTOR_PER_TASK
59-
unsigned long stack_canary;
60-
#endif
6158
struct cpu_context_save cpu_context; /* cpu context */
6259
__u32 abi_syscall; /* ABI type and syscall nr */
6360
__u8 used_cp[16]; /* thread used copro */

arch/arm/kernel/asm-offsets.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ int main(void)
6363
#ifdef CONFIG_IWMMXT
6464
DEFINE(TI_IWMMXT_STATE, offsetof(struct thread_info, fpstate.iwmmxt));
6565
#endif
66-
#ifdef CONFIG_STACKPROTECTOR_PER_TASK
67-
DEFINE(TI_STACK_CANARY, offsetof(struct thread_info, stack_canary));
68-
#endif
69-
DEFINE(THREAD_SZ_ORDER, THREAD_SIZE_ORDER);
7066
BLANK();
7167
DEFINE(S_R0, offsetof(struct pt_regs, ARM_r0));
7268
DEFINE(S_R1, offsetof(struct pt_regs, ARM_r1));

arch/arm/kernel/process.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,6 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start,
269269

270270
thread_notify(THREAD_NOTIFY_COPY, thread);
271271

272-
#ifdef CONFIG_STACKPROTECTOR_PER_TASK
273-
thread->stack_canary = p->stack_canary;
274-
#endif
275-
276272
return 0;
277273
}
278274

scripts/gcc-plugins/arm_ssp_per_task_plugin.c

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
__visible int plugin_is_GPL_compatible;
66

7-
static unsigned int sp_mask, canary_offset;
7+
static unsigned int canary_offset;
88

99
static unsigned int arm_pertask_ssp_rtl_execute(void)
1010
{
@@ -13,7 +13,7 @@ static unsigned int arm_pertask_ssp_rtl_execute(void)
1313
for (insn = get_insns(); insn; insn = NEXT_INSN(insn)) {
1414
const char *sym;
1515
rtx body;
16-
rtx mask, masked_sp;
16+
rtx current;
1717

1818
/*
1919
* Find a SET insn involving a SYMBOL_REF to __stack_chk_guard
@@ -30,19 +30,13 @@ static unsigned int arm_pertask_ssp_rtl_execute(void)
3030

3131
/*
3232
* Replace the source of the SET insn with an expression that
33-
* produces the address of the copy of the stack canary value
34-
* stored in struct thread_info
33+
* produces the address of the current task's stack canary value
3534
*/
36-
mask = GEN_INT(sext_hwi(sp_mask, GET_MODE_PRECISION(Pmode)));
37-
masked_sp = gen_reg_rtx(Pmode);
35+
current = gen_reg_rtx(Pmode);
3836

39-
emit_insn_before(gen_rtx_set(masked_sp,
40-
gen_rtx_AND(Pmode,
41-
stack_pointer_rtx,
42-
mask)),
43-
insn);
37+
emit_insn_before(gen_load_tp_hard(current), insn);
4438

45-
SET_SRC(body) = gen_rtx_PLUS(Pmode, masked_sp,
39+
SET_SRC(body) = gen_rtx_PLUS(Pmode, current,
4640
GEN_INT(canary_offset));
4741
}
4842
return 0;
@@ -72,7 +66,6 @@ __visible int plugin_init(struct plugin_name_args *plugin_info,
7266
const char * const plugin_name = plugin_info->base_name;
7367
const int argc = plugin_info->argc;
7468
const struct plugin_argument *argv = plugin_info->argv;
75-
int tso = 0;
7669
int i;
7770

7871
if (!plugin_default_version_check(version, &gcc_version)) {
@@ -91,11 +84,6 @@ __visible int plugin_init(struct plugin_name_args *plugin_info,
9184
return 1;
9285
}
9386

94-
if (!strcmp(argv[i].key, "tso")) {
95-
tso = atoi(argv[i].value);
96-
continue;
97-
}
98-
9987
if (!strcmp(argv[i].key, "offset")) {
10088
canary_offset = atoi(argv[i].value);
10189
continue;
@@ -105,9 +93,6 @@ __visible int plugin_init(struct plugin_name_args *plugin_info,
10593
return 1;
10694
}
10795

108-
/* create the mask that produces the base of the stack */
109-
sp_mask = ~((1U << (12 + tso)) - 1);
110-
11196
PASS_INFO(arm_pertask_ssp_rtl, "expand", 1, PASS_POS_INSERT_AFTER);
11297

11398
register_callback(plugin_info->base_name, PLUGIN_PASS_MANAGER_SETUP,

0 commit comments

Comments
 (0)