History

security: keys: trusted: Make sealed key properly interoperable

The current implementation appends a migratable flag to the end of a key, meaning the format isn't exactly interoperable because the using party needs to know to strip this extra byte. However, all other consumers of TPM sealed blobs expect the unseal to return exactly the key. Since TPM2 keys have a key property flag that corresponds to migratable, use that flag instead and make the actual key the only sealed quantity. This is secure because the key properties are bound to a hash in the private part, so if they're altered the key won't load. Backwards compatibility is implemented by detecting whether we're loading a new format key or not and correctly setting migratable from the last byte of old format keys.

James Bottomley committed 5 years ago

Commit e5fb5d21 parent f2219742 files · +40 −15Browse files

Showing the changes to security (1 of 2 files). Show all

Unified · Split

security/keys/trusted-keys/trusted_tpm2.c+38−15
@@ -231,6 +231,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip, · 230 unmodified lines
231 int blob_len = 0;231 int blob_len = 0;
232 struct tpm_buf buf;232 struct tpm_buf buf;
233 u32 hash;233 u32 hash;
234 u32 flags;
234 int i;235 int i;
235 int rc;236 int rc;
236 237
@@ -265,31 +266,32 @@ int tpm2_seal_trusted(struct tpm_chip *chip, · 28 unmodified lines
265 TPM_DIGEST_SIZE);266 TPM_DIGEST_SIZE);
266 267
267 /* sensitive */268 /* sensitive */
268 tpm_buf_append_u16(&buf, 4 + options->blobauth_len + payload->key_len + 1);269 tpm_buf_append_u16(&buf, 4 + options->blobauth_len + payload->key_len);
269 270
270 tpm_buf_append_u16(&buf, options->blobauth_len);271 tpm_buf_append_u16(&buf, options->blobauth_len);
271 if (options->blobauth_len)272 if (options->blobauth_len)
272 tpm_buf_append(&buf, options->blobauth, options->blobauth_len);273 tpm_buf_append(&buf, options->blobauth, options->blobauth_len);
273 274
274 tpm_buf_append_u16(&buf, payload->key_len + 1);275 tpm_buf_append_u16(&buf, payload->key_len);
275 tpm_buf_append(&buf, payload->key, payload->key_len);276 tpm_buf_append(&buf, payload->key, payload->key_len);
276 tpm_buf_append_u8(&buf, payload->migratable);
277 277
278 /* public */278 /* public */
279 tpm_buf_append_u16(&buf, 14 + options->policydigest_len);279 tpm_buf_append_u16(&buf, 14 + options->policydigest_len);
280 tpm_buf_append_u16(&buf, TPM_ALG_KEYEDHASH);280 tpm_buf_append_u16(&buf, TPM_ALG_KEYEDHASH);
281 tpm_buf_append_u16(&buf, hash);281 tpm_buf_append_u16(&buf, hash);
282 282
283 /* key properties */
284 flags = 0;
285 flags |= options->policydigest_len ? 0 : TPM2_OA_USER_WITH_AUTH;
286 flags |= payload->migratable ? (TPM2_OA_FIXED_TPM |
287 TPM2_OA_FIXED_PARENT) : 0;
288 tpm_buf_append_u32(&buf, flags);
289
283 /* policy */290 /* policy */
284 if (options->policydigest_len) {291 tpm_buf_append_u16(&buf, options->policydigest_len);
285 tpm_buf_append_u32(&buf, 0);292 if (options->policydigest_len)
286 tpm_buf_append_u16(&buf, options->policydigest_len);
287 tpm_buf_append(&buf, options->policydigest,293 tpm_buf_append(&buf, options->policydigest,
288 options->policydigest_len);294 options->policydigest_len);
289 } else {
290 tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH);
291 tpm_buf_append_u16(&buf, 0);
292 }
293 295
294 /* public parameters */296 /* public parameters */
295 tpm_buf_append_u16(&buf, TPM_ALG_NULL);297 tpm_buf_append_u16(&buf, TPM_ALG_NULL);
@@ -364,8 +366,9 @@ static int tpm2_load_cmd(struct tpm_chip *chip, · 68 unmodified lines
364 unsigned int private_len;366 unsigned int private_len;
365 unsigned int public_len;367 unsigned int public_len;
366 unsigned int blob_len;368 unsigned int blob_len;
367 u8 *blob;369 u8 *blob, *pub;
368 int rc;370 int rc;
371 u32 attrs;
369 372
370 rc = tpm2_key_decode(payload, options, &blob);373 rc = tpm2_key_decode(payload, options, &blob);
371 if (rc) {374 if (rc) {
@@ -392,6 +395,16 @@ static int tpm2_load_cmd(struct tpm_chip *chip, · 20 unmodified lines
392 if (private_len + 2 + public_len + 2 > payload->blob_len)395 if (private_len + 2 + public_len + 2 > payload->blob_len)
393 return -E2BIG;396 return -E2BIG;
394 397
398 pub = blob + 2 + private_len + 2;
399 /* key attributes are always at offset 4 */
400 attrs = get_unaligned_be32(pub + 4);
401
402 if ((attrs & (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT)) ==
403 (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT))
404 payload->migratable = 0;
405 else
406 payload->migratable = 1;
407
395 blob_len = private_len + public_len + 4;408 blob_len = private_len + public_len + 4;
396 if (blob_len > payload->blob_len)409 if (blob_len > payload->blob_len)
397 return -E2BIG;410 return -E2BIG;
@@ -472,7 +485,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, · 74 unmodified lines
472 if (!rc) {485 if (!rc) {
473 data_len = be16_to_cpup(486 data_len = be16_to_cpup(
474 (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);487 (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
475 if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE + 1) {488 if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE) {
476 rc = -EFAULT;489 rc = -EFAULT;
477 goto out;490 goto out;
478 }491 }
@@ -483,9 +496,19 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, · 4 unmodified lines
483 }496 }
484 data = &buf.data[TPM_HEADER_SIZE + 6];497 data = &buf.data[TPM_HEADER_SIZE + 6];
485 498
486 memcpy(payload->key, data, data_len - 1);499 if (payload->old_format) {
487 payload->key_len = data_len - 1;500 /* migratable flag is at the end of the key */
488 payload->migratable = data[data_len - 1];501 memcpy(payload->key, data, data_len - 1);
502 payload->key_len = data_len - 1;
503 payload->migratable = data[data_len - 1];
504 } else {
505 /*
506 * migratable flag already collected from key
507 * attributes
508 */
509 memcpy(payload->key, data, data_len);
510 payload->key_len = data_len;
511 }
489 }512 }
490 513
491out:514out:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t