Skip to content

Commit 91390c4

Browse files
George Almasiansasaki
George Almasi
authored andcommitted
Mutable log paths: allow IMA and MBA log paths to be overridden by keylime configuration.
Signed-off-by: George Almasi <[email protected]> Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
1 parent 63bb12d commit 91390c4

File tree

5 files changed

+98
-33
lines changed

5 files changed

+98
-33
lines changed

GNUmakefile

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ all: $(programs)
2525
$(programs):
2626
cargo build --target-dir="${TARGETDIR}" ${CARGO_ARGS}
2727

28+
.PHONY: clean
29+
clean::
30+
${RM} -rf target
31+
2832
.PHONY: install
2933
install: all
3034
mkdir -p /etc/keylime/

keylime-agent/src/common.rs

-22
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,13 @@ pub const API_VERSION: &str = "v2.1";
3939
pub const TPM_DATA_PCR: usize = 16;
4040
pub const IMA_PCR: usize = 10;
4141
pub static RSA_PUBLICKEY_EXPORTABLE: &str = "rsa placeholder";
42-
pub static IMA_ML: &str =
43-
"/sys/kernel/security/ima/ascii_runtime_measurements";
44-
pub static MEASUREDBOOT_ML: &str =
45-
"/sys/kernel/security/tpm0/binary_bios_measurements";
4642
pub static KEY: &str = "secret";
4743
pub const AGENT_UUID_LEN: usize = 36;
4844
pub const AUTH_TAG_LEN: usize = 48;
4945
pub const AES_128_KEY_LEN: usize = 16;
5046
pub const AES_256_KEY_LEN: usize = 32;
5147
pub const AES_BLOCK_SIZE: usize = 16;
5248

53-
cfg_if::cfg_if! {
54-
if #[cfg(test)] {
55-
// Secure mount of tpmfs (False is generally used for development environments)
56-
57-
pub(crate) fn ima_ml_path_get() -> PathBuf {
58-
Path::new(env!("CARGO_MANIFEST_DIR"))
59-
.join("test-data")
60-
.join("ima")
61-
.join("ascii_runtime_measurements")
62-
}
63-
} else {
64-
65-
pub(crate) fn ima_ml_path_get() -> PathBuf {
66-
Path::new(IMA_ML).to_path_buf()
67-
}
68-
}
69-
}
70-
7149
#[derive(Serialize, Deserialize, Debug)]
7250
pub(crate) struct APIVersion {
7351
major: u32,

keylime-agent/src/config.rs

+47
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ pub static DEFAULT_IAK_IDEVID_NAME_ALG: &str = "sha256";
6363
pub static DEFAULT_IAK_IDEVID_TEMPLATE: &str = "H-1";
6464
pub static DEFAULT_RUN_AS: &str = "keylime:tss";
6565
pub static DEFAULT_AGENT_DATA_PATH: &str = "agent_data.json";
66+
pub static DEFAULT_IMA_ML_PATH: &str =
67+
"/sys/kernel/security/ima/ascii_runtime_measurements";
68+
pub static DEFAULT_MEASUREDBOOT_ML_PATH: &str =
69+
"/sys/kernel/security/tpm0/binary_boot_measurements";
6670
pub static DEFAULT_CONFIG: &str = "/etc/keylime/agent.conf";
6771
pub static DEFAULT_CONFIG_SYS: &str = "/usr/etc/keylime/agent.conf";
6872

@@ -108,6 +112,8 @@ pub(crate) struct EnvConfig {
108112
pub iak_idevid_template: Option<String>,
109113
pub run_as: Option<String>,
110114
pub agent_data_path: Option<String>,
115+
pub ima_ml_path: Option<String>,
116+
pub measuredboot_ml_path: Option<String>,
111117
}
112118

113119
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
@@ -152,6 +158,8 @@ pub(crate) struct AgentConfig {
152158
pub iak_idevid_template: String,
153159
pub run_as: String,
154160
pub agent_data_path: String,
161+
pub ima_ml_path: String,
162+
pub measuredboot_ml_path: String,
155163
}
156164

157165
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
@@ -326,6 +334,15 @@ impl EnvConfig {
326334
_ = agent
327335
.insert("agent_data_path".to_string(), v.to_string().into());
328336
}
337+
if let Some(ref v) = self.ima_ml_path {
338+
_ = agent.insert("ima_ml_path".to_string(), v.to_string().into());
339+
}
340+
if let Some(ref v) = self.measuredboot_ml_path {
341+
_ = agent.insert(
342+
"measuredboot_ml_path".to_string(),
343+
v.to_string().into(),
344+
);
345+
}
329346
agent
330347
}
331348

