I wrote a simple code trying to find out if we can read and print the memory in code segment:
#include <stdio.h>
void main() {
int *code_ptr = 0x4;
printf("code_ptr = %x\n", code_ptr);
printf("*code_ptr = %x\n", *code_ptr);
}
My system is x86_64 + Ubuntu 19.04 (Disco Dingo). And the program failed with the following output:
code_ptr = 4
Segmentation fault (core dumped)
IIUC, in Linux, the code segment and data segment share the same base address. And if that's true, this program will read the memory in code segement, and I was expecting that there won't be any crash since 0x04
should be in the range of data segment (which starts at the beginning). And this should pass the paging check since the mapped memory for the code segment is read-only and we only read the memory here.
So did I miss anything or is there any other mechanisms that prevent us from reading from this %ds:0x4
?