Skip to content

Commit f2dcc6f

Browse files
Zhirang GuoJuergenReppSIT
authored andcommitted
pcr commands: Fix session leaks
All three PCR tools had session leaks in tpm2_tool_onstop(): - tpm2_pcrextend: Authorization sessions and auxiliary sessions were set up but never closed, leaking all sessions. - tpm2_pcrevent: The early return prevented auxiliary session cleanup, leaking all auxiliary sessions. - tpm2_pcrread: The auxiliary session cleanup section was completely empty, leaking all auxiliary sessions. This commit adds proper session cleanup to all three tools: - Close authorization sessions with error checking - Close auxiliary sessions in a loop with path guards - Return accumulated error status Signed-off-by: Zhirang Guo <jonny_guo@apple.com>
1 parent 764b063 commit f2dcc6f

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

tools/tpm2_pcrevent.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,26 @@ static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
453453
/*
454454
* 2. Close authorization sessions
455455
*/
456-
return tpm2_session_close(&ctx.auth.session);
456+
tool_rc rc = tool_rc_success;
457+
tool_rc tmp_rc = tpm2_session_close(&ctx.auth.session);
458+
if (tmp_rc != tool_rc_success) {
459+
rc = tmp_rc;
460+
}
457461

458462
/*
459463
* 3. Close auxiliary sessions
460464
*/
465+
size_t i;
466+
for (i = 0; i < ctx.aux_session_cnt; i++) {
467+
if (ctx.aux_session_path[i]) {
468+
tmp_rc = tpm2_session_close(&ctx.aux_session[i]);
469+
if (tmp_rc != tool_rc_success) {
470+
rc = tmp_rc;
471+
}
472+
}
473+
}
474+
475+
return rc;
461476
}
462477

463478
static void tpm2_tool_onexit(void) {

tools/tpm2_pcrextend.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,26 @@ static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
236236
/*
237237
* 2. Close authorization sessions
238238
*/
239+
tool_rc rc = tool_rc_success;
240+
tool_rc tmp_rc = tpm2_session_close(&ctx.auth.session);
241+
if (tmp_rc != tool_rc_success) {
242+
rc = tmp_rc;
243+
}
239244

240245
/*
241246
* 3. Close auxiliary sessions
242247
*/
248+
size_t i;
249+
for (i = 0; i < ctx.aux_session_cnt; i++) {
250+
if (ctx.aux_session_path[i]) {
251+
tmp_rc = tpm2_session_close(&ctx.aux_session[i]);
252+
if (tmp_rc != tool_rc_success) {
253+
rc = tmp_rc;
254+
}
255+
}
256+
}
243257

244-
return tool_rc_success;
258+
return rc;
245259
}
246260

247261
static void tpm2_tool_onexit(void) {

tools/tpm2_pcrread.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,19 @@ static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
313313
/*
314314
* 3. Close auxiliary sessions
315315
*/
316+
tool_rc rc = tool_rc_success;
317+
tool_rc tmp_rc = tool_rc_success;
318+
size_t i;
319+
for (i = 0; i < ctx.aux_session_cnt; i++) {
320+
if (ctx.aux_session_path[i]) {
321+
tmp_rc = tpm2_session_close(&ctx.aux_session[i]);
322+
if (tmp_rc != tool_rc_success) {
323+
rc = tmp_rc;
324+
}
325+
}
326+
}
316327

317-
return tool_rc_success;
328+
return rc;
318329
}
319330

320331
// Register this tool with tpm2_tool.c

0 commit comments

Comments
 (0)