My doubt is how to compile the binary without RELRO
? and why it is enabling FULL-RELRO when we are not providing any flags?
This is the code.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, int *argv[])
{
size_t *p = (size_t *) strtol(argv[1], NULL, 16);
p[0] = 0xDEADBEEF;
printf("RELRO: %p\n", p);
return 0;
}
While compiling the above code with the parameters:
$ gcc -g -Wl,-z,relro -o test test.c
And running the checksec
on the generated binary:
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Partial RELRO No canary found NX enabled No PIE No RPATH No RUNPATH 69 Symbols No 0 1 test
Compiling with the following command:
$ gcc -g -Wl,-z,relro,-z,now -o test test.c
And running the checksec
on generated binary:
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Full RELRO No canary found NX enabled PIE enabled No RPATH No RUNPATH 71 Symbols No 0 1 test-full
While compiling with the command:
$ gcc -o test test.c
And running the checksec
on the generated binary:
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Full RELRO No canary found NX enabled PIE enabled No RPATH No RUNPATH 66 Symbols No 0 1 test
gcc -dumpspecs
has the answer as to why it happens when you don't give the linker flag explicitly to the compiler driver. For example the packaged GCC on Ubuntu 20.04 appears to behave that way. So why not try-Wl,-z,norelro
instead? Also: this isn't exactly reverse engineering related, or where do you see the connection? Better to ask this on StackOverflow or so.