Linux Audio

Check our new training course

Loading...
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2016 Linaro Ltd;  <ard.biesheuvel@linaro.org>
 
 
 
 
 
  4 */
  5
  6#include <linux/efi.h>
  7#include <asm/efi.h>
  8
  9#include "efistub.h"
 10
 11typedef union efi_rng_protocol efi_rng_protocol_t;
 12
 13union efi_rng_protocol {
 14	struct {
 15		efi_status_t (__efiapi *get_info)(efi_rng_protocol_t *,
 16						  unsigned long *,
 17						  efi_guid_t *);
 18		efi_status_t (__efiapi *get_rng)(efi_rng_protocol_t *,
 19						 efi_guid_t *, unsigned long,
 20						 u8 *out);
 21	};
 22	struct {
 23		u32 get_info;
 24		u32 get_rng;
 25	} mixed_mode;
 26};
 27
 28/**
 29 * efi_get_random_bytes() - fill a buffer with random bytes
 30 * @size:	size of the buffer
 31 * @out:	caller allocated buffer to receive the random bytes
 32 *
 33 * The call will fail if either the firmware does not implement the
 34 * EFI_RNG_PROTOCOL or there are not enough random bytes available to fill
 35 * the buffer.
 36 *
 37 * Return:	status code
 38 */
 39efi_status_t efi_get_random_bytes(unsigned long size, u8 *out)
 40{
 41	efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
 42	efi_status_t status;
 43	efi_rng_protocol_t *rng = NULL;
 44
 45	status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng);
 
 46	if (status != EFI_SUCCESS)
 47		return status;
 48
 49	return efi_call_proto(rng, get_rng, NULL, size, out);
 50}
 51
 52/**
 53 * efi_random_get_seed() - provide random seed as configuration table
 54 *
 55 * The EFI_RNG_PROTOCOL is used to read random bytes. These random bytes are
 56 * saved as a configuration table which can be used as entropy by the kernel
 57 * for the initialization of its pseudo random number generator.
 58 *
 59 * If the EFI_RNG_PROTOCOL is not available or there are not enough random bytes
 60 * available, the configuration table will not be installed and an error code
 61 * will be returned.
 62 *
 63 * Return:	status code
 64 */
 65efi_status_t efi_random_get_seed(void)
 
 
 66{
 67	efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
 68	efi_guid_t rng_algo_raw = EFI_RNG_ALGORITHM_RAW;
 69	efi_guid_t rng_table_guid = LINUX_EFI_RANDOM_SEED_TABLE_GUID;
 70	struct linux_efi_random_seed *prev_seed, *seed = NULL;
 71	int prev_seed_size = 0, seed_size = EFI_RANDOM_SEED_SIZE;
 72	unsigned long nv_seed_size = 0, offset = 0;
 73	efi_rng_protocol_t *rng = NULL;
 74	efi_status_t status;
 75
 76	status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng);
 77	if (status != EFI_SUCCESS)
 78		seed_size = 0;
 79
 80	// Call GetVariable() with a zero length buffer to obtain the size
 81	get_efi_var(L"RandomSeed", &rng_table_guid, NULL, &nv_seed_size, NULL);
 82	if (!seed_size && !nv_seed_size)
 83		return status;
 84
 85	seed_size += nv_seed_size;
 
 86
 87	/*
 88	 * Check whether a seed was provided by a prior boot stage. In that
 89	 * case, instead of overwriting it, let's create a new buffer that can
 90	 * hold both, and concatenate the existing and the new seeds.
 91	 * Note that we should read the seed size with caution, in case the
 92	 * table got corrupted in memory somehow.
 93	 */
 94	prev_seed = get_efi_config_table(rng_table_guid);
 95	if (prev_seed && prev_seed->size <= 512U) {
 96		prev_seed_size = prev_seed->size;
 97		seed_size += prev_seed_size;
 98	}
 99
