diff --git a/test/fuzztest/interfaces/BUILD.gn b/test/fuzztest/interfaces/BUILD.gn index 95600caa6e047b50fe1e21076ffcbbd5b63eb17b..9012ffc2e03b9d90efffd889f3afa94499f9164c 100644 --- a/test/fuzztest/interfaces/BUILD.gn +++ b/test/fuzztest/interfaces/BUILD.gn @@ -22,6 +22,7 @@ group("fuzztest") { "register_fuzzer:fuzztest", "resumesensors_fuzzer:fuzztest", "sensoragent_fuzzer:fuzztest", + "setdevicestatus_fuzzer:fuzztest", "suspendsensors_fuzzer:fuzztest", "unregister_fuzzer:fuzztest", ] diff --git a/test/fuzztest/interfaces/setdevicestatus_fuzzer/BUILD.gn b/test/fuzztest/interfaces/setdevicestatus_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..1b330732bf88ddb0cf21356443806694ad04bbf4 --- /dev/null +++ b/test/fuzztest/interfaces/setdevicestatus_fuzzer/BUILD.gn @@ -0,0 +1,59 @@ +# Copyright (c) 2025 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("SetDeviceStatusFuzzTest") { + module_out_path = FUZZ_MODULE_OUT_PATH + + fuzz_config_file = + "$SUBSYSTEM_DIR/test/fuzztest/interfaces/setdevicestatus_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/interfaces/inner_api", + "$SUBSYSTEM_DIR/frameworks/native/include", + "$SUBSYSTEM_DIR/test/fuzztest/interfaces/setdevicestatus_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "setdevicestatus_fuzzer.cpp" ] + + deps = [ "$SUBSYSTEM_DIR/frameworks/native:sensor_interface_native" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken_shared", + "access_token:libtokensetproc_shared", + "c_utils:utils", + "hilog:libhilog", + ] +} + +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":SetDeviceStatusFuzzTest", + ] +} diff --git a/test/fuzztest/interfaces/setdevicestatus_fuzzer/corpus/init b/test/fuzztest/interfaces/setdevicestatus_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e010f5889b59a772ce45fc45b17e8a578984479c --- /dev/null +++ b/test/fuzztest/interfaces/setdevicestatus_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2025 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/test/fuzztest/interfaces/setdevicestatus_fuzzer/project.xml b/test/fuzztest/interfaces/setdevicestatus_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..2eb360c27f1b159e1b043e38846f10fcf37fa37c --- /dev/null +++ b/test/fuzztest/interfaces/setdevicestatus_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/test/fuzztest/interfaces/setdevicestatus_fuzzer/setdevicestatus_fuzzer.cpp b/test/fuzztest/interfaces/setdevicestatus_fuzzer/setdevicestatus_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95722157bb0324feb612f0e98df546c7df39c653 --- /dev/null +++ b/test/fuzztest/interfaces/setdevicestatus_fuzzer/setdevicestatus_fuzzer.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2025 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 "setdevicestatus_fuzzer.h" + +#include "accesstoken_kit.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "sensor_agent.h" +#include "sensor_errors.h" + +#undef LOG_TAG +#define LOG_TAG "SetDeviceStatusFuzzTest" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +namespace { +constexpr size_t DATA_MIN_SIZE = 4; +} // namespace + +template +void GetObject(const uint8_t *data, size_t size, T &object) +{ + size_t objectSize = sizeof(object); + if (objectSize > size) { + return; + } + memcpy_s(&object, objectSize, data, objectSize); +} + +void SetUpTestCase() +{ + const char **perms = new (std::nothrow) const char *[2]; + CHKPV(perms); + 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 = "SetDeviceStatusFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +void SetDeviceStatusFuzzTest(const uint8_t *data, size_t size) +{ + if (data == nullptr || size < DATA_MIN_SIZE) { + return; + } + SetUpTestCase(); + int32_t pid { -1 }; + GetObject(data, size, pid); + SetDeviceStatus(pid); +} +} // namespace Sensors +} // namespace OHOS + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + OHOS::Sensors::SetDeviceStatusFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/interfaces/setdevicestatus_fuzzer/setdevicestatus_fuzzer.h b/test/fuzztest/interfaces/setdevicestatus_fuzzer/setdevicestatus_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..4aa842b680ff2441d7e0329fcdb871c16626d094 --- /dev/null +++ b/test/fuzztest/interfaces/setdevicestatus_fuzzer/setdevicestatus_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 SET_DEVICE_STATUS_FUZZER_H +#define SET_DEVICE_STATUS_FUZZER_H + +#define FUZZ_PROJECT_NAME "setdevicestatus_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/services/BUILD.gn b/test/fuzztest/services/BUILD.gn index 618dd68153e853eacbe97201d42ab270118a9c76..e541b0ae5a68a7ded80d41f20a9cbe5e1737271f 100644 --- a/test/fuzztest/services/BUILD.gn +++ b/test/fuzztest/services/BUILD.gn @@ -35,6 +35,7 @@ group("fuzztest") { "service/reportactiveinfo_fuzzer:fuzztest", "service/reportonchangedata_fuzzer:fuzztest", "service/reportsensorsysevent_fuzzer:fuzztest", + "service/setdevicestatusservice_fuzzer:fuzztest", "suspendsensorsstub_fuzzer:fuzztest", ] } diff --git a/test/fuzztest/services/service/setdevicestatusservice_fuzzer/BUILD.gn b/test/fuzztest/services/service/setdevicestatusservice_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..8b0baf5a3002c2bfe87b514cf00c90dd62045c94 --- /dev/null +++ b/test/fuzztest/services/service/setdevicestatusservice_fuzzer/BUILD.gn @@ -0,0 +1,75 @@ +# Copyright (c) 2025 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("SetDeviceStatusServiceFuzzTest") { + module_out_path = FUZZ_MODULE_OUT_PATH + + fuzz_config_file = "$SUBSYSTEM_DIR/test/fuzztest/services/service/setdevicestatusservice_fuzzer" + + include_dirs = [ + "$SUBSYSTEM_DIR/frameworks/native/include", + "$SUBSYSTEM_DIR/interfaces/inner_api", + "$SUBSYSTEM_DIR/services/hdi_connection/interface/include", + "$SUBSYSTEM_DIR/services/hdi_connection/adapter/include", + "$SUBSYSTEM_DIR/services/hdi_connection/hardware/include", + "$SUBSYSTEM_DIR/services/include", + "$SUBSYSTEM_DIR/test/fuzztest/services/service/setdevicestatusservice_fuzzer", + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/utils/ipc/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-Dprivate=public", + "-Dprotected=public", + ] + + sources = [ "setdevicestatus_fuzzer.cpp" ] + + defines = sensor_default_defines + + deps = [ + "$SUBSYSTEM_DIR/frameworks/native:sensor_interface_native", + "$SUBSYSTEM_DIR/frameworks/native:sensor_service_stub", + "$SUBSYSTEM_DIR/services: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_single", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ + # deps file + ":SetDeviceStatusServiceFuzzTest", + ] +} diff --git a/test/fuzztest/services/service/setdevicestatusservice_fuzzer/corpus/init b/test/fuzztest/services/service/setdevicestatusservice_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..65af8ee8d11bf23407ea34d4de49f7cbb6a2b791 --- /dev/null +++ b/test/fuzztest/services/service/setdevicestatusservice_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2025 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/test/fuzztest/services/service/setdevicestatusservice_fuzzer/project.xml b/test/fuzztest/services/service/setdevicestatusservice_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..2eb360c27f1b159e1b043e38846f10fcf37fa37c --- /dev/null +++ b/test/fuzztest/services/service/setdevicestatusservice_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/test/fuzztest/services/service/setdevicestatusservice_fuzzer/setdevicestatus_fuzzer.cpp b/test/fuzztest/services/service/setdevicestatusservice_fuzzer/setdevicestatus_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe7b2a141959dc064c6ad90e3389eb753fede883 --- /dev/null +++ b/test/fuzztest/services/service/setdevicestatusservice_fuzzer/setdevicestatus_fuzzer.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2025 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 "setdevicestatus_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_service = SensorDelayedSpSingleton::GetInstance(); +} // namespace + +template +size_t GetObject(T &object, const uint8_t *data, size_t size) +{ + size_t objectSize = sizeof(object); + if (objectSize > size) { + return 0; + } + errno_t ret = memcpy_s(&object, objectSize, data, objectSize); + if (ret != EOK) { + return 0; + } + return objectSize; +} + +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 = "SetDeviceStatusServiceFuzzTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool SetDeviceStatusFuzzTest(const uint8_t *data, size_t size) +{ + SetUpTestCase(); + uint32_t deviceStatus = 0; + GetObject(deviceStatus, data, size); + g_service->SetDeviceStatus(deviceStatus); + 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::SetDeviceStatusFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/services/service/setdevicestatusservice_fuzzer/setdevicestatus_fuzzer.h b/test/fuzztest/services/service/setdevicestatusservice_fuzzer/setdevicestatus_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..fc323d41ac04f3b9d4b7e4af6a958285c3a87460 --- /dev/null +++ b/test/fuzztest/services/service/setdevicestatusservice_fuzzer/setdevicestatus_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 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 SET_DEVICE_STATUS_FUZZER_H +#define SET_DEVICE_STATUS_FUZZER_H + +#define FUZZ_PROJECT_NAME "setdevicestatus_fuzzer" + +#endif // SET_DEVICE_STATUS_FUZZER_H + diff --git a/test/unittest/interfaces/inner_api/sensor_agent_test.cpp b/test/unittest/interfaces/inner_api/sensor_agent_test.cpp index 7b548cf8629820fc8a38e623d08c23aa374665d8..1c6c640dff4c89dbcfe0ef900ef7a9d0d0db777d 100644 --- a/test/unittest/interfaces/inner_api/sensor_agent_test.cpp +++ b/test/unittest/interfaces/inner_api/sensor_agent_test.cpp @@ -38,6 +38,7 @@ using Security::AccessToken::AccessTokenID; namespace { constexpr int32_t SENSOR_ID { 1 }; constexpr int32_t INVALID_VALUE { -1 }; +constexpr int32_t DEVICE_STATUS { 0 }; PermissionStateFull g_infoManagerTestState = { .grantFlags = {1}, @@ -475,5 +476,15 @@ HWTEST_F(SensorAgentTest, SensorNativeApiTest_004, TestSize.Level1) int32_t ret = SetMode(SENSOR_ID, &user, SENSOR_DEFAULT_MODE); ASSERT_NE(ret, OHOS::Sensors::SUCCESS); } + +HWTEST_F(SensorAgentTest, SensorNativeApiTest_005, TestSize.Level1) +{ + SEN_HILOGI("SensorNativeApiTest_005 in"); + SetDeviceStatus(DEVICE_STATUS); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = SetMode(SENSOR_ID, &user, SENSOR_DEFAULT_MODE); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} } // namespace Sensors } // namespace OHOS