Skip to content

Commit 3af2075

Browse files
sarroutbiansasaki
authored andcommitted
Add uefi_log_handler.rs to parse UEFI binary
Resolves: #1020 Signed-off-by: Sergio Arroutbi <[email protected]>
1 parent 2a633dc commit 3af2075

File tree

10 files changed

+547
-46
lines changed

10 files changed

+547
-46
lines changed

Cargo.lock

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ actix-web = { version = "4", default-features = false, features = ["macros", "o
2020
anyhow = { version = "1.0", features = ["backtrace"] }
2121
assert_cmd = { version = "2.0.16" }
2222
base64 = "0.22"
23+
byteorder = "1.5.0"
2324
cfg-if = "1"
2425
chrono = { version = "0.4.40", features = ["serde"] }
2526
clap = { version = "4.5", features = ["derive"] }

keylime-push-model-agent/src/struct_filler.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use keylime::config::{KeylimeConfig, PushModelConfigTrait};
55
use keylime::context_info::ContextInfo;
66
use keylime::ima::ImaLog;
77
use keylime::structures;
8+
use keylime::uefi::uefi_log_handler;
89
use log::error;
910

1011
pub trait StructureFiller {
@@ -47,11 +48,32 @@ impl StructureFiller for FillerFromHardware<'_> {
4748

4849
pub struct FillerFromHardware<'a> {
4950
pub tpm_context_info: &'a mut ContextInfo,
51+
pub uefi_log_handler: Option<uefi_log_handler::UefiLogHandler>,
5052
}
5153

5254
impl<'a> FillerFromHardware<'a> {
5355
pub fn new(tpm_context_info: &'a mut ContextInfo) -> Self {
54-
FillerFromHardware { tpm_context_info }
56+
// TODO: Change config obtaining here to avoid repetitions
57+
let global_config = KeylimeConfig::new();
58+
let ml_path = match global_config {
59+
Ok(config) => config.agent.measuredboot_ml_path.clone(),
60+
Err(_) => "".to_string(),
61+
};
62+
let uefi_log_handler =
63+
uefi_log_handler::UefiLogHandler::new(&ml_path);
64+
match uefi_log_handler {
65+
Ok(handler) => FillerFromHardware {
66+
tpm_context_info,
67+
uefi_log_handler: Some(handler),
68+
},
69+
Err(e) => {
70+
error!("Failed to create UEFI log handler: {}", e);
71+
FillerFromHardware {
72+
tpm_context_info,
73+
uefi_log_handler: None,
74+
}
75+
}
76+
}
5577
}
5678
// TODO: Change this function to use the attestation request appropriately
5779
// Add self to the function signature to use the tpm_context
@@ -81,6 +103,10 @@ impl<'a> FillerFromHardware<'a> {
81103
0
82104
}
83105
};
106+
let uefi_count = self
107+
.uefi_log_handler
108+
.as_ref()
109+
.map_or(0, |handler| handler.get_entry_count());
84110
structures::AttestationRequest {
85111
data: structures::RequestData {
86112
type_: "attestation".to_string(),
@@ -111,7 +137,7 @@ impl<'a> FillerFromHardware<'a> {
111137
evidence_type: "uefi_log".to_string(),
112138
capabilities: structures::LogCapabilities {
113139
evidence_version: Some(config.get_uefi_logs_evidence_version()),
114-
entry_count: 0, // Placeholder, as we don't have a count here
140+
entry_count: uefi_count,
115141
supports_partial_access: config.get_uefi_logs_supports_partial_access(),
116142
appendable: config.get_uefi_logs_appendable(),
117143
formats: config.get_uefi_logs_formats(),

keylime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ version.workspace = true
1111
actix-web.workspace = true
1212
anyhow.workspace = true
1313
base64.workspace = true
14+
byteorder.workspace = true
1415
chrono.workspace = true
1516
config.workspace = true
1617
glob.workspace = true

keylime/src/cert.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ mod tests {
134134
fn test_cert_no_server_cert() {
135135
// Ensure the test cert file does not exist before the test
136136
const TEST_KEY_PATH: &str = "test_key.pem";
137+
let _ = std::fs::remove_file(TEST_KEY_PATH);
137138
let config = CertificateConfig {
138139
agent_uuid: "test-uuid".to_string(),
139140
contact_ip: "1.2.3.4".to_string(),

keylime/src/config/push_model.rs

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@ pub const DEFAULT_UEFI_LOGS_APPENDABLE: bool = true;
2222
pub const DEFAULT_UEFI_LOGS_EVIDENCE_VERSION: &str = "2.1";
2323
pub const DEFAULT_UEFI_LOGS_FORMATS: &[&str] = &["application/octet-stream"];
2424
pub const DEFAULT_UEFI_LOGS_SUPPORTS_PARTIAL_ACCESS: bool = true;
25-
pub const DEFAULT_MEASUREDBOOT_ML_DIRECTORY_PATH: &str =
26-
"/sys/kernel/security/tpm0";
27-
pub static DEFAULT_MEASUREDBOOT_ML_COUNT_FILE: Lazy<String> =
28-
Lazy::new(|| format!("{}/count", DEFAULT_MEASUREDBOOT_ML_DIRECTORY_PATH));
25+
26+
pub const DEFAULT_UEFI_LOGS_BINARY_PATH: &str = "/sys/kernel/security/tpm0";
27+
pub const DEFAULT_UEFI_LOGS_BINARY_FILE: &str = "binary_bios_measurements";
28+
pub static DEFAULT_UEFI_LOGS_BINARY_FILE_PATH: Lazy<String> =
29+
Lazy::new(|| {
30+
format!(
31+
"{}/{}",
32+
DEFAULT_UEFI_LOGS_BINARY_PATH, DEFAULT_UEFI_LOGS_BINARY_FILE
33+
)
34+
});
2935

3036
pub trait PushModelConfigTrait {
3137
fn get_certification_keys_server_identifier(&self) -> String;
3238
fn get_contact_ip(&self) -> String;
3339
fn get_contact_port(&self) -> u32;
3440
fn get_enable_iak_idevid(&self) -> bool;
3541
fn get_ek_handle(&self) -> String;
36-
fn get_measuredboot_ml_directory_path(&self) -> String;
37-
fn get_measuredboot_ml_count_file(&self) -> String;
3842
fn get_ima_logs_appendable(&self) -> bool;
3943
fn get_ima_logs_formats(&self) -> Vec<String>;
4044
fn get_ima_logs_supports_partial_access(&self) -> bool;
@@ -51,6 +55,7 @@ pub trait PushModelConfigTrait {
5155
fn get_registrar_api_versions(&self) -> Vec<String>;
5256
fn get_api_versions(&self) -> Vec<String>;
5357
fn get_uefi_logs_appendable(&self) -> bool;
58+
fn get_uefi_logs_binary_file_path(&self) -> String;
5459
fn get_uefi_logs_evidence_version(&self) -> String;
5560
fn get_uefi_logs_formats(&self) -> Vec<String>;
5661
fn get_uefi_logs_supports_partial_access(&self) -> bool;
@@ -78,11 +83,6 @@ impl Default for PushModelConfig {
7883
.to_string()
7984
.clone(),
8085
ima_ml_count_file: DEFAULT_IMA_ML_COUNT_FILE.to_string().clone(),
81-
measuredboot_ml_directory_path:
82-
DEFAULT_MEASUREDBOOT_ML_DIRECTORY_PATH.to_string().clone(),
83-
measuredboot_ml_count_file: DEFAULT_MEASUREDBOOT_ML_COUNT_FILE
84-
.to_string()
85-
.clone(),
8686
registrar_ip: DEFAULT_REGISTRAR_IP.to_string(),
8787
registrar_port: DEFAULT_REGISTRAR_PORT,
8888
registrar_api_versions: DEFAULT_REGISTRAR_API_VERSIONS
@@ -93,6 +93,8 @@ impl Default for PushModelConfig {
9393
server_key: DEFAULT_SERVER_KEY.to_string(),
9494
server_key_password: DEFAULT_SERVER_KEY_PASSWORD.to_string(),
9595
uefi_logs_appendable: DEFAULT_UEFI_LOGS_APPENDABLE,
96+
uefi_logs_binary_file_path: DEFAULT_UEFI_LOGS_BINARY_FILE_PATH
97+
.to_string(),
9698
uefi_logs_evidence_version: DEFAULT_UEFI_LOGS_EVIDENCE_VERSION
9799
.to_string(),
98100
uefi_logs_formats: DEFAULT_UEFI_LOGS_FORMATS
@@ -125,8 +127,6 @@ pub struct PushModelConfig {
125127
ima_logs_supports_partial_access: bool,
126128
ima_ml_directory_path: String,
127129
ima_ml_count_file: String,
128-
measuredboot_ml_directory_path: String,
129-
measuredboot_ml_count_file: String,
130130
registrar_api_versions: Vec<String>,
131131
registrar_ip: String,
132132
registrar_port: u32,
@@ -136,6 +136,7 @@ pub struct PushModelConfig {
136136
tpm_encryption_alg: String,
137137
tpm_hash_alg: String,
138138
tpm_signing_alg: String,
139+
uefi_logs_binary_file_path: String,
139140
uefi_logs_evidence_version: String,
140141
uefi_logs_supports_partial_access: bool,
141142
uefi_logs_appendable: bool,
@@ -190,14 +191,6 @@ impl PushModelConfigTrait for PushModelConfig {
190191
self.ima_ml_directory_path.clone()
191192
}
192193

193-
fn get_measuredboot_ml_directory_path(&self) -> String {
194-
self.measuredboot_ml_directory_path.clone()
195-
}
196-
197-
fn get_measuredboot_ml_count_file(&self) -> String {
198-
self.measuredboot_ml_count_file.clone()
199-
}
200-
201194
fn get_registrar_ip(&self) -> String {
202195
self.registrar_ip.clone()
203196
}
@@ -226,6 +219,10 @@ impl PushModelConfigTrait for PushModelConfig {
226219
self.uefi_logs_appendable
227220
}
228221

222+
fn get_uefi_logs_binary_file_path(&self) -> String {
223+
self.uefi_logs_binary_file_path.clone()
224+
}
225+
229226
fn get_uefi_logs_evidence_version(&self) -> String {
230227
self.uefi_logs_evidence_version.clone()
231228
}
@@ -265,9 +262,9 @@ impl PushModelConfigTrait for PushModelConfig {
265262
enable_iak_idevid: {}, ek_handle: {},
266263
ima_logs_appendable: {}, ima_logs_formats: {:?}, ima_logs_supports_partial_access: {},
267264
ima_ml_directory_path: {}, ima_ml_count_file: {},
268-
measuredboot_ml_directory_path: {}, measuredboot_ml_count_file: {},
269265
registrar_ip: {}, registrar_port: {}, server_cert: {},
270266
server_key: {}, server_key_password: {},
267+
uefi_logs_binary_file_path: {},
271268
uefi_logs_evidence_version: {}, uefi_logs_supports_partial_access: {},
272269
uefi_logs_appendable: {}, uefi_logs_formats: {:?},
273270
tpm_encryption_alg: {}, tpm_hash_alg: {}, tpm_signing_alg: {},
@@ -282,13 +279,12 @@ impl PushModelConfigTrait for PushModelConfig {
282279
self.ima_logs_supports_partial_access,
283280
self.ima_ml_directory_path,
284281
self.ima_ml_count_file,
285-
self.measuredboot_ml_directory_path,
286-
self.measuredboot_ml_count_file,
287282
self.registrar_ip,
288283
self.registrar_port,
289284
self.server_cert,
290285
self.server_key,
291286
self.server_key_password,
287+
self.uefi_logs_binary_file_path,
292288
self.uefi_logs_evidence_version,
293289
self.uefi_logs_supports_partial_access,
294290
self.uefi_logs_appendable,
@@ -343,19 +339,18 @@ mod tests {
343339
pmc.get_ima_ml_count_file()
344340
== DEFAULT_IMA_ML_COUNT_FILE.to_string()
345341
);
346-
assert!(
347-
pmc.get_measuredboot_ml_directory_path()
348-
== DEFAULT_MEASUREDBOOT_ML_DIRECTORY_PATH
349-
);
350-
assert!(
351-
pmc.get_measuredboot_ml_count_file()
352-
== DEFAULT_MEASUREDBOOT_ML_COUNT_FILE.to_string()
353-
);
354342
assert!(pmc.get_registrar_ip() == DEFAULT_REGISTRAR_IP);
355343
assert!(pmc.get_registrar_port() == DEFAULT_REGISTRAR_PORT);
356344
assert!(pmc.get_server_cert() == DEFAULT_SERVER_CERT);
357345
assert!(pmc.get_server_key() == DEFAULT_SERVER_KEY);
358346
assert!(pmc.get_server_key_password() == DEFAULT_SERVER_KEY_PASSWORD);
347+
assert!(
348+
pmc.get_uefi_logs_appendable() == DEFAULT_UEFI_LOGS_APPENDABLE
349+
);
350+
assert!(
351+
pmc.get_uefi_logs_binary_file_path()
352+
== DEFAULT_UEFI_LOGS_BINARY_FILE_PATH.to_string()
353+
);
359354
assert!(
360355
pmc.get_uefi_logs_evidence_version()
361356
== DEFAULT_UEFI_LOGS_EVIDENCE_VERSION
@@ -384,7 +379,7 @@ mod tests {
384379

385380
#[test]
386381
fn test_display_config() {
387-
let pmc = PushModelConfig::default();
382+
let pmc = PushModelConfig::new();
388383
let display_string = pmc.to_string();
389384
assert!(display_string
390385
.contains(&pmc.get_certification_keys_server_identifier()));
@@ -404,11 +399,6 @@ mod tests {
404399
));
405400
assert!(display_string.contains(&pmc.get_ima_ml_directory_path()));
406401
assert!(display_string.contains(&pmc.get_ima_ml_count_file()));
407-
assert!(display_string
408-
.contains(&pmc.get_measuredboot_ml_directory_path()));
409-
assert!(
410-
display_string.contains(&pmc.get_measuredboot_ml_count_file())
411-
);
412402
assert!(display_string.contains(&pmc.get_registrar_ip()));
413403
assert!(
414404
display_string.contains(&pmc.get_registrar_port().to_string())

keylime/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ pub enum Error {
112112
CertificateGeneration(
113113
#[from] crate::crypto::x509::CertificateBuilderError,
114114
),
115+
#[error("UEFI Log parser error: {0}")]
116+
UEFILog(String),
115117
#[error("{0}")]
116118
Other(String),
117119
}

keylime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod secure_mount;
2323
pub mod serialization;
2424
pub mod structures;
2525
pub mod tpm;
26+
pub mod uefi;
2627
pub mod version;
2728

2829
#[macro_use]

keylime/src/uefi/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod uefi_log_handler;
2+
3+
pub use uefi_log_handler::*;

0 commit comments

Comments
 (0)