Re: [PATCH] sha: prevent removal of memset as dead store in sha1_update()

Mikael Pettersson
2010-02-25T16:29:28Z

Pekka Enberg writes:
> On Thu, Feb 25, 2010 at 5:56 PM, Mikael Pettersson <mikpe@it.uu.se> wrote:
> > I fear that the only portable (across compiler versions) and safe
> > solution is to invoke an assembly-coded dummy function with prototype
> >
> >        void use(void *p);
> >
> > and rewrite the code above as
> >
> >        {
> >                u32 temp[...];
> >                ...
> >                memset(temp, 0, sizeof temp);
> >                use(temp);
> >        }
> >
> > This forces the compiler to consider the buffer live after the
> > memset, so the memset cannot be eliminated.
>
> So is there some "do not optimize" GCC magic that we could use for a
> memzero_secret() helper function?

I guess there's some -fno-builtin-... that might achieve this effect,
but that would disable all memset optimizations, not just those affecting
sensitive data.

You'd want a function attribute or magic type annotation and apply it
only to the specific cases where it's needed. Alas, I know of no such
attribute or annotation. ('volatile' doesn't work, I tried that.)

Ask on gcc@gcc.gnu.org.