Skip to content

Commit df87734

Browse files
committed
pkcs11_store: ignore a close without an open
wolfPKCS11 2.2.0 and earlier close a data record even when opening it failed, and pass NULL. wolfPKCS11_Store_CloseCommit(), which wolfPKCS11_Store_Close() calls, wrote through the handle and faulted. It now returns 0 for NULL. unit-pkcs11_store covers both closes.
1 parent ce85261 commit df87734

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

‎src/pkcs11_store.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,11 @@ int wolfPKCS11_Store_CloseCommit(void* store, int commit)
807807
int ret = 0;
808808

809809
(void)commit;
810+
/* Nothing was opened. wolfPKCS11 2.2.0 and earlier close a data record
811+
* even when opening it failed. */
812+
if (handle == NULL) {
813+
return 0;
814+
}
810815
/* Commit all pending sectors: the header sector last, so the header
811816
* is the atomic commit point of the window. */
812817
cache_flush_all();

‎tools/unit-tests/unit-pkcs11_store.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,16 @@ START_TEST(test_close_clears_handle_state)
364364
}
365365
END_TEST
366366

367+
START_TEST(test_close_null_store)
368+
{
369+
/* wolfPKCS11 2.2.0 and earlier close a data record even when opening it
370+
* failed, so a close without an open does nothing. */
371+
wolfPKCS11_Store_Close(NULL);
372+
ck_assert_int_eq(wolfPKCS11_Store_CloseCommit(NULL, 0), 0);
373+
ck_assert_int_eq(wolfPKCS11_Store_CloseCommit(NULL, 1), 0);
374+
}
375+
END_TEST
376+
367377
START_TEST(test_delete_object_ignores_metadata_prefix)
368378
{
369379
const int32_t type = DYNAMIC_TYPE_RSA;
@@ -1203,6 +1213,7 @@ Suite *wolfboot_suite(void)
12031213
tcase_add_test(tcase_store_and_load_objs, test_store_and_load_objs);
12041214
tcase_add_test(tcase_cross_sector_write, test_cross_sector_write_preserves_length);
12051215
tcase_add_test(tcase_close, test_close_clears_handle_state);
1216+
tcase_add_test(tcase_close, test_close_null_store);
12061217
tcase_add_test(tcase_delete_object, test_delete_object_ignores_metadata_prefix);
12071218
tcase_add_test(tcase_delete_corrupted, test_delete_object_corrupted_pos_no_oob);
12081219
tcase_add_test(tcase_delete_corrupted,

0 commit comments

Comments
 (0)