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