Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 38addce8 authored by Emese Revfy's avatar Emese Revfy Committed by Kees Cook
Browse files

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: default avatarEmese Revfy <re.emese@gmail.com>
[kees: expanded commit message and code comments]
Signed-off-by: default avatarKees Cook <keescook@chromium.org>
parent c8d2bc9b
Loading
Loading
Loading
Loading
+18 −0
Original line number Original line Diff line number Diff line
@@ -383,6 +383,24 @@ config GCC_PLUGIN_SANCOV
	  gcc-4.5 on). It is based on the commit "Add fuzzing coverage support"
	  gcc-4.5 on). It is based on the commit "Add fuzzing coverage support"
	  by Dmitry Vyukov <dvyukov@google.com>.
	  by Dmitry Vyukov <dvyukov@google.com>.


config GCC_PLUGIN_LATENT_ENTROPY
	bool "Generate some entropy during boot and runtime"
	depends on GCC_PLUGINS
	help
	  By saying Y here the kernel will instrument some kernel code to
	  extract some entropy from both original and artificially created
	  program state.  This will help especially embedded systems where
	  there is little 'natural' source of entropy normally.  The cost
	  is some slowdown of the boot process (about 0.5%) and fork and
	  irq processing.

	  Note that entropy extracted this way is not cryptographically
	  secure!

	  This plugin was ported from grsecurity/PaX. More information at:
	   * https://grsecurity.net/
	   * https://pax.grsecurity.net/

config HAVE_CC_STACKPROTECTOR
config HAVE_CC_STACKPROTECTOR
	bool
	bool
	help
	help
+5 −0
Original line number Original line Diff line number Diff line
@@ -14,6 +14,11 @@ CFLAGS_prom_init.o += -fPIC
CFLAGS_btext.o		+= -fPIC
CFLAGS_btext.o		+= -fPIC
endif
endif


CFLAGS_cputable.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
CFLAGS_init.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
CFLAGS_btext.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
CFLAGS_prom.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)

ifdef CONFIG_FUNCTION_TRACER
ifdef CONFIG_FUNCTION_TRACER
# Do not trace early boot code
# Do not trace early boot code
CFLAGS_REMOVE_cputable.o = -mno-sched-epilog $(CC_FLAGS_FTRACE)
CFLAGS_REMOVE_cputable.o = -mno-sched-epilog $(CC_FLAGS_FTRACE)
+11 −0
Original line number Original line Diff line number Diff line
@@ -18,6 +18,17 @@ struct random_ready_callback {
};
};


extern void add_device_randomness(const void *, unsigned int);
extern void add_device_randomness(const void *, unsigned int);

#if defined(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) && !defined(__CHECKER__)
static inline void add_latent_entropy(void)
{
	add_device_randomness((const void *)&latent_entropy,
			      sizeof(latent_entropy));
}
#else
static inline void add_latent_entropy(void) {}
#endif

extern void add_input_randomness(unsigned int type, unsigned int code,
extern void add_input_randomness(unsigned int type, unsigned int code,
				 unsigned int value);
				 unsigned int value);
extern void add_interrupt_randomness(int irq, int irq_flags);
extern void add_interrupt_randomness(int irq, int irq_flags);
+1 −0
Original line number Original line Diff line number Diff line
@@ -789,6 +789,7 @@ int __init_or_module do_one_initcall(initcall_t fn)
	}
	}
	WARN(msgbuf[0], "initcall %pF returned with %s\n", fn, msgbuf);
	WARN(msgbuf[0], "initcall %pF returned with %s\n", fn, msgbuf);


	add_latent_entropy();
	return ret;
	return ret;
}
}


+1 −0
Original line number Original line Diff line number Diff line
@@ -1780,6 +1780,7 @@ long _do_fork(unsigned long clone_flags,


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