Skip to content

Commit 01100ea

Browse files
Andrew Boienashif
authored andcommitted
kernel: add stack canary to libc partition
User mode needs to be able to read this value in compiler generated function prologues/epilogues. Special handling in init.c for arches that use _data_copy. This happens before _Cstart() gets called. We need to make sure that the compiler stack canary checks in _data_copy itself do not fail. Signed-off-by: Andrew Boie <[email protected]>
1 parent 17ce822 commit 01100ea

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

kernel/compiler_stack_protect.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <toolchain.h>
2323
#include <linker/sections.h>
2424
#include <kernel.h>
25+
#include <app_memory/app_memdomain.h>
2526

2627
/**
2728
*
@@ -45,7 +46,11 @@ void FUNC_NORETURN _StackCheckHandler(void)
4546
* Symbol referenced by GCC compiler generated code for canary value.
4647
* The canary value gets initialized in _Cstart().
4748
*/
48-
void __noinit *__stack_chk_guard;
49+
#ifdef CONFIG_APP_SHARED_MEM
50+
K_APP_DMEM(z_libc_partition) uintptr_t __stack_chk_guard;
51+
#else
52+
__noinit uintptr_t __stack_chk_guard;
53+
#endif
4954

5055
/**
5156
*

kernel/init.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ void _bss_zero(void)
159159
#endif
160160
}
161161

162+
#ifdef CONFIG_STACK_CANARIES
163+
extern volatile uintptr_t __stack_chk_guard;
164+
#endif /* CONFIG_STACK_CANARIES */
165+
162166

163167
#ifdef CONFIG_XIP
164168
/**
@@ -187,9 +191,29 @@ void _data_copy(void)
187191
data_copy_xip_relocation();
188192
#endif /* CONFIG_CODE_DATA_RELOCATION */
189193
#ifdef CONFIG_APP_SHARED_MEM
194+
#ifdef CONFIG_STACK_CANARIES
195+
/* stack canary checking is active for all C functions.
196+
* __stack_chk_guard is some uninitialized value living in the
197+
* app shared memory sections. Preserve it, and don't make any
198+
* function calls to perform the memory copy. The true canary
199+
* value gets set later in _Cstart().
200+
*/
201+
uintptr_t guard_copy = __stack_chk_guard;
202+
u8_t *src = (u8_t *)&_app_smem_rom_start;
203+
u8_t *dst = (u8_t *)&_app_smem_start;
204+
u32_t count = (u32_t)&_app_smem_end - (u32_t)&_app_smem_start;
205+
206+
guard_copy = __stack_chk_guard;
207+
while (count > 0) {
208+
*(dst++) = *(src++);
209+
count--;
210+
}
211+
__stack_chk_guard = guard_copy;
212+
#else
190213
(void)memcpy(&_app_smem_start, &_app_smem_rom_start,
191214
((u32_t) &_app_smem_end - (u32_t) &_app_smem_start));
192-
#endif
215+
#endif /* CONFIG_STACK_CANARIES */
216+
#endif /* CONFIG_APP_SHARED_MEM */
193217
}
194218
#endif
195219

@@ -439,10 +463,6 @@ u32_t z_early_boot_rand32_get(void)
439463
return sys_rand32_get();
440464
}
441465

442-
#ifdef CONFIG_STACK_CANARIES
443-
extern uintptr_t __stack_chk_guard;
444-
#endif /* CONFIG_STACK_CANARIES */
445-
446466
/**
447467
*
448468
* @brief Initialize kernel

0 commit comments

Comments
 (0)