@@ -515,6 +532,14 @@ impl Source for KeylimeConfig {
515532
"agent_data_path".to_string(),
516533
self.agent.agent_data_path.to_string().into(),
517534
);
535+
_ = m.insert(
536+
"ima_ml_path".to_string(),
537+
self.agent.ima_ml_path.to_string().into(),
538+
);
539+
_ = m.insert(
540+
"measuredboot_ml_path".to_string(),
541+
self.agent.measuredboot_ml_path.to_string().into(),
542+
);
518543

519544
Ok(Map::from([("agent".to_string(), m.into())]))
520545
}
@@ -580,6 +605,8 @@ impl Default for AgentConfig {
580605
.to_string(),
581606
iak_idevid_name_alg: DEFAULT_IAK_IDEVID_NAME_ALG.to_string(),
582607
iak_idevid_template: DEFAULT_IAK_IDEVID_TEMPLATE.to_string(),
608+
ima_ml_path: "default".to_string(),
609+
measuredboot_ml_path: "default".to_string(),
583610
}
584611
}
585612
}
@@ -708,13 +735,29 @@ fn config_translate_keywords(
708735
))
709736
})?;
710737

738+
let root_path = Path::new("/");
739+
711740
let mut agent_data_path = config_get_file_path(
712741
"agent_data_path",
713742
&config.agent.agent_data_path,
714743
keylime_dir,
715744
DEFAULT_AGENT_DATA_PATH,
716745
);
717746

747+
let mut ima_ml_path = config_get_file_path(
748+
"ima_ml_path",
749+
&config.agent.ima_ml_path,
750+
root_path,
751+
DEFAULT_IMA_ML_PATH,
752+
);
753+
754+
let mut measuredboot_ml_path = config_get_file_path(
755+
"measuredboot_ml_path",
756+
&config.agent.measuredboot_ml_path,
757+
root_path,
758+
DEFAULT_MEASUREDBOOT_ML_PATH,
759+
);
760+
718761
let mut server_key = config_get_file_path(
719762
"server_key",
720763
&config.agent.server_key,
@@ -802,6 +845,8 @@ fn config_translate_keywords(
802845
trusted_client_ca,
803846
ek_handle,
804847
agent_data_path,
848+
ima_ml_path,
849+
measuredboot_ml_path,
805850
revocation_cert,
806851
..config.agent.clone()
807852
},
@@ -1072,6 +1117,8 @@ mod tests {
10721117
("IAK_IDEVID_TEMPLATE", "override_iak_idevid_template"),
10731118
("RUN_AS", "override_run_as"),
10741119
("AGENT_DATA_PATH", "override_agent_data_path"),
1120+
("IMA_ML_PATH", "override_ima_ml_path"),
1121+
("MEASUREDBOOT_ML_PATH", "override_measuredboot_ml_path"),
10751122
]);
10761123

