Skip to content

Commit 7a35ae9

Browse files
committed
trustee: Combine approved image PCR values
Update trustee reference values with all possible combinations between approved PCR values, not just those that come directly from image PCRs. This computes reference values for possible stages during node updates in which a node could be booting some components from image A and some other from image B. This commit also adds a test to check that pcr4 is well covered in front of bootloader and kernel updates. The test is closer to an integration test than to an unit-test as it relies on values that are close to real values, and how the compute-pcrs lib's combine_images processes them. Signed-off-by: Beñat Gartzia Arruabarrena <bgartzia@redhat.com>
1 parent 0cde997 commit 7a35ae9

1 file changed

Lines changed: 125 additions & 2 deletions

File tree

operator/src/trustee.rs

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use anyhow::{Context, Result};
88
use base64::{Engine as _, engine::general_purpose};
99
use clevis_pin_trustee_lib::Key as ClevisKey;
10+
use compute_pcrs_lib::tpmevents::TPMEvent;
11+
use compute_pcrs_lib::tpmevents::combine::combine_images;
1012
use hex;
1113
use k8s_openapi::api::apps::v1::{Deployment, DeploymentSpec};
1214
use k8s_openapi::api::core::v1::{
@@ -66,10 +68,16 @@ pub fn get_image_pcrs(image_pcrs_map: ConfigMap) -> Result<ImagePcrs> {
6668
}
6769

6870
fn recompute_reference_values(image_pcrs: ImagePcrs) -> Vec<ReferenceValue> {
69-
// TODO many grub+shim:many OS image recompute once supported
7071
let mut reference_values_in =
7172
BTreeMap::from([("svn".to_string(), vec![JsonString("1".to_string())])]);
72-
for pcr in image_pcrs.0.values().flat_map(|v| &v.pcrs) {
73+
let tpm_events: Vec<Vec<TPMEvent>> = image_pcrs
74+
.0
75+
.values()
76+
.map(|v| v.pcrs.iter().flat_map(|p| p.events.clone()).collect())
77+
.collect();
78+
79+
let pcr_combinations = combine_images(&tpm_events);
80+
for pcr in pcr_combinations.iter().flatten() {
7381
reference_values_in
7482
.entry(format!("pcr{}", pcr.id))
7583
.or_default()
@@ -786,4 +794,119 @@ mod tests {
786794
let clos = |client| generate_kbs_deployment(client, Default::default(), "image");
787795
test_create_error(clos).await;
788796
}
797+
798+
#[test]
799+
fn test_recompute_reference_values_pcr4() {
800+
let image_pcrs = ImagePcrs(BTreeMap::from([
801+
(
802+
"cos1".to_string(),
803+
ImagePcr {
804+
first_seen: Utc::now(),
805+
pcrs: vec![Pcr {
806+
id: 4,
807+
value: hex::decode("852718920421131081032051205110114719423559841238794129122376912159784392212168").unwrap(),
808+
events: vec![
809+
TPMEvent {
810+
name: "EV_EFI_ACTION".into(),
811+
pcr: 4,
812+
hash: hex::decode("3d6772b4f84ed47595d72a2c4c5ffd15f5bb72c7507fe26f2aaee2c69d5633ba")
813+
.unwrap(),
814+
id: TPMEventID::Pcr4EfiCall,
815+
},
816+
TPMEvent {
817+
name: "EV_SEPARATOR".into(),
818+
pcr: 4,
819+
hash: hex::decode("df3f619804a92fdb4057192dc43dd748ea778adc52bc498ce80524c014b81119")
820+
.unwrap(),
821+
id: TPMEventID::Pcr4Separator,
822+
},
823+
TPMEvent {
824+
name: "EV_EFI_BOOT_SERVICES_APPLICATION".into(),
825+
pcr: 4,
826+
hash: hex::decode("94896c17d49fc8c8df0cc2836611586edab1615ce7cb58cf13fc5798de56b367")
827+
.unwrap(),
828+
id: TPMEventID::Pcr4Shim,
829+
},
830+
TPMEvent {
831+
name: "EV_EFI_BOOT_SERVICES_APPLICATION".into(),
832+
pcr: 4,
833+
hash: hex::decode("bc6844fc7b59b4f0c7da70a307fc578465411d7a2c34b0f4dc2cc154c873b644")
834+
.unwrap(),
835+
id: TPMEventID::Pcr4Grub,
836+
},
837+
TPMEvent {
838+
name: "EV_EFI_BOOT_SERVICES_APPLICATION".into(),
839+
pcr: 4,
840+
hash: hex::decode("2b1dc59bc61dbbc3db11a6f3b0708c948efd46cceb7f6c8ea2024b8d1b8c829a")
841+
.unwrap(),
842+
id: TPMEventID::Pcr4Vmlinuz,
843+
},
844+
],
845+
}],
846+
reference: "".to_string(),
847+
},
848+
),
849+
(
850+
"cos2".to_string(),
851+
ImagePcr {
852+
first_seen: Utc::now(),
853+
pcrs: vec![Pcr {
854+
id: 4,
855+
value: hex::decode("19925299236966772216371371471692276818611442625320115173412649113251558526237189").unwrap(),
856+
events: vec![
857+
TPMEvent {
858+
name: "EV_EFI_ACTION".into(),
859+
pcr: 4,
860+
hash: hex::decode("3d6772b4f84ed47595d72a2c4c5ffd15f5bb72c7507fe26f2aaee2c69d5633ba")
861+
.unwrap(),
862+
id: TPMEventID::Pcr4EfiCall,
863+
},
864+
TPMEvent {
865+
name: "EV_SEPARATOR".into(),
866+
pcr: 4,
867+
hash: hex::decode("df3f619804a92fdb4057192dc43dd748ea778adc52bc498ce80524c014b81119")
868+
.unwrap(),
869+
id: TPMEventID::Pcr4Separator,
870+
},
871+
TPMEvent {
872+
name: "EV_EFI_BOOT_SERVICES_APPLICATION".into(),
873+
pcr: 4,
874+
hash: hex::decode("1fed6fad5ca735adc80615d2a7e795e2f17f84e407b07979498c9edb1e04383f")
875+
.unwrap(),
876+
id: TPMEventID::Pcr4Shim,
877+
},
878+
TPMEvent {
879+
name: "EV_EFI_BOOT_SERVICES_APPLICATION".into(),
880+
pcr: 4,
881+
hash: hex::decode("8f3adc6b42da2defa6d5ef3202badc39a5a22ceec068f106760592163a505a0e")
882+
.unwrap(),
883+
id: TPMEventID::Pcr4Grub,
884+
},
885+
TPMEvent {
886+
name: "EV_EFI_BOOT_SERVICES_APPLICATION".into(),
887+
pcr: 4,
888+
hash: hex::decode("772c3a90820e4a76944d3715e6f700bc41e846b0049b7817f9feb3289a56d3f8")
889+
.unwrap(),
890+
id: TPMEventID::Pcr4Vmlinuz,
891+
},
892+
],
893+
}],
894+
reference: "".to_string(),
895+
},
896+
),
897+
]));
898+
899+
let result = recompute_reference_values(image_pcrs);
900+
assert_eq!(result.len(), 2);
901+
let vals = reference_values_from(&result, "tpm_pcr4");
902+
assert_eq!(
903+
vals,
904+
vec![
905+
"551bbd142a716c67cd78336593c2eb3b547b575e810ced4501d761082b5cd4a8",
906+
"c9c3add791efc98f59977c89e673a34ad0b357872e9eb2c43d14607488e5d9e2",
907+
"47b742b3a2244cc7249ff3221ec640198044aac533a95abafded7921237508c1",
908+
"c7fc63ec604348d8258993a9e344ba72041afd1473ad291a3171199b551aedbd",
909+
]
910+
);
911+
}
789912
}

0 commit comments

Comments
 (0)