1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "co_auth_stub_fuzzer.h"
17
18 #include <cinttypes>
19 #include <cstddef>
20 #include <cstdint>
21
22 #include "parcel.h"
23
24 #include "iam_fuzz_test.h"
25 #include "iam_logger.h"
26 #include "co_auth_service.h"
27 #include "executor_messenger_service.h"
28
29 #define LOG_TAG "USER_AUTH_SA"
30
31 using namespace std;
32
33 namespace OHOS {
34 namespace UserIam {
35 namespace UserAuth {
36 namespace {
37 constexpr uint32_t CO_AUTH_CODE_MIN = 0;
38 constexpr uint32_t CO_AUTH_CODE_MAX = 6;
39 const std::u16string CO_AUTH_INTERFACE_TOKEN = u"ohos.CoAuth.ICoAuth";
40 constexpr uint32_t EXECUTOR_MESSENGER_CODE_MIN = 0;
41 constexpr uint32_t EXECUTOR_MESSENGER_CODE_MAX = 3;
42 const std::u16string EXECUTOR_MESSENGER_INTERFACE_TOKEN = u"ohos.UserIam.AuthResPool.IExecutor_Messenger";
43
FuzzCoAuthStub(const uint8_t * rawData,size_t size)44 bool FuzzCoAuthStub(const uint8_t *rawData, size_t size)
45 {
46 IAM_LOGI("start");
47 if (rawData == nullptr) {
48 return false;
49 }
50
51 CoAuthService coAuthService;
52 for (uint32_t code = CO_AUTH_CODE_MIN; code < CO_AUTH_CODE_MAX; code++) {
53 MessageParcel data;
54 MessageParcel reply;
55 MessageOption optionSync = MessageOption::TF_SYNC;
56 MessageOption optionAsync = MessageOption::TF_ASYNC;
57 // Sync
58 data.WriteInterfaceToken(CO_AUTH_INTERFACE_TOKEN);
59 data.WriteBuffer(rawData, size);
60 data.RewindRead(0);
61 (void)coAuthService.OnRemoteRequest(code, data, reply, optionSync);
62 // Async
63 data.WriteInterfaceToken(CO_AUTH_INTERFACE_TOKEN);
64 data.WriteBuffer(rawData, size);
65 data.RewindRead(0);
66 (void)coAuthService.OnRemoteRequest(code, data, reply, optionAsync);
67 }
68 return true;
69 }
70
FuzzExecutorMessengerStub(const uint8_t * rawData,size_t size)71 bool FuzzExecutorMessengerStub(const uint8_t *rawData, size_t size)
72 {
73 IAM_LOGI("start");
74 if (rawData == nullptr) {
75 return false;
76 }
77
78 sptr<ExecutorMessengerService> executorMessengerService = ExecutorMessengerService::GetInstance();
79
80 if (executorMessengerService == nullptr) {
81 IAM_LOGE("executor messenger service is null");
82 return false;
83 }
84
85 for (uint32_t code = EXECUTOR_MESSENGER_CODE_MIN; code < EXECUTOR_MESSENGER_CODE_MAX; code++) {
86 MessageParcel data;
87 MessageParcel reply;
88 MessageOption optionSync = MessageOption::TF_SYNC;
89 MessageOption optionAsync = MessageOption::TF_ASYNC;
90 // Sync
91 data.WriteInterfaceToken(EXECUTOR_MESSENGER_INTERFACE_TOKEN);
92 data.WriteBuffer(rawData, size);
93 data.RewindRead(0);
94 (void)executorMessengerService->OnRemoteRequest(code, data, reply, optionSync);
95 // Async
96 data.WriteInterfaceToken(EXECUTOR_MESSENGER_INTERFACE_TOKEN);
97 data.WriteBuffer(rawData, size);
98 data.RewindRead(0);
99 (void)executorMessengerService->OnRemoteRequest(code, data, reply, optionAsync);
100 }
101 return true;
102 }
103
CoAuthStubFuzzTest(const uint8_t * data,size_t size)104 void CoAuthStubFuzzTest(const uint8_t *data, size_t size)
105 {
106 FuzzCoAuthStub(data, size);
107 FuzzExecutorMessengerStub(data, size);
108 return;
109 }
110 } // namespace
111 } // namespace UserAuth
112 } // namespace UserIam
113 } // namespace OHOS
114
115 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)116 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
117 {
118 /* Run your code on data */
119 OHOS::UserIam::UserAuth::CoAuthStubFuzzTest(data, size);
120 return 0;
121 }
122