Skip to content

Commit b7e3813

Browse files
committed
Store cpuid in kv. Check attestation against it.
1 parent 8d6f848 commit b7e3813

File tree

10 files changed

+67
-123
lines changed

10 files changed

+67
-123
lines changed

CMakeLists.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -654,13 +654,6 @@ if(BUILD_TESTS)
654654
snp_ioctl_test
655655
${CMAKE_CURRENT_SOURCE_DIR}/src/pal/test/snp_ioctl_test.cpp
656656
)
657-
658-
add_unit_test(
659-
snp_attestation_verification
660-
${CMAKE_CURRENT_SOURCE_DIR}/src/node/test/snp_attestation_verification.cpp
661-
${CCF_DIR}/src/node/quote.cpp
662-
)
663-
target_link_libraries(snp_attestation_verification PRIVATE ccf_kv.host)
664657
endif()
665658

666659
add_unit_test(map_test ${CMAKE_CURRENT_SOURCE_DIR}/src/ds/test/map_test.cpp)

include/ccf/pal/attestation_sev_snp.h

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "ccf/pal/attestation_sev_snp_endorsements.h"
77
#include "ccf/pal/measurement.h"
88
#include "ccf/pal/report_data.h"
9+
#include "ccf/pal/hardware_info.h"
910

1011
#include <array>
1112
#include <map>
@@ -278,33 +279,33 @@ QPHfbkH0CyPfhl1jWhJFZasCAwEAAQ==
278279
uint8_t extended_model : 4;
279280
uint8_t extended_family : 8;
280281
uint8_t reserved2 : 4;
281-
};
282-
static_assert(
283-
sizeof(CPUID) == sizeof(uint32_t), "Can't cast CPUID to uint32_t");
284282

285-
struct AttestChipModel
286-
{
287-
uint8_t family;
288-
uint8_t model;
289-
uint8_t stepping;
290-
291-
bool operator==(const AttestChipModel&) const = default;
283+
bool operator==(const CPUID&) const = default;
292284
std::string hex_str() const
293285
{
294286
auto begin = reinterpret_cast<const uint8_t*>(this);
295-
return ccf::ds::to_hex(begin, begin + sizeof(AttestChipModel));
287+
return ccf::ds::to_hex(begin, begin + sizeof(CPUID));
288+
}
289+
inline uint8_t get_family_id() const
290+
{
291+
return this->base_family + this->extended_family;
292+
}
293+
inline uint8_t get_model_id() const
294+
{
295+
return (this->extended_model << 4) | this->base_model;
296296
}
297297
};
298+
static_assert(
299+
sizeof(CPUID) == sizeof(uint32_t), "Can't cast CPUID to uint32_t");
298300
#pragma pack(pop)
299-
DECLARE_JSON_TYPE(AttestChipModel);
300-
DECLARE_JSON_REQUIRED_FIELDS(AttestChipModel, family, model, stepping);
301-
constexpr AttestChipModel get_attest_chip_model(const CPUID& cpuid)
301+
DECLARE_JSON_TYPE(CPUID);
302+
DECLARE_JSON_REQUIRED_FIELDS(CPUID, stepping, base_model, base_family, extended_model, extended_family);
303+
304+
static CPUID get_cpuid()
302305
{
303-
AttestChipModel model;
304-
model.family = cpuid.base_family + cpuid.extended_family;
305-
model.model = (cpuid.extended_model << 4) | cpuid.base_model;
306-
model.stepping = cpuid.stepping;
307-
return model;
306+
CpuidInfo cpuid_info{};
307+
cpuid(&cpuid_info, 1, 0);
308+
return *reinterpret_cast<CPUID*>(&cpuid_info.eax); // TODO validate
308309
}
309310
}
310311

@@ -313,24 +314,24 @@ namespace ccf::kv::serialisers
313314
// Use hex string to ensure uniformity between the endpoint perspective and
314315
// the kv's key
315316
template <>
316-
struct BlitSerialiser<ccf::pal::snp::AttestChipModel>
317+
struct BlitSerialiser<ccf::pal::snp::CPUID>
317318
{
318319
static SerialisedEntry to_serialised(
319-
const ccf::pal::snp::AttestChipModel& chip)
320+
const ccf::pal::snp::CPUID& chip)
320321
{
321322
auto hex_str = chip.hex_str();
322323
return SerialisedEntry(hex_str.begin(), hex_str.end());
323324
}
324325

