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 "getpropertystub_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 MockGetSetPropCallback : public OHOS::AccountSA::GetSetPropCallback {
38 public:
~MockGetSetPropCallback()39 virtual ~MockGetSetPropCallback() {}
OnResult(int32_t result,const Attributes & extraInfo)40 void OnResult(int32_t result, const Attributes &extraInfo) override
41 {
42 return;
43 }
44 };
45
GetPropertyStubFuzzTest(const uint8_t * data,size_t size)46 bool GetPropertyStubFuzzTest(const uint8_t *data, size_t size)
47 {
48 if ((data == nullptr) || (size == 0)) {
49 return false;
50 }
51 FuzzData fuzzData(data, size);
52 int32_t userId = fuzzData.GetData<int32_t>();
53 AuthType authType = static_cast<AuthType>(fuzzData.GenerateEnmu(IAMAuthType::TYPE_END));
54 std::vector<Attributes::AttributeKey> keys = {
55 fuzzData.GenerateEnmu(Attributes::AttributeKey::ATTR_AUTH_INTENTION)
56 };
57 GetPropertyRequest request = {
58 .authType = authType,
59 .keys = keys,
60 };
61 GetPropertyRequestIam requestIam;
62 requestIam.getPropertyRequest = request;
63 std::shared_ptr<GetSetPropCallback> ptr = make_shared<MockGetSetPropCallback>();
64 sptr<IGetSetPropCallback> callback = new (std::nothrow) GetSetPropCallbackService(ptr);
65
66 MessageParcel dataTemp;
67 if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
68 return false;
69 }
70 if (!dataTemp.WriteInt32(userId)) {
71 return false;
72 }
73 if (fuzzData.GenerateBool()) {
74 if (!dataTemp.WriteParcelable(&requestIam)) {
75 return false;
76 }
77 }
78 if (fuzzData.GenerateBool()) {
79 if (!dataTemp.WriteRemoteObject(callback->AsObject())) {
80 return false;
81 }
82 }
83 MessageParcel reply;
84 MessageOption option;
85 uint32_t code = static_cast<uint32_t>(IAccountIAMIpcCode::COMMAND_GET_PROPERTY);
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::GetPropertyStubFuzzTest(data, size);
125 return 0;
126 }
127