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