325-
static ccf::pal::snp::AttestChipModel from_serialised(
326+
static ccf::pal::snp::CPUID from_serialised(
326327
const SerialisedEntry& data)
327328
{
328-
ccf::pal::snp::AttestChipModel ret;
329+
ccf::pal::snp::CPUID ret;
329330
auto buf_ptr = reinterpret_cast<uint8_t*>(&ret);
330331
ccf::ds::from_hex(
331332
std::string(data.data(), data.end()),
332333
buf_ptr,
333-
buf_ptr + sizeof(ccf::pal::snp::AttestChipModel));
334+
buf_ptr + sizeof(ccf::pal::snp::CPUID));
334335
return ret;
335336
}
336337
};

include/ccf/service/tables/tcb_verification.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace ccf
1010
{
1111
using SnpTcbVersionMap =
12-
ServiceMap<pal::snp::AttestChipModel, pal::snp::TcbVersion>;
12+
ServiceMap<pal::snp::CPUID, pal::snp::TcbVersion>;
1313

1414
namespace Tables
1515
{

src/node/gov/handlers/service_state.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,19 @@ namespace ccf::gov::endpoints
615615
});
616616
snp_policy["uvmEndorsements"] = snp_endorsements;
617617

618+
auto snp_tcb_versions = nlohmann::json::object();
619+
auto tcb_versions_handle =
620+
ctx.tx.template ro<ccf::SnpTcbVersionMap>(
621+
ccf::Tables::SNP_TCB_VERSIONS);
622+
tcb_versions_handle->foreach(
623+
[&snp_tcb_versions](
624+
const pal::snp::CPUID& cpuid,
625+
const pal::snp::TcbVersion& tcb_version) {
626+
snp_tcb_versions[cpuid.hex_str()] = tcb_version;
627+
return true;
628+
});
629+
snp_policy["tcbVersions"] = snp_tcb_versions;
630+
618631
response_body["snp"] = snp_policy;
619632
}
620633

src/node/quote.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,20 @@ namespace ccf
275275
return QuoteVerificationResult::Verified;
276276
}
277277

278-
pal::snp::AttestChipModel cpuid = {
279-
.family = attestation.cpuid_fam_id,
280-
.model = attestation.cpuid_mod_id,
281-
.stepping = attestation.cpuid_step};
278+
std::optional<pal::snp::TcbVersion> min_tcb_opt = std::nullopt;
279+
280+
auto h = tx.ro<SnpTcbVersionMap>(Tables::SNP_TCB_VERSIONS);
281+
// expensive but there should not be many entries in this table only one per cpu
282+
h->foreach([&min_tcb_opt, &attestation](const pal::snp::CPUID& cpuid, const pal::snp::TcbVersion& v) {
283+
if (cpuid.get_family_id() == attestation.cpuid_fam_id &&
284+
cpuid.get_model_id() == attestation.cpuid_mod_id &&
285+
cpuid.stepping == attestation.cpuid_step) {
286+
min_tcb_opt = v;
287+
return false;
288+
}
289+
return true;
290+
});
282291

283-
auto min_tcb_opt =
284-
tx.ro<SnpTcbVersionMap>(Tables::SNP_TCB_VERSIONS)->get(cpuid);
285292
if (!min_tcb_opt.has_value())
286293
{
287294
return QuoteVerificationResult::FailedInvalidCPUID;

src/node/rpc/member_frontend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ namespace ccf
510510
if constexpr (
511511
std::is_same_v<typename T::Key, ccf::crypto::Sha256Hash> ||
512512
pal::is_attestation_measurement<typename T::Key>::value ||
513-
std::is_same_v<typename T::Key, ccf::pal::snp::AttestChipModel>)
513+
std::is_same_v<typename T::Key, ccf::pal::snp::CPUID>)
514514
{
515515
response_body[k.hex_str()] = v;
516516
}

src/node/rpc/node_frontend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,9 @@ namespace ccf
16381638
LOG_FAIL_FMT("Unable to extract host data from virtual quote");
16391639
}
16401640
break;
1641+
1642+
InternalTablesAccess::trust_static_snp_tcb_version(ctx.tx);
1643+
16411644
}
16421645

