Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tpm2: Update the policy for the PCR policy counter index #370

Merged

Conversation

chrisccoulson
Copy link
Collaborator

@chrisccoulson chrisccoulson commented Jan 27, 2025

This adds a new branch which permits using the TPM2_PolicyNV command
against the index without any authorization, much like there is already
a branch that permits TPM2_NV_Read.

This will make it compatible with automatic branch selection when the
tpm2 package starts to make use of Policy.Execute from the
github.com/canonical/go-tpm2/policyutil package in the future, without
having to retrospectively modify the policy of NV indexes on existing
devices.

This adds a new branch which permits using the TPM2_PolicyNV command
against the index without any authorization, much like there is already
a branch that permits TPM2_NV_Read.

This will make it compatible with automatic branch selection when the
tpm2 package starts to make use of Policy.Execute from the
github.com/canonical/go-tpm2/policyutil packagevin the future, without
having to retrospectively modify the policy of NV indexes on existing
devices.
@chrisccoulson chrisccoulson force-pushed the tpm2-update-nv-index-policy branch from 8114797 to b7a7bd3 Compare January 28, 2025 12:56
@chrisccoulson chrisccoulson marked this pull request as ready for review January 28, 2025 12:56
@chrisccoulson
Copy link
Collaborator Author

Note that most of the new code in this PR is a clone of the code in tpm2/policy_v0, with a few minor tweaks.

// pcrPolicyData_v3 represents version 3 of the PCR policy metadata for
// executing a policy session, and can be updated. It has the same format
// as version 2.
type pcrPolicyData_v3 = pcrPolicyData_v2
type pcrPolicyData_v3 struct {
Selection tpm2.PCRSelectionList
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't this just embed pcrPolicyData_v2 and replace below only the methods that have changes? it's very hard to spot what are difference between the methods are atm, are they mostly in executeRevocationCheck ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the changes are only in executeRevocationCheck. I'll try what you suggested.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've done this now, although it did require a bit more work in the unit tests.

Rather than fully reimplement pcrPolicyData_v3, which only has minor
differences to pcrPolicyData_v2, instead, just embed pcrPolicyData_v2 as
an anonymous member of the new pcrPolicyData_v3 and add an override for
the method that is different.
Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the change, one comment

Comment on lines +179 to +200
// XXX(chrisccoulson): This is the 3rd session we open here, with all 3 loaded
// on the TPM. PC Client TPMs are only guaranteed to support 3 loaded sessions,
// so we're pushing things a bit close to the wire here. This code does execute
// during early boot, but it might be better to make the HMAC session associated
// with the TPM connection unloaded by default (context saved), and only context
// load it when needed.
revocationCheckSession, err := tpm.StartAuthSession(nil, nil, tpm2.SessionTypePolicy, nil, counter.Name().Algorithm())
if err != nil {
return fmt.Errorf("cannot create policy session for revocation check: %w", err)
}
defer tpm.FlushContext(revocationCheckSession)

authPolicies, err := computeV3PcrPolicyCounterAuthPolicies(counter.Name().Algorithm(), updateKey)
if err != nil {
return policyDataError{fmt.Errorf("cannot compute auth policies for PCR policy counter: %w", err)}
}
if err := tpm.PolicyCommandCode(revocationCheckSession, tpm2.CommandPolicyNV); err != nil {
return err
}
if err := tpm.PolicyOR(revocationCheckSession, authPolicies); err != nil {
return err
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be good to have a comment here that explain why we create a new session and what we do with it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a comment now.

@pedronis pedronis self-requested a review January 29, 2025 08:44
Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@chrisccoulson chrisccoulson merged commit 191696d into canonical:master Jan 29, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants