Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage cleanup #670

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ srcs += adbg/src/adbg_case.c \
aes_perf.c \
benchmark_1000.c \
benchmark_2000.c \
clear_storage.c \
regression_4000.c \
regression_4100.c \
regression_5000.c \
Expand Down
1 change: 1 addition & 0 deletions host/xtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ set (SRC
aes_perf.c
benchmark_1000.c
benchmark_2000.c
clear_storage.c
regression_1000.c
regression_4000.c
regression_4100.c
Expand Down
1 change: 1 addition & 0 deletions host/xtest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ srcs += adbg/src/adbg_case.c \
aes_perf.c \
benchmark_1000.c \
benchmark_2000.c \
clear_storage.c \
regression_4000.c \
regression_4100.c \
regression_5000.c \
Expand Down
58 changes: 58 additions & 0 deletions host/xtest/clear_storage.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2023, Linaro Limited
*/

#include <err.h>
#include <ta_storage.h>
#include <tee_client_api.h>
#include <stdlib.h>
#include <util.h>

#include "clear_storage.h"

static int clear_storage_for_ta(TEEC_UUID *uuid)
{
TEEC_Result res = TEEC_ERROR_GENERIC;
TEEC_Context ctx = { };
TEEC_Session sess = { };
TEEC_Operation op = { };
uint32_t eo = 0;

res = TEEC_InitializeContext(NULL, &ctx);
if (res)
errx(EXIT_FAILURE, "TEEC_InitializeContext: %#"PRIx32, res);

res = TEEC_OpenSession(&ctx, &sess, uuid, TEEC_LOGIN_PUBLIC, NULL,
NULL, &eo);
if (res)
errx(EXIT_FAILURE,
"TEEC_OpenSession: res %#"PRIx32" err_orig %#"PRIx32,
res, eo);

op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
TEEC_NONE);
res = TEEC_InvokeCommand(&sess, TA_STORAGE_CMD_CLEAR_STORAGE, &op, &eo);
if (res)
errx(EXIT_FAILURE,
"TEEC_InvokeCommand: res %#"PRIx32" err_orig %#"PRIx32,
res, eo);

TEEC_CloseSession(&sess);
TEEC_FinalizeContext(&ctx);
return 0;
}

int clear_storage(void)
{
TEEC_UUID uuid[] = { TA_STORAGE_UUID, TA_STORAGE2_UUID };
size_t i = 0;
int res = 0;

for (i = 0; i < ARRAY_SIZE(uuid); i++) {
res = clear_storage_for_ta(uuid + i);
if (res)
break;
}
return res;
}
11 changes: 11 additions & 0 deletions host/xtest/clear_storage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2023, Linaro Limited
*/

#ifndef CLEAR_STORAGE_H
#define CLEAR_STORAGE_H

int clear_storage(void);

#endif /*CLEAR_STORAGE_H*/
6 changes: 5 additions & 1 deletion host/xtest/xtest_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
#include "xtest_helpers.h"

/* include here shandalone tests */
#include "clear_storage.h"
#include "crypto_common.h"
#include "install_ta.h"
#include "stats.h"


ADBG_SUITE_DEFINE(benchmark);
#ifdef WITH_GP_TESTS
ADBG_SUITE_DEFINE(gp);
Expand Down Expand Up @@ -109,6 +109,8 @@ void usage(char *program)
printf("\t--sdp-basic [opts] Basic Secure Data Path test setup ('-h' for usage)\n");
#endif
printf("\t--stats [opts] Various statistics ('-h' for usage)\n");
printf("\t--clear-storage Delete any persistent objects that may have been\n");
printf("\t left over by a previous run of this application\n");
printf("\n");
printf("Examples:\n");
printf("\txtest -t regression 4001 4003\n");
Expand Down Expand Up @@ -169,6 +171,8 @@ int main(int argc, char *argv[])
#endif
else if (argc > 1 && !strcmp(argv[1], "--stats"))
return stats_runner_cmd_parser(argc - 1, &argv[1]);
else if (argc == 2 && !strcmp(argv[1], "--clear-storage"))
return clear_storage();

