Skip to content

Commit

Permalink
Merge tag 'hardening-v5.18-rc3' of git://git.kernel.org/pub/scm/linux…
Browse files Browse the repository at this point in the history
…/kernel/git/kees/linux

Pull hardening fixes from Kees Cook:

 - latent_entropy: Use /dev/urandom instead of small GCC seed (Jason
   Donenfeld)

 - uapi/stddef.h: add missed include guards (Tadeusz Struk)

* tag 'hardening-v5.18-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux:
  gcc-plugins: latent_entropy: use /dev/urandom
  uapi/linux/stddef.h: Add include guards
  • Loading branch information
Linus Torvalds committed Apr 13, 2022
2 parents c1488c9 + c40160f commit a199448
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
4 changes: 4 additions & 0 deletions include/uapi/linux/stddef.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
#ifndef _UAPI_LINUX_STDDEF_H
#define _UAPI_LINUX_STDDEF_H

#include <linux/compiler_types.h>

#ifndef __always_inline
Expand Down Expand Up @@ -41,3 +44,4 @@
struct { } __empty_ ## NAME; \
TYPE NAME[]; \
}
#endif
44 changes: 27 additions & 17 deletions scripts/gcc-plugins/latent_entropy_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,31 @@ static struct plugin_info latent_entropy_plugin_info = {
.help = "disable\tturn off latent entropy instrumentation\n",
};

static unsigned HOST_WIDE_INT seed;
/*
* get_random_seed() (this is a GCC function) generates the seed.
* This is a simple random generator without any cryptographic security because
* the entropy doesn't come from here.
*/
static unsigned HOST_WIDE_INT deterministic_seed;
static unsigned HOST_WIDE_INT rnd_buf[32];
static size_t rnd_idx = ARRAY_SIZE(rnd_buf);
static int urandom_fd = -1;

static unsigned HOST_WIDE_INT get_random_const(void)
{
unsigned int i;
unsigned HOST_WIDE_INT ret = 0;

for (i = 0; i < 8 * sizeof(ret); i++) {
ret = (ret << 1) | (seed & 1);
seed >>= 1;
if (ret & 1)
seed ^= 0xD800000000000000ULL;
if (deterministic_seed) {
unsigned HOST_WIDE_INT w = deterministic_seed;
w ^= w << 13;
w ^= w >> 7;
w ^= w << 17;
deterministic_seed = w;
return deterministic_seed;
}

return ret;
if (urandom_fd < 0) {
urandom_fd = open("/dev/urandom", O_RDONLY);
gcc_assert(urandom_fd >= 0);
}
if (rnd_idx >= ARRAY_SIZE(rnd_buf)) {
gcc_assert(read(urandom_fd, rnd_buf, sizeof(rnd_buf)) == sizeof(rnd_buf));
rnd_idx = 0;
}
return rnd_buf[rnd_idx++];
}

static tree tree_get_random_const(tree type)
Expand Down Expand Up @@ -537,8 +543,6 @@ static void latent_entropy_start_unit(void *gcc_data __unused,
tree type, id;
int quals;

seed = get_random_seed(false);

if (in_lto_p)
return;

Expand Down Expand Up @@ -573,6 +577,12 @@ __visible int plugin_init(struct plugin_name_args *plugin_info,
const struct plugin_argument * const argv = plugin_info->argv;
int i;

/*
* Call get_random_seed() with noinit=true, so that this returns
* 0 in the case where no seed has been passed via -frandom-seed.
*/
deterministic_seed = get_random_seed(true);

static const struct ggc_root_tab gt_ggc_r_gt_latent_entropy[] = {
{
.base = &latent_entropy_decl,
Expand Down

0 comments on commit a199448

Please sign in to comment.