1 /*
2 * Copyright (c) 2024-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 "getpropertybycredentialidstub_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
35 namespace OHOS {
36 namespace {
37 const uint32_t TEST_VECTOR_MAX_SIZE = 102401;
38 }
39 const std::u16string IAMACCOUNT_TOKEN = u"ohos.accountfwk.IAccountIAM";
40
41 class MockGetSetPropCallback : public OHOS::AccountSA::GetSetPropCallback {
42 public:
~MockGetSetPropCallback()43 virtual ~MockGetSetPropCallback() {}
OnResult(int32_t result,const Attributes & extraInfo)44 void OnResult(int32_t result, const Attributes &extraInfo) override
45 {
46 return;
47 }
48 };
49
GetPropertyByCredentialIdStubFuzzTest(const uint8_t * data,size_t size)50 bool GetPropertyByCredentialIdStubFuzzTest(const uint8_t *data, size_t size)
51 {
52 if ((data == nullptr) || (size == 0)) {
53 return false;
54 }
55 FuzzData fuzzData(data, size);
56 uint64_t credentialId = fuzzData.GetData<uint64_t>();
57 std::vector<int32_t> keys = { fuzzData.GetData<int32_t>() };
58 std::shared_ptr<GetSetPropCallback> ptr = make_shared<MockGetSetPropCallback>();
59 sptr<IGetSetPropCallback> callback = new (std::nothrow) GetSetPropCallbackService(ptr);
60
61 MessageParcel dataTemp;
62 if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
63 return false;
64 }
65 if (!dataTemp.WriteUint64(credentialId)) {
66 return false;
67 }
68 int32_t vecSize = fuzzData.GenerateBool() ? TEST_VECTOR_MAX_SIZE : keys.size();
69 if (!dataTemp.WriteInt32(vecSize)) {
70 return false;
71 }
72 for (auto it = keys.begin(); it != keys.end(); ++it) {
73 if (!dataTemp.WriteInt32((*it))) {
74 return false;
75 }
76 }
77 if (fuzzData.GenerateBool()) {
78 if (!dataTemp.WriteRemoteObject(callback->AsObject())) {
79 return false;
80 }
81 }
82
83 MessageParcel reply;
84 MessageOption option;
85 uint32_t code = static_cast<uint32_t>(IAccountIAMIpcCode::COMMAND_GET_PROPERTY_BY_CREDENTIAL_ID);
86 auto iamAccountManagerService = std::make_shared<AccountIAMService>();
87 iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
88
89 return true;
90 }
91 } // namespace OHOS
92
NativeTokenGet()93 void NativeTokenGet()
94 {
95 uint64_t tokenId;
96 const char **perms = new const char *[1];
97 perms[0] = "ohos.permission.ACCESS_USER_AUTH_INTERNAL";
98 NativeTokenInfoParams infoInstance = {
99 .dcapsNum = 0,
100 .permsNum = 1,
101 .aclsNum = 0,
102 .perms = perms,
103 .acls = nullptr,
104 .aplStr = "system_core",
105 };
106 infoInstance.processName = "GET_PROPERTY";
107 tokenId = GetAccessTokenId(&infoInstance);
108 SetSelfTokenID(tokenId);
109 AccessTokenKit::ReloadNativeTokenInfo();
110 delete[] perms;
111 }
112
113 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)114 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
115 {
116 NativeTokenGet();
117 return 0;
118 }
119
120 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)121 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
122 {
123 /* Run your code on data */
124 OHOS::GetPropertyByCredentialIdStubFuzzTest(data, size);
125 return 0;
126 }
127