Skip to content

Commit 86e3a07

Browse files
committed
Shutdown a TPM 2 before terminating the swtpm process
We have to shut down the TPM 2 before terminating the swtpm process. To be able to do this we need to write the TPM device number and version into the state file as well. Signed-off-by: Stefan Berger <[email protected]>
1 parent 5d6e978 commit 86e3a07

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

libcontainer/vtpm/vtpm.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ type VTPM struct {
2727
CreateCerts bool `json:"createCerts"`
2828

2929
// Version of the TPM
30-
vtpmversion string
30+
Vtpmversion string `json:"vtpmversion"`
3131

3232
// The user under which to run the TPM emulator
3333
user string
3434

3535
// The TPM device number as returned from /dev/vtpmx ioctl
36-
tpm_dev_num uint32
36+
Tpm_dev_num uint32 `json:"tpm_dev_num"`
3737

3838
// The backend file descriptor
3939
fd int32
@@ -116,10 +116,10 @@ func NewVTPM(statepath, vtpmversion string, createcerts bool) (*VTPM, error) {
116116
}
117117

118118
return &VTPM{
119-
tpm_dev_num: VTPM_DEV_NUM_INVALID,
119+
Tpm_dev_num: VTPM_DEV_NUM_INVALID,
120120
user: "tss",
121121
StatePath: statepath,
122-
vtpmversion: vtpmversion,
122+
Vtpmversion: vtpmversion,
123123
CreateCerts: createcerts,
124124
}, nil
125125
}
@@ -129,12 +129,12 @@ func (vtpm *VTPM) createDev() error {
129129
vtpm_proxy_new_dev vtpm_proxy_new_dev
130130
)
131131

132-
if vtpm.tpm_dev_num != VTPM_DEV_NUM_INVALID {
132+
if vtpm.Tpm_dev_num != VTPM_DEV_NUM_INVALID {
133133
logrus.Info("Device already exists")
134134
return nil
135135
}
136136

137-
if vtpm.vtpmversion == VTPM_VERSION_2 {
137+
if vtpm.Vtpmversion == VTPM_VERSION_2 {
138138
vtpm_proxy_new_dev.flags = VTPM_FLAG_TPM2
139139
}
140140

@@ -143,7 +143,7 @@ func (vtpm *VTPM) createDev() error {
143143
return err
144144
}
145145

146-
vtpm.tpm_dev_num = vtpm_proxy_new_dev.tpm_dev_num
146+
vtpm.Tpm_dev_num = vtpm_proxy_new_dev.tpm_dev_num
147147
vtpm.fd = vtpm_proxy_new_dev.fd
148148
vtpm.major = vtpm_proxy_new_dev.major
149149
vtpm.minor = vtpm_proxy_new_dev.minor
@@ -166,7 +166,7 @@ func (vtpm *VTPM) getPidFromFile() (int, error) {
166166
return -1, err
167167
}
168168
if len(d) == 0 {
169-
return -1, fmt.Errorf("Empty Pidfile")
169+
return -1, fmt.Errorf("Empty pid file")
170170
}
171171

172172
pid, err := strconv.Atoi(string(d))
@@ -190,9 +190,33 @@ func (vtpm *VTPM) waitForPidFile(loops int) (int, error) {
190190
return -1, fmt.Errorf("swtpm's pid file did not appear")
191191
}
192192

193+
func (vtpm *VTPM) shutdown() error {
194+
var err error = nil
195+
196+
if vtpm.Tpm_dev_num != VTPM_DEV_NUM_INVALID && vtpm.Vtpmversion == VTPM_VERSION_2 {
197+
devname := vtpm.GetTPMDevname()
198+
dev, err := os.OpenFile(devname, os.O_RDWR, 0666)
199+
if err != nil {
200+
logrus.Errorf("Could not open %s: %v", devname, err)
201+
return err
202+
}
203+
defer dev.Close()
204+
205+
sd := []byte{0x80, 0x01, 0x00, 0x00, 0x00, 0x0c,
206+
0x00, 0x00, 0x01, 0x45, 0x00, 0x00}
207+
n, err := dev.Write(sd)
208+
if err != nil || n != len(sd) {
209+
logrus.Errorf("Could not write shutdown to %s: %v", devname, err)
210+
}
211+
}
212+
return err
213+
}
214+
193215
// stopByPidFile: Stop the vTPM by its PID file
194216
func (vtpm *VTPM) stopByPidFile() error {
195217

218+
vtpm.shutdown()
219+
196220
pid, err := vtpm.getPidFromFile()
197221
if err != nil {
198222
return err
@@ -285,7 +309,7 @@ func (vtpm *VTPM) setup(createCerts bool) error {
285309
cmd.Args = append(cmd.Args, "--create-ek-cert", "--create-platform-cert", "--lock-nvram")
286310
}
287311

288-
if vtpm.vtpmversion == VTPM_VERSION_2 {
312+
if vtpm.Vtpmversion == VTPM_VERSION_2 {
289313
cmd.Args = append(cmd.Args, "--tpm2")
290314
}
291315

@@ -369,7 +393,7 @@ again:
369393
logfile := fmt.Sprintf("file=%s", vtpm.getLogFile())
370394

371395
cmd := exec.Command("swtpm", "chardev", "--tpmstate", tpmstate, "--daemon", "--fd", fdstr, "--pid", pidfile, "--log", logfile, "--runas", vtpm.user)
372-
if vtpm.vtpmversion == VTPM_VERSION_2 {
396+
if vtpm.Vtpmversion == VTPM_VERSION_2 {
373397
cmd.Args = append(cmd.Args, "--tpm2")
374398
}
375399
file := os.NewFile(uintptr(vtpm.fd), "[vtpm]")
@@ -397,7 +421,7 @@ again:
397421
}
398422

399423
cmd = exec.Command("swtpm_bios", "-n", "-cs", "-u", "--tpm-device", tpmname)
400-
if vtpm.vtpmversion == VTPM_VERSION_2 {
424+
if vtpm.Vtpmversion == VTPM_VERSION_2 {
401425
cmd.Args = append(cmd.Args, "--tpm2")
402426
} else {
403427
// make sure the TPM 1.2 is activated
@@ -437,7 +461,7 @@ func (vtpm *VTPM) Stop(deleteStatePath bool) error {
437461

438462
vtpm.CloseServer()
439463

440-
vtpm.tpm_dev_num = VTPM_DEV_NUM_INVALID
464+
vtpm.Tpm_dev_num = VTPM_DEV_NUM_INVALID
441465

442466
if deleteStatePath {
443467
vtpm.DeleteStatePath()
@@ -448,11 +472,11 @@ func (vtpm *VTPM) Stop(deleteStatePath bool) error {
448472

449473
// Get the TPM device name; this method can be called after Start()
450474
func (vtpm *VTPM) GetTPMDevname() string {
451-
return fmt.Sprintf("/dev/tpm%d", vtpm.tpm_dev_num)
475+
return fmt.Sprintf("/dev/tpm%d", vtpm.Tpm_dev_num)
452476
}
453477

454478
func (vtpm *VTPM) GetTPMDevNum() uint32 {
455-
return vtpm.tpm_dev_num
479+
return vtpm.Tpm_dev_num
456480
}
457481

458482
// Get the major and minor numbers of the created device;

0 commit comments

Comments
 (0)