Please add -ftrivial-auto-var-init=zero to default build flags

Bug #1972043 reported by Kees Cook
30
This bug affects 4 people
Affects Status Importance Assigned to Milestone
dpkg (Ubuntu)
Confirmed
Wishlist
Unassigned
Kinetic
Confirmed
Wishlist
Unassigned
gcc-12 (Ubuntu)
Confirmed
Wishlist
Unassigned
Kinetic
Confirmed
Wishlist
Unassigned

Bug Description

Please add "-ftrivial-auto-var-init=zero" for GCC 12 (which is the first release of GCC to provide this flag).

It goes well with the other important security flaw mitigation flags already enabled in Ubuntu for GCC:
https://wiki.ubuntu.com/ToolChain/CompilerFlags

While many variables are initialized (due to -Wuninitialized), there is a blind spot for variables passed by reference, padding, and cases where -Wuninitialized just fails to track it. Universally wiping the variables eliminates nearly the entire class of uninitialized stack variable use (https://cwe.mitre.org/data/definitions/457.html) with nearly no overhead (e.g. any duplicate assignments will already be squashed during dead store elimination, etc).

Revision history for this message
Julian Andres Klode (juliank) wrote :

Does Wuninitialized continue working with that flag?

Tobias Heider (tobhe)
Changed in gcc-12 (Ubuntu):
importance: Undecided → Wishlist
Revision history for this message
Kees Cook (kees) wrote :

Yes, -Wuninitialized continues to warn, even if they were auto-initialized.

Revision history for this message
Alex Murray (alexmurray) wrote :

+1 from the Security team on this - looks like a good easy win for security with no overhead or other impact from what I can see.

tags: added: rls-kk-incoming
Revision history for this message
Matthias Klose (doko) wrote :

please not, add it to dpkg instead

Changed in dpkg (Ubuntu Kinetic):
importance: Undecided → Wishlist
tags: added: fr-2368
tags: removed: rls-kk-incoming
Revision history for this message
Kees Cook (kees) wrote :

Adding it to the compiler means *all* builds benefit, which is the reason this was done on the other options. People build their local projects, newer versions of tools from GitHub, etc etc.

This needs to be in the compiler directly.

Steve Beattie (sbeattie)
tags: added: sec-994
Revision history for this message
Alex Murray (alexmurray) wrote :

doko can you please provide more details on why you think this should be done in dpkg instead of gcc (as we have done for almost all the other hardening options)? As Kees says, adding it to gcc means not only does this benefit Ubuntu archive packages, but also any software which is built on a Ubuntu machine using gcc (ie snaps built by launchpad, packages built on Github using Ubuntu as the CI backend etc) - which is a great benefit IMO.

What advantages do you see in adding this to dpkg rather than gcc?

Revision history for this message
Julian Andres Klode (juliank) wrote :

Oh I have another question: Does this actually turn accessing the uninitialized variables into defined behavior, or can the optimizer still treat it as undefined behavior and thus do whatever it want?

Given that this *is* undefined behavior, turning it into defined behavior with a 0 value would be incredibly useful. It likely also breaks some code written to exploit legacy gcc behaviour, but oh well, that is broken anyway.

Revision history for this message
Julian Andres Klode (juliank) wrote :

I want to note the discussions on clang which recommend pattern instead of zero, as zero hides bugs and creates a new stuff. They did not intend to support zero long term.

https://reviews.llvm.org/D54604
https://reviews.llvm.org/D64742

Revision history for this message
Launchpad Janitor (janitor) wrote :

Status changed to 'Confirmed' because the bug affects multiple users.

Changed in dpkg (Ubuntu):
status: New → Confirmed
Changed in gcc-12 (Ubuntu):
status: New → Confirmed
Revision history for this message
Andres Freund (andres-anarazel) wrote :

FWIW, I recently measured the overhead of -ftrivial-auto-var-init=zero for postgres, and the overhead for *some* workloads could not fairly be described with "nearly no overhead". For read-only OLTP the overhead was in the 1-3% range, but for OLAP style queries it was higher, between 0-20% for the different queries in TPC-H.

Revision history for this message
Kees Cook (kees) wrote :

Any pathological workloads for specific projects should be analyzed to identify the source of the overhead (which is usually rapid large allocations that are immediately filled, so pre-initialization is not helpful). For these variables, projects can use the "uninitialized" variable attribute to opt out of the initialization.

But as a global option, it should stand.

Revision history for this message
Dimitri John Ledkov (xnox) wrote :

@andres can this flag be added to CI builds and start to identify codepaths that are hindered by this? And start marking them for uninitialised. Or contribute back to GCC to identify things that are allocated staright away and skip auto-init of those?

Revision history for this message
Matthias Klose (doko) wrote :

@alexmurray, I never said, that it has to be done in dpkg *only*. The bad thing of doing it in the compiler only is that nobody knows about it. Having the change *also* in dpkg lets these new flags appear in the build output, and people can see these compiler options.

Revision history for this message
Seth Arnold (seth-arnold) wrote : Re: [Bug 1972043] Re: Please add -ftrivial-auto-var-init=zero to default build flags

On Thu, Jul 06, 2023 at 12:16:46PM -0000, Matthias Klose wrote:
> @alexmurray, I never said, that it has to be done in dpkg *only*. The
> bad thing of doing it in the compiler only is that nobody knows about
> it. Having the change *also* in dpkg lets these new flags appear in the
> build output, and people can see these compiler options.

I can appreciate wanting the flags more visible in build logs, but I think
having them set twice: once loudly, once silently, will make debugging
problems more confusing than if they're only set once, even if silent.

Could we emit our special flags as part of standard output, so they'd be
more visible to developers when troubleshooting?

Thanks

Revision history for this message
Matthias Klose (doko) wrote :

I don't agree on the "more confusing". The command line is printed out by the build system, not gcc. You can see the flags passed to cc1/cc1plus when using the -v option for gcc.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.