100	/*
101	 * Use EFI_ACPI_RECLAIM_MEMORY here so that it is guaranteed that the
102	 * allocation will survive a kexec reboot (although we refresh the seed
103	 * beforehand)
104	 */
105	status = efi_bs_call(allocate_pool, EFI_ACPI_RECLAIM_MEMORY,
106			     struct_size(seed, bits, seed_size),
107			     (void **)&seed);
108	if (status != EFI_SUCCESS) {
109		efi_warn("Failed to allocate memory for RNG seed.\n");
110		goto err_warn;
111	}
112
113	if (rng) {
114		status = efi_call_proto(rng, get_rng, &rng_algo_raw,
115					EFI_RANDOM_SEED_SIZE, seed->bits);
116
117		if (status == EFI_UNSUPPORTED)
118			/*
119			 * Use whatever algorithm we have available if the raw algorithm
120			 * is not implemented.
121			 */
122			status = efi_call_proto(rng, get_rng, NULL,
123						EFI_RANDOM_SEED_SIZE, seed->bits);
124
125		if (status == EFI_SUCCESS)
126			offset = EFI_RANDOM_SEED_SIZE;
127	}
 
128
129	if (nv_seed_size) {
130		status = get_efi_var(L"RandomSeed", &rng_table_guid, NULL,
131				     &nv_seed_size, seed->bits + offset);
132
133		if (status == EFI_SUCCESS)
134			/*
135			 * We delete the seed here, and /hope/ that this causes
136			 * EFI to also zero out its representation on disk.
137			 * This is somewhat idealistic, but overwriting the
138			 * variable with zeros is likely just as fraught too.
139			 * TODO: in the future, maybe we can hash it forward
140			 * instead, and write a new seed.
141			 */
142			status = set_efi_var(L"RandomSeed", &rng_table_guid, 0,
143					     0, NULL);
144
145		if (status == EFI_SUCCESS)
146			offset += nv_seed_size;
147		else
148			memzero_explicit(seed->bits + offset, nv_seed_size);
149	}
150
151	if (!offset)
152		goto err_freepool;
153
154	if (prev_seed_size) {
155		memcpy(seed->bits + offset, prev_seed->bits, prev_seed_size);
156		offset += prev_seed_size;
157	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
159	seed->size = offset;
160	status = efi_bs_call(install_configuration_table, &rng_table_guid, seed);
161	if (status != EFI_SUCCESS)
162		goto err_freepool;
163
164	if (prev_seed_size) {
165		/* wipe and free the old seed if we managed to install the new one */
166		memzero_explicit(prev_seed->bits, prev_seed_size);
167		efi_bs_call(free_pool, prev_seed);
 
168	}
169	return EFI_SUCCESS;
170
171err_freepool:
172	memzero_explicit(seed, struct_size(seed, bits, seed_size));
173	efi_bs_call(free_pool, seed);
174	efi_warn("Failed to obtain seed from EFI_RNG_PROTOCOL or EFI variable\n");
175err_warn:
176	if (prev_seed)
177		efi_warn("Retaining bootloader-supplied seed only");
178	return status;
179}
v4.6
 
  1/*
  2 * Copyright (C) 2016 Linaro Ltd;  <ard.biesheuvel@linaro.org>
  3 *
  4 * This program is free software; you can redistribute it and/or modify
  5 * it under the terms of the GNU General Public License version 2 as
  6 * published by the Free Software Foundation.
  7 *
  8 */
  9
 10#include <linux/efi.h>
 11#include <asm/efi.h>
 12
 13#include "efistub.h"
 14
 15struct efi_rng_protocol {
 16	efi_status_t (*get_info)(struct efi_rng_protocol *,
 17				 unsigned long *, efi_guid_t *);
 18	efi_status_t (*get_rng)(struct efi_rng_protocol *,
 19				efi_guid_t *, unsigned long, u8 *out);
 
 
 
 
 
 
 
 
 
 
 20};
 21
 22efi_status_t efi_get_random_bytes(efi_system_table_t *sys_table_arg,
 23				  unsigned long size, u8 *out)
 
 
 
 
 
 
 
 
 
 
 24{
 25	efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
 26	efi_status_t status;
 27	struct efi_rng_protocol *rng;
 28
 29	status = efi_call_early(locate_protocol, &rng_proto, NULL,
 30				(void **)&rng);
 31	if (status != EFI_SUCCESS)
 32		return status;
 33
 34	return rng->get_rng(rng, NULL, size, out);
 35}
 36
 37/*
 38 * Return the number of slots covered by this entry, i.e., the number of
 39 * addresses it covers that are suitably aligned and supply enough room
 40 * for the allocation.
 
 
 
 
 
 
 
 
 41 */
 42static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
 43					 unsigned long size,
 44					 unsigned long align)
 45{
 46	u64 start, end;
 
 
 
 
 
 
 
 47
 48	if (md->type != EFI_CONVENTIONAL_MEMORY)
 49		return 0;
 
 50
 51	start = round_up(md->phys_addr, align);
 52	end = round_down(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - size,
 53			 align);
 
 54
 55	if (start > end)
 56		return 0;
 57
 58	return (end - start + 1) / align;
 59}
 
 
 
 
 
 
 
 
 
 
 60
 61/*
 62 * The UEFI memory descriptors have a virtual address field that is only used
 63 * when installing the virtual mapping using SetVirtualAddressMap(). Since it
 64 * is unused here, we can reuse it to keep track of each descriptor's slot
 65 * count.
 66 */
 67#define MD_NUM_SLOTS(md)	((md)->virt_addr)
 
 
 
 
 
 68
 69efi_status_t efi_random_alloc(efi_system_table_t *sys_table_arg,
 70			      unsigned long size,
 71			      unsigned long align,
 72			      unsigned long *addr,
 73			      unsigned long random_seed)
 74{
 75	unsigned long map_size, desc_size, total_slots = 0, target_slot;
 76	efi_status_t status;
 77	efi_memory_desc_t *memory_map;
 78	int map_offset;
 
 79
 80	status = efi_get_memory_map(sys_table_arg, &memory_map, &map_size,
 81				    &desc_size, NULL, NULL);
 82	if (status != EFI_SUCCESS)
 83		return status;
 84
 85	if (align < EFI_ALLOC_ALIGN)
 86		align = EFI_ALLOC_ALIGN;
 
 87
 88	/* count the suitable slots in each memory map entry */
 89	for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
 90		efi_memory_desc_t *md = (void *)memory_map + map_offset;
 91		unsigned long slots;
 
 
 
 
 
 
 
 92
 93		slots = get_entry_num_slots(md, size, align);
 94		MD_NUM_SLOTS(md) = slots;
 95		total_slots += slots;
 
 96	}
 97
 98	/* find a random number between 0 and total_slots */
 99	target_slot = (total_slots * (u16)random_seed) >> 16;
100
101	/*
102	 * target_slot is now a value in the range [0, total_slots), and so
103	 * it corresponds with exactly one of the suitable slots we recorded
104	 * when iterating over the memory map the first time around.
105	 *
106	 * So iterate over the memory map again, subtracting the number of
107	 * slots of each entry at each iteration, until we have found the entry
108	 * that covers our chosen slot. Use the residual value of target_slot
109	 * to calculate the randomly chosen address, and allocate it directly
110	 * using EFI_ALLOCATE_ADDRESS.
111	 */
112	for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
113		efi_memory_desc_t *md = (void *)memory_map + map_offset;
114		efi_physical_addr_t target;
115		unsigned long pages;
116
117		if (target_slot >= MD_NUM_SLOTS(md)) {
118			target_slot -= MD_NUM_SLOTS(md);
119			continue;
120		}
121
122		target = round_up(md->phys_addr, align) + target_slot * align;
123		pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
 
 
124
125		status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS,
126					EFI_LOADER_DATA, pages, &target);
127		if (status == EFI_SUCCESS)
128			*addr = target;
129		break;
130	}
 
131
132	efi_call_early(free_pool, memory_map);
133
 
 
 
 
 
134	return status;
135}