• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "createdatachannelstub_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 
SetUpTestCase()41 void SetUpTestCase()
42 {
43     const char **perms = new (std::nothrow) const char *[2];
44     if (perms == nullptr) {
45         return;
46     }
47     perms[0] = "ohos.permission.ACCELEROMETER";
48     perms[1] = "ohos.permission.MANAGE_SENSOR";
49     TokenInfoParams infoInstance = {
50         .dcapsNum = 0,
51         .permsNum = 2,
52         .aclsNum = 0,
53         .dcaps = nullptr,
54         .perms = perms,
55         .acls = nullptr,
56         .processName = "CreateDataChannelStubFuzzTest",
57         .aplStr = "system_core",
58     };
59     uint64_t tokenId = GetAccessTokenId(&infoInstance);
60     SetSelfTokenID(tokenId);
61     AccessTokenKit::ReloadNativeTokenInfo();
62     delete[] perms;
63 }
64 
65 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)66 size_t GetObject(T &object, const uint8_t *data, size_t size)
67 {
68     size_t objectSize = sizeof(object);
69     if (objectSize > size) {
70         return 0;
71     }
72     errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
73     if (ret != EOK) {
74         return 0;
75     }
76     return objectSize;
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     int32_t fd = 0;
88     GetObject<int32_t>(fd, data, size);
89     datas.WriteFileDescriptor(fd);
90     datas.WriteRemoteObject(g_remote);
91     datas.RewindRead(0);
92     MessageParcel reply;
93     MessageOption option;
94     g_service->OnRemoteRequest(static_cast<uint32_t>(ISensorServiceIpcCode::COMMAND_TRANSFER_DATA_CHANNEL),
95         datas, reply, option);
96     datas.RewindRead(0);
97     g_service->OnRemoteRequest(static_cast<uint32_t>(ISensorServiceIpcCode::COMMAND_DESTROY_SENSOR_CHANNEL),
98         datas, reply, option);
99     datas.RewindRead(0);
100     g_service->OnRemoteRequest(static_cast<uint32_t>(ISensorServiceIpcCode::COMMAND_CREATE_SOCKET_CHANNEL),
101         datas, reply, option);
102     datas.RewindRead(0);
103     g_service->OnRemoteRequest(static_cast<uint32_t>(ISensorServiceIpcCode::COMMAND_DESTROY_SOCKET_CHANNEL),
104         datas, reply, option);
105     return true;
106 }
107 } // namespace Sensors
108 } // namespace OHOS
109 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)110 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
111 {
112     /* Run your code on data */
113     if (data == nullptr) {
114         return 0;
115     }
116 
117     /* Validate the length of size */
118     if (size < OHOS::Sensors::U32_AT_SIZE) {
119         return 0;
120     }
121 
122     OHOS::Sensors::OnRemoteRequestFuzzTest(data, size);
123     return 0;
124 }
125