linux/scripts/gcc-plugins
Jason A. Donenfeld c40160f299 gcc-plugins: latent_entropy: use /dev/urandom
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
2022-04-12 11:31:40 -07:00
..
.gitignore .gitignore: prefix local generated files with a slash 2021-05-02 00:43:35 +09:00
arm_ssp_per_task_plugin.c gcc-plugins: arm-ssp: Prepare for THREAD_INFO_IN_TASK support 2021-09-27 16:54:01 +02:00
gcc-common.h gcc-plugins: remove duplicate include in gcc-common.h 2021-10-21 08:41:51 -07:00
gcc-generate-gimple-pass.h gcc-plugins: remove support for GCC 4.9 and older 2021-10-04 10:58:08 -07:00
gcc-generate-ipa-pass.h gcc-plugins: remove support for GCC 4.9 and older 2021-10-04 10:58:08 -07:00
gcc-generate-rtl-pass.h gcc-plugins: remove support for GCC 4.9 and older 2021-10-04 10:58:08 -07:00
gcc-generate-simple_ipa-pass.h gcc-plugins: remove support for GCC 4.9 and older 2021-10-04 10:58:08 -07:00
gen-random-seed.sh scripts: make some scripts executable 2021-08-10 09:13:25 +09:00
Kconfig gcc-plugins: Remove cyc_complexity 2021-10-21 08:41:47 -07:00
latent_entropy_plugin.c gcc-plugins: latent_entropy: use /dev/urandom 2022-04-12 11:31:40 -07:00
Makefile kbuild: rebuild GCC plugins when the compiler is upgraded 2021-03-11 14:40:50 +09:00
randomize_layout_plugin.c gcc-plugins: remove code for GCC versions older than 4.9 2020-12-04 14:09:41 -08:00
sancov_plugin.c gcc-plugins: remove code for GCC versions older than 4.9 2020-12-04 14:09:41 -08:00
stackleak_plugin.c gcc-plugins/stackleak: Ignore .noinstr.text and .entry.text 2022-02-06 10:55:03 -08:00
structleak_plugin.c gcc-plugins: remove support for GCC 4.9 and older 2021-10-04 10:58:08 -07:00