Skip to content

Commit 38addce

Browse files
ephox-gcc-pluginskees
authored andcommitted
gcc-plugins: Add latent_entropy plugin
This adds a new gcc plugin named "latent_entropy". It is designed to extract as much possible uncertainty from a running system at boot time as possible, hoping to capitalize on any possible variation in CPU operation (due to runtime data differences, hardware differences, SMP ordering, thermal timing variation, cache behavior, etc). At the very least, this plugin is a much more comprehensive example for how to manipulate kernel code using the gcc plugin internals. The need for very-early boot entropy tends to be very architecture or system design specific, so this plugin is more suited for those sorts of special cases. The existing kernel RNG already attempts to extract entropy from reliable runtime variation, but this plugin takes the idea to a logical extreme by permuting a global variable based on any variation in code execution (e.g. a different value (and permutation function) is used to permute the global based on loop count, case statement, if/then/else branching, etc). To do this, the plugin starts by inserting a local variable in every marked function. The plugin then adds logic so that the value of this variable is modified by randomly chosen operations (add, xor and rol) and random values (gcc generates separate static values for each location at compile time and also injects the stack pointer at runtime). The resulting value depends on the control flow path (e.g., loops and branches taken). Before the function returns, the plugin mixes this local variable into the latent_entropy global variable. The value of this global variable is added to the kernel entropy pool in do_one_initcall() and _do_fork(), though it does not credit any bytes of entropy to the pool; the contents of the global are just used to mix the pool. Additionally, the plugin can pre-initialize arrays with build-time random contents, so that two different kernel builds running on identical hardware will not have the same starting values. Signed-off-by: Emese Revfy <[email protected]> [kees: expanded commit message and code comments] Signed-off-by: Kees Cook <[email protected]>
1 parent c8d2bc9 commit 38addce

File tree

8 files changed

+689
-1
lines changed

8 files changed

+689
-1
lines changed

arch/Kconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,24 @@ config GCC_PLUGIN_SANCOV
383383
gcc-4.5 on). It is based on the commit "Add fuzzing coverage support"
384384
by Dmitry Vyukov <[email protected]>.
385385

386+
config GCC_PLUGIN_LATENT_ENTROPY
387+
bool "Generate some entropy during boot and runtime"
388+
depends on GCC_PLUGINS
389+
help
390+
By saying Y here the kernel will instrument some kernel code to
391+
extract some entropy from both original and artificially created
392+
program state. This will help especially embedded systems where
393+
there is little 'natural' source of entropy normally. The cost
394+
is some slowdown of the boot process (about 0.5%) and fork and
395+
irq processing.
396+
397+
Note that entropy extracted this way is not cryptographically
398+
secure!
399+
400+
This plugin was ported from grsecurity/PaX. More information at:
401+
* https://grsecurity.net/
402+
* https://pax.grsecurity.net/
403+
386404
config HAVE_CC_STACKPROTECTOR
387405
bool
388406
help

arch/powerpc/kernel/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ CFLAGS_prom_init.o += -fPIC
1414
CFLAGS_btext.o += -fPIC
1515
endif
1616

17+
CFLAGS_cputable.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
18+
CFLAGS_init.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
19+
CFLAGS_btext.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
20+
CFLAGS_prom.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
21+
1722
ifdef CONFIG_FUNCTION_TRACER
1823
# Do not trace early boot code
1924
CFLAGS_REMOVE_cputable.o = -mno-sched-epilog $(CC_FLAGS_FTRACE)

include/linux/random.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ struct random_ready_callback {
1818
};
1919

2020
extern void add_device_randomness(const void *, unsigned int);
21+
22+
#if defined(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) && !defined(__CHECKER__)
23+
static inline void add_latent_entropy(void)
24+
{
25+
add_device_randomness((const void *)&latent_entropy,
26+
sizeof(latent_entropy));
27+
}
28+
#else
29+
static inline void add_latent_entropy(void) {}
30+
#endif
31+
2132
extern void add_input_randomness(unsigned int type, unsigned int code,
2233
unsigned int value);
2334
extern void add_interrupt_randomness(int irq, int irq_flags);

init/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ int __init_or_module do_one_initcall(initcall_t fn)
789789
}
790790
WARN(msgbuf[0], "initcall %pF returned with %s\n", fn, msgbuf);
791791

792+
add_latent_entropy();
792793
return ret;
793794
}
794795

kernel/fork.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,7 @@ long _do_fork(unsigned long clone_flags,
17801780

17811781
p = copy_process(clone_flags, stack_start, stack_size,
17821782
child_tidptr, NULL, trace, tls, NUMA_NO_NODE);
1783+
add_latent_entropy();
17831784
/*
17841785
* Do this prior waking up the new thread - the thread pointer
17851786
* might get invalid after that point, if the thread exits quickly.

mm/page_alloc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ EXPORT_PER_CPU_SYMBOL(_numa_mem_);
9191
int _node_numa_mem_[MAX_NUMNODES];
9292
#endif
9393

94+
#ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY
95+
volatile u64 latent_entropy;
96+
EXPORT_SYMBOL(latent_entropy);
97+
#endif
98+
9499
/*
95100
* Array of node states.
96101
*/

scripts/Makefile.gcc-plugins

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ ifdef CONFIG_GCC_PLUGINS
66

77
gcc-plugin-$(CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) += cyc_complexity_plugin.so
88

9+
gcc-plugin-$(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) += latent_entropy_plugin.so
10+
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) += -DLATENT_ENTROPY_PLUGIN
11+
ifdef CONFIG_PAX_LATENT_ENTROPY
12+
DISABLE_LATENT_ENTROPY_PLUGIN += -fplugin-arg-latent_entropy_plugin-disable
13+
endif
14+
915
ifdef CONFIG_GCC_PLUGIN_SANCOV
1016
ifeq ($(CFLAGS_KCOV),)
1117
# It is needed because of the gcc-plugin.sh and gcc version checks.
@@ -21,7 +27,8 @@ ifdef CONFIG_GCC_PLUGINS
2127

2228
GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
2329

24-
export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR SANCOV_PLUGIN
30+
export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR
31+
export SANCOV_PLUGIN DISABLE_LATENT_ENTROPY_PLUGIN
2532

2633
ifneq ($(PLUGINCC),)
2734
# SANCOV_PLUGIN can be only in CFLAGS_KCOV because avoid duplication.

0 commit comments

Comments
 (0)