Skip to content

random: fix crash on multiple early calls to add_bootloader_randomness() #4829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2022

Conversation

pelwell
Copy link
Contributor

@pelwell pelwell commented Jan 14, 2022

This is a possible fix for #4828. Hat tip to @P33M for finding the upstream patch.

commit f7e67b8 upstream.

Currently, if CONFIG_RANDOM_TRUST_BOOTLOADER is enabled, multiple calls
to add_bootloader_randomness() are broken and can cause a NULL pointer
dereference, as noted by Ivan T. Ivanov. This is not only a hypothetical
problem, as qemu on arm64 may provide bootloader entropy via EFI and via
devicetree.

On the first call to add_hwgenerator_randomness(), crng_fast_load() is
executed, and if the seed is long enough, crng_init will be set to 1.
On subsequent calls to add_bootloader_randomness() and then to
add_hwgenerator_randomness(), crng_fast_load() will be skipped. Instead,
wait_event_interruptible() and then credit_entropy_bits() will be called.
If the entropy count for that second seed is large enough, that proceeds
to crng_reseed().

However, both wait_event_interruptible() and crng_reseed() depends
(at least in numa_crng_init()) on workqueues. Therefore, test whether
system_wq is already initialized, which is a sufficient indicator that
workqueue_init_early() has progressed far enough.

If we wind up hitting the !system_wq case, we later want to do what
would have been done there when wqs are up, so set a flag, and do that
work later from the rand_initialize() call.

Reported-by: Ivan T. Ivanov [email protected]
Fixes: 18b915a ("efi/random: Treat EFI_RNG_PROTOCOL output as bootloader randomness")
Cc: [email protected]
Signed-off-by: Dominik Brodowski [email protected]
[Jason: added crng_need_done state and related logic.]
Signed-off-by: Jason A. Donenfeld [email protected]

commit f7e67b8 upstream.

Currently, if CONFIG_RANDOM_TRUST_BOOTLOADER is enabled, multiple calls
to add_bootloader_randomness() are broken and can cause a NULL pointer
dereference, as noted by Ivan T. Ivanov. This is not only a hypothetical
problem, as qemu on arm64 may provide bootloader entropy via EFI and via
devicetree.

On the first call to add_hwgenerator_randomness(), crng_fast_load() is
executed, and if the seed is long enough, crng_init will be set to 1.
On subsequent calls to add_bootloader_randomness() and then to
add_hwgenerator_randomness(), crng_fast_load() will be skipped. Instead,
wait_event_interruptible() and then credit_entropy_bits() will be called.
If the entropy count for that second seed is large enough, that proceeds
to crng_reseed().

However, both wait_event_interruptible() and crng_reseed() depends
(at least in numa_crng_init()) on workqueues. Therefore, test whether
system_wq is already initialized, which is a sufficient indicator that
workqueue_init_early() has progressed far enough.

If we wind up hitting the !system_wq case, we later want to do what
would have been done there when wqs are up, so set a flag, and do that
work later from the rand_initialize() call.

Reported-by: Ivan T. Ivanov <[email protected]>
Fixes: 18b915a ("efi/random: Treat EFI_RNG_PROTOCOL output as bootloader randomness")
Cc: [email protected]
Signed-off-by: Dominik Brodowski <[email protected]>
[Jason: added crng_need_done state and related logic.]
Signed-off-by: Jason A. Donenfeld <[email protected]>
@colemickens
Copy link

Again confirming that this patch does resolve #4828.

@pelwell pelwell merged commit ec5ae69 into raspberrypi:rpi-5.10.y Jan 22, 2022
@pelwell pelwell deleted the is4828 branch January 22, 2022 06:51
popcornmix added a commit to raspberrypi/firmware that referenced this pull request Jan 27, 2022
See: raspberrypi/linux#4845

kernel: drm/vc4_hdmi: Add Broadcast RGB property to allow override of RGB range
See: raspberrypi/linux#4850

kernel: Update rpi-display overlay
See: raspberrypi/linux#4841

kernel: ARM: dts: BCM2711 AON_INTR2 generates IRQ edges
See: raspberrypi/linux#4838

kernel: drivers: bcm2835_unicam: Disable trigger mode operation
See: raspberrypi/linux#4834

kernel: random: fix crash on multiple early calls to add_bootloader_randomness
See: raspberrypi/linux#4829
popcornmix added a commit to raspberrypi/rpi-firmware that referenced this pull request Jan 27, 2022
See: raspberrypi/linux#4845

kernel: drm/vc4_hdmi: Add Broadcast RGB property to allow override of RGB range
See: raspberrypi/linux#4850

kernel: Update rpi-display overlay
See: raspberrypi/linux#4841

kernel: ARM: dts: BCM2711 AON_INTR2 generates IRQ edges
See: raspberrypi/linux#4838

kernel: drivers: bcm2835_unicam: Disable trigger mode operation
See: raspberrypi/linux#4834

kernel: random: fix crash on multiple early calls to add_bootloader_randomness
See: raspberrypi/linux#4829
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants