Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Useful GCC flags to improve security of your programs?

Tags:

c

security

gcc

By pure chance I stumbled over an article mentioning you can "enable" ASLR with -pie -fPIE (or, rather, make your application ASLR-aware). -fstack-protector is also commonly recommended (though I rarely see explanations how and against which kinds of attacks it protects).

Is there a list of useful options and explanations how they increase the security?

...

And how useful are such measures anyway, when your application uses about 30 libraries that use none of those? ;)

like image 696
Creshal Avatar asked Feb 22 '12 15:02

Creshal


People also ask

What is the purpose of the flag for the gcc command?

By default, GCC limits the size of functions that can be inlined. This flag allows coarse control of this limit. n is the size of functions that can be inlined in number of pseudo instructions.

Does the order of gcc flags matter?

Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.

Which gcc flag is used to generate debug information for any binary file?

gcc -g generates debug information to be used by GDB debugger.


3 Answers

Hardened Gentoo uses these flags:

CFLAGS="-fPIE -fstack-protector-all -D_FORTIFY_SOURCE=2" 
LDFLAGS="-Wl,-z,now -Wl,-z,relro"

I saw about 5-10% performance drop in comparison to optimized Gentoo linux (incl. PaX/SElinux and other measures, not just CFLAGS) in default phoronix benchmark suite.

like image 175
Tomas Pruzina Avatar answered Oct 22 '22 17:10

Tomas Pruzina


As for your final question:

And how useful are such measures anyway, when your application uses about 30 libraries that use none of those? ;)

PIE is only necessary for the main program to be able to be loaded at a random address. ASLR always works for shared libraries, so the benefit of PIE is the same whether you're using one shared library or 100.

Stack protector will only benefit the code that's compiled with stack protector, so using it just in your main program will not help if your libraries are full of vulnerabilities.

In any case, I would encourage you not to consider these options part of your application, but instead part of the whole system integration. If you're using 30+ libraries (probably most of which are junk when it comes to code quality and security) in a program that will be interfacing with untrusted, potentially-malicious data, it would be a good idea to build your whole system with stack protector and other security hardening options.

Do keep in mind, however, that the highest levels of _FORTIFY_SOURCE and perhaps some other new security options break valid things that legitimate, correct programs may need to do, and thus you may need to analyze whether it's safe to use them. One known-dangerous thing that one of the options does (I forget which) is making it so the %n specifier to printf does not work, at least in certain cases. If an application is using %n to get an offset into a generated string and needs to use that offset to later write in it, and the value isn't filled in, that's a potential vulnerability in itself...

like image 30
R.. GitHub STOP HELPING ICE Avatar answered Oct 22 '22 19:10

R.. GitHub STOP HELPING ICE


The Hardening page on the Debian wiki explains at least the most commons ones which are usable on Linux. Missing from your list is at least -D_FORTIFY_SOURCE=2, -Wformat, -Wformat-security, and for the dynamic loader the relro and now features.

like image 5
janneb Avatar answered Oct 22 '22 19:10

janneb