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 "setpropertystub_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
46
SetPropertyStubFuzzTest(const uint8_t * data,size_t size)47 bool SetPropertyStubFuzzTest(const uint8_t *data, size_t size)
48 {
49 if ((data == nullptr) || (size == 0)) {
50 return false;
51 }
52 FuzzData fuzzData(data, size);
53 int32_t userId = fuzzData.GetData<int32_t>();
54 std::vector<uint8_t> attr = {fuzzData.GetData<uint8_t>()};
55 SetPropertyRequestIam requestIam;
56 requestIam.setPropertyRequest.authType = static_cast<AuthType>(fuzzData.GenerateEnmu(IAMAuthType::TYPE_END));
57 requestIam.setPropertyRequest.mode = fuzzData.GenerateEnmu(PropertyMode::PROPERTY_MODE_NOTIFY_COLLECTOR_READY);
58 requestIam.setPropertyRequest.attrs = Attributes(attr);
59 std::shared_ptr<GetSetPropCallback> ptr = make_shared<MockGetSetPropCallback>();
60 sptr<IGetSetPropCallback> callback = new (std::nothrow) GetSetPropCallbackService(ptr);
61
62 MessageParcel dataTemp;
63 if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
64 return false;
65 }
66 if (!dataTemp.WriteInt32(userId)) {
67 return false;
68 }
69 if (fuzzData.GenerateBool()) {
70 if (!dataTemp.WriteParcelable(&requestIam)) {
71 return false;
72 }
73 }
74 if (fuzzData.GenerateBool()) {
75 if (!dataTemp.WriteRemoteObject(callback->AsObject())) {
76 return false;
77 }
78 }
79 MessageParcel reply;
80 MessageOption option;
81 uint32_t code = static_cast<uint32_t>(IAccountIAMIpcCode::COMMAND_SET_PROPERTY);
82 auto iamAccountManagerService = std::make_shared<AccountIAMService>();
83 iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
84
85 return true;
86 }
87 } // namespace OHOS
88
NativeTokenGet()89 void NativeTokenGet()
90 {
91 uint64_t tokenId;
92 const char **perms = new const char *[1];
93 perms[0] = "ohos.permission.ACCESS_USER_AUTH_INTERNAL";
94 NativeTokenInfoParams infoInstance = {
95 .dcapsNum = 0,
96 .permsNum = 1,
97 .aclsNum = 0,
98 .perms = perms,
99 .acls = nullptr,
100 .aplStr = "system_core",
101 };
102 infoInstance.processName = "SET_PROPERTY";
103 tokenId = GetAccessTokenId(&infoInstance);
104 SetSelfTokenID(tokenId);
105 AccessTokenKit::ReloadNativeTokenInfo();
106 delete [] perms;
107 }
108
109 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)110 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
111 {
112 NativeTokenGet();
113 return 0;
114 }
115
116 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)117 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
118 {
119 /* Run your code on data */
120 OHOS::SetPropertyStubFuzzTest(data, size);
121 return 0;
122 }
123