diff --git a/frameworks/native/sensor/include/sensors_ipc_interface_code.h b/frameworks/native/sensor/include/sensors_ipc_interface_code.h index 5cdd98609f4f06e470d459e6642d64d613fb9464..70c1e4e7e6bcfdae7124f45b582e997d06dfc5c3 100644 --- a/frameworks/native/sensor/include/sensors_ipc_interface_code.h +++ b/frameworks/native/sensor/include/sensors_ipc_interface_code.h @@ -22,8 +22,6 @@ namespace Sensors { enum class SensorInterfaceCode { ENABLE_SENSOR = 0, DISABLE_SENSOR, - GET_SENSOR_STATE, - RUN_COMMAND, GET_SENSOR_LIST, TRANSFER_DATA_CHANNEL, DESTROY_SENSOR_CHANNEL, diff --git a/services/sensor/test/fuzztest/BUILD.gn b/services/sensor/test/fuzztest/BUILD.gn index 8ea5c5644cde50af077e2fa7c28e9a072317efc9..164a12a5c02798c7ad2df6d1133ed84140aeadb4 100644 --- a/services/sensor/test/fuzztest/BUILD.gn +++ b/services/sensor/test/fuzztest/BUILD.gn @@ -17,5 +17,20 @@ import("//build/test.gni") group("fuzztest") { testonly = true - deps = [ "sensoronremoterequest_fuzzer:fuzztest" ] + deps = [ + "createdatachannelstub_fuzzer:fuzztest", + "createsocketchannelstub_fuzzer:fuzztest", + "destroydatachannelstub_fuzzer:fuzztest", + "destroysocketchannelstub_fuzzer:fuzztest", + "disableactiveinfocbstub_fuzzer:fuzztest", + "enableactiveinfocbstub_fuzzer:fuzztest", + "getactiveinfoliststub_fuzzer:fuzztest", + "getallsensorsstub_fuzzer:fuzztest", + "resetsensorsstub_fuzzer:fuzztest", + "resumesensorsstub_fuzzer:fuzztest", + "resumesensorsstub_fuzzer:fuzztest", + "sensorenablestub_fuzzer:fuzztest", + "sensoronremoterequest_fuzzer:fuzztest", + "suspendsensorsstub_fuzzer:fuzztest", + ] } diff --git a/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..b87d3fe218db7f8d6e04547dafb1810330ea5ad9 --- /dev/null +++ b/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/BUILD.gn @@ -0,0 +1,68 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("./../../../../../sensor.gni") + +ohos_fuzztest("CreateDataChannelStubFuzzTest") { + module_out_path = "sensors/sensor" + + fuzz_config_file = "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/createdatachannelstub_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/createdatachannelstub_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "createdatachannelstub_fuzzer.cpp" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/services/sensor:libsensor_service_static", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":CreateDataChannelStubFuzzTest", + ] +} diff --git a/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/corpus/init b/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/createdatachannelstub_fuzzer.cpp b/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/createdatachannelstub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c57a38870f21a714fd09447108ccf9ec59da266 --- /dev/null +++ b/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/createdatachannelstub_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "createdatachannelstub_fuzzer.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor.h" +#include "sensor_service.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t U32_AT_SIZE = 4; +auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); +const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "CreateDataChannelStubFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + MessageParcel datas; + datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + g_sensorService->OnRemoteRequest(static_cast(SensorInterfaceCode::TRANSFER_DATA_CHANNEL), + datas, reply, option); + return true; +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + /* Validate the length of size */ + if (size < OHOS::Sensors::U32_AT_SIZE) { + return 0; + } + + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); + return 0; +} diff --git a/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/createdatachannelstub_fuzzer.h b/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/createdatachannelstub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..64fb5a4203fbf630c3ba73749d7179853593c918 --- /dev/null +++ b/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/createdatachannelstub_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CREATE_DATA_CHANNEL_STUB_FUZZER_H +#define CREATE_DATA_CHANNEL_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "createdatachannelstub_fuzzer" + +#endif // CREATE_DATA_CHANNEL_STUB_FUZZER_H + diff --git a/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/project.xml b/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..53811f014e17dd5a94f7b3865facfc2aeb6ed19e --- /dev/null +++ b/services/sensor/test/fuzztest/createdatachannelstub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..756ac6213e236e3413daa0a01512e09e878dc51a --- /dev/null +++ b/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/BUILD.gn @@ -0,0 +1,68 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("./../../../../../sensor.gni") + +ohos_fuzztest("CreateSocketChannelStubFuzzTest") { + module_out_path = "sensors/sensor" + + fuzz_config_file = "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "createsocketchannelstub_fuzzer.cpp" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/services/sensor:libsensor_service_static", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":CreateSocketChannelStubFuzzTest", + ] +} diff --git a/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/corpus/init b/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/createsocketchannelstub_fuzzer.cpp b/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/createsocketchannelstub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb0d3ab268384a6595f66d175542d93022bebd8f --- /dev/null +++ b/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/createsocketchannelstub_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "createsocketchannelstub_fuzzer.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor.h" +#include "sensor_service.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t U32_AT_SIZE = 4; +auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); +const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "CreateSocketChannelStubFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + MessageParcel datas; + datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + g_sensorService->OnRemoteRequest(static_cast(SensorInterfaceCode::CREATE_SOCKET_CHANNEL), + datas, reply, option); + return true; +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + /* Validate the length of size */ + if (size < OHOS::Sensors::U32_AT_SIZE) { + return 0; + } + + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); + return 0; +} diff --git a/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/createsocketchannelstub_fuzzer.h b/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/createsocketchannelstub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..b83e68b575e8fd1696d898517bef926f06dbf9b3 --- /dev/null +++ b/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/createsocketchannelstub_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CREATE_SOCKET_CHANNEL_STUB_FUZZER_H +#define CREATE_SOCKET_CHANNEL_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "createsocketchannelstub_fuzzer" + +#endif // CREATE_SOCKET_CHANNEL_STUB_FUZZER_H + diff --git a/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/project.xml b/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..53811f014e17dd5a94f7b3865facfc2aeb6ed19e --- /dev/null +++ b/services/sensor/test/fuzztest/createsocketchannelstub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4455fe2a51944df5da6077a8bc758b769687297b --- /dev/null +++ b/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/BUILD.gn @@ -0,0 +1,68 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("./../../../../../sensor.gni") + +ohos_fuzztest("DestroyDataChannelStubFuzzTest") { + module_out_path = "sensors/sensor" + + fuzz_config_file = "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "destroydatachannelstub_fuzzer.cpp" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/services/sensor:libsensor_service_static", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":DestroyDataChannelStubFuzzTest", + ] +} diff --git a/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/corpus/init b/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/destroydatachannelstub_fuzzer.cpp b/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/destroydatachannelstub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91e0ed15389e6fe07f3887af0026679b00ae4df5 --- /dev/null +++ b/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/destroydatachannelstub_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "destroydatachannelstub_fuzzer.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor.h" +#include "sensor_service.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t U32_AT_SIZE = 4; +auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); +const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "DestroyDataChannelStubFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + MessageParcel datas; + datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + g_sensorService->OnRemoteRequest(static_cast(SensorInterfaceCode::DESTROY_SENSOR_CHANNEL), + datas, reply, option); + return true; +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + /* Validate the length of size */ + if (size < OHOS::Sensors::U32_AT_SIZE) { + return 0; + } + + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); + return 0; +} diff --git a/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/destroydatachannelstub_fuzzer.h b/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/destroydatachannelstub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..966eb49d74f188a325249029f985319ec0f32857 --- /dev/null +++ b/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/destroydatachannelstub_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DESTROY_DATA_CHANNEL_STUB_FUZZER_H +#define DESTROY_DATA_CHANNEL_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "destroydatachannelstub_fuzzer" + +#endif // DESTROY_DATA_CHANNEL_STUB_FUZZER_H + diff --git a/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/project.xml b/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..53811f014e17dd5a94f7b3865facfc2aeb6ed19e --- /dev/null +++ b/services/sensor/test/fuzztest/destroydatachannelstub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4753365d78a6bbf48ed7fe880347fe9c5a297809 --- /dev/null +++ b/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/BUILD.gn @@ -0,0 +1,68 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("./../../../../../sensor.gni") + +ohos_fuzztest("DestroySocketChannelStubFuzzTest") { + module_out_path = "sensors/sensor" + + fuzz_config_file = "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "destroysocketchannelstub_fuzzer.cpp" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/services/sensor:libsensor_service_static", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":DestroySocketChannelStubFuzzTest", + ] +} diff --git a/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/corpus/init b/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/destroysocketchannelstub_fuzzer.cpp b/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/destroysocketchannelstub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49148244373bcd06d8a0037988484b49d7183423 --- /dev/null +++ b/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/destroysocketchannelstub_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "destroysocketchannelstub_fuzzer.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor.h" +#include "sensor_service.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t U32_AT_SIZE = 4; +auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); +const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "DestroySocketChannelStubFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + MessageParcel datas; + datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + g_sensorService->OnRemoteRequest(static_cast(SensorInterfaceCode::DESTROY_SOCKET_CHANNEL), + datas, reply, option); + return true; +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + /* Validate the length of size */ + if (size < OHOS::Sensors::U32_AT_SIZE) { + return 0; + } + + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); + return 0; +} diff --git a/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/destroysocketchannelstub_fuzzer.h b/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/destroysocketchannelstub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..6cd6dcb2d0a76a96761ba6f176067c456fa84f50 --- /dev/null +++ b/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/destroysocketchannelstub_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DESTROY_SOCKET_CHANNEL_STUB_FUZZER_H +#define DESTROY_SOCKET_CHANNEL_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "destroysocketchannelstub_fuzzer" + +#endif // DESTROY_SOCKET_CHANNEL_STUB_FUZZER_H + diff --git a/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/project.xml b/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..53811f014e17dd5a94f7b3865facfc2aeb6ed19e --- /dev/null +++ b/services/sensor/test/fuzztest/destroysocketchannelstub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..a1323772f7982a1f0d755cf15a0d1e39d8f10080 --- /dev/null +++ b/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/BUILD.gn @@ -0,0 +1,68 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("./../../../../../sensor.gni") + +ohos_fuzztest("DisableActiveInfoCBStubFuzzTest") { + module_out_path = "sensors/sensor" + + fuzz_config_file = "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "disableactiveinfocbstub_fuzzer.cpp" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/services/sensor:libsensor_service_static", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":DisableActiveInfoCBStubFuzzTest", + ] +} diff --git a/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/corpus/init b/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/disableactiveinfocbstub_fuzzer.cpp b/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/disableactiveinfocbstub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef40f222d4bf0fcf4e9c8dce2eb64e7b3bdf665f --- /dev/null +++ b/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/disableactiveinfocbstub_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "disableactiveinfocbstub_fuzzer.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor.h" +#include "sensor_service.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t U32_AT_SIZE = 4; +auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); +const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "DisableActiveInfoCBStubFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + MessageParcel datas; + datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + g_sensorService->OnRemoteRequest(static_cast(SensorInterfaceCode::DISABLE_ACTIVE_INFO_CB), + datas, reply, option); + return true; +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + /* Validate the length of size */ + if (size < OHOS::Sensors::U32_AT_SIZE) { + return 0; + } + + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); + return 0; +} diff --git a/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/disableactiveinfocbstub_fuzzer.h b/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/disableactiveinfocbstub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..9759dab7493fc7ad2d7be086926cab5a9feb4a07 --- /dev/null +++ b/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/disableactiveinfocbstub_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISABLE_ACTIVE_INFO_CB_STUB_FUZZER_H +#define DISABLE_ACTIVE_INFO_CB_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "disableactiveinfocbstub_fuzzer" + +#endif // DISABLE_ACTIVE_INFO_CB_STUB_FUZZER_H + diff --git a/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/project.xml b/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..53811f014e17dd5a94f7b3865facfc2aeb6ed19e --- /dev/null +++ b/services/sensor/test/fuzztest/disableactiveinfocbstub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..aaf34c409cdc71a64106b81111bd815d3fd711e9 --- /dev/null +++ b/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/BUILD.gn @@ -0,0 +1,68 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("./../../../../../sensor.gni") + +ohos_fuzztest("EnableActiveInfoCBStubFuzzTest") { + module_out_path = "sensors/sensor" + + fuzz_config_file = "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "enableactiveinfocbstub_fuzzer.cpp" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/services/sensor:libsensor_service_static", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":EnableActiveInfoCBStubFuzzTest", + ] +} diff --git a/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/corpus/init b/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/enableactiveinfocbstub_fuzzer.cpp b/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/enableactiveinfocbstub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf0a2f4ae5d1df456d6d606c280bd84b35c32fcf --- /dev/null +++ b/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/enableactiveinfocbstub_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "enableactiveinfocbstub_fuzzer.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor.h" +#include "sensor_service.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t U32_AT_SIZE = 4; +auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); +const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "EnableActiveInfoCBStubFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + MessageParcel datas; + datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + g_sensorService->OnRemoteRequest(static_cast(SensorInterfaceCode::ENABLE_ACTIVE_INFO_CB), + datas, reply, option); + return true; +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + /* Validate the length of size */ + if (size < OHOS::Sensors::U32_AT_SIZE) { + return 0; + } + + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); + return 0; +} diff --git a/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/enableactiveinfocbstub_fuzzer.h b/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/enableactiveinfocbstub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..3bbf6bcd00ced3b4c94cbb70c83c407b9a2b49b8 --- /dev/null +++ b/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/enableactiveinfocbstub_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ENABLE_ACTIVE_INFO_CB_STUB_FUZZER_H +#define ENABLE_ACTIVE_INFO_CB_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "enableactiveinfocbstub_fuzzer" + +#endif // ENABLE_ACTIVE_INFO_CB_STUB_FUZZER_H + diff --git a/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/project.xml b/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..53811f014e17dd5a94f7b3865facfc2aeb6ed19e --- /dev/null +++ b/services/sensor/test/fuzztest/enableactiveinfocbstub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..fa3f4a604830e613e47ab017ea44dbfef510c8d4 --- /dev/null +++ b/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/BUILD.gn @@ -0,0 +1,68 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("./../../../../../sensor.gni") + +ohos_fuzztest("GetActiveInfoListStubFuzzTest") { + module_out_path = "sensors/sensor" + + fuzz_config_file = "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "getactiveinfoliststub_fuzzer.cpp" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/services/sensor:libsensor_service_static", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":GetActiveInfoListStubFuzzTest", + ] +} diff --git a/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/corpus/init b/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/getactiveinfoliststub_fuzzer.cpp b/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/getactiveinfoliststub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8095c369805bdb398c7615daaec68f19e4ba3aa0 --- /dev/null +++ b/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/getactiveinfoliststub_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "getactiveinfoliststub_fuzzer.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor.h" +#include "sensor_service.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t U32_AT_SIZE = 4; +auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); +const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "GetActiveInfoListStubFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + MessageParcel datas; + datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + g_sensorService->OnRemoteRequest(static_cast(SensorInterfaceCode::GET_ACTIVE_INFO_LIST), + datas, reply, option); + return true; +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + /* Validate the length of size */ + if (size < OHOS::Sensors::U32_AT_SIZE) { + return 0; + } + + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); + return 0; +} diff --git a/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/getactiveinfoliststub_fuzzer.h b/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/getactiveinfoliststub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..fa2035bef403300dcfa9cc57172a933917bc93d3 --- /dev/null +++ b/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/getactiveinfoliststub_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef GET_ACTIVE_INFO_LIST_STUB_FUZZER_H +#define GET_ACTIVE_INFO_LIST_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "getactiveinfoliststub_fuzzer" + +#endif // GET_ACTIVE_INFO_LIST_STUB_FUZZER_H + diff --git a/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/project.xml b/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..53811f014e17dd5a94f7b3865facfc2aeb6ed19e --- /dev/null +++ b/services/sensor/test/fuzztest/getactiveinfoliststub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e6552ef716175da39416f67b0d1fb257da1bd416 --- /dev/null +++ b/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/BUILD.gn @@ -0,0 +1,69 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("./../../../../../sensor.gni") + +ohos_fuzztest("GetAllSensorsStubFuzzTest") { + module_out_path = "sensors/sensor" + + fuzz_config_file = + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/getallsensorsstub_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/getallsensorsstub_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "getallsensorsstub_fuzzer.cpp" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/services/sensor:libsensor_service_static", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":GetAllSensorsStubFuzzTest", + ] +} diff --git a/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/corpus/init b/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/getallsensorsstub_fuzzer.cpp b/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/getallsensorsstub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a11acbf20a3d88176bf746b240bfbe92811eb7dc --- /dev/null +++ b/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/getallsensorsstub_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "getallsensorsstub_fuzzer.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor.h" +#include "sensor_service.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t U32_AT_SIZE = 4; +auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); +const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "GetAllSensorsStubFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + MessageParcel datas; + datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + g_sensorService->OnRemoteRequest(static_cast(SensorInterfaceCode::GET_SENSOR_LIST), + datas, reply, option); + return true; +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + /* Validate the length of size */ + if (size < OHOS::Sensors::U32_AT_SIZE) { + return 0; + } + + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); + return 0; +} diff --git a/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/getallsensorsstub_fuzzer.h b/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/getallsensorsstub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..47c44a306280bc63a4981b06cc9834cdbd628c6a --- /dev/null +++ b/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/getallsensorsstub_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef GET_ALL_SENSORS_STUB_FUZZER_H +#define GET_ALL_SENSORS_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "getallsensorsstub_fuzzer" + +#endif // GET_ALL_SENSORS_STUB_FUZZER_H + diff --git a/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/project.xml b/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..53811f014e17dd5a94f7b3865facfc2aeb6ed19e --- /dev/null +++ b/services/sensor/test/fuzztest/getallsensorsstub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..a6b6746ae84b0d50beb2e6b0aef6d2c5d5241450 --- /dev/null +++ b/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/BUILD.gn @@ -0,0 +1,69 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("./../../../../../sensor.gni") + +ohos_fuzztest("ResetSensorsStubFuzzTest") { + module_out_path = "sensors/sensor" + + fuzz_config_file = + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/resetsensorsstub_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/resetsensorsstub_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "resetsensorsstub_fuzzer.cpp" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/services/sensor:libsensor_service_static", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":ResetSensorsStubFuzzTest", + ] +} diff --git a/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/corpus/init b/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/project.xml b/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..53811f014e17dd5a94f7b3865facfc2aeb6ed19e --- /dev/null +++ b/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/resetsensorsstub_fuzzer.cpp b/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/resetsensorsstub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1f245f74dd821cd21cdc36944ad3498a347d76a --- /dev/null +++ b/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/resetsensorsstub_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "resetsensorsstub_fuzzer.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor.h" +#include "sensor_service.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t U32_AT_SIZE = 4; +auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); +const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "ResetSensorsStubFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + MessageParcel datas; + datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + g_sensorService->OnRemoteRequest(static_cast(SensorInterfaceCode::RESET_SENSORS), + datas, reply, option); + return true; +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + /* Validate the length of size */ + if (size < OHOS::Sensors::U32_AT_SIZE) { + return 0; + } + + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); + return 0; +} diff --git a/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/resetsensorsstub_fuzzer.h b/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/resetsensorsstub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..b650421fae1ac50e9f7f2460a18aa7efc76af8ec --- /dev/null +++ b/services/sensor/test/fuzztest/resetsensorsstub_fuzzer/resetsensorsstub_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RESET_SENSORS_STUB_FUZZER_H +#define RESET_SENSORS_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "resetsensorsstub_fuzzer" + +#endif // RESET_SENSORS_STUB_FUZZER_H + diff --git a/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..eb7568422f6eca0760442ad0dcd8095b77756841 --- /dev/null +++ b/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/BUILD.gn @@ -0,0 +1,69 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("./../../../../../sensor.gni") + +ohos_fuzztest("ResumeSensorsStubFuzzTest") { + module_out_path = "sensors/sensor" + + fuzz_config_file = + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/resumesensorsstub_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/resumesensorsstub_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "resumesensorsstub_fuzzer.cpp" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/services/sensor:libsensor_service_static", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":ResumeSensorsStubFuzzTest", + ] +} diff --git a/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/corpus/init b/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/project.xml b/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..53811f014e17dd5a94f7b3865facfc2aeb6ed19e --- /dev/null +++ b/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/resumesensorsstub_fuzzer.cpp b/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/resumesensorsstub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a889115ee2279b93e309df37aede5fbfa1a5ab60 --- /dev/null +++ b/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/resumesensorsstub_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "resumesensorsstub_fuzzer.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor.h" +#include "sensor_service.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t U32_AT_SIZE = 4; +auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); +const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "ResumeSensorsStubFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + MessageParcel datas; + datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + g_sensorService->OnRemoteRequest(static_cast(SensorInterfaceCode::RESUME_SENSORS), + datas, reply, option); + return true; +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + /* Validate the length of size */ + if (size < OHOS::Sensors::U32_AT_SIZE) { + return 0; + } + + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); + return 0; +} diff --git a/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/resumesensorsstub_fuzzer.h b/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/resumesensorsstub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..a501876ddeb27ebb3f4ad69b1935befc5cee24cf --- /dev/null +++ b/services/sensor/test/fuzztest/resumesensorsstub_fuzzer/resumesensorsstub_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RESUME_SENSORS_STUB_FUZZER_H +#define RESUME_SENSORS_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "resumesensorsstub_fuzzer" + +#endif // RESUME_SENSORS_STUB_FUZZER_H + diff --git a/services/sensor/test/fuzztest/sensordisablestub_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/sensordisablestub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..536c3efcf0202b93480ceb543fe66216816487ae --- /dev/null +++ b/services/sensor/test/fuzztest/sensordisablestub_fuzzer/BUILD.gn @@ -0,0 +1,69 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("./../../../../../sensor.gni") + +ohos_fuzztest("SensorDisableStubFuzzTest") { + module_out_path = "sensors/sensor" + + fuzz_config_file = + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/sensordisablestub_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/sensordisablestub_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "sensordisablestub_fuzzer.cpp" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/services/sensor:libsensor_service_static", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":SensorDisableStubFuzzTest", + ] +} diff --git a/services/sensor/test/fuzztest/sensordisablestub_fuzzer/corpus/init b/services/sensor/test/fuzztest/sensordisablestub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/services/sensor/test/fuzztest/sensordisablestub_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/services/sensor/test/fuzztest/sensordisablestub_fuzzer/project.xml b/services/sensor/test/fuzztest/sensordisablestub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..53811f014e17dd5a94f7b3865facfc2aeb6ed19e --- /dev/null +++ b/services/sensor/test/fuzztest/sensordisablestub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/services/sensor/test/fuzztest/sensordisablestub_fuzzer/sensordisablestub_fuzzer.cpp b/services/sensor/test/fuzztest/sensordisablestub_fuzzer/sensordisablestub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04d2cf74c2a8101cd6e5fae1b03905f43498ddc7 --- /dev/null +++ b/services/sensor/test/fuzztest/sensordisablestub_fuzzer/sensordisablestub_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sensordisablestub_fuzzer.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor.h" +#include "sensor_service.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t U32_AT_SIZE = 4; +auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); +const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "SensorDisableStubFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + MessageParcel datas; + datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + g_sensorService->OnRemoteRequest(static_cast(SensorInterfaceCode::DISABLE_SENSOR), + datas, reply, option); + return true; +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + /* Validate the length of size */ + if (size < OHOS::Sensors::U32_AT_SIZE) { + return 0; + } + + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); + return 0; +} diff --git a/services/sensor/test/fuzztest/sensordisablestub_fuzzer/sensordisablestub_fuzzer.h b/services/sensor/test/fuzztest/sensordisablestub_fuzzer/sensordisablestub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..01b481382838b2a114a19eaa32dd554f64c3ab03 --- /dev/null +++ b/services/sensor/test/fuzztest/sensordisablestub_fuzzer/sensordisablestub_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SENSOR_DISABLE_STUB_FUZZER_H +#define SENSOR_DISABLE_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "sensordisablestub_fuzzer" + +#endif // SENSOR_DISABLE_STUB_FUZZER_H + diff --git a/services/sensor/test/fuzztest/sensorenablestub_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/sensorenablestub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d30dc9f2a3d3433d10e1db70f3aaf6e4e8948567 --- /dev/null +++ b/services/sensor/test/fuzztest/sensorenablestub_fuzzer/BUILD.gn @@ -0,0 +1,69 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("./../../../../../sensor.gni") + +ohos_fuzztest("SensorEnableStubFuzzTest") { + module_out_path = "sensors/sensor" + + fuzz_config_file = + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/sensorenablestub_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/sensorenablestub_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "sensorenablestub_fuzzer.cpp" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/services/sensor:libsensor_service_static", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":SensorEnableStubFuzzTest", + ] +} diff --git a/services/sensor/test/fuzztest/sensorenablestub_fuzzer/corpus/init b/services/sensor/test/fuzztest/sensorenablestub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/services/sensor/test/fuzztest/sensorenablestub_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/services/sensor/test/fuzztest/sensorenablestub_fuzzer/project.xml b/services/sensor/test/fuzztest/sensorenablestub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..53811f014e17dd5a94f7b3865facfc2aeb6ed19e --- /dev/null +++ b/services/sensor/test/fuzztest/sensorenablestub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/services/sensor/test/fuzztest/sensorenablestub_fuzzer/sensorenablestub_fuzzer.cpp b/services/sensor/test/fuzztest/sensorenablestub_fuzzer/sensorenablestub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ede989d3666bf5035e960c4c5d6d9030e365a278 --- /dev/null +++ b/services/sensor/test/fuzztest/sensorenablestub_fuzzer/sensorenablestub_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sensorenablestub_fuzzer.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor.h" +#include "sensor_service.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t U32_AT_SIZE = 4; +auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); +const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "SensorEnableStubFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + MessageParcel datas; + datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + g_sensorService->OnRemoteRequest(static_cast(SensorInterfaceCode::ENABLE_SENSOR), + datas, reply, option); + return true; +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + /* Validate the length of size */ + if (size < OHOS::Sensors::U32_AT_SIZE) { + return 0; + } + + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); + return 0; +} diff --git a/services/sensor/test/fuzztest/sensorenablestub_fuzzer/sensorenablestub_fuzzer.h b/services/sensor/test/fuzztest/sensorenablestub_fuzzer/sensorenablestub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..6103456ac0ce7c6e1db7885972922ee5f610f2a6 --- /dev/null +++ b/services/sensor/test/fuzztest/sensorenablestub_fuzzer/sensorenablestub_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SENSOR_ENABLE_STUB_FUZZER_H +#define SENSOR_ENABLE_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "sensorenablestub_fuzzer" + +#endif // SENSOR_ENABLE_STUB_FUZZER_H + diff --git a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/BUILD.gn index 0370869897722e86447b2af8c1f3498eadb6b3da..b079fe3c429bece0f434ff6994f0e3fff73d3ffe 100644 --- a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/BUILD.gn +++ b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/BUILD.gn @@ -50,6 +50,8 @@ ohos_fuzztest("SensorOnRemoteRequestFuzzTest") { external_deps = [ "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", diff --git a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp index 66d962e607699a1c010076f895a8eba7af4ebac5..3b81755b0f950401a8def291594646e62c7eb6fe 100644 --- a/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp +++ b/services/sensor/test/fuzztest/sensoronremoterequest_fuzzer/sensoronremoterequest_fuzzer.cpp @@ -17,34 +17,64 @@ #include #include -#include +#include "accesstoken_kit.h" #include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + #include "sensor.h" -#include "sensor_delayed_sp_singleton.h" #include "sensor_service.h" namespace OHOS { namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; namespace { -constexpr size_t FOO_MAX_LEN = 1024; constexpr size_t U32_AT_SIZE = 4; +constexpr uint32_t IPC_CODE_COUNT = 13; auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; -} // namespace +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "SensorOnRemoteRequestFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} -uint32_t GetU32Data(const char *ptr) +uint32_t GetU32Data(const uint8_t *data) { // convert fuzz input data to an integer - return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; + return ((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]) % IPC_CODE_COUNT; } -bool OnRemoteRequestFuzzTest(const char *data, size_t size) +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) { + SetUpTestCase(); uint32_t code = GetU32Data(data); MessageParcel datas; datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); - datas.WriteBuffer(data, size); + datas.WriteBuffer(data + U32_AT_SIZE, size - U32_AT_SIZE); datas.RewindRead(0); MessageParcel reply; MessageOption option; @@ -58,30 +88,14 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { /* Run your code on data */ if (data == nullptr) { - std::cout << "invalid data" << std::endl; return 0; } /* Validate the length of size */ - if (size > OHOS::Sensors::FOO_MAX_LEN || size < OHOS::Sensors::U32_AT_SIZE) { + if (size < OHOS::Sensors::U32_AT_SIZE) { return 0; } - char *ch = (char *)malloc(size + 1); - if (ch == nullptr) { - std::cout << "malloc failed." << std::endl; - return 0; - } - - (void)memset_s(ch, size + 1, 0x00, size + 1); - if (memcpy_s(ch, size, data, size) != EOK) { - std::cout << "copy failed." << std::endl; - free(ch); - ch = nullptr; - return 0; - } - OHOS::Sensors::OnRemoteRequestFuzzTest(ch, size); - free(ch); - ch = nullptr; + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); return 0; } diff --git a/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/BUILD.gn b/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..07ff64f7020a4e36ae86ab7fa99be4a221b42a32 --- /dev/null +++ b/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/BUILD.gn @@ -0,0 +1,69 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("./../../../../../sensor.gni") + +ohos_fuzztest("SuspendSensorsStubFuzzTest") { + module_out_path = "sensors/sensor" + + fuzz_config_file = + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/sensor/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/sensor/include", + "$SUBSYSTEM_DIR/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "suspendsensorsstub_fuzzer.cpp" ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_interface_native", + "$SUBSYSTEM_DIR/services/sensor:libsensor_service_static", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/utils/ipc:libsensor_ipc", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":SuspendSensorsStubFuzzTest", + ] +} diff --git a/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/corpus/init b/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/project.xml b/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..53811f014e17dd5a94f7b3865facfc2aeb6ed19e --- /dev/null +++ b/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/suspendsensorsstub_fuzzer.cpp b/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/suspendsensorsstub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afc60de72f56d2d687045e0670fcdecbdeef8a0a --- /dev/null +++ b/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/suspendsensorsstub_fuzzer.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "suspendsensorsstub_fuzzer.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor.h" +#include "sensor_service.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t U32_AT_SIZE = 4; +auto g_sensorService = SensorDelayedSpSingleton::GetInstance(); +const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService"; +} // namespace + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + if (perms == nullptr) { + return; + } + perms[0] = "ohos.permission.ACCELEROMETER"; + perms[1] = "ohos.permission.MANAGE_SENSOR"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 2, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "SuspendSensorsStubFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + MessageParcel datas; + datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + g_sensorService->OnRemoteRequest(static_cast(SensorInterfaceCode::SUSPEND_SENSORS), + datas, reply, option); + return true; +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + /* Validate the length of size */ + if (size < OHOS::Sensors::U32_AT_SIZE) { + return 0; + } + + OHOS::Sensors::OnRemoteRequestFuzzTest(data, size); + return 0; +} diff --git a/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/suspendsensorsstub_fuzzer.h b/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/suspendsensorsstub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..46bc478eb470e124bfd6f76b148336c00ee7ac10 --- /dev/null +++ b/services/sensor/test/fuzztest/suspendsensorsstub_fuzzer/suspendsensorsstub_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SUSPEND_SENSORS_STUB_FUZZER_H +#define SUSPEND_SENSORS_STUB_FUZZER_H + +#define FUZZ_PROJECT_NAME "suspendsensorsstub_fuzzer" + +#endif // SUSPEND_SENSORS_STUB_FUZZER_H +