Skip to content

Commit 92f611e

Browse files
committed
feat(shim): initial shim support
1 parent 2093269 commit 92f611e

6 files changed

Lines changed: 249 additions & 40 deletions

File tree

src/actions/chainload.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::context::SproutContext;
22
use crate::integrations::bootloader_interface::BootloaderInterface;
3+
use crate::integrations::shim::{ShimInput, ShimSupport};
34
use crate::utils;
45
use crate::utils::media_loader::MediaLoaderHandle;
56
use crate::utils::media_loader::constants::linux::LINUX_EFI_INITRD_MEDIA_GUID;
@@ -40,15 +41,9 @@ pub fn chainload(context: Rc<SproutContext>, configuration: &ChainloadConfigurat
4041
)
4142
.context("unable to resolve chainload path")?;
4243

43-
// Load the image to chainload.
44-
let image = uefi::boot::load_image(
45-
sprout_image,
46-
uefi::boot::LoadImageSource::FromDevicePath {
47-
device_path: &resolved.full_path,
48-
boot_policy: uefi::proto::BootPolicy::ExactMatch,
49-
},
50-
)
51-
.context("unable to load image")?;
44+
// Load the image to chainload using the shim support integration.
45+
// It will determine if the image needs to be loaded via the shim or can be loaded directly.
46+
let image = ShimSupport::load(sprout_image, ShimInput::ResolvedPath(&resolved))?;
5247

5348
// Open the LoadedImage protocol of the image to chainload.
5449
let mut loaded_image_protocol = uefi::boot::open_protocol_exclusive::<LoadedImage>(image)

src/drivers.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::context::SproutContext;
2+
use crate::integrations::shim::{ShimInput, ShimSupport};
23
use crate::utils;
34
use anyhow::{Context, Result};
45
use log::info;
56
use serde::{Deserialize, Serialize};
67
use std::collections::BTreeMap;
78
use std::rc::Rc;
89
use uefi::boot::SearchType;
9-
use uefi::proto::device_path::LoadedImageDevicePath;
1010

