Bug 110816 - Emit initialization code for empty class under -ftrivial-auto-var-init
Summary: Emit initialization code for empty class under -ftrivial-auto-var-init
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-07-26 14:06 UTC by sergesanspaille
Modified: 2023-09-17 06:20 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-07-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description sergesanspaille 2023-07-26 14:06:12 UTC
As show cased by https://godbolt.org/z/o5asYGq8G, gcc doesn't fill the byte used by empty class/struct under -ftrivial-auto-var-init. This potentially leaks a byte of memory.
Comment 1 Jonathan Wakely 2023-07-26 14:38:15 UTC
The testcase is:


struct f { void crash(); };

void bar(bool cond) {
    f t;
    if(cond)
        t.crash();
    //user(&t);
}
Comment 2 Andrew Pinski 2023-07-26 16:05:06 UTC
> This potentially leaks a byte of memory.  
The only way to access that byte is to use memcpy or via char. 
-ftrivial-auto-var-init is not designed for security this way but rather for normal code ...

IIRC atomic compare and swap will zero it out too ...
Comment 3 Jonathan Wakely 2023-07-26 16:08:29 UTC
(In reply to Andrew Pinski from comment #2)
> The only way to access that byte is to use memcpy or via char. 
> -ftrivial-auto-var-init is not designed for security this way but rather for
> normal code ...

That's not what the manual says (emphasis mine):

"Initialize automatic variables with either a pattern or with zeroes to increase the security and predictability of a program by preventing **uninitialized memory disclosure** and use."

> IIRC atomic compare and swap will zero it out too ...

The std::atomic and std::atomic_ref compare_exchange members will zero it, but the compiler built-in won't.
Comment 4 Richard Biener 2023-07-27 07:15:09 UTC
(In reply to Jonathan Wakely from comment #3)
> (In reply to Andrew Pinski from comment #2)
> > The only way to access that byte is to use memcpy or via char. 
> > -ftrivial-auto-var-init is not designed for security this way but rather for
> > normal code ...
> 
> That's not what the manual says (emphasis mine):
> 
> "Initialize automatic variables with either a pattern or with zeroes to
> increase the security and predictability of a program by preventing
> **uninitialized memory disclosure** and use."

probably should add 'some' qualification here.

> > IIRC atomic compare and swap will zero it out too ...
> 
> The std::atomic and std::atomic_ref compare_exchange members will zero it,
> but the compiler built-in won't.
Comment 5 Jonathan Wakely 2023-07-27 08:31:37 UTC
And fix the spelling of zeros, which is generally preferred to zeroes for the noun.