linux-stable/scripts/gcc-plugins
Jason A. Donenfeld 846737e63c gcc-plugins: latent_entropy: use /dev/urandom
commit c40160f299 upstream.

While the latent entropy plugin mostly doesn't derive entropy from
get_random_const() for measuring the call graph, when __latent_entropy is
applied to a constant, then it's initialized statically to output from
get_random_const(). In that case, this data is derived from a 64-bit
seed, which means a buffer of 512 bits doesn't really have that amount
of compile-time entropy.

This patch fixes that shortcoming by just buffering chunks of
/dev/urandom output and doling it out as requested.

At the same time, it's important that we don't break the use of
-frandom-seed, for people who want the runtime benefits of the latent
entropy plugin, while still having compile-time determinism. In that
case, we detect whether gcc's set_random_seed() has been called by
making a call to get_random_seed(noinit=true) in the plugin init
function, which is called after set_random_seed() is called but before
anything that calls get_random_seed(noinit=false), and seeing if it's
zero or not. If it's not zero, we're in deterministic mode, and so we
just generate numbers with a basic xorshift prng.

Note that we don't detect if -frandom-seed is being used using the
documented local_tick variable, because it's assigned via:
   local_tick = (unsigned) tv.tv_sec * 1000 + tv.tv_usec / 1000;
which may well overflow and become -1 on its own, and so isn't
reliable: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105171

[kees: The 256 byte rnd_buf size was chosen based on average (250),
 median (64), and std deviation (575) bytes of used entropy for a
 defconfig x86_64 build]

Fixes: 38addce8b6 ("gcc-plugins: Add latent_entropy plugin")
Cc: stable@vger.kernel.org
Cc: PaX Team <pageexec@freemail.hu>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20220405222815.21155-1-Jason@zx2c4.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-04-20 09:08:32 +02:00
..
.gitignore gcc-plugins: Add the randstruct plugin 2017-06-22 16:15:45 -07:00
Makefile gcc-common.h: Update for GCC 10 2020-05-27 16:42:53 +02:00
cyc_complexity_plugin.c gcc-plugins: consolidate on PASS_INFO macro 2017-01-13 14:20:03 -08:00
gcc-common.h gcc-common.h: Update for GCC 10 2020-05-27 16:42:53 +02:00
gcc-generate-gimple-pass.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
gcc-generate-ipa-pass.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
gcc-generate-rtl-pass.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
gcc-generate-simple_ipa-pass.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
gen-random-seed.sh License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
latent_entropy_plugin.c gcc-plugins: latent_entropy: use /dev/urandom 2022-04-20 09:08:32 +02:00
randomize_layout_plugin.c randstruct: Check member structs in is_pure_ops_struct() 2019-10-05 12:48:05 +02:00
sancov_plugin.c gcc-plugins: fix sancov_plugin for gcc-5 2017-02-27 14:10:10 -08:00
structleak_plugin.c gcc-plugins: Use dynamic initializers 2018-09-05 09:26:42 +02:00