10771124
for (c, v) in override_map.into_iter() {

keylime-agent/src/crypto.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ mod tests {
664664
let iv = b"ABCDEFGHIJKLMNOP";
665665
let plaintext = b"test string, longer than the block size";
666666
let result = encrypt_aead(&key[..], &iv[..], &plaintext[..]);
667-
assert!(matches!(result, Err(_)));
667+
assert!(result.is_err())
668668
}
669669

670670
#[test]
@@ -673,15 +673,15 @@ mod tests {
673673
let iv = b"ABCDEFGHIJKLMN";
674674
let plaintext = b"test string, longer than the block size";
675675
let result = encrypt_aead(&key[..], &iv[..], &plaintext[..]);
676-
assert!(matches!(result, Err(_)));
676+
assert!(result.is_err())
677677
}
678678

679679
#[test]
680680
fn test_decrypt_aead_invalid_key_length() {
681681
let key = b"0123456789012345012345678901234";
682682
let ciphertext = hex::decode("4142434445464748494A4B4C4D4E4F50FCE7CA78C08FB1D5E04DB3C4AA6B6ED2F09C4AD7985BD1DB9FF15F9FDA869D0C01B27FF4618737BB53C84D256455AAB53B9AC7EAF88C4B").unwrap(); //#[allow_ci]
683683
let result = decrypt_aead(&key[..], &ciphertext[..]);
684-
assert!(matches!(result, Err(_)));
684+
assert!(result.is_err())
685685
}
686686

687687
#[test]

keylime-agent/src/main.rs

+44-8
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,34 @@ async fn main() -> Result<()> {
129129

130130
pretty_env_logger::init();
131131

132-
let ima_ml_path = ima_ml_path_get();
132+
// Load config
133+
let mut config = config::KeylimeConfig::new()?;
134+
135+
// load path for IMA logfile
136+
#[cfg(test)]
137+
fn ima_ml_path_get(_: &String) -> PathBuf {
138+
Path::new(env!("CARGO_MANIFEST_DIR"))
139+
.join("test-data")
140+
.join("ima")
141+
.join("ascii_runtime_measurements")
142+
}
143+
144+
#[cfg(not(test))]
145+
fn ima_ml_path_get(s: &String) -> PathBuf {
146+
Path::new(&s).to_path_buf()
147+
}
148+
149+
let ima_ml_path = ima_ml_path_get(&config.agent.ima_ml_path);
150+
151+
// check whether anyone has overridden the default
152+
if ima_ml_path.as_os_str() != config::DEFAULT_IMA_ML_PATH {
153+
warn!(
154+
"IMA measurement list location override: {}",
155+
ima_ml_path.display()
156+
);
157+
}
158+
159+
// check IMA logfile exists & accessible
133160
let ima_ml_file = if ima_ml_path.exists() {
134161
match fs::File::open(&ima_ml_path) {
135162
Ok(file) => Some(Mutex::new(file)),
@@ -149,16 +176,27 @@ async fn main() -> Result<()> {
149176
None
150177
};
151178

152-
let mut measuredboot_ml_path = Path::new(MEASUREDBOOT_ML);
153-
154-
// Allow setting the binary bios measurements log path when testing
179+
// load path for MBA logfile
180+
let mut measuredboot_ml_path =
181+
Path::new(&config.agent.measuredboot_ml_path);
155182
let env_mb_path: String;
156183
#[cfg(feature = "testing")]
157184
if let Ok(v) = std::env::var("TPM_BINARY_MEASUREMENTS") {
158185
env_mb_path = v;
159186
measuredboot_ml_path = Path::new(&env_mb_path);
160187
}
161188

189+
// check whether anyone has overridden the default MBA logfile
190+
if measuredboot_ml_path.as_os_str()
191+
!= config::DEFAULT_MEASUREDBOOT_ML_PATH
192+
{
193+
warn!(
194+
"Measured boot measurement list location override: {}",
195+
measuredboot_ml_path.display()
196+
);
197+
}
198+
199+
// check MBA logfile exists & accessible
162200
let measuredboot_ml_file = if measuredboot_ml_path.exists() {
163201
match fs::File::open(measuredboot_ml_path) {
164202
Ok(file) => Some(Mutex::new(file)),
@@ -178,9 +216,6 @@ async fn main() -> Result<()> {
178216
None
179217
};
180218

181-
// Load config
182-
let mut config = config::KeylimeConfig::new()?;
183-
184219
// The agent cannot run when a payload script is defined, but mTLS is disabled and insecure
185220
// payloads are not explicitly enabled
186221
if !config.agent.enable_agent_mtls
@@ -1060,7 +1095,8 @@ mod testing {
10601095
};
10611096

10621097
// Allow setting the binary bios measurements log path when testing
1063-
let mut measuredboot_ml_path = Path::new(MEASUREDBOOT_ML);
1098+
let mut measuredboot_ml_path =
1099+
Path::new(&test_config.agent.measuredboot_ml_path);
10641100
let env_mb_path;
10651101
#[cfg(feature = "testing")]
10661102
if let Ok(v) = std::env::var("TPM_BINARY_MEASUREMENTS") {

0 commit comments

Comments
 (0)