|
| 1 | +use crate::utils; |
| 2 | +use crate::utils::ResolvedPath; |
| 3 | +use anyhow::{Context, Result, anyhow, bail}; |
| 4 | +use std::ffi::c_void; |
| 5 | +use uefi::Handle; |
| 6 | +use uefi::boot::LoadImageSource; |
| 7 | +use uefi::proto::device_path::DevicePath; |
| 8 | +use uefi::proto::unsafe_protocol; |
| 9 | +use uefi_raw::{Guid, Status, guid}; |
| 10 | + |
| 11 | +/// Support for the shim loader application for Secure Boot. |
| 12 | +pub struct ShimSupport; |
| 13 | + |
| 14 | +/// Input to the shim mechanisms. |
| 15 | +pub enum ShimInput<'a> { |
| 16 | + /// Data loaded into a buffer and ready to be verified, owned. |
| 17 | + OwnedDataBuffer(Option<&'a ResolvedPath>, Vec<u8>), |
| 18 | + /// Data loaded into a buffer and ready to be verified. |
| 19 | + DataBuffer(Option<&'a ResolvedPath>, &'a [u8]), |
| 20 | + /// Data is provided as a resolved path. We will need to load the data to verify it. |
| 21 | + /// The output will them return the loaded data. |
| 22 | + ResolvedPath(&'a ResolvedPath), |
| 23 | +} |
| 24 | + |
| 25 | +impl<'a> ShimInput<'a> { |
| 26 | + /// Accesses the buffer behind the shim input, if available. |
| 27 | + pub fn buffer(&self) -> Option<&[u8]> { |
| 28 | + match self { |
| 29 | + ShimInput::OwnedDataBuffer(_, data) => Some(data), |
| 30 | + ShimInput::DataBuffer(_, data) => Some(data), |
| 31 | + ShimInput::ResolvedPath(_) => None, |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /// Accesses the full device path to the input. |
| 36 | + pub fn file_path(&self) -> Option<&DevicePath> { |
| 37 | + match self { |
| 38 | + ShimInput::OwnedDataBuffer(path, _) => path.as_ref().map(|it| it.full_path.as_ref()), |
| 39 | + ShimInput::DataBuffer(path, _) => path.as_ref().map(|it| it.full_path.as_ref()), |
| 40 | + ShimInput::ResolvedPath(path) => Some(path.full_path.as_ref()), |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// Converts this input into an owned data buffer, where the data is loaded. |
| 45 | + /// For ResolvedPath, this will read the file. |
| 46 | + pub fn into_owned_data_buffer(self) -> Result<ShimInput<'a>> { |
| 47 | + match self { |
| 48 | + ShimInput::OwnedDataBuffer(root, data) => Ok(ShimInput::OwnedDataBuffer(root, data)), |
| 49 | + |
| 50 | + ShimInput::DataBuffer(root, data) => { |
| 51 | + Ok(ShimInput::OwnedDataBuffer(root, data.to_vec())) |
| 52 | + } |
| 53 | + |
| 54 | + ShimInput::ResolvedPath(path) => { |
| 55 | + Ok(ShimInput::OwnedDataBuffer(Some(path), path.read_file()?)) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/// Output of the shim verification function. |
| 62 | +/// Since the shim needs to load the data from disk, we will optimize by using that as the data |
| 63 | +/// to actually boot. |
| 64 | +pub enum ShimVerificationOutput { |
| 65 | + /// The verification failed. |
| 66 | + VerificationFailed, |
| 67 | + /// The data provided to the verifier was already a buffer. |
| 68 | + VerifiedDataNotLoaded, |
| 69 | + /// Verifying the data resulted in loading the data from the source. |
| 70 | + /// This contains the data that was loaded, so it won't need to be loaded again. |
| 71 | + VerifiedDataBuffer(Vec<u8>), |
| 72 | +} |
| 73 | + |
| 74 | +/// The shim lock protocol as defined by the shim loader application. |
| 75 | +#[unsafe_protocol(ShimSupport::SHIM_LOCK_GUID)] |
| 76 | +struct ShimLockProtocol { |
| 77 | + /// Verify the data in `buffer` with the size `buffer_size` to determine if it is valid. |
| 78 | + pub shim_verify: unsafe extern "efiapi" fn(buffer: *mut c_void, buffer_size: u32) -> Status, |
| 79 | + /// Unused function that is defined by the shim. |
| 80 | + _generate_header: *mut c_void, |
| 81 | + /// Unused function that is defined by the shim. |
| 82 | + _read_header: *mut c_void, |
| 83 | +} |
| 84 | + |
| 85 | +impl ShimSupport { |
| 86 | + /// GUID for the shim lock protocol. |
| 87 | + const SHIM_LOCK_GUID: Guid = guid!("605dab50-e046-4300-abb6-3dd810dd8b23"); |
| 88 | + /// GUID for the shim image loader protocol. |
| 89 | + const SHIM_IMAGE_LOADER_GUID: Guid = guid!("1f492041-fadb-4e59-9e57-7cafe73a55ab"); |
| 90 | + |
| 91 | + /// Determines whether the shim is loaded. |
| 92 | + pub fn loaded() -> Result<bool> { |
| 93 | + Ok(utils::find_handle(&Self::SHIM_LOCK_GUID) |
| 94 | + .context("unable to find shim lock protocol")? |
| 95 | + .is_some()) |
| 96 | + } |
| 97 | + |
| 98 | + /// Determines whether the shim loader is available. |
| 99 | + pub fn loader_available() -> Result<bool> { |
| 100 | + Ok(utils::find_handle(&Self::SHIM_IMAGE_LOADER_GUID) |
| 101 | + .context("unable to find shim image loader protocol")? |
| 102 | + .is_some()) |
| 103 | + } |
| 104 | + |
| 105 | + /// Use the shim to validate the `input`, returning [ShimVerificationOutput] when complete. |
| 106 | + pub fn validate(input: ShimInput) -> Result<ShimVerificationOutput> { |
| 107 | + // Acquire the handle to the shim lock protocol. |
| 108 | + let handle = utils::find_handle(&Self::SHIM_LOCK_GUID) |
| 109 | + .context("unable to find shim lock protocol")? |
| 110 | + .ok_or_else(|| anyhow!("unable to find shim lock protocol"))?; |
| 111 | + // Acquire the protocol exclusively to the shim lock. |
| 112 | + let protocol = uefi::boot::open_protocol_exclusive::<ShimLockProtocol>(handle) |
| 113 | + .context("unable to open shim lock protocol")?; |
| 114 | + |
| 115 | + // If the input type is a device path, we need to load the data. |
| 116 | + let maybe_loaded_data = match input { |
| 117 | + ShimInput::OwnedDataBuffer(_, _data) => { |
| 118 | + bail!("owned data buffer is not supported in the verification function"); |
| 119 | + } |
| 120 | + ShimInput::DataBuffer(_, _) => None, |
| 121 | + ShimInput::ResolvedPath(path) => Some(path.read_file()?), |
| 122 | + }; |
| 123 | + |
| 124 | + // Convert the input to a buffer. |
| 125 | + // If the input provides the data buffer, we will use that. |
| 126 | + // Otherwise, we will use the data loaded by this function. |
| 127 | + let buffer = match input { |
| 128 | + ShimInput::OwnedDataBuffer(_root, _data) => { |
| 129 | + bail!("owned data buffer is not supported in the verification function"); |
| 130 | + } |
| 131 | + ShimInput::DataBuffer(_root, data) => data, |
| 132 | + ShimInput::ResolvedPath(_path) => maybe_loaded_data |
| 133 | + .as_deref() |
| 134 | + .context("expected data buffer to be loaded already")?, |
| 135 | + }; |
| 136 | + |
| 137 | + // Check if the buffer is too large to verify. |
| 138 | + if buffer.len() > u32::MAX as usize { |
| 139 | + bail!("buffer is too large to verify with shim lock protocol"); |
| 140 | + } |
| 141 | + |
| 142 | + // Call the shim verify function. |
| 143 | + // SAFETY: The shim verify function is specified by the shim lock protocol. |
| 144 | + // Calling this function is considered safe because |
| 145 | + let status = |
| 146 | + unsafe { (protocol.shim_verify)(buffer.as_ptr() as *mut c_void, buffer.len() as u32) }; |
| 147 | + |
| 148 | + // If the verification failed, return the verification failure output. |
| 149 | + if !status.is_success() { |
| 150 | + return Ok(ShimVerificationOutput::VerificationFailed); |
| 151 | + } |
| 152 | + |
| 153 | + // If verification succeeded, return the validation output, |
| 154 | + // which might include the loaded data. |
| 155 | + Ok(maybe_loaded_data |
| 156 | + .map(ShimVerificationOutput::VerifiedDataBuffer) |
| 157 | + .unwrap_or(ShimVerificationOutput::VerifiedDataNotLoaded)) |
| 158 | + } |
| 159 | + |
| 160 | + /// Load the image specified by the `input` and returns an image handle. |
| 161 | + pub fn load(current_image: Handle, input: ShimInput) -> Result<Handle> { |
| 162 | + // Determine whether the shim is loaded. |
| 163 | + let shim_loaded = Self::loaded().context("unable to determine if shim is loaded")?; |
| 164 | + |
| 165 | + // Determine whether the shim loader is available. |
| 166 | + let shim_loader_available = |
| 167 | + Self::loader_available().context("unable to determine if shim loader is available")?; |
| 168 | + |
| 169 | + // Determines whether LoadImage in Boot Services must be patched. |
| 170 | + // Version 16 of the shim doesn't require extra effort to load Secure Boot binaries. |
| 171 | + // If the image loader is installed, we can skip over the security override. |
| 172 | + let requires_security_override = shim_loaded && !shim_loader_available; |
| 173 | + |
| 174 | + // If the security override is required, we will bail for now. |
| 175 | + if requires_security_override { |
| 176 | + bail!("shim image loader protocol is not available, please upgrade to shim version 16"); |
| 177 | + } |
| 178 | + |
| 179 | + // Converts the shim input to an owned data buffer. |
| 180 | + let input = input |
| 181 | + .into_owned_data_buffer() |
| 182 | + .context("unable to convert input to loaded data buffer")?; |
| 183 | + |
| 184 | + // Constructs a LoadImageSource from the input. |
| 185 | + let source = LoadImageSource::FromBuffer { |
| 186 | + buffer: input.buffer().context("unable to get buffer from input")?, |
| 187 | + file_path: input.file_path(), |
| 188 | + }; |
| 189 | + |
| 190 | + // Loads the image using Boot Services LoadImage function. |
| 191 | + uefi::boot::load_image(current_image, source).context("unable to load image") |
| 192 | + } |
| 193 | +} |
0 commit comments