Search code examples
carrayslinux-kernelkernelstack-overflow

Stackoverflow in kernel module


I reading a book "Linux Kernel. Development. Third Edition." by Robert Love.

What I read about stack size in this book:

On x86, the stack size is configurable at compile-time and can be either 4KB or 8KB. Historically, the kernel stack is two pages, which generally implies that it is 8KB on 32-bit architectures and 16KB on 64-bit architectures—this size is fixed and absolute

I have VM with ubuntu 16.06 64-bit 4.15 kernel. So my stack size should be 16000 bytes (16KB)

I try to check stackoverflow behavior. I create array on the stack which use more than 16000 bytes.

#include <linux/module.h>
#include <linux/init.h>

int __init overflow_start(void)
{
        printk(KERN_INFO "Overflow Test\n");
        char array[170000] = {[0 ... 16999] =  'A'};

        printk(KERN_ERR "%c\n", array[16999]);

        return 0;
}

void __exit overflow_end(void)
{
        printk(KERN_ERR "Test success\n");
}

module_init(overflow_start);
module_exit(overflow_end);

MODULE_LICENSE("GPL");

I think that I should see kernel panic with stack smashing or something similar, but I see only correct output. Why it's not break the stack?


Solution

  • You could also make the array volatile and GCC won't optimize it.

    volatile char array[170000] = {[0 ... 16999] =  'A'};