Merge branch 'fix/crt-bundle-unaligned-reads_v5.5' into 'release/v5.5'

fix(mbedtls): read crt bundle byte-wise to avoid misaligned flash access (v5.5)

See merge request espressif/esp-idf!51725
This commit is contained in:
Jiang Jiang Jian
2026-08-25 10:52:21 +08:00
2 changed files with 21 additions and 14 deletions
@@ -64,14 +64,19 @@ typedef const uint8_t* cert_t;
static bundle_t s_crt_bundle;
// Read a 16-bit value stored in little-endian format from the given address
/* Read little-endian values byte-wise: bundle fields are byte-packed with no
* alignment guarantee, and a misaligned load from flash can spuriously fault
* on chips with SOC_CPU_MISALIGNED_ACCESS_ON_PMP_MISMATCH_ISSUE (DIG-694).
*/
static uint16_t get16_le(const uint8_t* ptr)
{
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
return *((const uint16_t*)ptr);
#else
return (((uint16_t)ptr[1]) << 8) | ptr[0];
#endif
return (uint16_t)ptr[0] | ((uint16_t)ptr[1] << 8);
}
static uint32_t get32_le(const uint8_t* ptr)
{
return (uint32_t)ptr[0] | ((uint32_t)ptr[1] << 8)
| ((uint32_t)ptr[2] << 16) | ((uint32_t)ptr[3] << 24);
}
static uint16_t esp_crt_get_name_len(const cert_t cert)
@@ -101,7 +106,7 @@ static uint16_t esp_crt_get_len(const cert_t cert)
static uint32_t esp_crt_get_cert_offset(const bundle_t bundle, const uint32_t index)
{
return ((const uint32_t*)bundle)[index];
return get32_le(bundle + index * sizeof(uint32_t));
}
static uint32_t esp_crt_get_certcount(const bundle_t bundle)
@@ -280,16 +285,15 @@ static bool esp_crt_check_bundle(const uint8_t* const x509_bundle, const size_t
return false;
}
// Pointer to the first offset entry
const uint32_t* offsets = (const uint32_t*)x509_bundle;
if (unlikely(offsets[0] == 0 || (offsets[0] % sizeof(uint32_t)) != 0)) {
// The bundle base may be unaligned; read offsets byte-wise via esp_crt_get_cert_offset()
if (unlikely(esp_crt_get_cert_offset(x509_bundle, 0) == 0
|| (esp_crt_get_cert_offset(x509_bundle, 0) % sizeof(uint32_t)) != 0)) {
// First offset is invalid.
// The first certificate must start after N uint32_t offset values.
return false;
}
if (unlikely(offsets[0] >= bundle_size)) {
if (unlikely(esp_crt_get_cert_offset(x509_bundle, 0) >= bundle_size)) {
// First cert starts beyond end of bundle
return false;
}
@@ -303,12 +307,13 @@ static bool esp_crt_check_bundle(const uint8_t* const x509_bundle, const size_t
// Check all offsets for consistency with certificate data
for (uint32_t i = 0; i < num_certs - 1; ++i) {
const uint32_t off = offsets[i];
const uint32_t off = esp_crt_get_cert_offset(x509_bundle, i);
cert_t cert = x509_bundle + off;
// The next offset in the list must point to right after the current cert
const uint32_t expected_next_offset = off + esp_crt_get_len(cert);
if (unlikely(offsets[i + 1] != expected_next_offset || expected_next_offset >= bundle_size)) {
if (unlikely(esp_crt_get_cert_offset(x509_bundle, i + 1) != expected_next_offset
|| expected_next_offset >= bundle_size)) {
return false;
}
}
@@ -83,6 +83,8 @@ append("${data}")
make_and_append_identifier("_binary_${varname}_end" "for objcopy compatibility")
append_line("")
# Keep the length aligned on the word boundary (read as a 32-bit `<name>_length`)
append_line(".balign 4")
if(FILE_TYPE STREQUAL "TEXT")
make_and_append_identifier("${varname}_length" "not including null byte")
else()