Bug 82411 - const is not always read-only
Summary: const is not always read-only
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-10-02 20:26 UTC by Kees Cook
Modified: 2017-10-05 00:46 UTC (History)
2 users (show)

See Also:
Host:
Target: powerpc*-*-*
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-10-02 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kees Cook 2017-10-02 20:26:53 UTC
On powerpc, a const variable may end up in the .sdata section, which is writable. This means authors cannot depend on the "const" marking to mean "read-only", as is required for sane Linux kernel memory protection security.

Thread here:
https://lkml.org/lkml/2017/10/2/488

At the very least, there should be a way to request never putting a const variable into a writable section.
Comment 1 Andrew Pinski 2017-10-02 20:54:03 UTC
Actually it is just undefined what happens when a write to a const variable. 

So a trap or the write happened for a const both are valid thing.

Now const really should be put in the read only section if possible.

Putting it in the sdata section is valid thing to do but I doubt some people want it there. 

Powerpc should have an option which disabled this optimization for const variables.

Right now there is a way to disable all of sdata by -G0 option which is a good workaround.
Comment 2 Segher Boessenkool 2017-10-02 21:25:29 UTC
To access this as sdata is faster and smaller than as rodata (one
instruction instead of two).

You can use -G0 as Andrew says, or -mno-sdata (or -msdata=none).
Comment 3 Kees Cook 2017-10-04 23:41:50 UTC
To clarify, using -mno-sdata means all things are removed from sdata, not just const, yes? I'd like to be able to leave writable stuff there, to avoid any additional performance penalty.
Comment 4 Segher Boessenkool 2017-10-05 00:46:39 UTC
That what it means yes.  You can use it as a workaround.

There is no option yet to put only writable data in sdata.