Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discussion][Security] Compiler and linker flags #4107

Closed
samoht0 opened this issue Jul 25, 2019 · 7 comments
Closed

[Discussion][Security] Compiler and linker flags #4107

samoht0 opened this issue Jul 25, 2019 · 7 comments

Comments

@samoht0
Copy link

samoht0 commented Jul 25, 2019

Didn't find another discussion, so opening.

General CFLAGS/CXXFLAGS are for both branches (termux_step_setup_toolchain.sh)
"-g3 -O1 -fstack-protector --param ssp-buffer-size=4 -D_FORTIFY_SOURCE=2"

-D_FORTIFY_SOURCE=2 => GOOD
-fstack-protector => OK

But what about some more security enhancements?
-fstack-protector-strong
-fstack-clash-protection
-fcf-protection (only x86, so probably not very useful)
-D_GLIBCXX_ASSERTIONS

What about -O2 instead -O1?
It's well tested and considered working fine. Should work better with -D_FORTIFY_SOURCE=2 AFAIK.

General LDFLAGS are:
" -Wl,-rpath=$TERMUX_PREFIX/lib -Wl,--enable-new-dtags"

Is there an option for improved linker options in Termux (likely not, I guess)?
-Wl,-z,relro
-Wl,-z,now

https://wiki.debian.org/Hardening

@ghost
Copy link

ghost commented Jul 25, 2019

  1. -D_FORTIFY_SOURCE=2 causes build failures due to NDK headers. (for some packages)

  2. -fstack-protector. From my experiments, it doesn't work at all or doesn't work properly. Again - due to NDK.

@ghost
Copy link

ghost commented Jul 25, 2019

-D_GLIBCXX_ASSERTIONS

This is glibc-specific thing at all.

-O2 instead -O1

Btw, the line -g3 -O1 -fstack-protector --param ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 is for debug builds only.

@samoht0
Copy link
Author

samoht0 commented Jul 25, 2019

Btw, the line -g3 -O1 -fstack-protector --param ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 is for debug builds only.

OK, so there's just -Oz as default option for regular builds in CFLAGS/CXXFLAGS.
Well, FORTIFY_SOURCE and regular stack-protection failed tests. I see, there seems to be no suitable option here.

@ghost
Copy link

ghost commented Jul 25, 2019

stack-protection failed tests

Stack protector doesn't cause build failures. It just don't work in same way as on PC.

@ghost
Copy link

ghost commented Jul 25, 2019

It just don't work in same way as on PC.

Hmm, in NDK 20 it works....

Screenshot_20190725-234422_Termux

So we can have:
C(XX)FLAGS: -Oz -fstack-protector-strong ...
LDFLAGS: -Wl,-rpath=$TERMUX_PREFIX/lib -Wl,--enable-new-dtags -Wl,-z,relro -Wl,-z,now...

Note that -Wl,-z,relro -Wl,-z,now is already used by some programs.

@ghost ghost added the discussion label Jul 25, 2019
@samoht0
Copy link
Author

samoht0 commented Jul 26, 2019

So we can have:
C(XX)FLAGS: -Oz -fstack-protector-strong ...
LDFLAGS: -Wl,-rpath=$TERMUX_PREFIX/lib -Wl,--enable-new-dtags -Wl,-z,relro -Wl,-z,now...

Fine. Some security improvements. Looks for me like best practice, currently.

Relating -fstack-clash-protection I found out, that this isn't even supported in Clang.

Leaving this open for some time, to give contributes/users the chance to comment.

ghost pushed a commit that referenced this issue Jul 29, 2019
See #4107.

In short:

 * Stack protector will be used by default.

 * GOT/PLT ELF sections will be read-only.
ghost pushed a commit that referenced this issue Jul 29, 2019
See #4107.

In short:

 * Stack protector will be used by default.

 * GOT/PLT ELF sections will be read-only.
ghost pushed a commit that referenced this issue Jul 29, 2019
See #4107.

In short:

 * Stack protector will be used by default.

 * GOT/PLT ELF sections will be read-only.
ghost pushed a commit that referenced this issue Jul 29, 2019
See #4107.

In short:

 * Stack protector will be used by default.

 * GOT/PLT ELF sections will be read-only.
@ghost
Copy link

ghost commented Jul 29, 2019

Done in e63524c.

@ghost ghost closed this as completed Jul 29, 2019
@ghost ghost locked and limited conversation to collaborators Oct 9, 2021
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant