1 /*
2 * Copyright (c) 2025 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 "getenrolledidstub_fuzzer.h"
17
18 #include <string>
19 #include <vector>
20 #include "access_token.h"
21 #include "access_token_error.h"
22 #include "accesstoken_kit.h"
23 #include "account_iam_callback_service.h"
24 #include "account_iam_service.h"
25 #include "account_log_wrapper.h"
26 #include "fuzz_data.h"
27 #include "account_i_a_m_stub.h"
28 #include "securec.h"
29 #include "nativetoken_kit.h"
30 #include "token_setproc.h"
31
32 using namespace std;
33 using namespace OHOS::AccountSA;
34 using namespace OHOS::Security::AccessToken;
35 namespace OHOS {
36 namespace {
37 const uint32_t TEST_CODE = 100;
38 }
39 const std::u16string IAMACCOUNT_TOKEN = u"ohos.accountfwk.IAccountIAM";
40
41 class GetEnrolledIdCallbackTest : public GetEnrolledIdCallback {
42 public:
43 virtual ~GetEnrolledIdCallbackTest() = default;
44
OnEnrolledId(int32_t result,uint64_t enrolledId)45 void OnEnrolledId(int32_t result, uint64_t enrolledId) override
46 {
47 return;
48 }
49 };
50
GetEnrolledIdStubFuzzTest(const uint8_t * data,size_t size)51 bool GetEnrolledIdStubFuzzTest(const uint8_t *data, size_t size)
52 {
53 if ((data == nullptr) || (size == 0)) {
54 return false;
55 }
56 MessageParcel dataTemp;
57 if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
58 return false;
59 }
60 FuzzData fuzzData(data, size);
61 int32_t userId = fuzzData.GetData<int32_t>();
62 if (!dataTemp.WriteInt32(userId)) {
63 return false;
64 }
65 int32_t authType = fuzzData.GetData<int32_t>();
66 if (!dataTemp.WriteInt32(authType)) {
67 return false;
68 }
69 auto callback = std::make_shared<GetEnrolledIdCallbackTest>();
70 sptr<IGetEnrolledIdCallback> wrapper = sptr<GetEnrolledIdCallbackService>::MakeSptr(callback);
71 if (fuzzData.GenerateBool()) {
72 if (!dataTemp.WriteRemoteObject(wrapper->AsObject())) {
73 return false;
74 }
75 }
76 MessageParcel reply;
77 MessageOption option;
78 uint32_t code = static_cast<uint32_t>(IAccountIAMIpcCode::COMMAND_GET_ENROLLED_ID);
79 auto iamAccountManagerService = std::make_shared<AccountIAMService>();
80 iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
81 iamAccountManagerService->OnRemoteRequest(TEST_CODE, dataTemp, reply, option);
82
83 return true;
84 }
85 } // namespace OHOS
86
NativeTokenGet()87 void NativeTokenGet()
88 {
89 uint64_t tokenId;
90 const char **perms = new const char *[1];
91 perms[0] = "ohos.permission.USE_USER_IDM";
92 NativeTokenInfoParams infoInstance = {
93 .dcapsNum = 0,
94 .permsNum = 1,
95 .aclsNum = 0,
96 .perms = perms,
97 .acls = nullptr,
98 .aplStr = "system_core",
99 };
100 infoInstance.processName = "GET_CREDENTIAL_INFO";
101 tokenId = GetAccessTokenId(&infoInstance);
102 SetSelfTokenID(tokenId);
103 AccessTokenKit::ReloadNativeTokenInfo();
104 delete [] perms;
105 }
106
107 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)108 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
109 {
110 NativeTokenGet();
111 return 0;
112 }
113
114 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)115 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
116 {
117 /* Run your code on data */
118 OHOS::GetEnrolledIdStubFuzzTest(data, size);
119 return 0;
120 }
121