Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

Nice stack smashing postmortem message

Open mcspr opened this issue 1 year ago • 1 comments

Wire everything that relies on stack smashing detection to call __stack_chk_fail() (aka what libssp / ssp / stack-protector uses) Expose it in our debugging header

Rename overflow -> smashing, as these are different things we are trying to detect (meaning, that we check for things writing there, not some kind of alloca issue or the way -fstack-check would have worked) ref. #8666

-fstack-protector continues to work as it always did CONT replaces abort(), also moves its check to the loop wrapper to avoid dumping otherwise useless SYS context memory StackThunk replaces a similar abort() call

mcspr avatar Sep 09 '22 11:09 mcspr

btw, something to reproduce failures

#include <Arduino.h>
#include <StackThunk.h>

#include <umm_malloc/umm_malloc.h>
#include <cont.h>
#include <user_interface.h>

#include <array>

// reproduce crash for all three methods, in sequence
constexpr uintptr_t RtcMem { 0x60001200 + 128 };
auto* count = reinterpret_cast<volatile uint32_t*>(RtcMem);

extern "C" rst_info resetInfo;

// there is only a single thunk in the app
extern "C" void thunk_failme();
extern "C" void failme() {
    printf("will ravage %p\n", stack_thunk_ptr);
    *stack_thunk_ptr = 0xfafafafa;
}

make_stack_thunk(failme)

// needs -fstack-protector to work
void ssp_failme() __attribute__((stack_protect));
void ssp_failme() {
    char buf[4];
    strcpy(buf, "ABCDE");
    puts(buf);
}

void setup() {
    Serial.begin(115200);

    switch (resetInfo.reason) {
    case REASON_DEFAULT_RST:
    case REASON_EXT_SYS_RST:
        *count = 0;
        break;
    }
}

void loop() {
    const auto last = *count;
    *count = ((last + 1) % 3);

    puts("");
    printf("%u - wait for it...\n", last);
    delay(3000);

    switch (last) {
    case 0:
        g_pcont->stack_guard1 = 0xfafafafa;
        g_pcont->stack_guard2 = 0xfafafafa;
        break;
    case 1:
        stack_thunk_add_ref();
        thunk_failme();
        break;
    case 2:
        ssp_failme();
        break;
    }
}

mcspr avatar Sep 13 '22 15:09 mcspr