Write a simple C arbitrary code execution exploit on ARM Cortex-M3?

青春壹個敷衍的年華 提交于 2019-12-05 10:16:23

Sorry for abusing the answer form, I have adapted your code a little and it blinks a LED right from the stack:

void (*_delay_ms)(uint32_t) = delay_ms;

static void loopit(void)
{
    while (1)
    {
        GPIOC->ODR ^= 1 << 13;
        _delay_ms(125);
    }
}

void attack(void)
{
    volatile uint8_t buffer[64] __attribute__((aligned(4)));
    memcpy(buffer, (void *)((uint32_t) loopit & ~1), sizeof(buffer));
    goto *(void *)((uint32_t) buffer | 1);
}

I wonder how soon I get complaints about UB.

I ended up not using goto and not trying to execute any functions from the function copied into stack memory. Also be sure to compile the stack function with noinline and O0.

I used the following code to cast the stack address into a function pointer:

// Needed a big buffer and copied to the middle of it
#define FUNC_SIZE 256
#define BUF_SIZE (FUNC_SIZE * 3)

uint8_t mybuf[BUF_SIZE] __attribute__((aligned(8)));
uintptr_t stackfunc = (uintptr_t) mybuf;
stackfunc += FUNC_SIZE;

memcpy((void *) stackfunc, (void *) (((uintptr_t) &flashfunc) & ~1), FUNC_SIZE);

void (*jump_to_stack)(void) = (void (*)(void)) ((uintptr_t) stackfunc | 1);
jump_to_stack();

Not sure why I had to make the buffer so big. I copied the function to the middle of the buffer.

void attack(void)
{
    uint16_t buffer[64];
    goto *((void *) (((unsigned int)(buffer)) | 1));
}

you asked it to do a branch, it does not need the lsbit set for a branch, a branch exchange sure. In this case let the tool do its job. Or if there is a concern use assembly language to perform the branch so that you can specifically control the instruction used and thus the address.

00000000 <attack>:
   0:   b0a0        sub sp, #128    ; 0x80
   2:   2301        movs    r3, #1
   4:   466a        mov r2, sp
   6:   4313        orrs    r3, r2
   8:   469f        mov pc, r3
   a:   46c0        nop         ; (mov r8, r8)

Not even a branch in this case but a mov pc (functionally the same). Which is definitely not on the list of interworked instructions. See the architectural reference manual.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!