Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 484714 - sys-devel/gcc: enable -fstack-protector by default
Summary: sys-devel/gcc: enable -fstack-protector by default
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Core system (show other bugs)
Hardware: All Linux
: Normal enhancement (vote)
Assignee: Gentoo Toolchain Maintainers
URL:
Whiteboard:
Keywords:
: 508420 (view as bug list)
Depends on: 513706
Blocks:
  Show dependency tree
 
Reported: 2013-09-13 05:56 UTC by Ryan Hill (RETIRED)
Modified: 2017-04-12 15:58 UTC (History)
10 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
09_all_default-ssp.patch (09_all_default-ssp.patch,8.80 KB, patch)
2013-09-13 05:56 UTC, Ryan Hill (RETIRED)
Details | Diff
Change make_gcc_hard in the toolchain eclass so we pass ssp as defult (toolchin.eclass.patch,3.17 KB, patch)
2013-12-31 16:45 UTC, Magnus Granberg
Details | Diff
Updated default_ssp patch (09_all_default-ssp.patch,10.89 KB, patch)
2013-12-31 16:48 UTC, Magnus Granberg
Details | Diff
Update default_ssp with gcc-specs-ssp support (09_all_default-ssp.patch,11.65 KB, patch)
2014-01-01 08:55 UTC, Magnus Granberg
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ryan Hill (RETIRED) gentoo-dev 2013-09-13 05:56:58 UTC
Created attachment 358566 [details, diff]
09_all_default-ssp.patch

Consensus on gentoo-dev is that we should enable -fstack-protector by default.  

I looked at a few different distro's SSP patches and decided to go with Debian's approach.  It works with our ESP patchset and unlike most others handles the issue of SSP not being available on some archs.

I've tested that the patch handles the different hardened gcc-config profiles correctly.  The default adds -fstack-protector-all and hardenednopiessp, hardenednossp, and vanilla all use -fstack-protector.

Beyond building it and playing with the options I've haven't done any other testing yet so YMMV.

Outstanding issues:

- the pie patchset has a "hack" for gcc-specs-* and _filter-hardened() for flag-o-matic.  this is required for stuff like `filter-flags -fstack-protector`.  I don't understand how this works or what the hell _gcc-specs-directive_raw() is doing.  Can someone explain it?

- 03_all_gcc48_Makefile.in.patch in the ESP patchset adds -fno-stack-protector to BOOT_CFLAGS, LIBCFLAGS and LIBCXXFLAGS.  Is that required because of -fstack-protector-all or is it needed for -fstack-protector too? (I don't see it in other distro's ssp patches)

- the ESP patchset drops -fstack-protector-all when -nostdlib or -nodefaultlibs are used.  Debian uses -nostdlib or -ffreestanding.

- should we also disable SSP with -D__KERNEL__ or rely on the kernel's build system to add -fno-stack-protector when CONFIG_CC_STACKPROTECTOR is disabled?

- ssp-buffer-size is reduced from the default 8 bytes to 4.  Most distros do this, so I kept it in.

- the glibc SSP core handler patches.  I think these are unrelated?

- glibc common.eblit has:

  use hardened && gcc-specs-ssp && append-cflags $(test-flags-CC -fno-stack-protector)