1111
/// Declares a driver configuration.
1212
/// Drivers allow extending the functionality of Sprout.
@@ -23,28 +23,17 @@ pub struct DriverDeclaration {
2323
fn load_driver(context: Rc<SproutContext>, driver: &DriverDeclaration) -> Result<()> {
2424
// Acquire the handle and device path of the loaded image.
2525
let sprout_image = uefi::boot::image_handle();
26-
let image_device_path_protocol =
27-
uefi::boot::open_protocol_exclusive::<LoadedImageDevicePath>(sprout_image)
28-
.context("unable to open loaded image device path protocol")?;
2926

30-
// Get the device path root of the sprout image.
31-
let mut full_path = utils::device_path_root(&image_device_path_protocol)?;
32-
33-
// Push the path of the driver from the root.
34-
full_path.push_str(&context.stamp(&driver.path));
35-
36-
// Convert the path to a device path.
37-
let device_path = utils::text_to_device_path(&full_path)?;
38-
39-
// Load the driver image.
40-
let image = uefi::boot::load_image(
41-
sprout_image,
42-
uefi::boot::LoadImageSource::FromDevicePath {
43-
device_path: &device_path,
44-
boot_policy: uefi::proto::BootPolicy::ExactMatch,
45-
},
27+
// Resolve the path to the driver image.
28+
let resolved = utils::resolve_path(
29+
context.root().loaded_image_path()?,
30+
&context.stamp(&driver.path),
4631
)
47-
.context("unable to load image")?;
32+
.context("unable to resolve path to driver")?;
33+
34+
// Load the driver image using the shim support integration.
35+
// It will determine if the image needs to be loaded via the shim or can be loaded directly.
36+
let image = ShimSupport::load(sprout_image, ShimInput::ResolvedPath(&resolved))?;
4837

4938
// Start the driver image, this is expected to return control to sprout.
5039
// There is no guarantee that the driver will actually return control as it is

src/integrations.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/// Implements support for the bootloader interface specification.
22
pub mod bootloader_interface;
3+
/// Implements support for the shim loader application for Secure Boot.
4+
pub mod shim;

src/integrations/shim.rs

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::platform::timer::PlatformTimer;
1616
use crate::secure::SecureBoot;
1717
use crate::utils::PartitionGuidForm;
1818
use anyhow::{Context, Result, bail};
19-
use log::{error, info};
19+
use log::{error, info, warn};
2020
use std::collections::BTreeMap;
2121
use std::ops::Deref;
2222
use std::time::Duration;
@@ -74,7 +74,7 @@ pub mod utils;
7474
fn run() -> Result<()> {
7575
// For safety reasons, we will bail early if Secure Boot is enabled.
7676
if SecureBoot::enabled().context("unable to determine Secure Boot status")? {
77-
bail!("Secure Boot is enabled. Sprout does not currently support Secure Boot.");
77+
warn!("Secure Boot is enabled. Sprout does not currently support Secure Boot.");
7878
}
7979

8080
// Start the platform timer.

src/utils.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::{Context, Result};
22
use std::ops::Deref;
3+
use uefi::boot::SearchType;
34
use uefi::fs::{FileSystem, Path};
45
use uefi::proto::device_path::text::{AllowShortcuts, DevicePathFromText, DisplayOnly};
56
use uefi::proto::device_path::{DevicePath, PoolDevicePath};
@@ -103,6 +104,20 @@ pub struct ResolvedPath {
103104
pub filesystem_handle: Handle,
104105
}
105106

107+
impl ResolvedPath {
108+
/// Read the file specified by this path into a buffer and return it.
109+
pub fn read_file(&self) -> Result<Vec<u8>> {
110+
let fs = uefi::boot::open_protocol_exclusive::<SimpleFileSystem>(self.filesystem_handle)
111+
.context("unable to open filesystem protocol")?;
112+
let mut fs = FileSystem::new(fs);
113+
let path = self
114+
.sub_path
115+
.to_string(DisplayOnly(false), AllowShortcuts(false))?;
116+
let content = fs.read(Path::new(&path));
117+
content.context("unable to read file contents")
118+
}
119+
}
120+
106121
/// Resolve a path specified by `input` to its various components.
107122
/// Uses `default_root_path` as the base root if one is not specified in the path.
108123
/// Returns [ResolvedPath] which contains the resolved components.
@@ -157,14 +172,7 @@ pub fn resolve_path(default_root_path: &DevicePath, input: &str) -> Result<Resol
157172
/// the filesystem handle protocol acquired.
158173
pub fn read_file_contents(default_root_path: &DevicePath, input: &str) -> Result<Vec<u8>> {
159174
let resolved = resolve_path(default_root_path, input)?;
160-
let fs = uefi::boot::open_protocol_exclusive::<SimpleFileSystem>(resolved.filesystem_handle)
161-
.context("unable to open filesystem protocol")?;
162-
let mut fs = FileSystem::new(fs);
163-
let path = resolved
164-
.sub_path
165-
.to_string(DisplayOnly(false), AllowShortcuts(false))?;
166-
let content = fs.read(Path::new(&path));
167-
content.context("unable to read file contents")
175+
resolved.read_file()
168176
}
169177

170178
/// Filter a string-like Option `input` such that an empty string is [None].
@@ -232,3 +240,25 @@ pub fn partition_guid(path: &DevicePath, form: PartitionGuidForm) -> Result<Opti
232240
Ok(None)
233241
}
234242
}
243+
244+
/// Find a handle that provides the specified `protocol`.
245+
pub fn find_handle(protocol: &Guid) -> Result<Option<Handle>> {
246+
// Locate the requested protocol handle.
247+
match uefi::boot::locate_handle_buffer(SearchType::ByProtocol(protocol)) {
248+
// If a handle is found, the protocol is available.
249+
Ok(handles) => Ok(if handles.is_empty() {
250+
None
251+
} else {
252+
Some(handles[0])
253+
}),
254+
// If an error occurs, check if it is because the protocol is not available.
255+
// If so, return false. Otherwise, return the error.
256+
Err(error) => {
257+
if error.status() == Status::NOT_FOUND {
258+
Ok(None)
259+
} else {
260+
Err(error).context("unable to determine if the protocol is available")
261+
}
262+
}
263+
}
264+
}

0 commit comments

Comments
 (0)