1 /*
2 * Copyright (c) 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 "hiview_service_ability_stub_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "accesstoken_kit.h"
22 #include "hiview_platform.h"
23 #include "hiview_service_ability.h"
24 #include "nativetoken_kit.h"
25 #include "token_setproc.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace {
30 constexpr int32_t OFFSET = 4;
31 bool g_isGranted = false;
32 HiviewService g_hiviewService;
33 auto hiviewServiceAbility = std::make_shared<HiviewServiceAbility>();
34
GenerateRandomCode(const uint8_t * rawData)35 uint32_t GenerateRandomCode(const uint8_t* rawData)
36 {
37 uint32_t code = *(reinterpret_cast<const uint32_t*>(rawData));
38 return code % 12 + 1001; // 12 and 1001 : remainder between 0 ~ 11, plus 1001 result in 1001 ~ 1012
39 }
40
EnablePermissionAccess()41 void EnablePermissionAccess()
42 {
43 if (g_isGranted) {
44 return;
45 }
46 const char* perms[] = {
47 "ohos.permission.WRITE_HIVIEW_SYSTEM",
48 "ohos.permission.READ_HIVIEW_SYSTEM",
49 "ohos.permission.DUMP",
50 };
51 NativeTokenInfoParams infoInstance = {
52 .dcapsNum = 0,
53 .permsNum = 3,
54 .aclsNum = 0,
55 .dcaps = nullptr,
56 .perms = perms,
57 .acls = nullptr,
58 .processName = "HiviewServiceAbilityStubFuzzTest",
59 .aplStr = "system_basic",
60 };
61 uint64_t tokenId = GetAccessTokenId(&infoInstance);
62 SetSelfTokenID(tokenId);
63 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
64 g_isGranted = true;
65 }
66 }
67
HiviewServiceAbilityStubFuzzTest(const uint8_t * rawData,size_t size)68 static void HiviewServiceAbilityStubFuzzTest(const uint8_t* rawData, size_t size)
69 {
70 HiviewPlatform platform;
71 (void)platform.IsReady();
72 EnablePermissionAccess();
73 if (size < OFFSET) {
74 return;
75 }
76 uint32_t code = GenerateRandomCode(rawData);
77
78 MessageParcel data;
79 data.WriteInterfaceToken(HiviewServiceAbilityStub::GetDescriptor());
80 data.WriteBuffer(rawData, size);
81 data.RewindRead(0);
82 MessageParcel reply;
83 MessageOption option;
84
85 hiviewServiceAbility->GetOrSetHiviewService(&g_hiviewService);
86 hiviewServiceAbility->OnRemoteRequest(code, data, reply, option);
87 }
88
89 } // namespace HiviewDFX
90 } // namespace OHOS
91
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
94 {
95 /* Run your code on data */
96 OHOS::HiviewDFX::HiviewServiceAbilityStubFuzzTest(data, size);
97 return 0;
98 }
99
100