2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
|
* include/linux/random.h
|
|
|
|
|
*
|
|
|
|
|
* Include file for the random number generator.
|
|
|
|
|
*/
|
|
|
|
|
#ifndef _LINUX_RANDOM_H
|
|
|
|
|
#define _LINUX_RANDOM_H
|
|
|
|
|
|
2015-06-09 18:19:39 +08:00
|
|
|
#include <linux/list.h>
|
2015-10-08 01:20:38 +02:00
|
|
|
#include <linux/once.h>
|
|
|
|
|
|
2012-10-13 10:46:48 +01:00
|
|
|
#include <uapi/linux/random.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2015-06-09 18:19:39 +08:00
|
|
|
struct random_ready_callback {
|
|
|
|
|
struct list_head list;
|
|
|
|
|
void (*func)(struct random_ready_callback *rdy);
|
|
|
|
|
struct module *owner;
|
|
|
|
|
};
|
|
|
|
|
|
2012-07-04 11:16:01 -04:00
|
|
|
extern void add_device_randomness(const void *, unsigned int);
|
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 <re.emese@gmail.com>
[kees: expanded commit message and code comments]
Signed-off-by: Kees Cook <keescook@chromium.org>
2016-06-20 20:41:19 +02:00
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
extern void add_input_randomness(unsigned int type, unsigned int code,
|
2016-06-20 20:42:34 +02:00
|
|
|
unsigned int value) __latent_entropy;
|
|
|
|
|
extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
extern void get_random_bytes(void *buf, int nbytes);
|
2015-06-09 18:19:39 +08:00
|
|
|
extern int add_random_ready_callback(struct random_ready_callback *rdy);
|
|
|
|
|
extern void del_random_ready_callback(struct random_ready_callback *rdy);
|
random: add new get_random_bytes_arch() function
Create a new function, get_random_bytes_arch() which will use the
architecture-specific hardware random number generator if it is
present. Change get_random_bytes() to not use the HW RNG, even if it
is avaiable.
The reason for this is that the hw random number generator is fast (if
it is present), but it requires that we trust the hardware
manufacturer to have not put in a back door. (For example, an
increasing counter encrypted by an AES key known to the NSA.)
It's unlikely that Intel (for example) was paid off by the US
Government to do this, but it's impossible for them to prove otherwise
--- especially since Bull Mountain is documented to use AES as a
whitener. Hence, the output of an evil, trojan-horse version of
RDRAND is statistically indistinguishable from an RDRAND implemented
to the specifications claimed by Intel. Short of using a tunnelling
electronic microscope to reverse engineer an Ivy Bridge chip and
disassembling and analyzing the CPU microcode, there's no way for us
to tell for sure.
Since users of get_random_bytes() in the Linux kernel need to be able
to support hardware systems where the HW RNG is not present, most
time-sensitive users of this interface have already created their own
cryptographic RNG interface which uses get_random_bytes() as a seed.
So it's much better to use the HW RNG to improve the existing random
number generator, by mixing in any entropy returned by the HW RNG into
/dev/random's entropy pool, but to always _use_ /dev/random's entropy
pool.
This way we get almost of the benefits of the HW RNG without any
potential liabilities. The only benefits we forgo is the
speed/performance enhancements --- and generic kernel code can't
depend on depend on get_random_bytes() having the speed of a HW RNG
anyway.
For those places that really want access to the arch-specific HW RNG,
if it is available, we provide get_random_bytes_arch().
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Cc: stable@vger.kernel.org
2012-07-05 10:35:23 -04:00
|
|
|
extern void get_random_bytes_arch(void *buf, int nbytes);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
#ifndef MODULE
|
2007-02-12 00:55:28 -08:00
|
|
|
extern const struct file_operations random_fops, urandom_fops;
|
2005-04-16 15:20:36 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
unsigned int get_random_int(void);
|
2016-02-26 15:19:34 -08:00
|
|
|
unsigned long get_random_long(void);
|
random: simplify API for random address requests
To date, all callers of randomize_range() have set the length to 0, and
check for a zero return value. For the current callers, the only way to
get zero returned is if end <= start. Since they are all adding a
constant to the start address, this is unnecessary.
We can remove a bunch of needless checks by simplifying the API to do just
what everyone wants, return an address between [start, start + range).
While we're here, s/get_random_int/get_random_long/. No current call site
is adversely affected by get_random_int(), since all current range
requests are < UINT_MAX. However, we should match caller expectations to
avoid coming up short (ha!) in the future.
All current callers to randomize_range() chose to use the start address if
randomize_range() failed. Therefore, we simplify things by just returning
the start address on error.
randomize_range() will be removed once all callers have been converted
over to randomize_addr().
Link: http://lkml.kernel.org/r/20160803233913.32511-2-jason@lakedaemon.net
Signed-off-by: Jason Cooper <jason@lakedaemon.net>
Acked-by: Kees Cook <keescook@chromium.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: "Roberts, William C" <william.c.roberts@intel.com>
Cc: Yann Droneaud <ydroneaud@opteya.com>
Cc: Russell King <linux@arm.linux.org.uk>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "H . Peter Anvin" <hpa@zytor.com>
Cc: Nick Kralevich <nnk@google.com>
Cc: Jeffrey Vander Stoep <jeffv@google.com>
Cc: Daniel Cashman <dcashman@android.com>
Cc: Chris Metcalf <cmetcalf@mellanox.com>
Cc: Guan Xuetao <gxt@mprc.pku.edu.cn>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-10-11 13:53:52 -07:00
|
|
|
unsigned long randomize_page(unsigned long start, unsigned long range);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2010-05-26 14:44:13 -07:00
|
|
|
/*
|
2020-07-31 07:51:14 +02:00
|
|
|
* This is designed to be standalone for just prandom
|
|
|
|
|
* users, but for now we include it from <linux/random.h>
|
|
|
|
|
* for legacy reasons.
|
2010-05-26 14:44:13 -07:00
|
|
|
*/
|
2020-07-31 07:51:14 +02:00
|
|
|
#include <linux/prandom.h>
|
2010-05-26 14:44:13 -07:00
|
|
|
|
2011-07-31 13:54:50 -07:00
|
|
|
#ifdef CONFIG_ARCH_RANDOM
|
|
|
|
|
# include <asm/archrandom.h>
|
|
|
|
|
#else
|
2016-06-08 12:38:38 -07:00
|
|
|
static inline bool arch_get_random_long(unsigned long *v)
|
2011-07-31 13:54:50 -07:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-06-08 12:38:38 -07:00
|
|
|
static inline bool arch_get_random_int(unsigned int *v)
|
2011-07-31 13:54:50 -07:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-06-08 12:38:38 -07:00
|
|
|
static inline bool arch_has_random(void)
|
2014-03-17 16:36:30 -07:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-06-08 12:38:38 -07:00
|
|
|
static inline bool arch_get_random_seed_long(unsigned long *v)
|
2014-03-17 16:36:27 -07:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-06-08 12:38:38 -07:00
|
|
|
static inline bool arch_get_random_seed_int(unsigned int *v)
|
2014-03-17 16:36:27 -07:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-06-08 12:38:38 -07:00
|
|
|
static inline bool arch_has_random_seed(void)
|
2014-03-17 16:36:30 -07:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2011-07-31 13:54:50 -07:00
|
|
|
#endif
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
#endif /* _LINUX_RANDOM_H */
|