Once gcc-specs-ssp works "hardened" will have to be dropped from this.
Comment 1 Magnus Granberg gentoo-dev 2013-09-13 21:09:38 UTC
(In reply to Ryan Hill from comment #0)
> Created attachment 358566 [details, diff] [details, diff]
> 09_all_default-ssp.patch
> 
> Consensus on gentoo-dev is that we should enable -fstack-protector by
> default.  
> 
> I looked at a few different distro's SSP patches and decided to go with
> Debian's approach.  It works with our ESP patchset and unlike most others
> handles the issue of SSP not being available on some archs.
>
The patch is applay only if the arch support it check debian/rules.patch.
Uclibc have problems if is not build with tls and nptl #470608

> I've tested that the patch handles the different hardened gcc-config
> profiles correctly.  The default adds -fstack-protector-all and
> hardenednopiessp, hardenednossp, and vanilla all use -fstack-protector.
> 
> Beyond building it and playing with the options I've haven't done any other
> testing yet so YMMV.
> 
> Outstanding issues:
> 
> - the pie patchset has a "hack" for gcc-specs-* and _filter-hardened() for
> flag-o-matic.  this is required for stuff like `filter-flags
> -fstack-protector`.  I don't understand how this works or what the hell
> _gcc-specs-directive_raw() is doing.  Can someone explain it?
>
In short the gcc-specs-directive_raw() make a specdump and include any sub spec rules and spec files that get included and sort out the stacking order of the spec rules.

gcc-specs-ssp grep the raw cc1 spec rule for "!fno-stack-protector:".
All the gcc-specs-* was before my time and when i move the hardened spec rules
from the cc1/link_command spec, i still needed to support gcc 3.X that still did
have the old way so i added the hack for the cc1 spec so the fuctions should
work for the new spec rules to dectect if the compiler was hardened or not.

> - 03_all_gcc48_Makefile.in.patch in the ESP patchset adds
> -fno-stack-protector to BOOT_CFLAGS, LIBCFLAGS and LIBCXXFLAGS.  Is that
> required because of -fstack-protector-all or is it needed for
> -fstack-protector too? (I don't see it in other distro's ssp patches)
>
if you look in the debian/rules2 file you will see that it get added if ssp is
enable

> - the ESP patchset drops -fstack-protector-all when -nostdlib or
> -nodefaultlibs are used.  Debian uses -nostdlib or -ffreestanding.
>

-nodefaultlibs is something that was added under the time with kevquinn and psm that did alot of the gcc 4.X work and it was before my time. If you want some more reading you should look in the README file in the piepatch.
-ffreestanding should be added to be on the safe side.

> - should we also disable SSP with -D__KERNEL__ or rely on the kernel's build
> system to add -fno-stack-protector when CONFIG_CC_STACKPROTECTOR is disabled?
>
I would add check for -D__KERNEL__ to be on the safe side.
We did use that before the kernel did support ssp.

> - ssp-buffer-size is reduced from the default 8 bytes to 4.  Most distros do
> this, so I kept it in.
> 
> - the glibc SSP core handler patches.  I think these are unrelated?
> 
> - glibc common.eblit has:
> 
>   use hardened && gcc-specs-ssp && append-cflags $(test-flags-CC
> -fno-stack-protector)
> 
> Once gcc-specs-ssp works "hardened" will have to be dropped from this.
You only need to add "%{!fno-stack-protector: }" to cc1 spec to make gcc-specs-ssp work.
Comment 2 SpanKY gentoo-dev 2013-09-13 21:55:58 UTC
i'm not sure why we should go this route when we already have the specs logic.  isn't it as simple as always doing the minispec logic and then changing the default specs profile to hardenednopie ?
Comment 3 Ryan Hill (RETIRED) gentoo-dev 2013-09-14 02:45:21 UTC
Well, the SSP hardened is using is -fstack-protector-all, so that's what hardenednopie would get you (and -fstack-check... maybe some other goodies).  I don't know how hard it would be to add another ssp-light spec to just do -fstack-protector or if that is a good idea, but I don't see how always installing hardened minispecs + people using non-hardened profiles selecting them = win.

I see this as any other patch changing a default option.  This one just happens to do it using a spec rule.  It might be possible to do it using GCC_SPECS and a custom spec file but I'm not sure why that would be preferable and wouldn't have time to write it if it was. :/
Comment 4 SpanKY gentoo-dev 2013-12-16 18:26:26 UTC
(In reply to Ryan Hill from comment #3)

afaik, the point of minispecs was to make it easy to add fragments

the reason i think this route is preferable is:
 - the hardened team is already using this
 - we have just one code path (we just always enable minispecs) for building and patching which makes live easier for us (toolchain & hardened)
 - it'd allow users & devs to easily switch their system between variations

ssp-all should not be the default for non-hardened builds due to its overhead in trivial functions.  the Chromium OS project has adopted a third SSP variant called "strong":
  http://gcc.gnu.org/ml/gcc-patches/2012-06/msg00974.html

and they're keeping it up-to-date, so that'd be nice for us to adopt too.
Comment 5 Ryan Hill (RETIRED) gentoo-dev 2013-12-17 03:11:09 UTC
Hardened, you guys want to work on this?
Comment 6 Magnus Granberg gentoo-dev 2013-12-31 16:45:40 UTC
Created attachment 366644 [details, diff]
Change make_gcc_hard in the toolchain eclass so we pass ssp as defult

Have change so it add -DEFAULT_SSP and move the needed sed's for the Makefile.in
Comment 7 Magnus Granberg gentoo-dev 2013-12-31 16:48:34 UTC
Created attachment 366646 [details, diff]
Updated default_ssp patch

Have added needed -fno-stack-protector and a check for -DEFAULT_SSP
Comment 8 Magnus Granberg gentoo-dev 2013-12-31 18:49:44 UTC
Going the Debian/Ubuntu way is the best way without messing to mutch around on patches and toolchain.eclass i think. What i have done is add the needed
-fno-stack-protector on BOOT_CFLAGS, LIBCFLAGS and LIBCXXFLAGS but we may need to move that to new patch and add it to EPATCH_EXCLUDE, if we can't have it on all arches. I use hardened_gcc_works() that check SSP_STABLE or SSP_UCLIBC_STABLE from the ebuild to define what arch can have ssp on as default. We pass
-DEFAULT_SSP with the help of sed's and the makefile.in. In the default_ssp patch
i have added a check for the -DEFAULT_SSP so it enable the ssp spec. Haven't add -D__KERNEL__ and the needed stuff for gcc-specs-ssp to the patch. Do we want a nossp gcc gcc-config option?. Toolchain is this something we should move on or use a diffrent approch?

Haven't added the updated pie patches. We have stop useing the minispecs from gcc 4.4.4 and forward. The spec in gcc did change alot and to minisise the patches changes and to support sparc we move it to DRIVER_SELF_SPECS and it is the way it will be past to upstream. Ssp on Uclibc will have problems if is not build with tls and nptl #470608.
Comment 9 Magnus Granberg gentoo-dev 2014-01-01 08:55:51 UTC
Created attachment 366682 [details, diff]
Update default_ssp with gcc-specs-ssp support

Have updated the patch to support gcc-specs-ssp
Comment 10 Magnus Granberg gentoo-dev 2014-01-04 13:44:29 UTC
Have added the needed default-ssp patch to the gcc 4.8.2 gentoo patchset dir
and updated the piepatch dir with the needed changes to support ssp as default.
Comment 11 Magnus Granberg gentoo-dev 2014-01-10 15:27:35 UTC
Stuff is commited to cvs.
Comment 12 Ryan Hill (RETIRED) gentoo-dev 2014-01-10 15:51:17 UTC
Yeah, that needed at least a revision bump and a news item.  Please revert 4.8.2 back and add a 4.8.2-r1 that's package.masked.  I'll write the news item and send it to the list.  You made sure the eclass changes don't affect older versions right?
Comment 13 Magnus Granberg gentoo-dev 2014-01-10 16:26:06 UTC
Have done the revbump to -r1, mask and removed the keywords
(In reply to Ryan Hill from comment #12)
> Yeah, that needed at least a revision bump and a news item.  Please revert
> 4.8.2 back and add a 4.8.2-r1 that's package.masked.  I'll write the news
> item and send it to the list.  You made sure the eclass changes don't affect
> older versions right?
Have done the revbump to -r1, mask and removed the keywords
Thats way i added the tc_version_is_at_least 4.8.2 thing and you still need the default ssp patch else you only get the sed's added that have been added by hardened for some time.
Comment 14 SpanKY gentoo-dev 2014-04-26 21:38:49 UTC
*** Bug 508420 has been marked as a duplicate of this bug. ***
Comment 15 Agostino Sarubbo gentoo-dev 2014-05-04 07:59:17 UTC
Are you providing a way to disable, if the user wants, the stack-protector?
Comment 16 Ryan Hill (RETIRED) gentoo-dev 2014-05-12 02:35:31 UTC
-fno-stack-protector.  I think, but it's been a while, that USE=nossp might also work.

Also, Magnus is making strong the default for 4.9.
Comment 17 Ryan Hill (RETIRED) gentoo-dev 2014-06-16 02:16:13 UTC
Fixed in 4.8.3.