diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 62f9de3a30af8fbadcb559d1f3ad3a2c492dd862..b69a4aaaa19b451c98f0fc0156b12a2c4a45bee0 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -17,6 +17,7 @@ group("fuzztest") { deps = [ # deps file "backupsa_fuzzer:BackupSaFuzzTest", + "fileshare_fuzzer:FileShareFuzzTest", "remotefileshare_fuzzer:RemoteFileShareFuzzTest", ] } diff --git a/test/fuzztest/fileshare_fuzzer/BUILD.gn b/test/fuzztest/fileshare_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d9f20cbec624f1cb86f0d3463106880ec3e26c33 --- /dev/null +++ b/test/fuzztest/fileshare_fuzzer/BUILD.gn @@ -0,0 +1,49 @@ +# 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/ohos.gni") +import("//build/test.gni") + +#####################hydra-fuzz################### +import("//foundation/filemanagement/app_file_service/app_file_service.gni") + +##############################fuzztest########################################## +ohos_fuzztest("FileShareFuzzTest") { + module_out_path = "app_file_service/app_file_service" + + include_dirs = [ + "${app_file_service_path}/interfaces/innerkits/native/file_share/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "fileshare_fuzzer.cpp" ] + + fuzz_config_file = "${app_file_service_path}/test/fuzztest/fileshare_fuzzer" + + deps = [ + "${app_file_service_path}/interfaces/innerkits/native:fileshare_native", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + ] +} diff --git a/test/fuzztest/fileshare_fuzzer/corpus/init b/test/fuzztest/fileshare_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..3ea16af51fd6f79e4841914ad81bef2ae719dc6a --- /dev/null +++ b/test/fuzztest/fileshare_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/fileshare_fuzzer/fileshare_fuzzer.cpp b/test/fuzztest/fileshare_fuzzer/fileshare_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e96721719c952fd9bff74d4d1bde3dd4572ac43e --- /dev/null +++ b/test/fuzztest/fileshare_fuzzer/fileshare_fuzzer.cpp @@ -0,0 +1,132 @@ +/* + * 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 "fileshare_fuzzer.h" + +#include +#include + +#include "file_permission.h" +#include "accesstoken_kit.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" + +namespace OHOS { +constexpr size_t THRESHOLD = 10; +constexpr size_t LENGTH = 4; +constexpr size_t REMAINDER = 3; +using namespace OHOS::Security::AccessToken; + +uint32_t ConvertToUint32(const uint8_t *ptr, size_t size) +{ + if (ptr == nullptr || (size < sizeof(uint32_t))) { + return 0; + } + return *(reinterpret_cast(ptr)); +} + +void GetInfo(const uint8_t *data, size_t size, std::vector &info) +{ + OHOS::AppFileService::UriPolicyInfo policyInfo; + size_t lenth = size - LENGTH; + std::string uri(reinterpret_cast(data), lenth); + policyInfo.uri = uri; + policyInfo.mode = ConvertToUint32(data + lenth, LENGTH) % REMAINDER; + info.push_back(policyInfo); +} + +void GrantNativePermission() +{ + const char **perms = new const char *[1]; + perms[0] = "ohos.permission.FILE_ACCESS_PERSIST"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "app_file_service", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +void PersistPermissionFuzzTest(const uint8_t *data, size_t size) +{ + if (size < LENGTH) { + return; + } + std::deque result; + std::vector info; + GetInfo(data, size, info); + GrantNativePermission(); + OHOS::AppFileService::FilePermission::PersistPermission(info, result); +} + +void RevokePermissionFuzzTest(const uint8_t *data, size_t size) +{ + if (size < LENGTH) { + return; + } + std::deque result; + std::vector info; + GetInfo(data, size, info); + GrantNativePermission(); + OHOS::AppFileService::FilePermission::RevokePermission(info, result); +} + +void ActivatePermissionFuzzTest(const uint8_t *data, size_t size) +{ + if (size < LENGTH) { + return; + } + std::deque result; + std::vector info; + GetInfo(data, size, info); + GrantNativePermission(); + OHOS::AppFileService::FilePermission::ActivatePermission(info, result); +} + +void DeactivatePermissionFuzzTest(const uint8_t *data, size_t size) +{ + if (size < LENGTH) { + return; + } + std::deque result; + std::vector info; + GetInfo(data, size, info); + GrantNativePermission(); + OHOS::AppFileService::FilePermission::DeactivatePermission(info, result); +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + if (size < OHOS::THRESHOLD) { + return 0; + } + + /* Run your code on data */ + OHOS::PersistPermissionFuzzTest(data, size); + OHOS::RevokePermissionFuzzTest(data, size); + OHOS::ActivatePermissionFuzzTest(data, size); + OHOS::DeactivatePermissionFuzzTest(data, size); + return 0; +} +} diff --git a/test/fuzztest/fileshare_fuzzer/fileshare_fuzzer.h b/test/fuzztest/fileshare_fuzzer/fileshare_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..bea04241580182226eba60d1626fc8f1cfd94ab7 --- /dev/null +++ b/test/fuzztest/fileshare_fuzzer/fileshare_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 FULLSHARE_FUZZER_H +#define FULLSHARE_FUZZER_H + +#define FUZZ_PROJECT_NAME "fileshare_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/fileshare_fuzzer/project.xml b/test/fuzztest/fileshare_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..7133b2b92440904a5ed04b838733acea0f97486a --- /dev/null +++ b/test/fuzztest/fileshare_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + +