UEFI Dangers: the startup.nsh script


Photo of an UEFI screen

What is startup.nsh?

startup.nsh is a one line script whose purpose is to execute UEFI v2 shell code. It is located in the EFI system partition (traditionally mounted as /boot or /boot/efi). The script can be useful in case you e.g. have an EFISTUB kernel and for some reason can’t/don’t want to use a tool such as efibootmgr to create the UEFI boot entry. The script allows for passing and modifying things like kernel parameters and the location of the kernel.

Threat scenario

This scenario assumes that the /boot directory has incorrectly been mounted such that a normal user, a normal group, or others can modify its contents. Any intruder—be it malware or an actual attacker with enough privileges—will be able to write to this directory, in doing so, they can create and/or modify the startup.nsh file.

They are able to, for example, pass kernel parameters that can weaken the system and open it up for more attacks (e.g. meltdown and spectre if they chose to set mitigations=off). They are able to copy their own kernel to the ESP and modify startup.nsh so that the the UEFI will boot it; from a few innocent-looking write permissions they’ll have control over the whole system.

This is an obscure way of messing with the ESP, generally speaking, if the intruder already has write permissions, they will directly modify the kernel and bootloader parameters instead.

This type of attack is particularly useful if secure boot is enabled and the kernel is already signed. In this case the attacker can modify the aforementioned kernel parameters whitout caring about any of that.

Defense

The majority of distro installations already mount the ESP with options that prevent anyone but root from modifying it, more specifically setting fmask and dmask to 0077 or 0022.

Section 3.2 of the UEFI Shell Spec 2.0 describes an option (-nostartup) that can be passed to the UEFI shell to prevent the loading of the script. But it is unclear to me if it is relevant in this context.