From d12e6da372a33d186209154897334006efc2b42d Mon Sep 17 00:00:00 2001 From: Patrick Georgi Date: Tue, 27 Aug 2019 17:18:03 +0200 Subject: host/lib/crossystem: free SharedData when leaving context Minor leak avoidance as identified by Coverity Scan. BUG=none BRANCH=none TEST=none Change-Id: I4e1d0f8c7eeaf12c5cf52fb94bdcf4ed7fef3224 Signed-off-by: Patrick Georgi Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/1771970 Reviewed-by: Julius Werner Commit-Queue: Patrick Georgi Tested-by: Patrick Georgi --- host/lib/crossystem.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/host/lib/crossystem.c b/host/lib/crossystem.c index c8938225..061d4b71 100644 --- a/host/lib/crossystem.c +++ b/host/lib/crossystem.c @@ -103,13 +103,18 @@ int vb2_get_nv_storage(enum vb2_nv_param param) VbSharedDataHeader* sh = VbSharedDataRead(); static struct vb2_context cached_ctx; + if (!sh) + return -1; + /* TODO: locking around NV access */ if (!vnc_read) { memset(&cached_ctx, 0, sizeof(cached_ctx)); if (sh && sh->flags & VBSD_NVDATA_V2) cached_ctx.flags |= VB2_CONTEXT_NVDATA_V2; - if (0 != vb2_read_nv_storage(&cached_ctx)) + if (0 != vb2_read_nv_storage(&cached_ctx)) { + free(sh); return -1; + } vb2_nv_init(&cached_ctx); /* TODO: If vnc.raw_changed, attempt to reopen NVRAM for write @@ -118,6 +123,7 @@ int vb2_get_nv_storage(enum vb2_nv_param param) vnc_read = 1; } + free(sh); return (int)vb2_nv_get(&cached_ctx, param); } @@ -126,22 +132,30 @@ int vb2_set_nv_storage(enum vb2_nv_param param, int value) VbSharedDataHeader* sh = VbSharedDataRead(); struct vb2_context ctx; + if (!sh) + return -1; + /* TODO: locking around NV access */ memset(&ctx, 0, sizeof(ctx)); if (sh && sh->flags & VBSD_NVDATA_V2) ctx.flags |= VB2_CONTEXT_NVDATA_V2; - if (0 != vb2_read_nv_storage(&ctx)) + if (0 != vb2_read_nv_storage(&ctx)) { + free(sh); return -1; + } vb2_nv_init(&ctx); vb2_nv_set(&ctx, param, (uint32_t)value); if (ctx.flags & VB2_CONTEXT_NVDATA_CHANGED) { vnc_read = 0; - if (0 != vb2_write_nv_storage(&ctx)) + if (0 != vb2_write_nv_storage(&ctx)) { + free(sh); return -1; + } } /* Success */ + free(sh); return 0; } -- cgit v1.2.1