• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "user_idm_client_stub_fuzzer.h"
17 
18 #include "parcel.h"
19 
20 #include "iam_fuzz_test.h"
21 #include "iam_logger.h"
22 #include "iam_ptr.h"
23 #include "user_idm_client.h"
24 #include "user_idm_callback_service.h"
25 
26 #define LOG_TAG "USER_AUTH_SDK"
27 
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
31 namespace {
32 constexpr uint32_t IDM_CALLBACK_CODE_MIN = 0;
33 constexpr uint32_t IDM_CALLBACK_CODE_MAX = 3;
34 const std::u16string IDM_CALLBACK_INTERFACE_TOKEN = u"ohos.useridm.IIDMCallback";
35 constexpr uint32_t GET_CREDINFO_CALLBACK_CODE_MIN = 0;
36 constexpr uint32_t GET_CREDINFO_CALLBACK_CODE_MAX = 2;
37 const std::u16string GET_CREDINFO_CALLBACK_TOKEN = u"ohos.useridm.IGetInfoCallback";
38 constexpr uint32_t GET_SECURE_USERINFO_CALLBACK_CODE_MIN = 0;
39 constexpr uint32_t GET_SECURE_USERINFO_CALLBACK_CODE_MAX = 2;
40 const std::u16string GET_SECURE_USERINFO_CALLBACK_TOKEN = u"ohos.useridm.IGetSecInfoCallback";
41 class DummyUserIdmClientCallback final : public UserIdmClientCallback {
42 public:
OnAcquireInfo(int32_t module,uint32_t acquireInfo,const Attributes & extraInfo)43     void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const Attributes &extraInfo)
44     {
45         IAM_LOGI("start");
46         static_cast<void>(module);
47         static_cast<void>(acquireInfo);
48         static_cast<void>(extraInfo);
49     }
50 
OnResult(int32_t result,const Attributes & extraInfo)51     void OnResult(int32_t result, const Attributes &extraInfo)
52     {
53         IAM_LOGI("start");
54         static_cast<void>(result);
55         static_cast<void>(extraInfo);
56     }
57 };
58 
59 class DummyGetCredentialInfoCallback final : public GetCredentialInfoCallback {
60 public:
OnCredentialInfo(int32_t result,const std::vector<CredentialInfo> & infoList)61     void OnCredentialInfo(int32_t result, const std::vector<CredentialInfo> &infoList)
62     {
63         IAM_LOGI("start");
64         static_cast<void>(result);
65         static_cast<void>(infoList);
66     }
67 };
68 
69 class DummyGetSecUserInfoCallback final : public GetSecUserInfoCallback {
70 public:
OnSecUserInfo(int32_t result,const SecUserInfo & info)71     void OnSecUserInfo(int32_t result, const SecUserInfo &info)
72     {
73         IAM_LOGI("start");
74         static_cast<void>(result);
75         static_cast<void>(info);
76     }
77 };
78 
FuzzIdmCallbackStub(const uint8_t * rawData,size_t size)79 bool FuzzIdmCallbackStub(const uint8_t *rawData, size_t size)
80 {
81     IAM_LOGI("start");
82     if (rawData == nullptr) {
83         IAM_LOGI("%{public}s:rawData is null.", __func__);
84         return false;
85     }
86     auto idmCallbackService =
87         Common::MakeShared<IdmCallbackService>(Common::MakeShared<DummyUserIdmClientCallback>());
88     if (idmCallbackService == nullptr) {
89         IAM_LOGI("%{public}s:new idmCallbackService failed.", __func__);
90         return false;
91     }
92     for (uint32_t code = IDM_CALLBACK_CODE_MIN; code < IDM_CALLBACK_CODE_MAX; code++) {
93         MessageParcel data;
94         MessageParcel reply;
95         MessageOption optionSync = MessageOption::TF_SYNC;
96         MessageOption optionAsync = MessageOption::TF_ASYNC;
97         // Sync
98         data.WriteInterfaceToken(IDM_CALLBACK_INTERFACE_TOKEN);
99         data.WriteBuffer(rawData, size);
100         data.RewindRead(0);
101         (void)idmCallbackService->OnRemoteRequest(code, data, reply, optionSync);
102         // Async
103         data.WriteInterfaceToken(IDM_CALLBACK_INTERFACE_TOKEN);
104         data.WriteBuffer(rawData, size);
105         data.RewindRead(0);
106         (void)idmCallbackService->OnRemoteRequest(code, data, reply, optionAsync);
107     }
108     return true;
109 }
110 
FuzzIdmGetInfoCallbackStub(const uint8_t * rawData,size_t size)111 bool FuzzIdmGetInfoCallbackStub(const uint8_t *rawData, size_t size)
112 {
113     IAM_LOGI("start");
114     if (rawData == nullptr) {
115         IAM_LOGI("%{public}s:rawData is null.", __func__);
116         return false;
117     }
118     auto idmGetCredInfoCallbackService =
119         Common::MakeShared<IdmGetCredInfoCallbackService>(Common::MakeShared<DummyGetCredentialInfoCallback>());
120     if (idmGetCredInfoCallbackService == nullptr) {
121         IAM_LOGI("%{public}s:new idmGetCredInfoCallbackService failed.", __func__);
122         return false;
123     }
124     for (uint32_t code = GET_CREDINFO_CALLBACK_CODE_MIN; code < GET_CREDINFO_CALLBACK_CODE_MAX; code++) {
125         MessageParcel data;
126         MessageParcel reply;
127         MessageOption optionSync = MessageOption::TF_SYNC;
128         MessageOption optionAsync = MessageOption::TF_ASYNC;
129         // Sync
130         data.WriteInterfaceToken(GET_CREDINFO_CALLBACK_TOKEN);
131         data.WriteBuffer(rawData, size);
132         data.RewindRead(0);
133         (void)idmGetCredInfoCallbackService->OnRemoteRequest(code, data, reply, optionSync);
134         // Async
135         data.WriteInterfaceToken(GET_CREDINFO_CALLBACK_TOKEN);
136         data.WriteBuffer(rawData, size);
137         data.RewindRead(0);
138         (void)idmGetCredInfoCallbackService->OnRemoteRequest(code, data, reply, optionAsync);
139     }
140     return true;
141 }
142 
FuzzIdmGetSecureUserInfoCallbackstub(const uint8_t * rawData,size_t size)143 bool FuzzIdmGetSecureUserInfoCallbackstub(const uint8_t *rawData, size_t size)
144 {
145     IAM_LOGI("start");
146     if (rawData == nullptr) {
147         IAM_LOGI("%{public}s:rawData is null.", __func__);
148         return false;
149     }
150     auto idmGetSecureUserInfoCallbackService =
151         Common::MakeShared<IdmGetSecureUserInfoCallbackService>(Common::MakeShared<DummyGetSecUserInfoCallback>());
152     if (idmGetSecureUserInfoCallbackService == nullptr) {
153         IAM_LOGI("%{public}s:new idmGetSecureUserInfoCallbackService failed.", __func__);
154         return false;
155     }
156     for (uint32_t code = GET_SECURE_USERINFO_CALLBACK_CODE_MIN; code < GET_SECURE_USERINFO_CALLBACK_CODE_MAX; code++) {
157         MessageParcel data;
158         MessageParcel reply;
159         MessageOption optionSync = MessageOption::TF_SYNC;
160         MessageOption optionAsync = MessageOption::TF_ASYNC;
161         // Sync
162         data.WriteInterfaceToken(GET_SECURE_USERINFO_CALLBACK_TOKEN);
163         data.WriteBuffer(rawData, size);
164         data.RewindRead(0);
165         (void)idmGetSecureUserInfoCallbackService->OnRemoteRequest(code, data, reply, optionSync);
166         // Async
167         data.WriteInterfaceToken(GET_SECURE_USERINFO_CALLBACK_TOKEN);
168         data.WriteBuffer(rawData, size);
169         data.RewindRead(0);
170         (void)idmGetSecureUserInfoCallbackService->OnRemoteRequest(code, data, reply, optionAsync);
171     }
172     return true;
173 }
174 
UserIdmClientFuzzTest(const uint8_t * data,size_t size)175 void UserIdmClientFuzzTest(const uint8_t *data, size_t size)
176 {
177     FuzzIdmCallbackStub(data, size);
178     FuzzIdmGetInfoCallbackStub(data, size);
179     FuzzIdmGetSecureUserInfoCallbackstub(data, size);
180     return;
181 }
182 } // namespace
183 } // namespace UserAuth
184 } // namespace UserIam
185 } // namespace OHOS
186 
187 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)188 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
189 {
190     OHOS::UserIam::UserAuth::UserIdmClientFuzzTest(data, size);
191     return 0;
192 }
193