16431646
case QuoteFormat::amd_sev_snp_v1:

src/node/test/snp_attestation_verification.cpp

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/pal/quote_generation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "ccf/crypto/hash_provider.h"
66
#include "ds/files.h"
7+
#include "ccf/pal/attestation.h"
78

89
#include <nlohmann/json.hpp>
910
#include <string>

src/service/internal_tables_access.h

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -827,14 +827,13 @@ namespace ccf
827827
{
828828
auto h = tx.wo<ccf::SnpTcbVersionMap>(Tables::SNP_TCB_VERSIONS);
829829

830-
constexpr auto milan_chip_id = pal::snp::get_attest_chip_model(
831-
{.stepping = 0x1,
830+
constexpr pal::snp::CPUID milan_chip_id {.stepping = 0x1,
832831
.base_model = 0x1,
833832
.base_family = 0xF,
834833
.reserved = 0,
835834
.extended_model = 0x0,
836835
.extended_family = 0x0A,
837-
.reserved2 = 0});
836+
.reserved2 = 0};
838837
constexpr pal::snp::TcbVersion milan_tcb_version = {
839838
.boot_loader = 0,
840839
.tee = 0,
@@ -843,14 +842,13 @@ namespace ccf
843842
.microcode = 0xDB};
844843
h->put(milan_chip_id, milan_tcb_version);
845844

846-
constexpr auto milan_x_chip_id = pal::snp::get_attest_chip_model(
847-
{.stepping = 0x2,
845+
constexpr pal::snp::CPUID milan_x_chip_id {.stepping = 0x2,
848846
.base_model = 0x1,
849847
.base_family = 0xF,
850848
.reserved = 0,
851849
.extended_model = 0x0,
852850
.extended_family = 0x0A,
853-
.reserved2 = 0});
851+
.reserved2 = 0};
854852
constexpr pal::snp::TcbVersion milan_x_tcb_version = {
855853
.boot_loader = 0,
856854
.tee = 0,
@@ -859,14 +857,13 @@ namespace ccf
859857
.microcode = 0x44};
860858
h->put(milan_x_chip_id, milan_x_tcb_version);
861859

862-
constexpr auto genoa_chip_id = pal::snp::get_attest_chip_model(
863-
{.stepping = 0x1,
860+
constexpr pal::snp::CPUID genoa_chip_id {.stepping = 0x1,
864861
.base_model = 0x1,
865862
.base_family = 0xF,
866863
.reserved = 0,
867864
.extended_model = 0x1,
868865
.extended_family = 0x0A,
869-
.reserved2 = 0});
866+
.reserved2 = 0};
870867
constexpr pal::snp::TcbVersion genoa_tcb_version = {
871868
.boot_loader = 0,
872869
.tee = 0,
@@ -875,14 +872,13 @@ namespace ccf
875872
.microcode = 0x54};
876873
h->put(genoa_chip_id, genoa_tcb_version);
877874

878-
constexpr auto genoa_x_chip_id = pal::snp::get_attest_chip_model(
879-
{.stepping = 0x2,
875+
constexpr pal::snp::CPUID genoa_x_chip_id {.stepping = 0x2,
880876
.base_model = 0x1,
881877
.base_family = 0xF,
882878
.reserved = 0,
883879
.extended_model = 0x1,
884880
.extended_family = 0x0A,
885-
.reserved2 = 0});
881+
.reserved2 = 0};
886882
constexpr pal::snp::TcbVersion genoa_x_tcb_version = {
887883
.boot_loader = 0,
888884
.tee = 0,
@@ -897,13 +893,9 @@ namespace ccf
897893
{
898894
if (attestation.version >= pal::snp::MIN_TCB_VERIF_VERSION)
899895
{
900-
pal::snp::AttestChipModel chip_id{
901-
.family = attestation.cpuid_fam_id,
902-
.model = attestation.cpuid_mod_id,
903-
.stepping = attestation.cpuid_step,
904-
};
896+
auto cpuid = pal::snp::get_cpuid();
905897
auto h = tx.wo<ccf::SnpTcbVersionMap>(Tables::SNP_TCB_VERSIONS);
906-
h->put(chip_id, attestation.reported_tcb);
898+
h->put(cpuid, attestation.reported_tcb);
907899
}
908900
}
909901

0 commit comments

Comments
 (0)