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