diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index a0edecf3..ccd943ad 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -18,6 +18,7 @@ group("storage_service_fuzztest") { # deps file "${storage_service_path}/test/fuzztest/bundlestats_fuzzer:BundleStatsFuzzTest", "${storage_service_path}/test/fuzztest/disk_fuzzer:DiskFuzzTest", + "${storage_service_path}/test/fuzztest/filesystemcrypto_fuzzer:FileSystemCryptoFuzzTest", "${storage_service_path}/test/fuzztest/fileutils_fuzzer:FileUtilsFuzzTest", "${storage_service_path}/test/fuzztest/fscryptutils_fuzzer:FscryptUtilsFuzzTest", "${storage_service_path}/test/fuzztest/keycontrol_fuzzer:KeyControlFuzzTest", @@ -27,6 +28,7 @@ group("storage_service_fuzztest") { "${storage_service_path}/test/fuzztest/storagemanager_fuzzer:StorageManagerFuzzTest", "${storage_service_path}/test/fuzztest/storagemanagerproxy_fuzzer:StorageManagerProxyFuzzTest", "${storage_service_path}/test/fuzztest/storagestats_fuzzer:StorageStatsFuzzTest", + "${storage_service_path}/test/fuzztest/storagestatusservice_fuzzer:StorageStatusServiceFuzzTest", "${storage_service_path}/test/fuzztest/storagetotalstatusservice_fuzzer:StorageTotalStatusServiceFuzzTest", "${storage_service_path}/test/fuzztest/stringutils_fuzzer:StringUtilsFuzzTest", "${storage_service_path}/test/fuzztest/sysparamdynamic_fuzzer:SysparamDynamicFuzzTest", diff --git a/test/fuzztest/filesystemcrypto_fuzzer/BUILD.gn b/test/fuzztest/filesystemcrypto_fuzzer/BUILD.gn new file mode 100644 index 00000000..000db321 --- /dev/null +++ b/test/fuzztest/filesystemcrypto_fuzzer/BUILD.gn @@ -0,0 +1,54 @@ +# Copyright (c) 2024 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/test.gni") +import("//foundation/filemanagement/storage_service/storage_service_aafwk.gni") + +ohos_fuzztest("FileSystemCryptoFuzzTest") { + module_out_path = "storage_service/storage_manager" + fuzz_config_file = + "${storage_service_path}/test/fuzztest/filesystemcrypto_fuzzer" + include_dirs = [ + "${storage_manager_path}/include", + "${storage_interface_path}/innerkits/storage_manager/native", + "${storage_service_common_path}/include", + "${storage_daemon_path}/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "${storage_manager_path}/crypto/filesystem_crypto.cpp", + "${storage_manager_path}/storage_daemon_communication/src/storage_daemon_communication.cpp", + "./filesystemcrypto_fuzzer.cpp", + ] + defines = [ "STORAGE_LOG_TAG = \"storage_service\"" ] + deps = [ "${storage_daemon_path}/libfscrypt:libfscryptutils" ] + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "storage_service:storage_manager_sa_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":FileSystemCryptoFuzzTest" ] +} diff --git a/test/fuzztest/filesystemcrypto_fuzzer/corpus/init b/test/fuzztest/filesystemcrypto_fuzzer/corpus/init new file mode 100644 index 00000000..6198079a --- /dev/null +++ b/test/fuzztest/filesystemcrypto_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2024 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/filesystemcrypto_fuzzer/filesystemcrypto_fuzzer.cpp b/test/fuzztest/filesystemcrypto_fuzzer/filesystemcrypto_fuzzer.cpp new file mode 100644 index 00000000..b0ce0956 --- /dev/null +++ b/test/fuzztest/filesystemcrypto_fuzzer/filesystemcrypto_fuzzer.cpp @@ -0,0 +1,222 @@ +/* + * Copyright (c) 2024 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 "filesystemcrypto_fuzzer.h" +#include "crypto/filesystem_crypto.h" +#include "storage_service_errno.h" +#include "storage_service_log.h" +#include "vector" + +namespace OHOS { +namespace StorageManager { + +template +T TypeCast(const uint8_t *data, int *pos = nullptr) +{ + if (pos) { + *pos += sizeof(T); + } + return *(reinterpret_cast(data)); +} + +std::shared_ptr fileSystem = + DelayedSingleton::GetInstance(); + +bool GenerateUserKeysFuzzTest(const uint8_t *data, size_t size) +{ + if (data == nullptr || size <= sizeof(uint32_t)) { + return false; + } + + int pos = 0; + uint32_t userId = TypeCast(data, &pos); + uint32_t flags = TypeCast(data + pos); + + int32_t result = fileSystem->GenerateUserKeys(userId, flags); + if (result != E_OK) { + LOGI("file system crypto fuzz test of interface FileSystemCrypto::GenerateUserKeysTest failed!"); + return false; + } + return true; +} + +bool DeleteUserKeysFuzzTest(const uint8_t *data, size_t size) +{ + if (data == nullptr || size <= sizeof(uint32_t)) { + return false; + } + + int pos = 0; + uint32_t userId = TypeCast(data, &pos); + + int32_t result = fileSystem->DeleteUserKeys(userId); + if (result != E_OK) { + LOGI("file system crypto fuzz test of interface FileSystemCrypto::DeleteUserKeys failed!"); + return false; + } + return true; +} + +bool UpdateUserAuthFuzzTest(const uint8_t *data, size_t size) +{ + if (data == nullptr || size <= sizeof(uint32_t)) { + return false; + } + + int pos = 0; + uint32_t userId = TypeCast(data, &pos); + uint64_t secureUid = TypeCast(data + pos); + + std::vector token; + std::vector oldSecret; + std::vector newSecret; + token.push_back(*data); + oldSecret.push_back(*data); + newSecret.push_back(*data); + + int32_t result = fileSystem->UpdateUserAuth(userId, secureUid, token, oldSecret, newSecret); + if (result != E_OK) { + LOGI("file system crypto fuzz test of interface FileSystemCrypto::UpdateUserAuth failed!"); + return false; + } + return true; +} + +bool ActiveUserKeyFuzzTest(const uint8_t *data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return false; + } + + int pos = 0; + uint32_t userId = TypeCast(data, &pos); + + std::vector token; + std::vector secret; + token.push_back(*data); + secret.push_back(*data); + + int32_t result = fileSystem->ActiveUserKey(userId, token, secret); + if (result != E_OK) { + LOGI("file system crypto fuzz test of interface FileSystemCrypto::ActiveUserKey failed!"); + return false; + } + return true; +} + +bool InactiveUserKeyFuzzTest(const uint8_t *data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return false; + } + + int pos = 0; + uint32_t userId = TypeCast(data, &pos); + + int32_t result = fileSystem->InactiveUserKey(userId); + if (result != E_OK) { + LOGI("file system crypto fuzz test of interface FileSystemCrypto::InactiveUserKey failed!"); + return false; + } + return true; +} + +bool UpdateKeyContextFuzzTest(const uint8_t *data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return false; + } + + int pos = 0; + uint32_t userId = TypeCast(data, &pos); + + int32_t result = fileSystem->UpdateKeyContext(userId); + if (result != E_OK) { + LOGI("file system crypto fuzz test of interface FileSystemCrypto::UpdateKeyContext failed!"); + return false; + } + return true; +} + +bool LockUserScreenFuzzTest(const uint8_t *data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return false; + } + + int pos = 0; + uint32_t userId = TypeCast(data, &pos); + + int32_t result = fileSystem->LockUserScreen(userId); + if (result != E_OK) { + LOGI("file system crypto fuzz test of interface FileSystemCrypto::LockUserScreen failed!"); + return false; + } + return true; +} + +bool UnlockUserScreenFuzzTest(const uint8_t *data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return false; + } + + int pos = 0; + uint32_t userId = TypeCast(data, &pos); + + int32_t result = fileSystem->UnlockUserScreen(userId); + if (result != E_OK) { + LOGI("file system crypto fuzz test of interface FileSystemCrypto::UnlockUserScreen failed!"); + return false; + } + return true; +} + +bool GetLockScreenStatusFuzzTest(const uint8_t *data, size_t size) +{ + if (data == nullptr || size <= sizeof(uint32_t)) { + return false; + } + + int pos = 0; + uint32_t userId = TypeCast(data, &pos); + bool lockScreenStatus = TypeCast(data + pos); + + int32_t result = fileSystem->GetLockScreenStatus(userId, lockScreenStatus); + if (result != E_OK) { + LOGI("file system crypto fuzz test of interface FileSystemCrypto::GetLockScreenStatus failed!"); + return false; + } + return true; +} +} // namespace StorageManager +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::StorageManager::GenerateUserKeysFuzzTest(data, size); + OHOS::StorageManager::DeleteUserKeysFuzzTest(data, size); + OHOS::StorageManager::UpdateUserAuthFuzzTest(data, size); + OHOS::StorageManager::ActiveUserKeyFuzzTest(data, size); + OHOS::StorageManager::InactiveUserKeyFuzzTest(data, size); + OHOS::StorageManager::UpdateKeyContextFuzzTest(data, size); + OHOS::StorageManager::LockUserScreenFuzzTest(data, size); + OHOS::StorageManager::UnlockUserScreenFuzzTest(data, size); + OHOS::StorageManager::GetLockScreenStatusFuzzTest(data, size); + + return 0; +} diff --git a/test/fuzztest/filesystemcrypto_fuzzer/filesystemcrypto_fuzzer.h b/test/fuzztest/filesystemcrypto_fuzzer/filesystemcrypto_fuzzer.h new file mode 100644 index 00000000..145f936e --- /dev/null +++ b/test/fuzztest/filesystemcrypto_fuzzer/filesystemcrypto_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 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 FILESYSTEMCRYPTO_FUZZER_H +#define FILESYSTEMCRYPTO_FUZZER_H + +#define FUZZ_PROJECT_NAME "filesystemcrypto_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/filesystemcrypto_fuzzer/project.xml b/test/fuzztest/filesystemcrypto_fuzzer/project.xml new file mode 100644 index 00000000..85e7ef2c --- /dev/null +++ b/test/fuzztest/filesystemcrypto_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/storagemanagerproxy_fuzzer/storagemanagerproxy_fuzzer.cpp b/test/fuzztest/storagemanagerproxy_fuzzer/storagemanagerproxy_fuzzer.cpp index 9cdc691b..649ccdd0 100644 --- a/test/fuzztest/storagemanagerproxy_fuzzer/storagemanagerproxy_fuzzer.cpp +++ b/test/fuzztest/storagemanagerproxy_fuzzer/storagemanagerproxy_fuzzer.cpp @@ -33,15 +33,14 @@ bool StorageManagerProxyFuzzTest(const uint8_t *data, size_t size) std::vector secret; const sptr impl; StorageManagerProxy prePar(impl); - std::string path((const char *)data, size); - std::string fsUuid((const char *)data, size); + std::string path(reinterpret_cast(data), size); + std::string fsUuid(reinterpret_cast(data), size); int32_t userId = *(reinterpret_cast(data)); int32_t flags = *(reinterpret_cast(data)); int32_t fsType = *(reinterpret_cast(data)); uint64_t secureUid = *(reinterpret_cast(data)); - std::string volumeUuid((const char *)data, size); - std::string description((const char *)data, size); - + std::string volumeUuid(reinterpret_cast(data), size); + std::string description(reinterpret_cast(data), size); token.push_back(*data); secret.push_back(*data); prePar.StopUser(userId); @@ -76,16 +75,16 @@ bool StorageManagerProxyGetFuzzTest(const uint8_t *data, size_t size) VolumeExternal vc1; const sptr impl; StorageManagerProxy getStor(impl); - std::string volumeUuid((const char *)data, size); - std::string pkgName((const char *)data, size); + std::string volumeUuid(reinterpret_cast(data), size); + std::string pkgName(reinterpret_cast(data), size); int32_t userId = *(reinterpret_cast(data)); - std::string fsUuid((const char *)data, size); + std::string fsUuid(reinterpret_cast(data), size); int64_t systemSize = *(reinterpret_cast(data)); int64_t totalSize = *(reinterpret_cast(data)); int64_t freeSize = *(reinterpret_cast(data)); int64_t freeVolSize = *(reinterpret_cast(data)); int64_t totalVolSize = *(reinterpret_cast(data)); - std::string type((const char *)data, size); + std::string type(reinterpret_cast(data), size); BundleStats bundleStats; StorageStats storageStats; std::vector vecOfVol; @@ -95,7 +94,7 @@ bool StorageManagerProxyGetFuzzTest(const uint8_t *data, size_t size) std::vector bundleName; incrementalBackTimes.push_back(*data); pkgFileSizes.push_back(*data); - bundleName.push_back((const char *)data); + bundleName.push_back(reinterpret_cast(data)); getStor.GetAllVolumes(vecOfVol); getStor.GetAllDisks(vecOfDisk); getStor.GetSystemSize(systemSize); diff --git a/test/fuzztest/storagestatusservice_fuzzer/BUILD.gn b/test/fuzztest/storagestatusservice_fuzzer/BUILD.gn index 4b514167..04c59ab2 100644 --- a/test/fuzztest/storagestatusservice_fuzzer/BUILD.gn +++ b/test/fuzztest/storagestatusservice_fuzzer/BUILD.gn @@ -20,8 +20,12 @@ ohos_fuzztest("StorageStatusServiceFuzzTest") { fuzz_config_file = "${storage_service_path}/test/fuzztest/storagestatusservice_fuzzer" include_dirs = [ + "${bundlemanager_framework_path}/services/bundlemgr/include", + "${storage_interface_path}/innerkits/storage_manager/native", "${storage_service_common_path}/include", + "${storage_daemon_path}/include", "${storage_manager_path}/include", + "${storage_service_path}/utils/include", ] cflags = [ "-g", @@ -29,17 +33,19 @@ ohos_fuzztest("StorageStatusServiceFuzzTest") { "-Wno-unused-variable", "-fno-omit-frame-pointer", ] - sources = [ - "${storage_manager_path}/utils/src/storage_utils.cpp", - "storagestatusservice_fuzzer.cpp", - ] + sources = [ "./storagestatusservice_fuzzer.cpp" ] defines = [ "STORAGE_LOG_TAG = \"storage_service\"" ] - deps = [ "${bundlemanager_framework_path}/services/bundlemgr:libbms" ] + deps = [ "${storage_service_path}/services/storage_manager:storage_manager" ] external_deps = [ + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", "hitrace:hitrace_meter", + "ipc:ipc_core", + "storage_service:storage_manager_sa_proxy", ] } diff --git a/test/fuzztest/storagestatusservice_fuzzer/storagestatusservice_fuzzer.cpp b/test/fuzztest/storagestatusservice_fuzzer/storagestatusservice_fuzzer.cpp index 4d0fcaed..f008894c 100644 --- a/test/fuzztest/storagestatusservice_fuzzer/storagestatusservice_fuzzer.cpp +++ b/test/fuzztest/storagestatusservice_fuzzer/storagestatusservice_fuzzer.cpp @@ -27,14 +27,14 @@ bool StorageStatusServiceFuzzTest(const uint8_t *data, size_t size) std::shared_ptr service = DelayedSingleton::GetInstance(); int32_t userId = *(reinterpret_cast(data)); - std::string pkgName((const char *) data, size); - std::string type((const char *) data, size); + std::string pkgName(reinterpret_cast(data), size); + std::string type(reinterpret_cast(data), size); BundleStats bundleStats; StorageStats storageStats; std::vector bundleName; std::vector incrementalBackTimes; std::vector pkgFileSizes; - bundleName.push_back((const char *) data); + bundleName.push_back(reinterpret_cast(data)); incrementalBackTimes.push_back(*data); pkgFileSizes.push_back(*data); int32_t result = service->GetBundleStats(pkgName, bundleStats);