• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 
13 #include "libteecvendorclosesession_fuzzer.h"
14 
15 #include <cstddef>
16 #include <cstdint>
17 #include "tee_client_api.h"
18 #include "tee_client_constants.h"
19 #include "tee_client_type.h"
20 #include "tee_client_inner.h"
21 #include "tee_client_inner_api.h"
22 
23 namespace OHOS {
LibteecVendorCloseSessionFuzzTest(const uint8_t * data,size_t size)24     bool LibteecVendorCloseSessionFuzzTest(const uint8_t *data, size_t size)
25     {
26         bool result = false;
27         if (size >= sizeof(TEEC_Context) + sizeof(TEEC_Session)) {
28             uint8_t *temp = const_cast<uint8_t *>(data);
29             TEEC_Context context = *reinterpret_cast<TEEC_Context *>(temp);
30             temp += sizeof(TEEC_Context);
31             TEEC_Session session = *reinterpret_cast<TEEC_Session *>(temp);
32             session.context = &context;
33 
34             (void)TEEC_CloseSession(&session);
35         }
36         return result;
37     }
TEEC_CloseSessionInnerTest_001(const uint8_t * data,size_t size)38     void TEEC_CloseSessionInnerTest_001(const uint8_t *data, size_t size)
39     {
40         TEEC_Session session = { 0 };
41         TEEC_ContextInner contextInner = { 0 };
42 
43         (void)TEEC_CloseSessionInner(nullptr, nullptr);
44         (void)TEEC_CloseSessionInner(&session, nullptr);
45 
46         TEEC_Context context = { 0 };
47         TEEC_Result result = TEEC_InitializeContext(NULL, &context);
48         (void)TEEC_CloseSessionInner(&session, &contextInner);
49         (void)data;
50         (void)size;
51         (void)result;
52     }
TEEC_CloseSessionTest_001(const uint8_t * data,size_t size)53     void TEEC_CloseSessionTest_001(const uint8_t *data, size_t size)
54     {
55         TEEC_Session session = { 0 };
56 
57         TEEC_CloseSession(NULL);
58 
59         session.context = NULL;
60         TEEC_CloseSession(&session);
61         (void)data;
62         (void)size;
63     }
64 }
65 
66 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
68 {
69     /* Run your code on data */
70     OHOS::LibteecVendorCloseSessionFuzzTest(data, size);
71     OHOS::TEEC_CloseSessionInnerTest_001(data, size);
72     OHOS::TEEC_CloseSessionTest_001(data, size);
73     return 0;
74 }
75