1 /*
2 * Copyright (c) 2023 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 "cloudservicestub_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "accesstoken_kit.h"
22 #include "cloud_service_impl.h"
23 #include "ipc_skeleton.h"
24 #include "message_parcel.h"
25 #include "securec.h"
26 #include "token_setproc.h"
27
28 using namespace OHOS::CloudData;
29 using namespace OHOS::Security::AccessToken;
30
31 namespace OHOS {
32 constexpr int USER_ID = 100;
33 constexpr int INST_INDEX = 0;
34 const std::u16string INTERFACE_TOKEN = u"OHOS.CloudData.CloudServer";
35 constexpr uint32_t CODE_MIN = 0;
36 constexpr uint32_t CODE_MAX = CloudService::TransId::TRANS_BUTT + 1;
37 constexpr size_t NUM_MIN = 5;
38 constexpr size_t NUM_MAX = 12;
39
AllocAndSetHapToken()40 void AllocAndSetHapToken()
41 {
42 HapInfoParams info = {
43 .userID = USER_ID,
44 .bundleName = "ohos.test.demo",
45 .instIndex = INST_INDEX,
46 .appIDDesc = "ohos.test.demo",
47 .isSystemApp = true
48 };
49
50 HapPolicyParams policy = {
51 .apl = APL_NORMAL,
52 .domain = "test.domain",
53 .permList = {},
54 .permStateList = {
55 {
56 .permissionName = "ohos.permission.CLOUDDATA_CONFIG",
57 .isGeneral = true,
58 .resDeviceID = { "local" },
59 .grantStatus = { PermissionState::PERMISSION_GRANTED },
60 .grantFlags = { 1 }
61 }
62 }
63 };
64 auto tokenID = AccessTokenKit::AllocHapToken(info, policy);
65 SetSelfTokenID(tokenID.tokenIDEx);
66 }
67
OnRemoteRequestFuzz(const uint8_t * data,size_t size)68 bool OnRemoteRequestFuzz(const uint8_t *data, size_t size)
69 {
70 std::shared_ptr<CloudServiceImpl> cloudServiceImpl = std::make_shared<CloudServiceImpl>();
71 std::shared_ptr<ExecutorPool> executor = std::make_shared<ExecutorPool>(NUM_MAX, NUM_MIN);
72 cloudServiceImpl->OnBind(
73 { "CloudServiceStubFuzz", static_cast<uint32_t>(IPCSkeleton::GetSelfTokenID()), std::move(executor) });
74
75 AllocAndSetHapToken();
76
77 uint32_t code = static_cast<uint32_t>(*data) % (CODE_MAX - CODE_MIN + 1) + CODE_MIN;
78 MessageParcel request;
79 request.WriteInterfaceToken(INTERFACE_TOKEN);
80 request.WriteBuffer(data, size);
81 request.RewindRead(0);
82 MessageParcel reply;
83 std::shared_ptr<CloudServiceStub> cloudServiceStub = cloudServiceImpl;
84 cloudServiceStub->OnRemoteRequest(code, request, reply);
85 return true;
86 }
87 } // namespace OHOS
88
89 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
91 {
92 if (data == nullptr) {
93 return 0;
94 }
95
96 OHOS::OnRemoteRequestFuzz(data, size);
97
98 return 0;
99 }