1 /*
2 * Copyright (c) 2023-2025 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 "getactivesensorinfos_fuzzer.h"
17
18 #include "accesstoken_kit.h"
19 #include "nativetoken_kit.h"
20 #include "securec.h"
21 #include "token_setproc.h"
22
23 #include "sensor_agent.h"
24 #include "sensor_agent_type.h"
25 #include "sensor_errors.h"
26 #include "sensor.h"
27 #include "sensor_service_client.h"
28
29 #undef LOG_TAG
30 #define LOG_TAG "GetActiveSensorInfosFuzzTest"
31 #define SEN_CLIENT SensorServiceClient::GetInstance()
32
33 namespace OHOS {
34 namespace Sensors {
35 using namespace OHOS::HiviewDFX;
36 using namespace Security::AccessToken;
37 using Security::AccessToken::AccessTokenID;
38 namespace {
39 constexpr size_t DATA_MIN_SIZE = 4;
40 } // namespace
41
42 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)43 void GetObject(const uint8_t *data, size_t size, T &object)
44 {
45 size_t objectSize = sizeof(object);
46 if (objectSize > size) {
47 return;
48 }
49 memcpy_s(&object, objectSize, data, objectSize);
50 }
51
SetUpTestCase()52 void SetUpTestCase()
53 {
54 const char **perms = new (std::nothrow) const char *[2];
55 CHKPV(perms);
56 perms[0] = "ohos.permission.ACCELEROMETER";
57 perms[1] = "ohos.permission.MANAGE_SENSOR";
58 TokenInfoParams infoInstance = {
59 .dcapsNum = 0,
60 .permsNum = 2,
61 .aclsNum = 0,
62 .dcaps = nullptr,
63 .perms = perms,
64 .acls = nullptr,
65 .processName = "GetActiveSensorInfosFuzzTest",
66 .aplStr = "system_core",
67 };
68 uint64_t tokenId = GetAccessTokenId(&infoInstance);
69 SetSelfTokenID(tokenId);
70 AccessTokenKit::ReloadNativeTokenInfo();
71 delete[] perms;
72 }
73
GetActiveSensorInfosFuzzTest(const uint8_t * data,size_t size)74 void GetActiveSensorInfosFuzzTest(const uint8_t *data, size_t size)
75 {
76 if (data == nullptr || size < DATA_MIN_SIZE) {
77 return;
78 }
79 SetUpTestCase();
80 int32_t pid { 0 };
81 GetObject<int32_t>(data, size, pid);
82 SensorActiveInfo *sensorActiveInfos { nullptr };
83 int32_t count { 0 };
84 GetActiveSensorInfos(pid, &sensorActiveInfos, &count);
85 std::vector<ActiveInfo> sensorActiveInfoList;
86 SEN_CLIENT.GetActiveInfoList(pid, sensorActiveInfoList);
87 }
88 } // namespace Sensors
89 } // namespace OHOS
90
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
92 {
93 OHOS::Sensors::GetActiveSensorInfosFuzzTest(data, size);
94 return 0;
95 }
96