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