while ((opt = getopt(argc, argv, "d:l:t:h")) != -1) {
switch (opt) {
Expand Down
3 changes: 3 additions & 0 deletions ta/include/ta_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* All rights reserved.
*/

#include <stdint.h>

#ifndef __TA_STORAGE_H
#define __TA_STORAGE_H

Expand Down Expand Up @@ -48,5 +50,6 @@ struct ta_storage_obj_info {
#define TA_STORAGE_CMD_CREATE_ID_IN_SHM 23
#define TA_STORAGE_CMD_CREATEOVER_ID_IN_SHM 24
#define TA_STORAGE_CMD_RENAME_ID_IN_SHM 25
#define TA_STORAGE_CMD_CLEAR_STORAGE 26

#endif /*__TA_STORAGE_H*/
2 changes: 2 additions & 0 deletions ta/storage/include/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ TEE_Result ta_storage_cmd_free_obj(uint32_t param_types, TEE_Param params[4]);
TEE_Result ta_storage_cmd_reset_obj(uint32_t param_types, TEE_Param params[4]);
TEE_Result ta_storage_cmd_get_obj_info(uint32_t param_types,
TEE_Param params[4]);
TEE_Result ta_storage_cmd_clear_storage(uint32_t param_types,
TEE_Param params[4]);

#endif /*STORAGE_H */
76 changes: 76 additions & 0 deletions ta/storage/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include "ta_storage.h"

#include <tee_api.h>
#include <tee_api_defines_extensions.h>
#include <trace.h>
#include <user_ta_header_defines.h>

#define ASSERT_PARAM_TYPE(pt) \
do { \
Expand Down Expand Up @@ -662,3 +664,77 @@ TEE_Result ta_storage_cmd_get_obj_info(uint32_t param_types,

return res;
}

static TEE_Result clear_storage(uint32_t storage_id)
{
TEE_ObjectEnumHandle oe = TEE_HANDLE_NULL;
TEE_Result enum_res = TEE_ERROR_GENERIC;
TEE_ObjectHandle o = TEE_HANDLE_NULL;
TEE_Result res = TEE_ERROR_GENERIC;
TEE_UUID uuid = TA_UUID;
TEE_ObjectInfo oi = { };
size_t obj_id_sz = 0;
void *obj_id = NULL;
size_t i = 0;

IMSG("Clearing TA storage (UUID: %pUl, storage ID: 0x%x)",
(void *)&uuid, storage_id);
res = TEE_AllocatePersistentObjectEnumerator(&oe);
if (res)
return res;
res = TEE_StartPersistentObjectEnumerator(oe, storage_id);
if (res == TEE_ERROR_ITEM_NOT_FOUND) {
IMSG("No object found");
res = TEE_SUCCESS;
goto out;
}
if (res)
goto out;
obj_id = TEE_Malloc(TEE_OBJECT_ID_MAX_LEN, 0);
if (!obj_id) {
res = TEE_ERROR_OUT_OF_MEMORY;
goto out;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should set res = TEE_ERROR_OUT_OF_MEMORY

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

}

while (true) {
enum_res = TEE_GetNextPersistentObject(oe, &oi, obj_id,
&obj_id_sz);
if (enum_res == TEE_ERROR_ITEM_NOT_FOUND)
break;
if (enum_res) {
res = enum_res;
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case relates to an unexpected error. I think it should be reported by the TA as its storage may not have been wiped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code updated to set res = enum_res;.

}
IMSG("Deleting persistent object #%zu", i);
res = TEE_OpenPersistentObject(storage_id, obj_id, obj_id_sz,
TEE_DATA_FLAG_ACCESS_WRITE_META,
&o);
if (res)
break;
TEE_CloseAndDeletePersistentObject1(o);
i++;
}

out:
TEE_FreePersistentObjectEnumerator(oe);
TEE_Free(obj_id);
return res;
}

TEE_Result ta_storage_cmd_clear_storage(uint32_t param_types,
TEE_Param params[4])
{
uint32_t id[] = { TEE_STORAGE_PRIVATE_REE, TEE_STORAGE_PRIVATE_RPMB };
TEE_Result res = TEE_ERROR_GENERIC;
size_t i = 0;

(void)param_types;
(void)params;

for (i = 0; i < sizeof(id) / sizeof(id[0]); i++) {
res = clear_storage(id[i]);
if (res)
break;
}
return res;
}
3 changes: 3 additions & 0 deletions ta/storage/ta_entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ TEE_Result TA_InvokeCommandEntryPoint(void *pSessionContext,
case TA_STORAGE_CMD_GET_OBJ_INFO:
return ta_storage_cmd_get_obj_info(nParamTypes, pParams);

case TA_STORAGE_CMD_CLEAR_STORAGE:
return ta_storage_cmd_clear_storage(nParamTypes, pParams);

default:
return TEE_ERROR_BAD_PARAMETERS;
}
Expand Down