Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | * |
| 5 | * Functions for loading a kernel from disk. |
| 6 | * (Firmware portion) |
| 7 | */ |
| 8 | |
| 9 | #include "vboot_kernel.h" |
| 10 | |
| 11 | #include "boot_device.h" |
| 12 | #include "cgptlib.h" |
| 13 | #include "load_kernel_fw.h" |
| 14 | #include "rollback_index.h" |
| 15 | #include "utility.h" |
Randall Spangler | 83c88cf | 2010-06-11 16:14:18 -0700 | [diff] [blame] | 16 | #include "vboot_common.h" |
| 17 | |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 18 | |
| 19 | #define KBUF_SIZE 65536 /* Bytes to read at start of kernel partition */ |
| 20 | |
Randall Spangler | 83c88cf | 2010-06-11 16:14:18 -0700 | [diff] [blame] | 21 | |
| 22 | /* Allocates and reads GPT data from the drive. The sector_bytes and |
| 23 | * drive_sectors fields should be filled on input. The primary and |
| 24 | * secondary header and entries are filled on output. |
| 25 | * |
| 26 | * Returns 0 if successful, 1 if error. */ |
| 27 | int AllocAndReadGptData(GptData* gptdata) { |
| 28 | |
| 29 | uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes; |
| 30 | |
| 31 | /* No data to be written yet */ |
| 32 | gptdata->modified = 0; |
| 33 | |
| 34 | /* Allocate all buffers */ |
| 35 | gptdata->primary_header = (uint8_t*)Malloc(gptdata->sector_bytes); |
| 36 | gptdata->secondary_header = (uint8_t*)Malloc(gptdata->sector_bytes); |
| 37 | gptdata->primary_entries = (uint8_t*)Malloc(TOTAL_ENTRIES_SIZE); |
| 38 | gptdata->secondary_entries = (uint8_t*)Malloc(TOTAL_ENTRIES_SIZE); |
| 39 | |
| 40 | if (gptdata->primary_header == NULL || gptdata->secondary_header == NULL || |
| 41 | gptdata->primary_entries == NULL || gptdata->secondary_entries == NULL) |
| 42 | return 1; |
| 43 | |
| 44 | /* Read data from the drive, skipping the protective MBR */ |
| 45 | if (0 != BootDeviceReadLBA(1, 1, gptdata->primary_header)) |
| 46 | return 1; |
| 47 | if (0 != BootDeviceReadLBA(2, entries_sectors, gptdata->primary_entries)) |
| 48 | return 1; |
| 49 | if (0 != BootDeviceReadLBA(gptdata->drive_sectors - entries_sectors - 1, |
| 50 | entries_sectors, gptdata->secondary_entries)) |
| 51 | return 1; |
| 52 | if (0 != BootDeviceReadLBA(gptdata->drive_sectors - 1, |
| 53 | 1, gptdata->secondary_header)) |
| 54 | return 1; |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | /* Writes any changes for the GPT data back to the drive, then frees |
| 61 | * the buffers. |
| 62 | * |
| 63 | * Returns 0 if successful, 1 if error. */ |
| 64 | int WriteAndFreeGptData(GptData* gptdata) { |
| 65 | |
| 66 | uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes; |
| 67 | |
| 68 | if (gptdata->primary_header) { |
| 69 | if (gptdata->modified & GPT_MODIFIED_HEADER1) { |
| 70 | if (0 != BootDeviceWriteLBA(1, 1, gptdata->primary_header)) |
| 71 | return 1; |
| 72 | } |
| 73 | Free(gptdata->primary_header); |
| 74 | } |
| 75 | |
| 76 | if (gptdata->primary_entries) { |
| 77 | if (gptdata->modified & GPT_MODIFIED_ENTRIES1) { |
| 78 | if (0 != BootDeviceWriteLBA(2, entries_sectors, |
| 79 | gptdata->primary_entries)) |
| 80 | return 1; |
| 81 | } |
| 82 | Free(gptdata->primary_entries); |
| 83 | } |
| 84 | |
| 85 | if (gptdata->secondary_entries) { |
| 86 | if (gptdata->modified & GPT_MODIFIED_ENTRIES2) { |
| 87 | if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - entries_sectors - 1, |
| 88 | entries_sectors, gptdata->secondary_entries)) |
| 89 | return 1; |
| 90 | } |
| 91 | Free(gptdata->secondary_entries); |
| 92 | } |
| 93 | |
| 94 | if (gptdata->secondary_header) { |
| 95 | if (gptdata->modified & GPT_MODIFIED_HEADER2) { |
| 96 | if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - 1, 1, |
| 97 | gptdata->secondary_header)) |
| 98 | return 1; |
| 99 | } |
| 100 | Free(gptdata->secondary_header); |
| 101 | } |
| 102 | |
| 103 | /* Success */ |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 108 | int LoadKernel2(LoadKernelParams* params) { |
| 109 | |
| 110 | VbPublicKey* kernel_subkey = (VbPublicKey*)params->header_sign_key_blob; |
| 111 | |
| 112 | GptData gpt; |
| 113 | uint64_t part_start, part_size; |
| 114 | uint64_t blba = params->bytes_per_lba; |
| 115 | uint64_t kbuf_sectors = KBUF_SIZE / blba; |
| 116 | uint8_t* kbuf = NULL; |
| 117 | int found_partitions = 0; |
| 118 | int good_partition = -1; |
| 119 | uint16_t tpm_key_version = 0; |
| 120 | uint16_t tpm_kernel_version = 0; |
| 121 | uint64_t lowest_key_version = 0xFFFF; |
| 122 | uint64_t lowest_kernel_version = 0xFFFF; |
| 123 | int is_dev = ((BOOT_FLAG_DEVELOPER & params->boot_flags) && |
| 124 | !(BOOT_FLAG_RECOVERY & params->boot_flags)); |
| 125 | int is_normal = (!(BOOT_FLAG_DEVELOPER & params->boot_flags) && |
| 126 | !(BOOT_FLAG_RECOVERY & params->boot_flags)); |
| 127 | |
| 128 | /* Clear output params in case we fail */ |
| 129 | params->partition_number = 0; |
| 130 | params->bootloader_address = 0; |
| 131 | params->bootloader_size = 0; |
| 132 | |
| 133 | if (is_normal) { |
| 134 | /* Read current kernel key index from TPM. Assumes TPM is already |
| 135 | * initialized. */ |
| 136 | if (0 != GetStoredVersions(KERNEL_VERSIONS, |
| 137 | &tpm_key_version, |
| 138 | &tpm_kernel_version)) |
| 139 | return LOAD_KERNEL_RECOVERY; |
| 140 | } else if (is_dev) { |
| 141 | /* In developer mode, we ignore the kernel subkey, and just use |
| 142 | * the SHA-512 hash to verify the key block. */ |
| 143 | kernel_subkey = NULL; |
| 144 | } |
| 145 | |
| 146 | do { |
| 147 | /* Read GPT data */ |
| 148 | gpt.sector_bytes = blba; |
| 149 | gpt.drive_sectors = params->ending_lba + 1; |
| 150 | if (0 != AllocAndReadGptData(&gpt)) |
| 151 | break; |
| 152 | |
| 153 | /* Initialize GPT library */ |
| 154 | if (GPT_SUCCESS != GptInit(&gpt)) |
| 155 | break; |
| 156 | |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 157 | /* Allocate kernel header buffers */ |
| 158 | kbuf = (uint8_t*)Malloc(KBUF_SIZE); |
| 159 | if (!kbuf) |
| 160 | break; |
| 161 | |
| 162 | /* Loop over candidate kernel partitions */ |
| 163 | while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) { |
| 164 | VbKeyBlockHeader* key_block; |
| 165 | VbKernelPreambleHeader* preamble; |
| 166 | RSAPublicKey* data_key; |
| 167 | uint64_t key_version; |
| 168 | uint64_t body_offset; |
| 169 | |
| 170 | /* Found at least one kernel partition. */ |
| 171 | found_partitions++; |
| 172 | |
| 173 | /* Read the first part of the kernel partition */ |
| 174 | if (part_size < kbuf_sectors) |
| 175 | continue; |
| 176 | if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf)) |
| 177 | continue; |
| 178 | |
| 179 | /* Verify the key block */ |
| 180 | key_block = (VbKeyBlockHeader*)kbuf; |
Randall Spangler | 729b872 | 2010-06-11 11:16:20 -0700 | [diff] [blame] | 181 | if ((0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey))) |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 182 | continue; |
| 183 | |
| 184 | /* Check the key block flags against the current boot mode */ |
| 185 | if (!(key_block->key_block_flags && |
| 186 | ((BOOT_FLAG_DEVELOPER & params->boot_flags) ? |
| 187 | KEY_BLOCK_FLAG_DEVELOPER_1 : KEY_BLOCK_FLAG_DEVELOPER_0))) |
| 188 | continue; |
| 189 | if (!(key_block->key_block_flags && |
| 190 | ((BOOT_FLAG_RECOVERY & params->boot_flags) ? |
| 191 | KEY_BLOCK_FLAG_RECOVERY_1 : KEY_BLOCK_FLAG_RECOVERY_0))) |
| 192 | continue; |
| 193 | |
| 194 | /* Check for rollback of key version. Note this is implicitly |
| 195 | * skipped in recovery and developer modes because those set |
| 196 | * key_version=0 above. */ |
| 197 | key_version = key_block->data_key.key_version; |
| 198 | if (key_version < tpm_key_version) |
| 199 | continue; |
| 200 | |
| 201 | /* Get the key for preamble/data verification from the key block */ |
| 202 | data_key = PublicKeyToRSA(&key_block->data_key); |
| 203 | if (!data_key) |
| 204 | continue; |
| 205 | |
| 206 | /* Verify the preamble, which follows the key block */ |
| 207 | preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size); |
| 208 | if ((0 != VerifyKernelPreamble2(preamble, |
| 209 | KBUF_SIZE - key_block->key_block_size, |
| 210 | data_key))) { |
| 211 | RSAPublicKeyFree(data_key); |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | /* Check for rollback of kernel version. Note this is implicitly |
| 216 | * skipped in recovery and developer modes because those set |
| 217 | * key_version=0 and kernel_version=0 above. */ |
| 218 | if (key_version == tpm_key_version && |
| 219 | preamble->kernel_version < tpm_kernel_version) { |
| 220 | RSAPublicKeyFree(data_key); |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | /* Check for lowest key version from a valid header. */ |
| 225 | if (lowest_key_version > key_version) { |
| 226 | lowest_key_version = key_version; |
| 227 | lowest_kernel_version = preamble->kernel_version; |
| 228 | } |
| 229 | else if (lowest_key_version == key_version && |
| 230 | lowest_kernel_version > preamble->kernel_version) { |
| 231 | lowest_kernel_version = preamble->kernel_version; |
| 232 | } |
| 233 | |
| 234 | /* If we already have a good kernel, no need to read another |
| 235 | * one; we only needed to look at the versions to check for |
| 236 | * rollback. */ |
| 237 | if (-1 != good_partition) |
| 238 | continue; |
| 239 | |
| 240 | /* Verify body load address matches what we expect */ |
| 241 | if (preamble->body_load_address != (size_t)params->kernel_buffer) { |
| 242 | RSAPublicKeyFree(data_key); |
| 243 | continue; |
| 244 | } |
| 245 | |
| 246 | /* Verify kernel body starts at a multiple of the sector size. */ |
| 247 | body_offset = key_block->key_block_size + preamble->preamble_size; |
| 248 | if (0 != body_offset % blba) { |
| 249 | RSAPublicKeyFree(data_key); |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | /* Verify kernel body fits in the partition */ |
| 254 | if (body_offset + preamble->body_signature.data_size > |
| 255 | part_size * blba) { |
| 256 | RSAPublicKeyFree(data_key); |
| 257 | continue; |
| 258 | } |
| 259 | |
| 260 | /* Read the kernel data */ |
| 261 | if (0 != BootDeviceReadLBA( |
| 262 | part_start + (body_offset / blba), |
| 263 | (preamble->body_signature.data_size + blba - 1) / blba, |
| 264 | params->kernel_buffer)) { |
| 265 | RSAPublicKeyFree(data_key); |
| 266 | continue; |
| 267 | } |
| 268 | |
| 269 | /* Verify kernel data */ |
| 270 | if (0 != VerifyData((const uint8_t*)params->kernel_buffer, |
| 271 | &preamble->body_signature, data_key)) { |
| 272 | RSAPublicKeyFree(data_key); |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | /* Done with the kernel signing key, so can free it now */ |
| 277 | RSAPublicKeyFree(data_key); |
| 278 | |
| 279 | /* If we're still here, the kernel is valid. */ |
| 280 | /* Save the first good partition we find; that's the one we'll boot */ |
| 281 | if (-1 == good_partition) { |
| 282 | good_partition = gpt.current_kernel; |
| 283 | params->partition_number = gpt.current_kernel; |
| 284 | params->bootloader_address = preamble->bootloader_address; |
| 285 | params->bootloader_size = preamble->bootloader_size; |
| 286 | /* If we're in developer or recovery mode, there's no rollback |
| 287 | * protection, so we can stop at the first valid kernel. */ |
| 288 | if (!is_normal) |
| 289 | break; |
| 290 | |
| 291 | /* Otherwise, we're in normal boot mode, so we do care about |
| 292 | * the key index in the TPM. If the good partition's key |
| 293 | * version is the same as the tpm, then the TPM doesn't need |
| 294 | * updating; we can stop now. Otherwise, we'll check all the |
| 295 | * other headers to see if they contain a newer key. */ |
| 296 | if (key_version == tpm_key_version && |
| 297 | preamble->kernel_version == tpm_kernel_version) |
| 298 | break; |
| 299 | } |
| 300 | } /* while(GptNextKernelEntry) */ |
| 301 | } while(0); |
| 302 | |
| 303 | /* Free kernel buffer */ |
| 304 | if (kbuf) |
| 305 | Free(kbuf); |
| 306 | |
| 307 | /* Write and free GPT data */ |
| 308 | WriteAndFreeGptData(&gpt); |
| 309 | |
| 310 | /* Handle finding a good partition */ |
| 311 | if (good_partition >= 0) { |
| 312 | |
| 313 | /* See if we need to update the TPM */ |
| 314 | if (is_normal) { |
| 315 | /* We only update the TPM in normal boot mode. In developer |
| 316 | * mode, the kernel is self-signed by the developer, so we can't |
| 317 | * trust the key version and wouldn't want to roll the TPM |
| 318 | * forward. In recovery mode, the TPM stays PP-unlocked, so |
| 319 | * anything we write gets blown away by the firmware when we go |
| 320 | * back to normal mode. */ |
| 321 | if ((lowest_key_version > tpm_key_version) || |
| 322 | (lowest_key_version == tpm_key_version && |
| 323 | lowest_kernel_version > tpm_kernel_version)) { |
| 324 | if (0 != WriteStoredVersions(KERNEL_VERSIONS, |
| 325 | lowest_key_version, |
| 326 | lowest_kernel_version)) |
| 327 | return LOAD_KERNEL_RECOVERY; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | if (!(BOOT_FLAG_RECOVERY & params->boot_flags)) { |
| 332 | /* We can lock the TPM now, since we've decided which kernel we |
| 333 | * like. If we don't find a good kernel, we leave the TPM |
| 334 | * unlocked so we can try again on the next boot device. If no |
| 335 | * kernels are good, we'll reboot to recovery mode, so it's ok to |
| 336 | * leave the TPM unlocked in that case too. |
| 337 | * |
| 338 | * If we're already in recovery mode, we need to leave PP unlocked, |
| 339 | * so don't lock the kernel versions. */ |
| 340 | if (0 != LockKernelVersionsByLockingPP()) |
| 341 | return LOAD_KERNEL_RECOVERY; |
| 342 | } |
| 343 | |
| 344 | /* Success! */ |
| 345 | return LOAD_KERNEL_SUCCESS; |
| 346 | } |
| 347 | |
| 348 | // Handle error cases |
| 349 | if (found_partitions) |
| 350 | return LOAD_KERNEL_INVALID; |
| 351 | else |
| 352 | return LOAD_KERNEL_NOT_FOUND; |
| 353 | } |