Attestation: Free JSON from the Wasm module heap (#2803)

The JSON evidence is allocated on the module instance heap, but no API
was given to dispose of this memory buffer. The sample mentions using
the function free, which behaves differently depending on the
execution context.

This fix provides a new function called librats_dispose_evidence_json,
enabling freeing the JSON evidence directly from the Wasm app.
This commit is contained in:
Jämes Ménétrey 2023-11-22 03:48:14 +01:00 committed by GitHub
parent 4d5eb346fc
commit f9e8b9535e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View File

@ -17,7 +17,7 @@
#include "lib_rats_common.h" #include "lib_rats_common.h"
static int static int
librats_collect_wrapper(wasm_exec_env_t exec_env, char **evidence_json, librats_collect_wrapper(wasm_exec_env_t exec_env, uint32_t *evidence_json,
const char *buffer, uint32_t buffer_size) const char *buffer, uint32_t buffer_size)
{ {
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
@ -47,7 +47,7 @@ librats_collect_wrapper(wasm_exec_env_t exec_env, char **evidence_json,
return (int)RATS_ATTESTER_ERR_NO_MEM; return (int)RATS_ATTESTER_ERR_NO_MEM;
} }
bh_memcpy_s(str_ret, json_size, json, json_size); bh_memcpy_s(str_ret, json_size, json, json_size);
*((int *)evidence_json) = str_ret_offset; *evidence_json = str_ret_offset;
free(json); free(json);
return 0; return 0;
@ -96,6 +96,15 @@ librats_parse_evidence_wrapper(wasm_exec_env_t exec_env,
return 0; return 0;
} }
static void
librats_dispose_evidence_json_wrapper(wasm_exec_env_t exec_env,
uint32_t evidence_json)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
module_free(evidence_json);
}
/* clang-format off */ /* clang-format off */
#define REG_NATIVE_FUNC(func_name, signature) \ #define REG_NATIVE_FUNC(func_name, signature) \
{ #func_name, func_name##_wrapper, signature, NULL } { #func_name, func_name##_wrapper, signature, NULL }
@ -104,7 +113,8 @@ librats_parse_evidence_wrapper(wasm_exec_env_t exec_env,
static NativeSymbol native_symbols_lib_rats[] = { static NativeSymbol native_symbols_lib_rats[] = {
REG_NATIVE_FUNC(librats_collect, "(**~)i"), REG_NATIVE_FUNC(librats_collect, "(**~)i"),
REG_NATIVE_FUNC(librats_verify, "(*~*~)i"), REG_NATIVE_FUNC(librats_verify, "(*~*~)i"),
REG_NATIVE_FUNC(librats_parse_evidence, "(*~*~)i") REG_NATIVE_FUNC(librats_parse_evidence, "(*~*~)i"),
REG_NATIVE_FUNC(librats_dispose_evidence_json, "(i)")
}; };
uint32_t uint32_t

View File

@ -41,6 +41,9 @@ librats_parse_evidence(const char *evidence_json, uint32_t json_size,
evidence_json ? strlen(evidence_json) + 1 : 0, \ evidence_json ? strlen(evidence_json) + 1 : 0, \
evidence, sizeof(rats_sgx_evidence_t)) evidence, sizeof(rats_sgx_evidence_t))
void
librats_dispose_evidence_json(char *evidence_json);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -106,7 +106,7 @@ main(int argc, char **argv)
err: err:
if (evidence_json) { if (evidence_json) {
free(evidence_json); librats_dispose_evidence_json(evidence_json);
} }
if (evidence) { if (evidence) {