Skip to content

Commit f1cc568

Browse files
committed
TMP store hex string of cpuid
1 parent df38f54 commit f1cc568

File tree

7 files changed

+75
-62
lines changed

7 files changed

+75
-62
lines changed

include/ccf/pal/attestation_sev_snp.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,10 @@ QPHfbkH0CyPfhl1jWhJFZasCAwEAAQ==
282282
bool operator==(const CPUID&) const = default;
283283
std::string hex_str() const
284284
{
285-
uint8_t buf[sizeof(CPUID)];
286-
memcpy(buf, this, sizeof(CPUID));
287-
std::reverse(buf, buf + sizeof(CPUID)); // fix little endianness of AMD
288-
return ccf::ds::to_hex(buf, buf + sizeof(CPUID));
285+
CPUID buf = *this;
286+
auto buf_ptr = reinterpret_cast<uint8_t*>(&buf);
287+
std::reverse(buf_ptr, buf_ptr + sizeof(CPUID));
288+
return ccf::ds::to_hex(buf_ptr, buf_ptr + sizeof(CPUID));
289289
}
290290
inline uint8_t get_family_id() const
291291
{
@@ -312,18 +312,20 @@ QPHfbkH0CyPfhl1jWhJFZasCAwEAAQ==
312312
return ret;
313313
}
314314

315-
union UnionedCPUID
316-
{
317-
uint32_t eax;
318-
CPUID cpuid;
319-
};
320-
321315
static CPUID get_cpuid()
322316
{
323-
UnionedCPUID cpuid_eax;
324-
cpuid_eax.eax = 0;
325-
asm volatile("cpuid" : "=a"(cpuid_eax.eax) : "a"(1));
326-
return cpuid_eax.cpuid;
317+
uint32_t ieax = 1;
318+
uint64_t iebx = 0;
319+
uint64_t iecx = 0;
320+
uint64_t iedx = 0;
321+
uint32_t oeax = 0;
322+
uint64_t oebx = 0;
323+
uint64_t oecx = 0;
324+
uint64_t oedx = 0;
325+
// pass in e{b,c,d}x to prevent cpuid from blatting other registers
326+
asm volatile("cpuid" : "=a"(oeax), "=b"(oebx), "=c"(oecx), "=d"(oedx): "a"(ieax), "b"(iebx), "c"(iecx), "d"(iedx));
327+
auto cpuid = *reinterpret_cast<CPUID*>(&oeax);
328+
return cpuid;
327329
}
328330
}
329331

