Skip to contentSkip to conversation

um/random: reproducible random

RFC New Johannes Berg 1 participant 4 messages Nov 19

Subsystems (2)
lore
Share & tools
lore

Johannes Berg

author maintainer
#0 lore raw

cover um/random: reproducible random

So these patches are *clearly* (except the first one maybe) not something we actually want to apply upstream, but we've been carrying them for years out-of-tree (no problem there) and Lingao just asked because they have the same need when testing BT (we did it for testing WiFi):
https://lore.kernel.org/linux-um/[email protected]/

The idea is to even get things like key derivation into a reproducible state for tests, to make it easier to compare and be sure the runs are the same.

So at least I figured I'd send them, maybe we _could_ consider doing something like that behind a scary Kconfig option or one that depends already on time-travel in UML, or ...

But even if this is going to fly like a lead balloon then the patches are now out there for whoever wants to pick them up and run with them when they have the same need.

johannes

author maintainer
#1 lore raw

RFC 1/3 um: Use fixed random seed if UML_RANDOM is set

From: Benjamin Berg <[email protected]>

For a more reproducible /dev/random inside test runs, enable setting the UML_RANDOM environment variable. This helps create reproducable test runs.

---
 arch/um/os-Linux/util.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
arch/um/os-Linux/util.c +36 -0
index e3ad71a0d13c..3a3baf4df587 100644
@@ -98,6 +98,42 @@ static inline void __attribute__ ((noreturn)) uml_abort(void)
 
 ssize_t os_getrandom(void *buf, size_t len, unsigned int flags)
 {
+	static char random_pattern[128];
+	static ssize_t random_pattern_len = -1;
+
+	/* This happens when called by setup_arch */
+	if (random_pattern_len == -1) {
+		const char *env;
+
+		env = getenv("UML_RANDOM");
+		if (env) {
+			random_pattern_len =
+				strlen(env) > sizeof(random_pattern) ?
+				sizeof(random_pattern) : strlen(env);
+			memcpy(random_pattern, env, random_pattern_len);
+		} else {
+			random_pattern_len = 0;
+		}
+	}
+
+	if (random_pattern_len > 0) {
+		size_t tail = len;
+		/*
+		 * If the returned length is too short, then the kernel might
+		 * loop trying to generate random from the passing of time.
+		 * Which seems to possibly infinite loop in time-travel mode.
+		 * So just repeat the given pattern.
+		 */
+		while (tail > random_pattern_len) {
+			memcpy(buf, random_pattern, random_pattern_len);
+			tail -= random_pattern_len;
+			buf += random_pattern_len;
+		}
+		memcpy(buf, random_pattern, tail);
+
+		return len;
+	}
+
 	return getrandom(buf, len, flags);
 }
 
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

RFC 2/3 random: do not include utsname in early random

In reply to Johannes Berg

From: Benjamin Berg <[email protected]>

The uts name includes information about the compile time and such and changes. Exclude it, so that different kernel compilations will see the same random numbers.

---
 drivers/char/random.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/random.c +1 -1
index b4da1fb976c1..64d0cfc99ac4 100644
@@ -877,7 +877,7 @@ void __init random_init_early(const char *command_line)
 		++i;
 	}
 
-	_mix_pool_bytes(init_utsname(), sizeof(*(init_utsname())));
+	/* _mix_pool_bytes(init_utsname(), sizeof(*(init_utsname()))); */
 	_mix_pool_bytes(command_line, strlen(command_line));
 
 	/* Reseed if already seeded by earlier phases. */
Signature · 1 line
2.56.1
author maintainer
#3 lore raw

RFC 3/3 random: disable interrupt random source

In reply to Johannes Berg

From: Benjamin Berg <[email protected]>

Interrupts in our UML environment are signals, which can be delivered at somewhat random times depending on host scheduling. Disable these as a source to make the random reproducible between runs.

---
 drivers/char/random.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/char/random.c +3 -0
index 64d0cfc99ac4..be9f744f3e72 100644
@@ -1104,6 +1104,9 @@ void add_interrupt_randomness(int irq)
 	struct pt_regs *regs = get_irq_regs();
 	unsigned int new_count;
 
+	/* UML seems to not always get the signals entirely at the same time */
+	return;
+
 	fast_mix(fast_pool->pool, entropy,
 		 (regs ? instruction_pointer(regs) : _RET_IP_) ^ swab(irq));
 	new_count = ++fast_pool->count;
Signature · 1 line
2.56.1