Skip to content

Commit 2cc9dd5

Browse files
committed
nvm_flash_log: fix mem clean on obj destroy + minors
1 parent 392dbcf commit 2cc9dd5

2 files changed

Lines changed: 43 additions & 29 deletions

File tree

src/wh_nvm_flash_log.c

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
typedef struct {
6666
union {
6767
whNvmMetadata meta;
68-
uint8_t _pad[PAD_SIZE(sizeof(whNvmMetadata))];
68+
uint8_t WH_PAD[PAD_SIZE(sizeof(whNvmMetadata))];
6969
};
7070
} whNvmFlashLogMetadata;
7171

@@ -172,7 +172,7 @@ static int nfl_PartitionChoose(whNvmFlashLogContext* ctx)
172172
part1_blank = (ret == 0);
173173

174174
if (part0_blank && part1_blank) {
175-
/* Both partitions are blank, start with partition 0 */
175+
/* Both partitions headers are blank, start with partition 0 */
176176
ret = nfl_PartitionErase(ctx, 0);
177177
if (ret != 0)
178178
return ret;
@@ -236,6 +236,7 @@ static int nfl_ObjectDestroy(whNvmFlashLogContext* ctx, whNvmId id)
236236
whNvmFlashLogMetadata* obj;
237237
uint32_t len;
238238
uint32_t off;
239+
uint32_t tail;
239240

240241
if (ctx == NULL || id == WH_NVM_ID_INVALID)
241242
return WH_ERROR_BADARGS;
@@ -244,12 +245,12 @@ static int nfl_ObjectDestroy(whNvmFlashLogContext* ctx, whNvmId id)
244245
if (obj == NULL)
245246
return WH_ERROR_OK;
246247

247-
len = sizeof(whNvmFlashLogMetadata) + PAD_SIZE(obj->meta.len);
248-
off = (uint8_t*)obj - ctx->directory.data;
249-
/* zero out the object to prevent leaking */
250-
memset(obj, 0, len);
251-
memmove(obj, (uint8_t*)obj + len,
252-
ctx->directory.header.size - (off + len));
248+
len = sizeof(whNvmFlashLogMetadata) + PAD_SIZE(obj->meta.len);
249+
off = (uint8_t*)obj - ctx->directory.data;
250+
tail = ctx->directory.header.size - (off + len);
251+
memmove(obj, (uint8_t*)obj + len, tail);
252+
/* be sure to clean-up moved objects from memory */
253+
memset((uint8_t*)obj + tail, 0, len);
253254
ctx->directory.header.size -= len;
254255
return WH_ERROR_OK;
255256
}
@@ -372,19 +373,25 @@ int wh_NvmFlashLog_Init(void* c, const void* cf)
372373
}
373374

374375
/* unlock partitions */
375-
ret = context->flash_cb->WriteUnlock(context->flash_ctx, 0,
376-
context->partition_size);
377-
if (ret != 0)
378-
return ret;
379-
ret = context->flash_cb->WriteUnlock(
380-
context->flash_ctx, context->partition_size, context->partition_size);
381-
if (ret != 0)
382-
return ret;
376+
if (context->flash_cb->WriteUnlock != NULL) {
377+
ret = context->flash_cb->WriteUnlock(context->flash_ctx, 0,
378+
context->partition_size);
379+
if (ret != 0)
380+
return ret;
381+
ret = context->flash_cb->WriteUnlock(context->flash_ctx,
382+
context->partition_size,
383+
context->partition_size);
384+
if (ret != 0)
385+
return ret;
386+
}
383387

384388
ret = nfl_PartitionChoose(context);
385389
if (ret != 0)
386390
return ret;
387391
ret = nfl_PartitionRead(context);
392+
if (ret != 0)
393+
return ret;
394+
ret = nfl_PartitionErase(context, (context->active_partition == 0) ? 1 : 0);
388395
if (ret != 0)
389396
return ret;
390397

@@ -394,35 +401,42 @@ int wh_NvmFlashLog_Init(void* c, const void* cf)
394401

395402
int wh_NvmFlashLog_Cleanup(void* c)
396403
{
397-
int ret;
398404
whNvmFlashLogContext* context = (whNvmFlashLogContext*)c;
405+
int ret0, ret1;
406+
399407
if (context == NULL || !context->is_initialized)
400408
return WH_ERROR_BADARGS;
401409

410+
context->is_initialized = 0;
411+
402412
/* lock partitions */
403-
ret = context->flash_cb->WriteLock(context->flash_ctx, 0,
404-
context->partition_size);
405-
if (ret != 0)
406-
return ret;
407-
ret = context->flash_cb->WriteLock(
413+
if (context->flash_cb->WriteLock == NULL)
414+
return WH_ERROR_OK;
415+
416+
ret0 = context->flash_cb->WriteLock(context->flash_ctx, 0,
417+
context->partition_size);
418+
ret1 = context->flash_cb->WriteLock(
408419
context->flash_ctx, context->partition_size, context->partition_size);
409-
if (ret != 0)
410-
return ret;
411420

412-
context->is_initialized = 0;
421+
if (ret0 != WH_ERROR_OK)
422+
return ret0;
423+
if (ret1 != WH_ERROR_OK)
424+
return ret1;
425+
413426
return WH_ERROR_OK;
414427
}
415428

416429
/* List objects */
417430
int wh_NvmFlashLog_List(void* c, whNvmAccess access, whNvmFlags flags,
418431
whNvmId start_id, whNvmId* out_count, whNvmId* out_id)
419432
{
433+
whNvmFlashLogContext* ctx = (whNvmFlashLogContext*)c;
434+
whNvmFlashLogMetadata *next_obj = NULL, *start_obj = NULL;
435+
uint32_t count = 0;
436+
420437
/* TODO: Implement access and flag matching */
421438
(void)access;
422439
(void)flags;
423-
whNvmFlashLogContext* ctx = (whNvmFlashLogContext*)c;
424-
whNvmFlashLogMetadata* next_obj = NULL, *start_obj = NULL;
425-
uint32_t count = 0;
426440

427441
if (ctx == NULL || !ctx->is_initialized)
428442
return WH_ERROR_BADARGS;

wolfhsm/wh_nvm_flash_log.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
typedef struct {
4141
uint32_t partition_epoch;
4242
uint32_t size;
43-
uint8_t _pad[WH_NVM_FLASH_LOG_WRITE_GRANULARITY - sizeof(uint32_t) * 2];
43+
uint8_t WH_PAD[WH_NVM_FLASH_LOG_WRITE_GRANULARITY - sizeof(uint32_t) * 2];
4444
} whNvmFlashLogPartitionHeader;
4545

4646
/* In-memory representation of a partition */

0 commit comments

Comments
 (0)