CVE-2022-30330 (C, published 2022-05-07T04:15Z)
Description: value: In the KeepKey firmware before 7.3.2,Flaws in the supervisor interface can be exploited to bypass important security restrictions on firmware operations. Using these flaws, malicious firmware code can elevate privileges, permanently make the device inoperable or overwrite the trusted bootloader code to compromise the hardware wallet across reboots or storage wipes.
Severity: MEDIUM (CVSS v3 base score: 6.6)
Weakness: CWE-20 \u2014 Improper Input Validation
The product receives input or data, but it does
        not validate or incorrectly validates that the input has the
        properties that are required to process the data safely and
        correctly.
Repository: https://github.com/keepkey/keepkey-firmware
Files changed:
lib/board/supervise.c
Commit message: fix: more robust address range checks in svhandler_flash_*


Vulnerable code:
  uint32_t sector = _param_1;
  // Do not allow firmware to erase bootstrap or bootloader sectors.
  if ((sector == FLASH_BOOTSTRAP_SECTOR) ||
      (sector >= FLASH_BOOT_SECTOR_FIRST && sector <= FLASH_BOOT_SECTOR_LAST)) {
    return;
  }
  // Protect from overflow.
  if (beginAddr + length < beginAddr) return;

  // Do not allow firmware to erase bootstrap or bootloader sectors.
  if (((beginAddr >= BSTRP_FLASH_SECT_START) &&
       (beginAddr <= (BSTRP_FLASH_SECT_START + BSTRP_FLASH_SECT_LEN - 1))) ||
      (((beginAddr + length) >= BSTRP_FLASH_SECT_START) &&
       ((beginAddr + length) <=
        (BSTRP_FLASH_SECT_START + BSTRP_FLASH_SECT_LEN - 1)))) {
    return;
  }

  if (((beginAddr >= BLDR_FLASH_SECT_START) &&
       (beginAddr <= (BLDR_FLASH_SECT_START + 2 * BLDR_FLASH_SECT_LEN - 1))) ||
      (((beginAddr + length) >= BLDR_FLASH_SECT_START) &&
       ((beginAddr + length) <=
        (BLDR_FLASH_SECT_START + 2 * BLDR_FLASH_SECT_LEN - 1)))) {
    return;
  }
  // Do not allow firmware to erase bootstrap or bootloader sectors.
  if ((dst >= BSTRP_FLASH_SECT_START) &&
      (dst <= (BSTRP_FLASH_SECT_START + BSTRP_FLASH_SECT_LEN))) {
    return;
  }

  if ((dst >= BLDR_FLASH_SECT_START) &&
      (dst <= (BLDR_FLASH_SECT_START + 2 * BLDR_FLASH_SECT_LEN))) {
    return;
  }

Fixed code:
bool do_memory_ranges_overlap(size_t range1Start, size_t range1End, size_t range2Start, size_t range2End) {
  if (range1Start <= range2Start) {
    return range2Start < range1End;
  } else {
    return range1Start < range2End;
  }
}

bool allow_svhandler_flash_sector(const FlashSector* sector) {
  return sector->use == FLASH_STORAGE1 ||
         sector->use == FLASH_STORAGE2 ||
         sector->use == FLASH_STORAGE3 ||
         sector->use == FLASH_UNUSED0 ||
         sector->use == FLASH_APP;
}

bool allow_svhandler_flash_sector_num(int sector) {
  for (const FlashSector *s = flash_sector_map; s->use != FLASH_INVALID; s++) {
    if (s->sector == sector) return allow_svhandler_flash_sector(s);
  }
  return false;
}

bool allow_svhandler_flash_range(size_t start, size_t end) {
  // Protect from overflow.
  if (start > end) return false;

  // Disallow non-flash writes.
  if (start < FLASH_ORIGIN || end > FLASH_END) return false;

  // Disallow writes to any sectors which aren't allowed.
  bool startAllowed = false;
  bool endAllowed = false;
  for (const FlashSector* s = flash_sector_map; s->use != FLASH_INVALID; s++) {
    if (allow_svhandler_flash_sector(s)) {
      if (!startAllowed &&
          start + 1 > start &&
          do_memory_ranges_overlap(start, start + 1, s->start, s->start + s->len)) {
        startAllowed = true;
      }
      if (!endAllowed &&
          end - 1 < end &&
          do_memory_ranges_overlap(end - 1, end, s->start, s->start + s->len)) {
        endAllowed = true;
      }
    } else {
      if (do_memory_ranges_overlap(start, end, s->start, s->start + s->len)) return false;
    }
  }

  // Ensure writes start and end in allowed sectors. As long as flash_sector_map consists of
  // contiguous sectors, this will ensure no writes can target flash outside the map.
  if (!startAllowed || !endAllowed) return false;

  return true;
}

  uint8_t sector = _param_1;
  // Verify requested sector is allowed.
  if (!allow_svhandler_flash_sector_num(sector)) return;
  // Verify requested address range is allowed.
  if (!allow_svhandler_flash_range(beginAddr, beginAddr + length)) return;
  // Verify requested address range is allowed.
  if (!allow_svhandler_flash_range(dst, dst + sizeof(uint32_t))) return;