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 "suspendsensors_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_errors.h"
25
26 #undef LOG_TAG
27 #define LOG_TAG "SuspendSensorsFuzzTest"
28
29 namespace OHOS {
30 namespace Sensors {
31 using namespace OHOS::HiviewDFX;
32 using namespace Security::AccessToken;
33 using Security::AccessToken::AccessTokenID;
34 namespace {
35 constexpr size_t DATA_MIN_SIZE = 4;
36 } // namespace
37
38 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)39 void GetObject(const uint8_t *data, size_t size, T &object)
40 {
41 size_t objectSize = sizeof(object);
42 if (objectSize > size) {
43 return;
44 }
45 memcpy_s(&object, objectSize, data, objectSize);
46 }
47
SetUpTestCase()48 void SetUpTestCase()
49 {
50 const char **perms = new (std::nothrow) const char *[2];
51 CHKPV(perms);
52 perms[0] = "ohos.permission.ACCELEROMETER";
53 perms[1] = "ohos.permission.MANAGE_SENSOR";
54 TokenInfoParams infoInstance = {
55 .dcapsNum = 0,
56 .permsNum = 2,
57 .aclsNum = 0,
58 .dcaps = nullptr,
59 .perms = perms,
60 .acls = nullptr,
61 .processName = "SuspendSensorsFuzzTest",
62 .aplStr = "system_core",
63 };
64 uint64_t tokenId = GetAccessTokenId(&infoInstance);
65 SetSelfTokenID(tokenId);
66 AccessTokenKit::ReloadNativeTokenInfo();
67 delete[] perms;
68 }
69
SuspendSensorsFuzzTest(const uint8_t * data,size_t size)70 void SuspendSensorsFuzzTest(const uint8_t *data, size_t size)
71 {
72 if (data == nullptr || size < DATA_MIN_SIZE) {
73 return;
74 }
75 SetUpTestCase();
76 int32_t pid { -1 };
77 GetObject<int32_t>(data, size, pid);
78 SuspendSensors(pid);
79 }
80 } // namespace Sensors
81 } // namespace OHOS
82
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
84 {
85 OHOS::Sensors::SuspendSensorsFuzzTest(data, size);
86 return 0;
87 }
88