Skip to content

Commit 932a03f

Browse files
shjalaeriknordmark
authored andcommitted
tpm : streamline the error massges
Signed-off-by: Shahriyar Jalayeri <[email protected]>
1 parent a91053e commit 932a03f

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

pkg/pillar/cmd/vaultmgr/vaultmgr.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,18 +260,27 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar
260260

261261
// initialize publishing handles
262262
initializeSelfPublishHandles(ps, &ctx)
263-
if etpm.IsTpmEnabled() {
263+
tpmEnabled := etpm.IsTpmEnabled()
264+
if tpmEnabled {
264265
// TPM is enabled. Check if defaultVault directory exists, if not set vaultconfig
265266
tpmKeyOnlyMode := checkAndPublishVaultConfig(&ctx)
266267
handler.SetHandlerOptions(vault.HandlerOptions{TpmKeyOnlyMode: tpmKeyOnlyMode})
267268
}
269+
270+
if tpmEnabled {
271+
log.Noticef("about to setup the vault and fetch the disk key from TPM")
272+
} else {
273+
log.Noticef("about to setup the vault without TPM")
274+
}
275+
// if TPM available, this sets up the fscrypt and eventually calls FetchSealedVaultKey
268276
if err := handler.SetupDefaultVault(); err != nil {
269-
log.Errorf("setupDefaultVault failed, err: %v", err)
277+
log.Errorf("SetupDefaultVault failed, err: %v", err)
270278
getAndPublishAllVaultStatuses(&ctx)
271279
} else {
280+
log.Noticef("vault is setup and unlocked successfully")
272281
ctx.defaultVaultUnlocked = true
273282
}
274-
if ctx.defaultVaultUnlocked || !etpm.IsTpmEnabled() {
283+
if ctx.defaultVaultUnlocked || !tpmEnabled {
275284
// Now that vault is unlocked, run any upgrade converter handler if needed
276285
// In case of non-TPM platforms, we do this irrespective of
277286
// defaultVaultUnlocked

pkg/pillar/evetpm/tpm.go

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ func ReadOwnerCrdl() (string, error) {
189189
// TpmSign is used by external packages to get a digest signed by
190190
// device key in TPM
191191
func TpmSign(digest []byte) (*big.Int, *big.Int, error) {
192-
193192
rw, err := tpm2.OpenTPM(TpmDevicePath)
194193
if err != nil {
195194
return nil, nil, err
@@ -198,7 +197,7 @@ func TpmSign(digest []byte) (*big.Int, *big.Int, error) {
198197

199198
tpmOwnerPasswd, err := ReadOwnerCrdl()
200199
if err != nil {
201-
return nil, nil, fmt.Errorf("Error in fetching TPM credentials: %v", err)
200+
return nil, nil, fmt.Errorf("fetching TPM credentials failed: %v", err)
202201
}
203202

204203
//XXX This "32" should really come from Hash algo used.
@@ -213,7 +212,7 @@ func TpmSign(digest []byte) (*big.Int, *big.Int, error) {
213212
sig, err := tpm2.Sign(rw, TpmDeviceKeyHdl,
214213
tpmOwnerPasswd, digest, nil, scheme)
215214
if err != nil {
216-
return nil, nil, fmt.Errorf("Sign using TPM failed with error %v", err)
215+
return nil, nil, fmt.Errorf("signing data using TPM failed: %v", err)
217216
}
218217
return sig.ECC.R, sig.ECC.S, nil
219218
}
@@ -266,7 +265,6 @@ func GetFirmwareVersion(v1 uint32, v2 uint32) string {
266265

267266
// GetTpmProperty fetches a given property id, and returns it as uint32
268267
func GetTpmProperty(propID tpm2.TPMProp) (uint32, error) {
269-
270268
rw, err := tpm2.OpenTPM(TpmDevicePath)
271269
if err != nil {
272270
return 0, err
@@ -280,7 +278,7 @@ func GetTpmProperty(propID tpm2.TPMProp) (uint32, error) {
280278
}
281279
prop, ok := v[0].(tpm2.TaggedProperty)
282280
if !ok {
283-
return 0, fmt.Errorf("Unable to fetch property %d", propID)
281+
return 0, fmt.Errorf("fetching TPM property %X failed", propID)
284282
}
285283
return prop.Value, nil
286284
}
@@ -340,7 +338,6 @@ const (
340338

341339
// FetchTpmHwInfo returns TPM Hardware properties in a string
342340
func FetchTpmHwInfo() (string, error) {
343-
344341
//If we had done this earlier, return the last result
345342
if tpmHwInfo != "" {
346343
return tpmHwInfo, nil
@@ -386,7 +383,7 @@ func FetchVaultKey(log *base.LogObject) ([]byte, error) {
386383
//First try to read from TPM, if it was stored earlier
387384
key, err := readDiskKey()
388385
if err != nil {
389-
log.Noticef("Generating VaultKey")
386+
log.Noticef("can't read the legacy disk key, generating a new one")
390387
//
391388
//Note on why we are using GetRandom here:
392389
//We are using raw_key option to protect the encryption/decryption protector:
@@ -405,14 +402,14 @@ func FetchVaultKey(log *base.LogObject) ([]byte, error) {
405402
//
406403
key, err = GetRandom(vaultKeyLength)
407404
if err != nil {
408-
return nil, fmt.Errorf("FetchVaultKey: Error in GetRandom: %v", err)
405+
return nil, fmt.Errorf("GetRandom failed: %v", err)
409406
}
410407
err = writeDiskKey(key)
411408
if err != nil {
412-
return nil, fmt.Errorf("FetchVaultKey: Writing Key to TPM failed: %v", err)
409+
return nil, fmt.Errorf("writing legacy Key to TPM failed: %v", err)
413410
}
414411
} else {
415-
log.Noticef("Found VaultKey")
412+
log.Noticef("successfully read the legacy disk key from TPM")
416413
}
417414
return key, nil
418415
}
@@ -484,7 +481,7 @@ func FetchSealedVaultKey(log *base.LogObject) ([]byte, error) {
484481
legacyKeyPresent := isLegacyKeyPresent()
485482

486483
if !sealedKeyPresent && !legacyKeyPresent {
487-
log.Noticef("FetchSealedVaultKey generate new key")
484+
log.Noticef("neither legacy nor sealed disk key present, generating a fresh key")
488485
//Fresh install, generate a new key
489486
//
490487
//Note on why we are using GetRandom here:
@@ -504,16 +501,18 @@ func FetchSealedVaultKey(log *base.LogObject) ([]byte, error) {
504501
//
505502
key, err := GetRandom(vaultKeyLength)
506503
if err != nil {
507-
return nil, fmt.Errorf("FetchSealedVaultKey: GetRandom failed, %v", err)
504+
return nil, fmt.Errorf("GetRandom failed: %v", err)
508505
}
509506
err = SealDiskKey(key, DiskKeySealingPCRs)
510507
if err != nil {
511-
return nil, fmt.Errorf("FetchSealedVaultKey: Sealing failed: %v", err)
508+
return nil, fmt.Errorf("sealing the fresh disk key failed: %v", err)
512509
}
510+
511+
log.Noticef("successfully sealed the fresh disk key into TPM")
513512
}
514513

515514
if !sealedKeyPresent && legacyKeyPresent {
516-
log.Noticef("FetchSealedVaultKey legacy present")
515+
log.Noticef("only legacy disk key present, using it")
517516
//XXX: we need a migration path for existing installations.
518517
//hence re-using the current key here. i.e. if we end up creating
519518
//a new random key here, and we fail the upgrade, the fallback
@@ -526,20 +525,29 @@ func FetchSealedVaultKey(log *base.LogObject) ([]byte, error) {
526525
//Upgrade path will be to first upgrade to a) first release and then b)
527526
key, err := readDiskKey()
528527
if err != nil {
529-
return nil, fmt.Errorf("Error in retrieving old key")
528+
return nil, fmt.Errorf("retrieving the legacy disk key from TPM failed: %v", err)
530529
}
530+
531+
log.Noticef("try to convert the legacy key into a sealed key")
532+
531533
err = SealDiskKey(key, DiskKeySealingPCRs)
532534
if err != nil {
533-
return nil, fmt.Errorf("FetchSealedVaultKey: Sealing failed: %v", err)
535+
return nil, fmt.Errorf("sealing the legacy disk key into TPM failed: %v", err)
534536
}
535537
}
536538
//sealedKeyPresent && !legacyKeyPresent : unseal
537539
//sealedKeyPresent && legacyKeyPresent : unseal
538540
if sealedKeyPresent {
539-
log.Noticef("FetchSealedVaultKey unseal key")
541+
log.Noticef("sealed disk key present int TPM, about to unseal it")
540542
}
541543
//By this, we have a key sealed into TPM
542-
return UnsealDiskKey(DiskKeySealingPCRs)
544+
key, err := UnsealDiskKey(DiskKeySealingPCRs)
545+
if err == nil {
546+
// be more verbose, lets celebrate
547+
log.Noticef("successfully unsealed the disk key from TPM")
548+
}
549+
550+
return key, err
543551
}
544552

545553
// SealDiskKey seals key into TPM2.0, with provided PCRs
@@ -574,12 +582,12 @@ func SealDiskKey(key []byte, pcrSel tpm2.PCRSelection) error {
574582

575583
//Don't need the handle, we need only the policy for sealing
576584
if err := tpm2.FlushContext(rw, session); err != nil {
577-
return fmt.Errorf("Unable to flush session handle %v: %v", session, err)
585+
return fmt.Errorf("flushing session handle %v failed: %v", session, err)
578586
}
579587

580588
priv, public, err := tpm2.Seal(rw, TpmSRKHdl, EmptyPassword, EmptyPassword, policy, key)
581589
if err != nil {
582-
return fmt.Errorf("Unable to seal key: %v", err)
590+
return fmt.Errorf("sealing the disk key into TPM failed: %v", err)
583591
}
584592

585593
// Define space in NV storage and clean up afterwards or subsequent runs will fail.
@@ -667,7 +675,7 @@ func UnsealDiskKey(pcrSel tpm2.PCRSelection) ([]byte, error) {
667675

668676
sealedObjHandle, _, err := tpm2.Load(rw, TpmSRKHdl, "", pub, priv)
669677
if err != nil {
670-
return nil, fmt.Errorf("Load failed: %v", err)
678+
return nil, fmt.Errorf("loading the disk key into TPM failed: %v", err)
671679
}
672680
defer tpm2.FlushContext(rw, sealedObjHandle)
673681

@@ -683,7 +691,7 @@ func UnsealDiskKey(pcrSel tpm2.PCRSelection) ([]byte, error) {
683691
// information about the failure by finding the mismatching PCR index.
684692
mismatch, newErr := findMismatchingPCRs(TpmSavedDiskSealingPcrs)
685693
if newErr != nil {
686-
return nil, fmt.Errorf("UnsealWithSession failed: %v, failed to get more info: %v", err, newErr)
694+
return nil, fmt.Errorf("UnsealWithSession failed: %v, getting more info failed: %v", err, newErr)
687695
}
688696

689697
return nil, fmt.Errorf("UnsealWithSession failed: %v, possibly mismatching PCR indexes %v", err, mismatch)
@@ -717,7 +725,7 @@ func PolicyPCRSession(rw io.ReadWriteCloser, pcrSel tpm2.PCRSelection) (tpmutil.
717725

718726
policy, err := tpm2.PolicyGetDigest(rw, session)
719727
if err != nil {
720-
return session, nil, fmt.Errorf("Unable to get policy digest: %v", err)
728+
return session, nil, fmt.Errorf("PolicyGetDigest failed: %v", err)
721729
}
722730
return session, policy, nil
723731
}

pkg/pillar/vault/handler_ext4.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (h *Ext4Handler) SetupDefaultVault() error {
117117
return fmt.Errorf("error in setting up vault %s:%v", defaultVault, err)
118118
}
119119
// Log the type of key used for unlocking default vault
120-
h.log.Noticef("default vault unlocked using key type %s",
120+
h.log.Noticef("default vault unlocked using key type: %s",
121121
etpm.CompareLegacyandSealedKey().String())
122122
return nil
123123
}

0 commit comments

Comments
 (0)