mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Zack Weinberg <zackw@panix.com>
Cc: gcc@gcc.gnu.org, llvmdev@cs.uiuc.edu,
	GNU C Library <libc-alpha@sourceware.org>,
	musl@lists.openwall.com
Subject: Re: Compiler support for erasure of sensitive data
Date: Wed, 9 Sep 2015 12:42:28 -0400	[thread overview]
Message-ID: <20150909164228.GD17773@brightrain.aerifal.cx> (raw)
In-Reply-To: <55F05FF1.3000405@panix.com>

On Wed, Sep 09, 2015 at 12:36:01PM -0400, Zack Weinberg wrote:
> The first, simpler problem is strictly optimization.  explicit_bzero
> can be optimized to memset followed by a vacuous use of the memory
> region (generating no machine instructions, but preventing the stores
> from being deleted as dead); this is valuable because the sensitive
> data is often small and fixed-size, so the memset can in turn be
> replaced by inline code.  (This also facilitates implementation of
> -D_FORTIFY_SOURCE checks for explicit_bzero.)  Again looking at
> libressl, 92 of those 152 uses are improved by a crude version of this
> optimization:
> 
>     void explicit_bzero(void *, size_t);
>     extern inline __attribute__((gnu_inline, always_inline))
>     void explicit_bzero_constn(void *ptr, size_t len)
>     {
>       typedef struct {char x[len];} memblk;
>       memset(ptr, 0, len);
>       asm("" : : "m" (*(memblk __attribute__((may_alias)) *)ptr));
>     }
>     #define explicit_bzero(s, n)                          \
>       (__extension__(__builtin_constant_p(n) && (n) > 0   \
>                      ? explicit_bzero_constn(s, n)        \
>                      : explicit_bzero(s, n)))
> 
> I call this "crude" because it only works in GCC, when compiling C,
> and when the length parameter is compile-time constant.  GCC issues no
> error for this code when 'len' is not compile-time constant, but it is
> not documented to work reliably.  When compiling C++, GCC does not
> accept a structure containing an array whose size is not *lexically*
> constant; even if the body of explicit_bzero_constn is moved into the
> macro so that the whole thing is guarded by __builtin_constant_p,
> using explicit_bzero with a non-constant size will cause a compile
> error.  The same is true for Clang whether compiling C or C++.
> 
> This problem could be solved with a very simple feature addition:
> 
>     extern inline __attribute__((gnu_inline, always_inline))
>     void explicit_bzero(void *ptr, size_t len)
>     {
>       memset(ptr, 0, len);
>       __builtin_use_memory(ptr, len);
>     }

You're making this harder than it needs to be. The "m" constraint is
the wrong thing to use here. Simply use:

	__asm__(""::"r"(ptr):"memory");

The memory constraint implies that the asm can read or write any
memory that's reachable by it. The lack of output constraints implies
__volatile__ which is also needed.

Rich


  parent reply	other threads:[~2015-09-09 16:42 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-09 16:36 Zack Weinberg
2015-09-09 16:39 ` Fwd: " Zack Weinberg
2015-09-09 16:41   ` Zack Weinberg
2015-09-09 16:42 ` Rich Felker [this message]
2015-09-09 16:47   ` [musl] " Zack Weinberg
2015-09-09 17:13     ` Rich Felker
2015-09-09 18:48       ` [musl] " Zack Weinberg
2015-09-09 20:05         ` Rich Felker
2015-09-09 16:52 ` Paul_Koning
2015-09-09 16:58   ` Zack Weinberg
2015-09-09 17:25     ` [musl] " Rich Felker
2015-09-09 17:54 ` David Edelsohn
2015-09-09 18:02   ` Paul_Koning
2015-09-09 18:11     ` David Edelsohn
2015-09-09 19:03     ` Zack Weinberg
2015-09-09 20:26       ` Szabolcs Nagy
2015-10-22 16:02         ` [musl] " Denys Vlasenko
2015-10-22 16:09 ` Denys Vlasenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150909164228.GD17773@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=gcc@gcc.gnu.org \
    --cc=libc-alpha@sourceware.org \
    --cc=llvmdev@cs.uiuc.edu \
    --cc=musl@lists.openwall.com \
    --cc=zackw@panix.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).