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 "transferclientremoteobjectstub_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "accesstoken_kit.h"
22 #include "message_parcel.h"
23 #include "nativetoken_kit.h"
24 #include "securec.h"
25 #include "token_setproc.h"
26
27 #include "sensor.h"
28 #include "sensor_service.h"
29
30 namespace OHOS {
31 namespace Sensors {
32 using namespace Security::AccessToken;
33 using Security::AccessToken::AccessTokenID;
34 namespace {
35 constexpr size_t U32_AT_SIZE = 4;
36 auto g_service = SensorDelayedSpSingleton<SensorService>::GetInstance();
37 const std::u16string SENSOR_INTERFACE_TOKEN = u"OHOS.Sensors.ISensorService";
38 static sptr<IRemoteObject> g_remote = new (std::nothrow) IPCObjectStub();
39 } // namespace
40
41 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)42 size_t GetObject(T &object, const uint8_t *data, size_t size)
43 {
44 size_t objectSize = sizeof(object);
45 if (objectSize > size) {
46 return 0;
47 }
48 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
49 if (ret != EOK) {
50 return 0;
51 }
52 return objectSize;
53 }
54
SetUpTestCase()55 void SetUpTestCase()
56 {
57 const char **perms = new (std::nothrow) const char *[2];
58 if (perms == nullptr) {
59 return;
60 }
61 perms[0] = "ohos.permission.ACCELEROMETER";
62 perms[1] = "ohos.permission.MANAGE_SENSOR";
63 TokenInfoParams infoInstance = {
64 .dcapsNum = 0,
65 .permsNum = 2,
66 .aclsNum = 0,
67 .dcaps = nullptr,
68 .perms = perms,
69 .acls = nullptr,
70 .processName = "TransferClientRemoteObjectStubFuzzTest",
71 .aplStr = "system_core",
72 };
73 uint64_t tokenId = GetAccessTokenId(&infoInstance);
74 SetSelfTokenID(tokenId);
75 AccessTokenKit::ReloadNativeTokenInfo();
76 delete[] perms;
77 }
78
OnRemoteRequestFuzzTest(const uint8_t * data,size_t size)79 bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size)
80 {
81 SetUpTestCase();
82 if (g_remote == nullptr || g_service == nullptr) {
83 return false;
84 }
85 MessageParcel datas;
86 datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN);
87 datas.WriteRemoteObject(g_remote);
88 int32_t pid = 0;
89 GetObject<int32_t>(pid, data, size);
90 datas.WriteInt32(pid);
91 datas.RewindRead(0);
92 MessageParcel reply;
93 MessageOption option;
94 g_service->OnRemoteRequest(static_cast<uint32_t>(ISensorServiceIpcCode::COMMAND_TRANSFER_CLIENT_REMOTE_OBJECT),
95 datas, reply, option);
96 return true;
97 }
98 } // namespace Sensors
99 } // namespace OHOS
100
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)101 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
102 {
103 /* Run your code on data */
104 if (data == nullptr) {
105 return 0;
106 }
107
108 /* Validate the length of size */
109 if (size < OHOS::Sensors::U32_AT_SIZE) {
110 return 0;
111 }
112
113 OHOS::Sensors::OnRemoteRequestFuzzTest(data, size);
114 return 0;
115 }
116