include/ccf/service/tables/tcb_verification.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace ccf
1010
{
11-
using SnpTcbVersionMap = ServiceMap<pal::snp::CPUID, pal::snp::TcbVersion>;
11+
using SnpTcbVersionMap = ServiceMap<std::string, pal::snp::TcbVersion>;
1212

1313
namespace Tables
1414
{

samples/constitutions/default/actions.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,14 +1117,13 @@ const actions = new Map([
11171117
"tcb_version.microcode",
11181118
);
11191119
},
1120-
function (args) {
1120+
function (args, proposalId) {
11211121
ccf.kv["public:ccf.gov.nodes.snp.tcb_versions"].set(
1122-
hexStrToBuf(args.cpuid),
1122+
ccf.strToBuf(args.cpuid),
11231123
ccf.jsonToSnpTcbVersion(args.tcb_version),
11241124
);
11251125

1126-
// Is this required?
1127-
//invalidateOtherOpenProposals(proposalId);
1126+
invalidateOtherOpenProposals(proposalId);
11281127
},
11291128
),
11301129
],

src/node/gov/handlers/service_state.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,11 +619,12 @@ namespace ccf::gov::endpoints
619619
auto tcb_versions_handle =
620620
ctx.tx.template ro<ccf::SnpTcbVersionMap>(
621621
ccf::Tables::SNP_TCB_VERSIONS);
622+
622623
tcb_versions_handle->foreach(
623624
[&snp_tcb_versions](
624-
const pal::snp::CPUID& cpuid,
625+
const std::string& cpuid,
625626
const pal::snp::TcbVersion& tcb_version) {
626-
snp_tcb_versions[cpuid.hex_str()] = tcb_version;
627+
snp_tcb_versions[cpuid] = tcb_version;
627628
return true;
628629
});
629630
snp_policy["tcbVersions"] = snp_tcb_versions;

src/node/quote.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ namespace ccf
280280
auto h = tx.ro<SnpTcbVersionMap>(Tables::SNP_TCB_VERSIONS);
281281
// expensive but there should not be many entries
282282
h->foreach([&min_tcb_opt, &attestation](
283-
const pal::snp::CPUID& cpuid, const pal::snp::TcbVersion& v) {
283+
const std::string cpuid_hex, const pal::snp::TcbVersion& v) {
284+
auto cpuid = pal::snp::cpuid_from_hex(cpuid_hex);
284285
if (
285286
cpuid.get_family_id() == attestation.cpuid_fam_id &&
286287
cpuid.get_model_id() == attestation.cpuid_mod_id &&

src/service/internal_tables_access.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ namespace ccf
841841
.reserved = {0},
842842
.snp = 0x18,
843843
.microcode = 0xDB};
844-
h->put(milan_chip_id, milan_tcb_version);
844+
h->put(milan_chip_id.hex_str(), milan_tcb_version);
845845

846846
constexpr pal::snp::CPUID milan_x_chip_id{
847847
.stepping = 0x2,
@@ -857,7 +857,7 @@ namespace ccf
857857
.reserved = {0},
858858
.snp = 0x18,
859859
.microcode = 0x44};
860-
h->put(milan_x_chip_id, milan_x_tcb_version);
860+
h->put(milan_x_chip_id.hex_str(), milan_x_tcb_version);
861861

862862
constexpr pal::snp::CPUID genoa_chip_id{
863863
.stepping = 0x1,
@@ -873,7 +873,7 @@ namespace ccf
873873
.reserved = {0},
874874
.snp = 0x17,
875875
.microcode = 0x54};
876-
h->put(genoa_chip_id, genoa_tcb_version);
876+
h->put(genoa_chip_id.hex_str(), genoa_tcb_version);
877877

878878
constexpr pal::snp::CPUID genoa_x_chip_id{
879879
.stepping = 0x2,
@@ -889,7 +889,7 @@ namespace ccf
889889
.reserved = {0},
890890
.snp = 0x17,
891891
.microcode = 0x4F};
892-
h->put(genoa_x_chip_id, genoa_x_tcb_version);
892+
h->put(genoa_x_chip_id.hex_str(), genoa_x_tcb_version);
893893
}
894894

895895
static void trust_node_snp_tcb_version(
@@ -919,7 +919,7 @@ namespace ccf
919919
return;
920920
}
921921
auto h = tx.wo<ccf::SnpTcbVersionMap>(Tables::SNP_TCB_VERSIONS);
922-
h->put(cpuid, attestation.reported_tcb);
922+
h->put(cpuid.hex_str(), attestation.reported_tcb);
923923
}
924924

925925
static void init_configuration(

tests/code_update.py

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,16 @@ def test_tcb_version_tables(network, args):
286286
assert len(versions) == 1, f"Expected one TCB version, {versions}"
287287
cpuid, tcb_version = next(iter(versions.items()))
288288

289+
LOG.info("Change current cpuid's TCB version")
290+
test_tcb_version = {"boot_loader": 0, "microcode": 0, "snp": 0, "tee": 0}
291+
network.consortium.add_snp_tcb_version(primary, cpuid, test_tcb_version)
292+
with primary.api_versioned_client(api_version=args.gov_api_version) as client:
293+
r = client.get("/gov/service/join-policy")
294+
assert r.status_code == http.HTTPStatus.OK, r
295+
versions = r.body.json()["snp"]["tcbVersions"]
296+
assert cpuid in versions, f"Expected {cpuid} in TCB versions, {versions}"
297+
assert versions[cpuid] == test_tcb_version, f"TCB version does not match, {versions}"
298+
289299
LOG.info("Removing current cpuid's TCB version")
290300
network.consortium.remove_snp_tcb_version(primary, cpuid)
291301
with primary.api_versioned_client(api_version=args.gov_api_version) as client:
@@ -768,43 +778,43 @@ def run(args):
768778
) as network:
769779
network.start_and_open(args)
770780

771-
test_verify_quotes(network, args)
772-
773-
# Measurements
774-
test_measurements_tables(network, args)
775-
if not snp.IS_SNP:
776-
test_add_node_with_untrusted_measurement(network, args)
777-
778-
# Host data/security policy
779-
test_host_data_tables(network, args)
780-
test_add_node_with_untrusted_host_data(network, args)
781-
781+
# test_verify_quotes(network, args)
782+
#
783+
# # Measurements
784+
# test_measurements_tables(network, args)
785+
# if not snp.IS_SNP:
786+
# test_add_node_with_untrusted_measurement(network, args)
787+
#
788+
# # Host data/security policy
789+
# test_host_data_tables(network, args)
790+
# test_add_node_with_untrusted_host_data(network, args)
791+
#
782792
if snp.IS_SNP:
783-
# Virtual has no security policy, _only_ host data (unassociated with anything)
784-
test_add_node_with_stubbed_security_policy(network, args)
785-
test_start_node_with_mismatched_host_data(network, args)
786-
test_add_node_without_security_policy(network, args)
793+
# # Virtual has no security policy, _only_ host data (unassociated with anything)
794+
# test_add_node_with_stubbed_security_policy(network, args)
795+
# test_start_node_with_mismatched_host_data(network, args)
796+
# test_add_node_without_security_policy(network, args)
787797
test_tcb_version_tables(network, args)
788-
789-
# Endorsements
790-
test_endorsements_tables(network, args)
791-
test_add_node_with_no_uvm_endorsements(network, args)
792-
793-
if not snp.IS_SNP:
794-
# NB: Assumes the current nodes are still using args.package, so must run before test_update_all_nodes
795-
test_proposal_invalidation(network, args)
796-
797-
# This is in practice equivalent to either "unknown measurement" or "unknown host data", but is explicitly
798-
# testing that (without artifically removing/corrupting those values) a replacement package differs
799-
# in one of these values
800-
test_add_node_with_different_package(network, args)
801-
test_update_all_nodes(network, args)
802-
803-
# Run again at the end to confirm current nodes are acceptable
804-
test_verify_quotes(network, args)
805-
806-
if snp.IS_SNP:
807-
test_add_node_with_no_uvm_endorsements_in_kv(network, args)
798+
#
799+
# # Endorsements
800+
# test_endorsements_tables(network, args)
801+
# test_add_node_with_no_uvm_endorsements(network, args)
802+
#
803+
# if not snp.IS_SNP:
804+
# # NB: Assumes the current nodes are still using args.package, so must run before test_update_all_nodes
805+
# test_proposal_invalidation(network, args)
806+
#
807+
# # This is in practice equivalent to either "unknown measurement" or "unknown host data", but is explicitly
808+
# # testing that (without artifically removing/corrupting those values) a replacement package differs
809+
# # in one of these values
810+
# test_add_node_with_different_package(network, args)
811+
# test_update_all_nodes(network, args)
812+
#
813+
# # Run again at the end to confirm current nodes are acceptable
814+
# test_verify_quotes(network, args)
815+
#
816+
# if snp.IS_SNP:
817+
# test_add_node_with_no_uvm_endorsements_in_kv(network, args)
808818

809819

810820
if __name__ == "__main__":

0 commit comments

Comments
 (0)