lx_emul/random: do not use jitterentropy in case initialization failed

Until now, in case 'jent_entropy_init' failed an error has been produced
and the following jitterentropy functions (i.e.,
'jent_entropy_collector_alloc') where called nevertheless. In case we
received a bad time source error, for example because the performance
counters are not working on the platform, the entropy collector did not return (endless
loop).

Therefore, this commit treats the failed jitterentropy initialization
not as an error but prints a warning about poor randomness quality and
stops using the jitterentropy library from this point on. The
'Jitterentropy::gen_random_u64' will in this case return the address of
a stack variable * some counter.

This is only a interim solution to make platforms work where performance
counters or TSC values do not exist/work.

issue #5104
This commit is contained in:
Sebastian Sumpf 2024-01-31 14:17:26 +01:00 committed by Christian Helmuth
parent 8c9b23ef56
commit 651eb9d4f2

View File

@ -147,6 +147,8 @@ class Jitterentropy : public Entropy_source
{
private:
bool _initialized { false };
rand_data *_rand_data { nullptr };
Jitterentropy(Jitterentropy const &) = delete;
@ -159,13 +161,18 @@ class Jitterentropy : public Entropy_source
{
jitterentropy_init(alloc);
if (jent_entropy_init() != 0) {
error("jitterentropy library could not be initialized!");
int err = jent_entropy_init();
if (err != 0) {
warning("jitterentropy: initialization error (", err,
") randomness is poor quality");
return;
}
_rand_data = jent_entropy_collector_alloc(0, 0);
if (_rand_data == nullptr) {
error("jitterentropy could not allocate entropy collector!");
}
_initialized = true;
}
@ -176,6 +183,13 @@ class Jitterentropy : public Entropy_source
uint64_t gen_random_u64() override
{
uint64_t result;
/* jitterentropy initialization failed and cannot be used, return something */
if (!_initialized) {
static uint64_t counter = 1;
return (uint64_t)&result * ++counter;
}
jent_read_entropy(_rand_data, (char*)&result, sizeof(result));
return result;
}