"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Christian Weisgerber <naddy@mips.inka.de>
Subject:
Re: const-ify tables
To:
Ted Bullock <tbullock@comlore.com>
Cc:
gameoftrees@openbsd.org
Date:
Tue, 15 Feb 2022 22:12:34 +0100

Download raw body.

Thread
Ted Bullock:

> Interesting question to me, if you force writing to one of these const
> variables (ignoring compiler warnings) what happens to the program? Are
> there any security benefits to moving data to supposedly read only memory?

They are actually placed in a memory section that is mapped read-only.

$ cat t.c
const int i = 1;

int
main(void)
{
        int *j;

        j = &i;
        *j = 2;

        return 0;
}
$ cc -o t t.c
t.c:8:4: warning: assigning to 'int *' from 'const int *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
        j = &i;
          ^ ~~
1 warning generated.
$ objdump -t t
...
00000000000004c8 g     O .rodata        0000000000000004 i
...
$ ./t
Segmentation fault (core dumped) 

The tables in got.c etc. contain pointers, so they are placed in
the .data.rel.ro section, where the ld.so(1) dynamic linker first
updates the pointers to their final values and then maps the region
read-only before calling the main program.

-- 
Christian "naddy" Weisgerber                          naddy@mips.inka.de