• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "updatecredentialstub_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_client.h"
25 #include "account_iam_service.h"
26 #include "account_log_wrapper.h"
27 #include "fuzz_data.h"
28 #include "nativetoken_kit.h"
29 #include "token_setproc.h"
30 
31 using namespace std;
32 using namespace OHOS::AccountSA;
33 using namespace OHOS::Security::AccessToken;
34 namespace OHOS {
35 const std::u16string IAMACCOUNT_TOKEN = u"ohos.accountfwk.IAccountIAM";
36 
37 class MockIDMCallback : public OHOS::AccountSA::IDMCallback {
38 public:
~MockIDMCallback()39     virtual ~MockIDMCallback() {}
OnAcquireInfo(int32_t module,uint32_t acquireInfo,const Attributes & extraInfo)40     void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const Attributes &extraInfo) override
41     {
42         return;
43     }
OnResult(int32_t result,const Attributes & extraInfo)44     void OnResult(int32_t result, const Attributes &extraInfo) override
45     {
46         return;
47     }
48 };
49 
UpdateCredentialStubFuzzTest(const uint8_t * data,size_t size)50 bool UpdateCredentialStubFuzzTest(const uint8_t *data, size_t size)
51 {
52     if ((data == nullptr) || (size == 0)) {
53         return false;
54     }
55     FuzzData fuzzData(data, size);
56     int32_t userId = fuzzData.GetData<int32_t>();
57     AuthType authType = static_cast<AuthType>(fuzzData.GenerateEnmu(IAMAuthType::TYPE_END));
58     std::optional<PinSubType> pinType = {fuzzData.GenerateEnmu(PinSubType::PIN_MAX)};
59     std::vector<uint8_t> token = {fuzzData.GetData<uint8_t>()};
60     CredentialParameters credentialParameters = {
61         .authType = authType,
62         .pinType = pinType,
63         .token = token,
64     };
65     CredentialParametersIam credInfoIam;
66     credInfoIam.credentialParameters = credentialParameters;
67     std::shared_ptr<IDMCallback> ptr = make_shared<MockIDMCallback>();
68     sptr<IIDMCallback> callback = new (std::nothrow) IDMCallbackService(userId, ptr);
69     MessageParcel dataTemp;
70     if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
71         return false;
72     }
73     if (!dataTemp.WriteInt32(userId)) {
74         return false;
75     }
76     if (fuzzData.GenerateBool()) {
77         if (!dataTemp.WriteParcelable(&credInfoIam)) {
78             return false;
79         }
80     }
81     if (fuzzData.GenerateBool()) {
82         if (!dataTemp.WriteRemoteObject(callback->AsObject())) {
83             return false;
84         }
85     }
86 
87     MessageParcel reply;
88     MessageOption option;
89     uint32_t code = static_cast<uint32_t>(IAccountIAMIpcCode::COMMAND_UPDATE_CREDENTIAL);
90     auto iamAccountManagerService = std::make_shared<AccountIAMService>();
91     iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
92 
93     return true;
94 }
95 } // namespace OHOS
96 
NativeTokenGet()97 void NativeTokenGet()
98 {
99     uint64_t tokenId;
100     const char **perms = new const char *[1];
101     perms[0] = "ohos.permission.MANAGE_USER_IDM";
102     NativeTokenInfoParams infoInstance = {
103         .dcapsNum = 0,
104         .permsNum = 1,
105         .aclsNum = 0,
106         .perms = perms,
107         .acls = nullptr,
108         .aplStr = "system_core",
109     };
110     infoInstance.processName = "UPDATE_CREDENTIAL";
111     tokenId = GetAccessTokenId(&infoInstance);
112     SetSelfTokenID(tokenId);
113     AccessTokenKit::ReloadNativeTokenInfo();
114     delete [] perms;
115 }
116 
117 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)118 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
119 {
120     NativeTokenGet();
121     return 0;
122 }
123 
124 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)125 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
126 {
127     /* Run your code on data */
128     OHOS::UpdateCredentialStubFuzzTest(data, size);
129     return 0;
130 }
131