| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"github.com/google/go-tpm-tools/client" |
| 7 |
"github.com/google/go-tpm/legacy/tpm2" |
| 8 |
"github.com/spf13/cobra" |
| 9 |
) |
| 10 |
|
| 11 |
var handleNames = map[string][]tpm2.HandleType{ |
| 12 |
"all": {tpm2.HandleTypeLoadedSession, tpm2.HandleTypeSavedSession, tpm2.HandleTypeTransient}, |
| 13 |
"loaded": {tpm2.HandleTypeLoadedSession}, |
| 14 |
"saved": {tpm2.HandleTypeSavedSession}, |
| 15 |
"transient": {tpm2.HandleTypeTransient}, |
| 16 |
"persistent": {tpm2.HandleTypePersistent}, |
| 17 |
} |
| 18 |
|
| 19 |
var flushCmd = &cobra.Command{ |
| 20 |
Use: "flush <all | loaded | saved | transient | persistent>", |
| 21 |
Short: "Close active handles on the TPM", |
| 22 |
Long: `Close some or all currently active handles on the TPM |
| 23 |
|
| 24 |
Most TPM operations require an active handle, representing some object within |
| 25 |
the TPM. However, most TPMs also limit the number of simultaneous active handles |
| 26 |
(usually a max of 3). This command allows for "leaked" handles (handles that |
| 27 |
have not been properly closed) to be flushed, freeing up memory for new handles |
| 28 |
to be used with future TPM operations. |
| 29 |
|
| 30 |
The TPM can also take an active handle and "persist" it to NVRAM. This frees up |
| 31 |
memory for more transient handles. It can also allow for caching the creation of |
| 32 |
slow keys (such as the RSA-based EK or SRK). These handles can be evicted from |
| 33 |
NVRAM using the "persistent" argument, but are not flushed with "all", as this |
| 34 |
can result in data loss (if the persisted key cannot be regenerated). |
| 35 |
|
| 36 |
Which handles are flushed depends on the argument passed: |
| 37 |
loaded - only flush the loaded session handles |
| 38 |
saved - only flush the saved session handles |
| 39 |
transient - only flush the transient handles |
| 40 |
all - flush all loaded, saved, and transient handles |
| 41 |
persistent - only evict the persistent handles`, |
| 42 |
ValidArgs: func() []string { |
| 43 |
// The keys from the handleNames map are our valid arguments |
| 44 |
keys := make([]string, len(handleNames)) |
| 45 |
for k := range handleNames { |
| 46 |
keys = append(keys, k) |
| 47 |
} |
| 48 |
return keys |
| 49 |
}(), |
| 50 |
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), |
| 51 |
RunE: func(_ *cobra.Command, args []string) error { |
| 52 |
rwc, err := openTpm() |
| 53 |
if err != nil { |
| 54 |
return err |
| 55 |
} |
| 56 |
defer rwc.Close() |
| 57 |
|
| 58 |
totalHandles := 0 |
| 59 |
for _, handleType := range handleNames[args[0]] { |
| 60 |
handles, err := client.Handles(rwc, handleType) |
| 61 |
if err != nil { |
| 62 |
return fmt.Errorf("getting handles: %w", err) |
| 63 |
} |
| 64 |
for _, handle := range handles { |
| 65 |
if handleType == tpm2.HandleTypePersistent { |
| 66 |
if err = tpm2.EvictControl(rwc, "", tpm2.HandleOwner, handle, handle); err != nil { |
| 67 |
return fmt.Errorf("evicting handle 0x%x: %w", handle, err) |
| 68 |
} |
| 69 |
fmt.Fprintf(debugOutput(), "Handle 0x%x evicted\n", handle) |
| 70 |
} else { |
| 71 |
if err = tpm2.FlushContext(rwc, handle); err != nil { |
| 72 |
return fmt.Errorf("flushing handle 0x%x: %w", handle, err) |
| 73 |
} |
| 74 |
fmt.Fprintf(debugOutput(), "Handle 0x%x flushed\n", handle) |
| 75 |
} |
| 76 |
totalHandles++ |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
fmt.Fprintf(messageOutput(), "%d handles flushed\n", totalHandles) |
| 81 |
return nil |
| 82 |
}, |
| 83 |
} |
| 84 |
|
| 85 |
func init() { |
| 86 |
RootCmd.AddCommand(flushCmd) |
| 87 |
} |