Skip to content

Commit 88befb6

Browse files
committed
Preserve neighboring store objects on truncate (addressed Fenrir's
review comment)
1 parent 893973a commit 88befb6

2 files changed

Lines changed: 60 additions & 24 deletions

File tree

src/pkcs11_store.c

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,35 @@ static void update_store_size(struct obj_hdr *hdr, uint32_t size)
360360
cache_commit(0);
361361
}
362362

363+
static void erase_object_payload(uint8_t *buf)
364+
{
365+
uint32_t erase_off;
366+
uint32_t erase_end;
367+
uint32_t sector_base;
368+
369+
erase_off = (uint32_t)((uintptr_t)buf - (uintptr_t)vault_base) +
370+
(2U * sizeof(uint32_t));
371+
erase_end = (uint32_t)((uintptr_t)buf - (uintptr_t)vault_base) +
372+
KEYVAULT_OBJ_SIZE;
373+
sector_base = erase_off - (erase_off % WOLFBOOT_SECTOR_SIZE);
374+
375+
while (sector_base < erase_end) {
376+
uint32_t erase_start = erase_off;
377+
uint32_t erase_stop = sector_base + WOLFBOOT_SECTOR_SIZE;
378+
379+
if (erase_start < sector_base)
380+
erase_start = sector_base;
381+
if (erase_stop > erase_end)
382+
erase_stop = erase_end;
383+
384+
memcpy(cached_sector, vault_base + sector_base, WOLFBOOT_SECTOR_SIZE);
385+
memset(cached_sector + (erase_start - sector_base), 0xFF,
386+
erase_stop - erase_start);
387+
cache_commit(sector_base);
388+
sector_base += WOLFBOOT_SECTOR_SIZE;
389+
}
390+
}
391+
363392
/* Find a free handle in openstores_handles[] array
364393
* to manage the interaction with the API.
365394
*
@@ -436,18 +465,7 @@ int wolfPKCS11_Store_Open(int type, CK_ULONG id1, CK_ULONG id2, int read,
436465
* prior (longer) payload. New objects are already in a fresh sector
437466
* from create_object(), so only do this for existing objects. */
438467
if (!is_new) {
439-
uint32_t obj_tok = ((uint32_t *)buf)[0];
440-
uint32_t obj_id_val = ((uint32_t *)buf)[1];
441-
uint32_t obj_off = (uint32_t)((uintptr_t)buf - (uintptr_t)vault_base);
442-
uint32_t s;
443-
for (s = 0; s < KEYVAULT_OBJ_SIZE; s += WOLFBOOT_SECTOR_SIZE) {
444-
memset(cached_sector, 0xFF, WOLFBOOT_SECTOR_SIZE);
445-
if (s == 0) {
446-
((uint32_t *)cached_sector)[0] = obj_tok;
447-
((uint32_t *)cached_sector)[1] = obj_id_val;
448-
}
449-
cache_commit(obj_off + s);
450-
}
468+
erase_object_payload(buf);
451469
}
452470
}
453471

src/psa_store.c

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,35 @@ static void update_store_size(struct obj_hdr *hdr, uint32_t size)
359359
cache_commit(0);
360360
}
361361

362+
static void erase_object_payload(uint8_t *buf)
363+
{
364+
uint32_t erase_off;
365+
uint32_t erase_end;
366+
uint32_t sector_base;
367+
368+
erase_off = (uint32_t)((uintptr_t)buf - (uintptr_t)vault_base) +
369+
(2U * sizeof(uint32_t));
370+
erase_end = (uint32_t)((uintptr_t)buf - (uintptr_t)vault_base) +
371+
KEYVAULT_OBJ_SIZE;
372+
sector_base = erase_off - (erase_off % WOLFBOOT_SECTOR_SIZE);
373+
374+
while (sector_base < erase_end) {
375+
uint32_t erase_start = erase_off;
376+
uint32_t erase_stop = sector_base + WOLFBOOT_SECTOR_SIZE;
377+
378+
if (erase_start < sector_base)
379+
erase_start = sector_base;
380+
if (erase_stop > erase_end)
381+
erase_stop = erase_end;
382+
383+
memcpy(cached_sector, vault_base + sector_base, WOLFBOOT_SECTOR_SIZE);
384+
memset(cached_sector + (erase_start - sector_base), 0xFF,
385+
erase_stop - erase_start);
386+
cache_commit(sector_base);
387+
sector_base += WOLFBOOT_SECTOR_SIZE;
388+
}
389+
}
390+
362391
/* Find a free handle in openstores_handles[] array
363392
* to manage the interaction with the API.
364393
*
@@ -434,18 +463,7 @@ int wolfPSA_Store_Open(int type, unsigned long id1, unsigned long id2, int read,
434463
/* Erase the object data region so a shorter write does not leave
435464
* residual key material from the previous (longer) payload. */
436465
if (!is_new) {
437-
uint32_t obj_tok = ((uint32_t *)buf)[0];
438-
uint32_t obj_id_val = ((uint32_t *)buf)[1];
439-
uint32_t obj_off = (uint32_t)((uintptr_t)buf - (uintptr_t)vault_base);
440-
uint32_t s;
441-
for (s = 0; s < KEYVAULT_OBJ_SIZE; s += WOLFBOOT_SECTOR_SIZE) {
442-
memset(cached_sector, 0xFF, WOLFBOOT_SECTOR_SIZE);
443-
if (s == 0) {
444-
((uint32_t *)cached_sector)[0] = obj_tok;
445-
((uint32_t *)cached_sector)[1] = obj_id_val;
446-
}
447-
cache_commit(obj_off + s);
448-
}
466+
erase_object_payload(buf);
449467
}
450468
}
451469

0 commit comments

Comments
 (0)