I know the stack size is controllable through the limits facility, but how does the kernel enforce some of these limits, such as RLIMIT_STACK? Since linux is not involved in stack operations (it's just a mov or push instruction), how does the kernel issue SIGSEGV when you exceed the limit? I understand that for virtual addressing, the CPU provides a facility the linux kernel can use. Is this similar to how the stack size limit is enforced? Or does linux perform a routine check on stack sizes and issue segfaults 'after the crime has occurred'? Or is there some third option?
Asked
Active
Viewed 1,258 times
1 Answers
3
The kernel can control this due to the virtual memory. The virtual memory (also known as memory mapping), is basically a list of virtual memory areas (base + size) and a target physically memory area that the kernel can manipulate that is unique to each program. When a program tries to access an address that is not on this list, an exception happens. This exception will cause a context switch into kernel mode. The kernel can look up the fault. If the memory is to become valid, it will be put into place before the program can continue (swap and mmap not read from disk yet for instance) or a SEGFAULT can be generated.
In order to decide the stack size limit, the kernel simply manipulates the virtual memory map.

Stian Skjelstad
- 2,277
- 1
- 9
- 19
-
thanks, that makes sense. related, does this mean anything in the top 8mb (or stack size limit) of memory will be mapped on exception by the kernel, whereas areas below that must be requested via SYS_BRK? – no no Jun 01 '16 at 19:58
-
brk/sbrk can be used my libc to increase the heap-space. Man page specifies that this normally follows the data area. The stack-memory can in theory be placed at any memory location, and many kernels and OS's does exactly this, to make it harder to exploit software with security faults in them. – Stian Skjelstad Jun 01 '16 at 20:22