From 75ef720e36b10ba2a9cfa3553eb838b211e837d7 Mon Sep 17 00:00:00 2001 From: lishengming14 Date: Thu, 10 Aug 2023 02:03:18 +0000 Subject: [PATCH 01/13] =?UTF-8?q?ipc=E6=A8=A1=E5=9D=97=E5=BC=B9=E6=80=A7?= =?UTF-8?q?=E9=83=A8=E7=BD=B2=E6=95=B4=E6=94=B9=EF=BC=8C=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BF=AE=E6=94=B9ipc=5Fjs=E4=BE=9D=E8=B5=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lishengming14 --- test/fuzztest/externalfileaccess_fuzzer/BUILD.gn | 2 +- test/fuzztest/medialibraryfileaccess_fuzzer/BUILD.gn | 2 +- test/unittest/BUILD.gn | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/fuzztest/externalfileaccess_fuzzer/BUILD.gn b/test/fuzztest/externalfileaccess_fuzzer/BUILD.gn index 0a95c33f..1dcca0ea 100644 --- a/test/fuzztest/externalfileaccess_fuzzer/BUILD.gn +++ b/test/fuzztest/externalfileaccess_fuzzer/BUILD.gn @@ -52,7 +52,7 @@ ohos_fuzztest("ExternalFileAccessFuzzTest") { "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", - "ipc_js:rpc", + "ipc:ipc_rpc", "samgr:samgr_proxy", ] diff --git a/test/fuzztest/medialibraryfileaccess_fuzzer/BUILD.gn b/test/fuzztest/medialibraryfileaccess_fuzzer/BUILD.gn index 1753cfaf..c430ccf9 100644 --- a/test/fuzztest/medialibraryfileaccess_fuzzer/BUILD.gn +++ b/test/fuzztest/medialibraryfileaccess_fuzzer/BUILD.gn @@ -52,7 +52,7 @@ ohos_fuzztest("MedialibraryFileAccessFuzzTest") { "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", - "ipc_js:rpc", + "ipc:ipc_rpc", "samgr:samgr_proxy", ] diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index a47b415e..72505d4c 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -49,7 +49,7 @@ ohos_unittest("medialibrary_file_access_test") { "c_utils:utils", "image_framework:image_native", "ipc:ipc_core", - "ipc_js:rpc", + "ipc:ipc_rpc", "samgr:samgr_proxy", ] @@ -92,7 +92,7 @@ ohos_unittest("external_file_access_test") { "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", - "ipc_js:rpc", + "ipc:ipc_rpc", "samgr:samgr_proxy", ] } -- Gitee From 67bb2f971a850f22c297d6e9895e9ceaf720abf7 Mon Sep 17 00:00:00 2001 From: zhangxiaoliang25 Date: Mon, 14 Aug 2023 03:06:48 +0000 Subject: [PATCH 02/13] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangxiaoliang25 Change-Id: I4740e828b60b86fc4a7bdbfc252e9c2cd6eb2fb5 --- .../FileExtensionAbility.ts | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts index e62440ba..844f6401 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts @@ -40,6 +40,7 @@ const CREATE_EVENT = 0; const DELETE_EVENT = 1; const UPDATE_EVENT = 2; const CONVERT_TO_HEX = 16; +const MOVE_MODLE_CODE = 3; // ['IN_ACCESS', 0x00000001], // ['IN_MODIFY', 0x00000002], @@ -355,24 +356,24 @@ export default class FileExtAbility extends Extension { let statOld = fs.statSync(oldPath); if (!statOld) { - return { - uri: '', - code: E_GETRESULT, - } + return { + uri: '', + code: E_GETRESULT, + } } // isDir if (statOld.isDirectory()) { - fs.moveDirSync(oldPath, getPath(targetParentUri), 3); - return { - uri: newFileUri, - code: ERR_OK, - }; + fs.moveDirSync(oldPath, getPath(targetParentUri), MOVE_MODLE_CODE); + return { + uri: newFileUri, + code: ERR_OK, + }; } // when targetFile is exist, delete it let isAccessNewPath = fs.accessSync(newPath); if (isAccessNewPath) { - fs.unlinkSync(newPath); + fs.unlinkSync(newPath); } fs.moveFileSync(oldPath, newPath, 0); @@ -575,7 +576,7 @@ export default class FileExtAbility extends Extension { /* * selectFileRelativePath formate: /storage/Users/currentUser/filename */ - getFileInfoFromRelativePath(selectFileRelativePath) { + getFileInfoFromRelativePath(selectFileRelativePath): {fileInfo:object, code:number} { let fileInfo = {}; if (!this.checkRelativePath(selectFileRelativePath)) { return { -- Gitee From 03262a302f5d650bb4a1008b44681497fcf4f1a6 Mon Sep 17 00:00:00 2001 From: yxn Date: Thu, 17 Aug 2023 09:21:41 +0800 Subject: [PATCH 03/13] =?UTF-8?q?photoPicker=E9=80=82=E9=85=8D=E5=9B=BE?= =?UTF-8?q?=E5=BA=93=E6=95=B4=E6=94=B9want=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yxn --- interfaces/kits/picker/picker.js | 1 + 1 file changed, 1 insertion(+) diff --git a/interfaces/kits/picker/picker.js b/interfaces/kits/picker/picker.js index 5fa8419f..83871456 100644 --- a/interfaces/kits/picker/picker.js +++ b/interfaces/kits/picker/picker.js @@ -86,6 +86,7 @@ function parsePhotoPickerSelectOption(args) { parameters: { uri: 'multipleselect', }, + uri: 'multipleselect', }; if (args.length > ARGS_ZERO && typeof args[ARGS_ZERO] === 'object') { -- Gitee From fd2441ca952d38eb35b2b8506c0f24bb023f55c4 Mon Sep 17 00:00:00 2001 From: caochuan Date: Wed, 7 Jun 2023 08:47:41 +0800 Subject: [PATCH 04/13] feat:the last commit Signed-off-by: caochuan --- bundle.json | 4 +- .../napi/common/file_extension_info_napi.cpp | 16 ++ .../js/napi/common/file_extension_info_napi.h | 1 + .../js/napi/file_access_module/BUILD.gn | 7 +- .../napi_fileaccess_helper.cpp | 127 +++++++++------- .../napi_observer_callback.cpp | 9 +- .../napi_observer_callback.h | 11 +- .../native_fileaccess_module.cpp | 1 + interfaces/inner_api/file_access/BUILD.gn | 2 + .../include/file_access_ext_ability.h | 3 +- .../include/file_access_ext_proxy.h | 2 +- .../include/file_access_ext_stub_impl.h | 2 +- .../include/file_access_extension_info.h | 9 ++ .../file_access/include/file_access_helper.h | 5 +- .../include/file_access_observer_common.h | 4 +- .../include/ifile_access_ext_base.h | 2 +- .../include/js_file_access_ext_ability.h | 3 +- .../src/file_access_ext_ability.cpp | 9 +- .../file_access/src/file_access_ext_proxy.cpp | 8 +- .../file_access/src/file_access_ext_stub.cpp | 20 ++- .../src/file_access_ext_stub_impl.cpp | 4 +- .../file_access/src/file_access_helper.cpp | 114 +++++++++++++- .../src/js_file_access_ext_ability.cpp | 40 +++-- services/5010.json | 12 ++ services/5010.xml | 24 --- services/BUILD.gn | 81 +++++++++- services/file_access_service.cfg | 3 +- .../FileExtensionAbility.ts | 114 ++++++++------ services/libfile_access_service.map | 26 ++++ .../include/file_access_service.h | 3 +- .../file_access_service_ipc_interface_code.h | 24 +++ .../include/file_access_service_proxy.h | 15 +- .../include/file_access_service_stub.h | 1 + .../include/holder_manager.h | 2 +- .../include/ifile_access_service_base.h | 8 +- .../include/observer_callback_proxy.h | 2 +- .../include/observer_callback_stub.h | 10 +- .../src/file_access_service.cpp | 78 +++++++--- .../src/file_access_service_proxy.cpp | 139 ++++++++++++++---- .../src/file_access_service_stub.cpp | 55 ++++--- .../src/observer_callback_proxy.cpp | 68 +++++++++ .../src/observer_callback_stub.cpp | 87 +++++++++++ utils/file_access_framework_errno.h | 4 +- 43 files changed, 895 insertions(+), 264 deletions(-) create mode 100755 services/5010.json delete mode 100755 services/5010.xml create mode 100644 services/libfile_access_service.map create mode 100644 services/native/file_access_service/include/file_access_service_ipc_interface_code.h create mode 100644 services/native/file_access_service/src/observer_callback_proxy.cpp create mode 100644 services/native/file_access_service/src/observer_callback_stub.cpp diff --git a/bundle.json b/bundle.json index e70b1f26..9701271f 100644 --- a/bundle.json +++ b/bundle.json @@ -26,6 +26,7 @@ "ability_runtime", "ipc", "samgr", + "safwk", "napi", "file_api", "bundle_framework", @@ -34,7 +35,8 @@ "access_token", "c_utils", "image_framework", - "common_event_service" + "common_event_service", + "ability_tools" ] }, "build": { diff --git a/frameworks/js/napi/common/file_extension_info_napi.cpp b/frameworks/js/napi/common/file_extension_info_napi.cpp index 7f690c89..55bb12a3 100644 --- a/frameworks/js/napi/common/file_extension_info_napi.cpp +++ b/frameworks/js/napi/common/file_extension_info_napi.cpp @@ -204,5 +204,21 @@ void InitCopyResult(napi_env env, napi_value exports) sizeof(desc) / sizeof(*desc), desc, &obj); napi_set_named_property(env, exports, className, obj); } + +void InitNotifyType(napi_env env, napi_value exports) +{ + char className[] = "NotifyType"; + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("NOTIFY_ADD", NVal::CreateInt32(env, ADD_EVENT).val_), + DECLARE_NAPI_STATIC_PROPERTY("NOTIFY_DELETE", NVal::CreateInt32(env, DELETE_EVENT).val_), + DECLARE_NAPI_STATIC_PROPERTY("NOTIFY_MOVED_TO", NVal::CreateInt32(env, MOVED_TO).val_), + DECLARE_NAPI_STATIC_PROPERTY("NOTIFY_MOVED_FROM", NVal::CreateInt32(env, MOVED_FROM).val_), + DECLARE_NAPI_STATIC_PROPERTY("NOTIFY_MOVE_SELF", NVal::CreateInt32(env, MOVED_SELF).val_), + }; + napi_value obj = nullptr; + napi_define_class(env, className, NAPI_AUTO_LENGTH, RootInfoConstructor, nullptr, + sizeof(desc) / sizeof(*desc), desc, &obj); + napi_set_named_property(env, exports, className, obj); +} } // namespace FileAccessFwk } // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/common/file_extension_info_napi.h b/frameworks/js/napi/common/file_extension_info_napi.h index fd80ab8d..aa5f4033 100644 --- a/frameworks/js/napi/common/file_extension_info_napi.h +++ b/frameworks/js/napi/common/file_extension_info_napi.h @@ -28,6 +28,7 @@ void InitRootInfo(napi_env env, napi_value exports); void InitOpenFlags(napi_env env, napi_value exports); void InitQueryFlags(napi_env env, napi_value exports); void InitCopyResult(napi_env env, napi_value exports); +void InitNotifyType(napi_env env, napi_value exports); } // namespace FileAccessFwk } // namespace OHOS #endif // FILE_EXTENSION_INFO_NAPI_H \ No newline at end of file diff --git a/frameworks/js/napi/file_access_module/BUILD.gn b/frameworks/js/napi/file_access_module/BUILD.gn index 809dbabb..7479d273 100644 --- a/frameworks/js/napi/file_access_module/BUILD.gn +++ b/frameworks/js/napi/file_access_module/BUILD.gn @@ -35,13 +35,17 @@ ohos_shared_library("fileaccess") { "file_info/napi_file_info_exporter.cpp", "file_info/napi_file_iterator_exporter.cpp", "napi_fileaccess_helper.cpp", + "napi_observer_callback.cpp", "napi_utils.cpp", "native_fileaccess_module.cpp", "root_info/napi_root_info_exporter.cpp", "root_info/napi_root_iterator_exporter.cpp", ] - deps = [ "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit" ] + deps = [ + "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit", + "${user_file_service_path}/services:file_access_service", + ] external_deps = [ "ability_base:want", @@ -55,6 +59,7 @@ ohos_shared_library("fileaccess") { "file_api:filemgmt_libn", "hilog:libhilog", "image_framework:image", + "ipc:ipc_core", "napi:ace_napi", ] } diff --git a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp index c70cd466..78e78b99 100644 --- a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp @@ -240,8 +240,8 @@ napi_value FileAccessHelperInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getFileInfoFromUri", NAPI_GetFileInfoFromUri), DECLARE_NAPI_FUNCTION("getFileInfoFromRelativePath", NAPI_GetFileInfoFromRelativePath), DECLARE_NAPI_FUNCTION("getThumbnail", NAPI_GetThumbnail), - DECLARE_NAPI_FUNCTION("on", NAPI_RegisterObserver), - DECLARE_NAPI_FUNCTION("off", NAPI_UnregisterObserver) + DECLARE_NAPI_FUNCTION("registerObserver", NAPI_RegisterObserver), + DECLARE_NAPI_FUNCTION("unregisterObserver", NAPI_UnregisterObserver) }; napi_value cons = nullptr; NAPI_CALL(env, @@ -1217,6 +1217,8 @@ static bool parseRegisterObserverArgs(napi_env env, NFuncArg &funcArg, std::stri NError(EINVAL).ThrowErr(env); return false; } + + uri = uriPtr.get(); std::tie(succ, notifyForDescendants) = NVal(env, funcArg[NARG_POS::SECOND]).ToBool(); if (!succ) { NError(EINVAL).ThrowErr(env); @@ -1231,60 +1233,77 @@ static bool parseRegisterObserverArgs(napi_env env, NFuncArg &funcArg, std::stri return succ; } -napi_value NAPI_RegisterObserver(napi_env env, napi_callback_info info) +static bool RegisterObserver(napi_env env, NFuncArg &funcArg, sptr &observer) { - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::THREE)) { - NError(EINVAL).ThrowErr(env); - return nullptr; - } - std::string uriString; bool notifyForDescendants = false; if (!parseRegisterObserverArgs(env, funcArg, uriString, notifyForDescendants)) { NError(EINVAL).ThrowErr(env); HILOG_ERROR("parse Args error"); - return nullptr; - } - - sptr callback; - if (callback == nullptr) { - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto finalize = [](napi_env env, void *data, void *hint) { - FileObserverCallbackWrapper *observerWrapper = static_cast(data); - if (observerWrapper != nullptr) { - delete observerWrapper; - } - }; - std::unique_ptr observerWrapper = std::make_unique(); - observerWrapper->callback = callback; - napi_value napiCallback = funcArg[NARG_POS::THIRD]; - if (napi_wrap(env, napiCallback, observerWrapper.get(), finalize, nullptr, nullptr) != napi_ok) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return false; } - observerWrapper.release(); + OHOS::Uri uri(uriString); FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar()); if (fileAccessHelper == nullptr) { - return nullptr; + return false; } - OHOS::Uri uri(uriString); - auto retCode = fileAccessHelper->RegisterNotify(uri, callback, notifyForDescendants); + + auto retCode = fileAccessHelper->RegisterNotify(uri, notifyForDescendants, observer); if (retCode != ERR_OK) { NError(retCode).ThrowErr(env); + return false; + } + return true; +} + +napi_value NAPI_RegisterObserver(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::THREE)) { + NError(EINVAL).ThrowErr(env); return nullptr; } + + napi_value napiCallback = funcArg[NARG_POS::THIRD]; + FileObserverCallbackWrapper* wrapper = nullptr; + if (napi_unwrap(env, napiCallback, (void **)&wrapper) != napi_ok || wrapper == nullptr) { + std::shared_ptr observer = std::make_shared(env, napiCallback); + sptr callback(new (std::nothrow) NapiObserverCallback(observer)); + if (callback == nullptr) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + if (!RegisterObserver(env, funcArg, callback)) { + return nullptr; + } + + auto finalize = [](napi_env env, void *data, void *hint) { + FileObserverCallbackWrapper *observerWrapper = static_cast(data); + if (observerWrapper != nullptr) { + delete observerWrapper; + } + }; + std::unique_ptr observerWrapper = std::make_unique(); + observerWrapper->callback = callback; + if (napi_wrap(env, napiCallback, observerWrapper.get(), finalize, nullptr, nullptr) != napi_ok) { + NError(EINVAL).ThrowErr(env); + HILOG_ERROR("napi_wrap error"); + return nullptr; + } + observerWrapper.release(); + } else { + if (!RegisterObserver(env, funcArg, wrapper->callback)) { + return nullptr; + } + } return NVal::CreateUndefined(env).val_; } napi_value NAPI_UnregisterObserver(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::TWO)) { + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { NError(EINVAL).ThrowErr(env); return nullptr; } @@ -1298,23 +1317,6 @@ napi_value NAPI_UnregisterObserver(napi_env env, napi_callback_info info) return nullptr; } - napi_value napiCallback = funcArg[NARG_POS::SECOND]; - if (!NVal(env, napiCallback).TypeIs(napi_function)) { - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - std::unique_ptr observerWrapper; - if (napi_unwrap(env, napiCallback, (void **)&(observerWrapper)) != napi_ok) { - NError(EINVAL).ThrowErr(env); - return nullptr; - } - if (observerWrapper == nullptr) { - NError(EINVAL).ThrowErr(env); - return nullptr; - } - auto wrapper = observerWrapper.release(); - FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar()); if (fileAccessHelper == nullptr) { return nullptr; @@ -1322,7 +1324,26 @@ napi_value NAPI_UnregisterObserver(napi_env env, napi_callback_info info) std::string uriString(uriPtr.get()); OHOS::Uri uri(uriString); - auto retCode = fileAccessHelper->UnregisterNotify(uri, wrapper->callback); + int retCode = EINVAL; + if (funcArg.GetArgc() == NARG_CNT::ONE) { + retCode = fileAccessHelper->UnregisterNotify(uri); + } else { + napi_value napiCallback = funcArg[NARG_POS::SECOND]; + if (!NVal(env, napiCallback).TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + std::unique_ptr observerWrapper; + if (napi_unwrap(env, napiCallback, (void **)&(observerWrapper)) != napi_ok || observerWrapper == nullptr) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto wrapper = observerWrapper.release(); + retCode = fileAccessHelper->UnregisterNotify(uri, wrapper->callback); + } + if (retCode != ERR_OK) { NError(retCode).ThrowErr(env); return nullptr; diff --git a/frameworks/js/napi/file_access_module/napi_observer_callback.cpp b/frameworks/js/napi/file_access_module/napi_observer_callback.cpp index 26709762..7ea78ef4 100644 --- a/frameworks/js/napi/file_access_module/napi_observer_callback.cpp +++ b/frameworks/js/napi/file_access_module/napi_observer_callback.cpp @@ -83,11 +83,11 @@ void NapiObserver::OnChange(NotifyMessage ¬ifyMessage) work_ = std::make_unique(); } - std::unique_ptr callbackParam = std::make_unique(this, notifyMessage); if (work_ == nullptr) { HILOG_ERROR("napi_get_work failed"); return; } + std::unique_ptr callbackParam = std::make_unique(this, notifyMessage); work_->data = callbackParam.get(); int ret = uv_queue_work(loop, work_.get(), [](uv_work_t *work) {}, @@ -107,9 +107,9 @@ void NapiObserver::OnChange(NotifyMessage ¬ifyMessage) napi_value callback = nullptr; napi_value args[ARGS_ONE] = {napiNotifyMessage.val_}; - napi_status status = napi_get_reference_value(param->napiObserver->env_, param->napiObserver->cbOnRef_, + napi_status ret = napi_get_reference_value(param->napiObserver->env_, param->napiObserver->cbOnRef_, &callback); - if (status != napi_ok) { + if (ret != napi_ok) { HILOG_ERROR("Notify get reference failed, status:%{public}d.", status); napi_close_handle_scope(param->napiObserver->env_, scope); return; @@ -117,7 +117,7 @@ void NapiObserver::OnChange(NotifyMessage ¬ifyMessage) napi_value global = nullptr; napi_get_global(param->napiObserver->env_, &global); napi_value result = nullptr; - napi_status ret = napi_call_function(param->napiObserver->env_, global, callback, ARGS_ONE, args, &result); + ret = napi_call_function(param->napiObserver->env_, global, callback, ARGS_ONE, args, &result); if (ret != napi_ok) { HILOG_ERROR("Notify failed, status:%{public}d.", ret); napi_close_handle_scope(param->napiObserver->env_, scope); @@ -127,6 +127,7 @@ void NapiObserver::OnChange(NotifyMessage ¬ifyMessage) }); if (ret == 0) { callbackParam.release(); + work_.release(); } } } // namespace FileAccessFwk diff --git a/frameworks/js/napi/file_access_module/napi_observer_callback.h b/frameworks/js/napi/file_access_module/napi_observer_callback.h index a3977a62..87e3cb27 100644 --- a/frameworks/js/napi/file_access_module/napi_observer_callback.h +++ b/frameworks/js/napi/file_access_module/napi_observer_callback.h @@ -18,10 +18,12 @@ #include "file_access_framework_errno.h" #include "hilog_wrapper.h" +#include "observer_callback_stub.h" #include "napi/native_common.h" #include "napi/native_node_api.h" #include "napi/native_api.h" #include "uri.h" +#include "uv.h" namespace OHOS { namespace FileAccessFwk { @@ -42,11 +44,12 @@ private: std::unique_ptr work_ = nullptr; }; -class NapiObserverCallback { +class NapiObserverCallback : public ObserverCallbackStub { public: - explicit NapiObserverCallback(std::shared_ptr observer): observer_(observer) {} - virtual ~NapiObserverCallback() {} - void OnChange(NotifyMessage ¬ifyMessage) + explicit NapiObserverCallback(std::shared_ptr observer): ObserverCallbackStub(), + observer_(observer) {} + virtual ~NapiObserverCallback() = default; + void OnChange(NotifyMessage ¬ifyMessage) override { observer_->OnChange(notifyMessage); } diff --git a/frameworks/js/napi/file_access_module/native_fileaccess_module.cpp b/frameworks/js/napi/file_access_module/native_fileaccess_module.cpp index ec6c987d..9721c3ff 100644 --- a/frameworks/js/napi/file_access_module/native_fileaccess_module.cpp +++ b/frameworks/js/napi/file_access_module/native_fileaccess_module.cpp @@ -44,6 +44,7 @@ static napi_value Init(napi_env env, napi_value exports) InitOpenFlags(env, exports); InitQueryFlags(env, exports); InitCopyResult(env, exports); + InitNotifyType(env, exports); std::vector> products; products.emplace_back(std::make_unique(env, exports)); diff --git a/interfaces/inner_api/file_access/BUILD.gn b/interfaces/inner_api/file_access/BUILD.gn index dfd61355..6ef2cb02 100644 --- a/interfaces/inner_api/file_access/BUILD.gn +++ b/interfaces/inner_api/file_access/BUILD.gn @@ -51,9 +51,11 @@ ohos_shared_library("file_access_extension_ability_kit") { include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context", "${file_api_path}/utils/filemgmt_libn/include", + "${user_file_service_path}/services/native/file_access_service/include", ] sources = [ + "${user_file_service_path}/services/native/file_access_service/src/file_access_service_proxy.cpp", "src/file_access_ext_ability.cpp", "src/file_access_ext_connection.cpp", "src/file_access_ext_proxy.cpp", diff --git a/interfaces/inner_api/file_access/include/file_access_ext_ability.h b/interfaces/inner_api/file_access/include/file_access_ext_ability.h index 81c306db..2479bc65 100644 --- a/interfaces/inner_api/file_access/include/file_access_ext_ability.h +++ b/interfaces/inner_api/file_access/include/file_access_ext_ability.h @@ -62,8 +62,7 @@ public: virtual int Access(const Uri &uri, bool &isExist); static void SetCreator(const CreatorFunc& creator); virtual int StartWatcher(const Uri &uri); - virtual int StopWatcher(const Uri &uri); - virtual int Notify(Uri &uri, NotifyType notifyType); + virtual int StopWatcher(const Uri &uri, bool isUnregisterAll); private: static CreatorFunc creator_; }; diff --git a/interfaces/inner_api/file_access/include/file_access_ext_proxy.h b/interfaces/inner_api/file_access/include/file_access_ext_proxy.h index c681ae6b..7b981fd3 100644 --- a/interfaces/inner_api/file_access/include/file_access_ext_proxy.h +++ b/interfaces/inner_api/file_access/include/file_access_ext_proxy.h @@ -58,7 +58,7 @@ public: virtual int GetRoots(std::vector &rootInfoVec) override; virtual int Access(const Uri &uri, bool &isExist) override; virtual int StartWatcher(const Uri &uri) override; - virtual int StopWatcher(const Uri &uri) override; + virtual int StopWatcher(const Uri &uri, bool isUnregisterAll) override; private: static inline BrokerDelegator delegator_; }; diff --git a/interfaces/inner_api/file_access/include/file_access_ext_stub_impl.h b/interfaces/inner_api/file_access/include/file_access_ext_stub_impl.h index 9acb2c65..07db5e0b 100644 --- a/interfaces/inner_api/file_access/include/file_access_ext_stub_impl.h +++ b/interfaces/inner_api/file_access/include/file_access_ext_stub_impl.h @@ -55,7 +55,7 @@ public: int GetRoots(std::vector &rootInfoVec) override; int Access(const Uri &uri, bool &isExist) override; int StartWatcher(const Uri &uri) override; - int StopWatcher(const Uri &uri) override; + int StopWatcher(const Uri &uri, bool isUnregisterAll) override; private: std::shared_ptr GetOwner(); diff --git a/interfaces/inner_api/file_access/include/file_access_extension_info.h b/interfaces/inner_api/file_access/include/file_access_extension_info.h index 3d9781c1..141f2c8a 100644 --- a/interfaces/inner_api/file_access/include/file_access_extension_info.h +++ b/interfaces/inner_api/file_access/include/file_access_extension_info.h @@ -71,6 +71,15 @@ constexpr int32_t READ = 0; constexpr int32_t WRITE = 1; constexpr int32_t WRITE_READ = 2; +/** + * Indicates the supported Event change type. + */ +constexpr int32_t ADD_EVENT = 0; +constexpr int32_t DELETE_EVENT = 1; +constexpr int32_t MOVED_TO = 2; +constexpr int32_t MOVED_FROM = 3; +constexpr int32_t MOVED_SELF = 4; + struct FileInfo : public virtual OHOS::Parcelable { public: std::string uri { "" }; diff --git a/interfaces/inner_api/file_access/include/file_access_helper.h b/interfaces/inner_api/file_access/include/file_access_helper.h index 8404b768..4fb45998 100644 --- a/interfaces/inner_api/file_access/include/file_access_helper.h +++ b/interfaces/inner_api/file_access/include/file_access_helper.h @@ -86,11 +86,12 @@ public: int GetFileInfoFromUri(Uri &selectFile, FileInfo &fileInfo); int GetFileInfoFromRelativePath(std::string &selectFile, FileInfo &fileInfo); int GetRoots(std::vector &rootInfoVec); - int RegisterNotify(Uri uri, sptr &observer, bool notifyForDescendants); + int RegisterNotify(Uri uri, bool notifyForDescendants, sptr &observer); int UnregisterNotify(Uri uri, sptr &observer); + int UnregisterNotify(Uri uri); private: int StartWatcher(Uri &uri); - int StopWatcher(Uri &uri); + int StopWatcher(Uri &uri, bool isUnregisterAll); sptr GetProxyByUri(Uri &uri); sptr GetProxyByBundleName(const std::string &bundleName); bool GetProxy(); diff --git a/interfaces/inner_api/file_access/include/file_access_observer_common.h b/interfaces/inner_api/file_access/include/file_access_observer_common.h index 06e7b6cc..75f600f1 100644 --- a/interfaces/inner_api/file_access/include/file_access_observer_common.h +++ b/interfaces/inner_api/file_access/include/file_access_observer_common.h @@ -29,7 +29,9 @@ namespace FileAccessFwk { enum NotifyType { NOTIFY_ADD = 0, NOTIFY_DELETE, - NOTIFY_UPDATE + NOTIFY_MOVE_TO, + NOTIFY_MOVE_FROM, + NOTIFY_MOVE_SELE }; struct NotifyMessage : public OHOS::Parcelable { diff --git a/interfaces/inner_api/file_access/include/ifile_access_ext_base.h b/interfaces/inner_api/file_access/include/ifile_access_ext_base.h index 4a30083e..765d05a8 100644 --- a/interfaces/inner_api/file_access/include/ifile_access_ext_base.h +++ b/interfaces/inner_api/file_access/include/ifile_access_ext_base.h @@ -75,7 +75,7 @@ public: virtual int GetRoots(std::vector &rootInfoVec) = 0; virtual int Access(const Uri &uri, bool &isExist) = 0; virtual int StartWatcher(const Uri &uri) = 0; - virtual int StopWatcher(const Uri &uri) = 0; + virtual int StopWatcher(const Uri &uri, bool isUnregisterAll) = 0; }; } // namespace FileAccessFwk } // namespace OHOS diff --git a/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h b/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h index ee5c59f1..af4fe90d 100644 --- a/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h +++ b/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h @@ -76,13 +76,14 @@ public: int Access(const Uri &uri, bool &isExist) override; int Query(const Uri &uri, std::vector &columns, std::vector &results) override; int StartWatcher(const Uri &uri) override; - int StopWatcher(const Uri &uri) override; + int StopWatcher(const Uri &uri, bool isUnregisterAll) override; private: NativeValue* CallObjectMethod(const char *name, NativeValue * const *argv = nullptr, size_t argc = 0); int CallJsMethod(const std::string &funcName, JsRuntime &jsRuntime, NativeReference *jsObj, InputArgsParser argParser, ResultValueParser retParser); void GetSrcPath(std::string &srcPath); + static int Notify(Uri &uri, NotifyType notifyType); static NativeValue* FuncCallback(NativeEngine *engine, NativeCallbackInfo *info); JsRuntime &jsRuntime_; std::shared_ptr jsObj_; diff --git a/interfaces/inner_api/file_access/src/file_access_ext_ability.cpp b/interfaces/inner_api/file_access/src/file_access_ext_ability.cpp index c56c4fd6..74f60456 100644 --- a/interfaces/inner_api/file_access/src/file_access_ext_ability.cpp +++ b/interfaces/inner_api/file_access/src/file_access_ext_ability.cpp @@ -20,6 +20,7 @@ #include "file_access_framework_errno.h" #include "hilog_wrapper.h" #include "hitrace_meter.h" +#include "if_system_ability_manager.h" #include "js_file_access_ext_ability.h" #include "runtime.h" @@ -159,16 +160,10 @@ int FileAccessExtAbility::StartWatcher(const Uri &uri) return EPERM; } -int FileAccessExtAbility::StopWatcher(const Uri &uri) +int FileAccessExtAbility::StopWatcher(const Uri &uri, bool isUnregisterAll) { HILOG_ERROR("FileAccessExtAbility::StopWatcher Undefined operation"); return EPERM; } - -int FileAccessExtAbility::Notify(Uri &uri, NotifyType notifyType) -{ - HILOG_ERROR("FileAccessExtAbility::Notify Undefined operation"); - return EPERM; -} } // namespace FileAccessFwk } // namespace OHOS diff --git a/interfaces/inner_api/file_access/src/file_access_ext_proxy.cpp b/interfaces/inner_api/file_access/src/file_access_ext_proxy.cpp index 130b4ab3..3e129e9a 100644 --- a/interfaces/inner_api/file_access/src/file_access_ext_proxy.cpp +++ b/interfaces/inner_api/file_access/src/file_access_ext_proxy.cpp @@ -989,7 +989,7 @@ int FileAccessExtProxy::StartWatcher(const Uri &uri) return ERR_OK; } -int FileAccessExtProxy::StopWatcher(const Uri &uri) +int FileAccessExtProxy::StopWatcher(const Uri &uri, bool isUnregisterAll) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "StopWatcher"); MessageParcel data; @@ -1006,6 +1006,12 @@ int FileAccessExtProxy::StopWatcher(const Uri &uri) return E_IPCS; } + if (!data.WriteBool(isUnregisterAll)) { + HILOG_ERROR("fail to WriteBool isUnregisterAll"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + MessageParcel reply; MessageOption option; int err = Remote()->SendRequest(CMD_STOP_WATCHER, data, reply, option); diff --git a/interfaces/inner_api/file_access/src/file_access_ext_stub.cpp b/interfaces/inner_api/file_access/src/file_access_ext_stub.cpp index 4c2e17b8..d9c7b240 100644 --- a/interfaces/inner_api/file_access/src/file_access_ext_stub.cpp +++ b/interfaces/inner_api/file_access/src/file_access_ext_stub.cpp @@ -71,12 +71,6 @@ int FileAccessExtStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Messa MessageOption& option) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnRemoteRequest"); - if (!CheckCallingPermission(FILE_ACCESS_PERMISSION)) { - HILOG_ERROR("permission error"); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return E_PERMISSION; - } - std::u16string descriptor = FileAccessExtStub::GetDescriptor(); std::u16string remoteDescriptor = data.ReadInterfaceToken(); if (descriptor != remoteDescriptor) { @@ -90,6 +84,11 @@ int FileAccessExtStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Messa return (this->*(itFunc->second))(data, reply); } + if (!CheckCallingPermission(FILE_ACCESS_PERMISSION)) { + HILOG_ERROR("permission error"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_PERMISSION; + } FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } @@ -767,8 +766,15 @@ ErrCode FileAccessExtStub::CmdStopWatcher(MessageParcel &data, MessageParcel &re return EINVAL; } + bool isUnregisterAll = false; + if (!data.ReadBool(isUnregisterAll)) { + HILOG_ERROR("Parameter Copy fail to ReadBool isUnregisterAll"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + Uri uri(uriString); - int ret = StopWatcher(uri); + int ret = StopWatcher(uri, isUnregisterAll); if (!reply.WriteInt32(ret)) { HILOG_ERROR("Parameter StopWatcher fail to WriteInt32 ret"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); diff --git a/interfaces/inner_api/file_access/src/file_access_ext_stub_impl.cpp b/interfaces/inner_api/file_access/src/file_access_ext_stub_impl.cpp index dc53c8f8..64427690 100644 --- a/interfaces/inner_api/file_access/src/file_access_ext_stub_impl.cpp +++ b/interfaces/inner_api/file_access/src/file_access_ext_stub_impl.cpp @@ -262,7 +262,7 @@ int FileAccessExtStubImpl::StartWatcher(const Uri &uri) return ret; } -int FileAccessExtStubImpl::StopWatcher(const Uri &uri) +int FileAccessExtStubImpl::StopWatcher(const Uri &uri, bool isUnregisterAll) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "StopWatcher"); if (extension_ == nullptr) { @@ -271,7 +271,7 @@ int FileAccessExtStubImpl::StopWatcher(const Uri &uri) return E_IPCS; } - int ret = extension_->StopWatcher(uri); + int ret = extension_->StopWatcher(uri, isUnregisterAll); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return ret; } diff --git a/interfaces/inner_api/file_access/src/file_access_helper.cpp b/interfaces/inner_api/file_access/src/file_access_helper.cpp index 49892471..eede30ab 100644 --- a/interfaces/inner_api/file_access/src/file_access_helper.cpp +++ b/interfaces/inner_api/file_access/src/file_access_helper.cpp @@ -20,6 +20,7 @@ #include "bundle_mgr_proxy.h" #include "file_access_framework_errno.h" #include "file_access_extension_info.h" +#include "file_access_service_proxy.h" #include "hilog_wrapper.h" #include "hitrace_meter.h" #include "if_system_ability_manager.h" @@ -1190,7 +1191,7 @@ int FileAccessHelper::StartWatcher(Uri &uri) return ERR_OK; } -int FileAccessHelper::StopWatcher(Uri &uri) +int FileAccessHelper::StopWatcher(Uri &uri, bool isUnregisterAll) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "StopWatcher"); sptr fileExtProxy = GetProxyByUri(uri); @@ -1200,9 +1201,9 @@ int FileAccessHelper::StopWatcher(Uri &uri) return E_IPCS; } - int ret = fileExtProxy->StopWatcher(uri); + int ret = fileExtProxy->StopWatcher(uri, isUnregisterAll); if (ret != ERR_OK) { - HILOG_ERROR("Delete get result error, code:%{public}d", ret); + HILOG_ERROR("StopWatcher get result error, code:%{public}d", ret); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return ret; } @@ -1211,16 +1212,117 @@ int FileAccessHelper::StopWatcher(Uri &uri) return ERR_OK; } -int FileAccessHelper::RegisterNotify(Uri uri, sptr &observer, bool notifyForDescendants) +int FileAccessHelper::RegisterNotify(Uri uri, bool notifyForDescendants, sptr &observer) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "RegisterNotify"); - return ERR_OK; + if (!IsSystemApp()) { + HILOG_ERROR("FileAccessHelper::RegisterNotify check IsSystemAppByFullTokenID failed"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_PERMISSION_SYS; + } + + if (!CheckUri(uri) || observer == nullptr) { + HILOG_ERROR("parameter check error."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return EINVAL; + } + + auto proxy = FileAccessServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOG_ERROR("RegisterNotify get SA failed"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_LOAD_SA; + } + + int ret = proxy->RegisterNotify(uri, notifyForDescendants, observer); + if (ret != ERR_OK) { + HILOG_ERROR("RegisterNotify error ret = %{public}d", ret); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + + ret = StartWatcher(uri); + if (ret != ERR_OK) { + HILOG_ERROR("StartWatcher error ret = %{public}d", ret); + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; } int FileAccessHelper::UnregisterNotify(Uri uri, sptr &observer) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "UnregisterNotify"); - return ERR_OK; + if (!IsSystemApp()) { + HILOG_ERROR("FileAccessHelper::UnregisterNotify check IsSystemAppByFullTokenID failed"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_PERMISSION_SYS; + } + + if (!CheckUri(uri) || observer == nullptr) { + HILOG_ERROR("parameter check error."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return EINVAL; + } + + auto proxy = FileAccessServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOG_ERROR("UnregisterNotify get SA failed"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_LOAD_SA; + } + + int ret = proxy->UnregisterNotify(uri, observer); + if (ret != ERR_OK) { + HILOG_ERROR("UnregisterNotify error ret = %{public}d", ret); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + + bool isUnregisterAll = false; + ret = StopWatcher(uri, isUnregisterAll); + if (ret != ERR_OK) { + HILOG_ERROR("StopWatcher error ret = %{public}d", ret); + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; +} + +int FileAccessHelper::UnregisterNotify(Uri uri) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "UnregisterNotify"); + if (!IsSystemApp()) { + HILOG_ERROR("FileAccessHelper::UnregisterNotify check IsSystemAppByFullTokenID failed"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_PERMISSION_SYS; + } + + if (!CheckUri(uri)) { + HILOG_ERROR("parameter check error."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return EINVAL; + } + auto proxy = FileAccessServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOG_ERROR("UnregisterNotify get SA failed"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_LOAD_SA; + } + + sptr observer = nullptr; + int ret = proxy->UnregisterNotify(uri, observer); + if (ret != ERR_OK) { + HILOG_ERROR("UnregisterNotify error ret = %{public}d", ret); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + + bool isUnregisterAll = true; + ret = StopWatcher(uri, isUnregisterAll); + if (ret != ERR_OK) { + HILOG_ERROR("StopWatcher error ret = %{public}d", ret); + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; } void FileAccessDeathRecipient::OnRemoteDied(const wptr &remote) diff --git a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp index 1c20aa4e..897e287d 100644 --- a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp +++ b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp @@ -18,13 +18,16 @@ #include "ability_info.h" #include "accesstoken_kit.h" #include "extension_context.h" +#include "file_access_service_proxy.h" #include "file_access_ext_stub_impl.h" #include "file_access_observer_common.h" #include "file_access_extension_info.h" #include "file_access_framework_errno.h" #include "hilog_wrapper.h" #include "hitrace_meter.h" +#include "if_system_ability_manager.h" #include "ipc_skeleton.h" +#include "iservice_registry.h" #include "js_runtime.h" #include "js_runtime_utils.h" #include "napi/native_api.h" @@ -32,6 +35,7 @@ #include "napi_common_util.h" #include "napi_common_want.h" #include "napi_remote_object.h" +#include "system_ability_definition.h" namespace OHOS { namespace FileAccessFwk { @@ -1378,16 +1382,9 @@ NativeValue* JsFileAccessExtAbility::FuncCallback(NativeEngine* engine, NativeCa int32_t event = UnwrapInt32FromJS(reinterpret_cast(engine), reinterpret_cast(info->argv[ARGC_ONE])); - JsFileAccessExtAbility* jsExtension = static_cast(info->functionInfo->data); - if (jsExtension == nullptr) { - HILOG_ERROR("invalid JsFileAccessExtAbility."); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return engine->CreateUndefined(); - } - Uri uri(uriString); NotifyType notifyType = static_cast(event); - auto ret = jsExtension->Notify(uri, notifyType); + auto ret = Notify(uri, notifyType); if (ret != ERR_OK) { HILOG_ERROR("JsFileAccessExtAbility notify error, ret:%{public}d", ret); } @@ -1445,7 +1442,7 @@ int JsFileAccessExtAbility::StartWatcher(const Uri &uri) return ERR_OK; } -int JsFileAccessExtAbility::StopWatcher(const Uri &uri) +int JsFileAccessExtAbility::StopWatcher(const Uri &uri, bool isUnregisterAll) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "StopWatcher"); auto ret = std::make_shared(); @@ -1455,14 +1452,16 @@ int JsFileAccessExtAbility::StopWatcher(const Uri &uri) return E_GETRESULT; } - auto argParser = [uri](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { + auto argParser = [uri, isUnregisterAll](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { NativeValue *nativeUri = engine.CreateString(uri.ToString().c_str(), uri.ToString().length()); if (nativeUri == nullptr) { HILOG_ERROR("create uri native js value fail."); return false; } + NativeValue *isCleanAll = engine.CreateBoolean(isUnregisterAll); argv[ARGC_ZERO] = nativeUri; - argc = ARGC_ONE; + argv[ARGC_ONE] = isCleanAll; + argc = ARGC_TWO; return true; }; @@ -1490,5 +1489,24 @@ int JsFileAccessExtAbility::StopWatcher(const Uri &uri) FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return ERR_OK; } + +int JsFileAccessExtAbility::Notify(Uri &uri, NotifyType notifyType) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Notify"); + auto proxy = FileAccessServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOG_ERROR("Notify get SA failed"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_LOAD_SA; + } + auto ret = proxy->OnChange(uri, notifyType); + if (ret != ERR_OK) { + HILOG_ERROR("notify error, ret:%{public}d", ret); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; +} } // namespace FileAccessFwk } // namespace OHOS diff --git a/services/5010.json b/services/5010.json new file mode 100755 index 00000000..8f40348d --- /dev/null +++ b/services/5010.json @@ -0,0 +1,12 @@ +{ + "process": "file_access_service", + "systemability": [ + { + "name": 5010, + "libpath": "libfile_access_service.z.so", + "run-on-create": false, + "distributed": false, + "dump_level": 1 + } + ] +} \ No newline at end of file diff --git a/services/5010.xml b/services/5010.xml deleted file mode 100755 index eb3849d6..00000000 --- a/services/5010.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - file_access_service - - 5010 - libfile_access_service.z.so - false - false - 1 - - \ No newline at end of file diff --git a/services/BUILD.gn b/services/BUILD.gn index 9f2c88e9..0aa5c711 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -15,7 +15,12 @@ import("//build/ohos.gni") import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") group("user_file_managers") { - deps = [ ":external_file_manager_hap" ] + deps = [ + ":external_file_manager_hap", + ":file_access_service", + ":file_access_service.cfg", + ":file_access_service_profile", + ] } ohos_hap("external_file_manager_hap") { @@ -42,8 +47,82 @@ ohos_app_scope("external_file_manager_app_profile") { sources = [ "file_extension_hap/AppScope/resources" ] } +ohos_sa_profile("file_access_service_profile") { + sources = [ "5010.json" ] + part_name = "user_file_service" +} + +ohos_prebuilt_etc("file_access_service.cfg") { + source = "file_access_service.cfg" + relative_install_dir = "init" + subsystem_name = "filemanagement" + part_name = "user_file_service" +} + +config("ability_config") { + visibility = [ ":*" ] + include_dirs = [ + "native/file_access_service/include", + "${user_file_service_path}/utils", + "${user_file_service_path}/interfaces/inner_api/file_access/include", + "${user_file_service_path}/frameworks/js/napi/file_access_module", + "${ability_runtime_services_path}/common/include", + "${ability_runtime_kits_path}/ability/native/include/continuation/distributed", + "${ability_runtime_kits_path}/ability/native/include/continuation/kits", + "${ability_runtime_kits_path}/appkit/native/app/include", + "${ability_runtime_napi_path}/inner/napi_common", + "${access_token_path}/interfaces/innerkits/accesstoken/include", + ] +} + +config("ability_public_config") { + visibility = [ ":*" ] + include_dirs = [ + "native/file_access_service/include", + "${user_file_service_path}/utils", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/app", + "${ability_runtime_kits_path}/appkit/native/app/include", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/context", + "${user_file_service_path}/interfaces/kits/js/src/common", + ] +} + ohos_resources("external_file_manager_resources") { sources = [ "file_extension_hap/entry/src/main/resources" ] deps = [ ":external_file_manager_app_profile" ] hap_profile = "file_extension_hap/entry/src/main/module.json" } + +ohos_shared_library("file_access_service") { + include_dirs = [ + "${user_file_service_path}/services/native/file_access_service/include", + "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context", + ] + shlib_type = "sa" + sources = [ + "native/file_access_service/src/file_access_service.cpp", + "native/file_access_service/src/file_access_service_proxy.cpp", + "native/file_access_service/src/file_access_service_stub.cpp", + "native/file_access_service/src/observer_callback_proxy.cpp", + "native/file_access_service/src/observer_callback_stub.cpp", + ] + configs = [ ":ability_config" ] + version_script = "libfile_access_service.map" + public_configs = [ ":ability_public_config" ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "c_utils:utils", + "hilog:libhilog", + "hitrace:hitrace_meter", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + subsystem_name = "filemanagement" + part_name = "user_file_service" +} diff --git a/services/file_access_service.cfg b/services/file_access_service.cfg index 8b3e76a2..f7428302 100644 --- a/services/file_access_service.cfg +++ b/services/file_access_service.cfg @@ -1,8 +1,9 @@ { "services" : [{ "name" : "file_access_service", - "path" : ["/system/bin/sa_main", "/system/profile/file_access_service.xml"], + "path" : ["/system/bin/sa_main", "/system/profile/file_access_service.json"], "uid" : "file_manager", + "ondemand" : true, "secon" : "u:r:file_access_service:s0" } ] diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts index 844f6401..5766ed36 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts @@ -34,13 +34,27 @@ const E_INVAL = 13900020; const E_URIS = 14300002; const E_GETRESULT = 14300004; const CREATE_EVENT_CODE = 0x00000100; -const DELETE_EVENT_CODE = 0x00000200 | 0x00000400; -const UPDATE_EVENT_CODE = 0x00000040 | 0x00000080 | 0x00000800; +const IN_DELETE_EVENT_CODE = 0x00000200; +const DELETE_SELF_EVENT_CODE = 0x00000400; +const MOVE_TO_CODE = 0x00000080; +const MOVED_FROM_CODE = 0x00000040; +const MOVE_SELF_CODE = 0x00000800; +const MOVE_MODLE_CODE = 3; const CREATE_EVENT = 0; const DELETE_EVENT = 1; -const UPDATE_EVENT = 2; -const CONVERT_TO_HEX = 16; -const MOVE_MODLE_CODE = 3; +const MOVED_TO = 2; +const MOVED_FROM = 3; +const MOVED_SELF = 4; +let observerMap = new Map(); +let watcherCountMap = new Map(); +let eventMap = new Map([ + [CREATE_EVENT_CODE, CREATE_EVENT], + [IN_DELETE_EVENT_CODE, DELETE_EVENT], + [DELETE_SELF_EVENT_CODE, DELETE_EVENT], + [MOVE_TO_CODE, MOVED_TO], + [MOVED_FROM_CODE, MOVED_FROM], + [MOVE_SELF_CODE, MOVED_SELF] +]); // ['IN_ACCESS', 0x00000001], // ['IN_MODIFY', 0x00000002], @@ -55,21 +69,6 @@ const MOVE_MODLE_CODE = 3; // ['IN_DELETE_SELF', 0x00000400], // ['IN_MOVE_SELF', 0x00000800] -enum createEvent { - create = 100 -} - -enum deleteEvent { - delete = 200, - deleteSelf = 400 -} - -enum updataEvent { - moveSelf = 800, - moveFrom = 40, - moveTo = 80 -} - export default class FileExtAbility extends Extension { onCreate(want): void { hilog.info(DOMAIN_CODE, TAG, 'Extension init process'); @@ -378,8 +377,8 @@ export default class FileExtAbility extends Extension { fs.moveFileSync(oldPath, newPath, 0); return { - uri: newFileUri, - code: ERR_OK, + uri: newFileUri, + code: ERR_OK, }; } catch (e) { hilog.error(DOMAIN_CODE, TAG, 'move error ' + e.message); @@ -760,29 +759,34 @@ export default class FileExtAbility extends Extension { } let watchPath = getPath(uri); try { - let watcher = fs.createWatcher(watchPath, CREATE_EVENT_CODE | DELETE_EVENT_CODE | UPDATE_EVENT_CODE, (data) => { - try { - let notifyEvent = (data.event).toString(CONVERT_TO_HEX); - if (notifyEvent in createEvent) { - notifyEvent = CREATE_EVENT; - } - - if (notifyEvent in deleteEvent) { - notifyEvent = DELETE_EVENT; + if (!observerMap.has(uri)) { + let watcher = fs.createWatcher(watchPath, CREATE_EVENT_CODE | IN_DELETE_EVENT_CODE | DELETE_SELF_EVENT_CODE | + MOVE_TO_CODE | MOVED_FROM_CODE | MOVE_SELF_CODE, (data) => { + try { + let eventCode = -1; + eventMap.forEach((value, key) => { + if (data.event & key) { + eventCode = value; + } + }); + let targetUri = FILE_PREFIX_NAME + BUNDLE_NAME + data.fileName; + targetUri = this.encode(targetUri); + if (eventCode >= 0) { + callback(targetUri, eventCode); + } else { + hilog.info(DOMAIN_CODE, TAG, 'eventCode =' + data.event); + } + } catch (error) { + hilog.error(DOMAIN_CODE, TAG, 'onchange error ' + error.message); } - - if (notifyEvent in updataEvent) { - notifyEvent = UPDATE_EVENT; - } - let watchUri = FILE_PREFIX_NAME + BUNDLE_NAME + watchPath; - watchUri = this.encode(watchUri); - callback(watchUri, notifyEvent); - } catch (error) { - hilog.error(DOMAIN_CODE, TAG, 'onchange error ' + error.message); - } - }); - observerMap.set(uri, watcher); - watcher.start(); + }); + watcher.start(); + observerMap.set(uri, watcher); + watcherCountMap.set(uri, 1); + } else { + let temp = watcherCountMap.get(uri) + 1; + watcherCountMap.set(uri, temp); + } } catch (e) { hilog.error(DOMAIN_CODE, TAG, 'startWatcher error ' + e.message); return E_GETRESULT; @@ -790,7 +794,7 @@ export default class FileExtAbility extends Extension { return ERR_OK; } - stopWatcher(uri): number { + stopWatcher(uri, isUnregisterAll): number { uri = this.decode(uri); if (uri === '') { return { @@ -802,9 +806,23 @@ export default class FileExtAbility extends Extension { return E_URIS; } try { - let watcher = observerMap.get(uri); - watcher.stop(); - observerMap.delete(uri); + if (!watcherCountMap.has(uri)) { + return E_GETRESULT; + } + if (isUnregisterAll) { + watcherCountMap.set(uri, 0); + } else { + let temp = watcherCountMap.get(uri) - 1; + watcherCountMap.set(uri, temp); + } + if (watcherCountMap.get(uri) === 0) { + watcherCountMap.delete(uri); + let watcher = observerMap.get(uri); + if (typeof watcher !== undefined) { + watcher.stop(); + observerMap.delete(uri); + } + } } catch (e) { hilog.error(DOMAIN_CODE, TAG, 'stopWatcher error ' + e.message); return E_GETRESULT; diff --git a/services/libfile_access_service.map b/services/libfile_access_service.map new file mode 100644 index 00000000..e7160ffa --- /dev/null +++ b/services/libfile_access_service.map @@ -0,0 +1,26 @@ +# 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. + +1.0 { + global: + extern "C++" { + *FileAccessServiceProxy*; + *FileAccessService*; + *FileAccessServiceStub*; + *ObserverCallbackStub*; + *ObserverCallbackProxy*; + **; + }; + local: + *; +}; \ No newline at end of file diff --git a/services/native/file_access_service/include/file_access_service.h b/services/native/file_access_service/include/file_access_service.h index afc984ec..b34ca915 100644 --- a/services/native/file_access_service/include/file_access_service.h +++ b/services/native/file_access_service/include/file_access_service.h @@ -90,8 +90,9 @@ public: int32_t Dump(int32_t fd, const std::vector &args) override; public: - int32_t RegisterNotify(Uri uri, const sptr &observer, bool notifyForDescendants) override; + int32_t RegisterNotify(Uri uri, bool notifyForDescendants, const sptr &observer) override; int32_t UnregisterNotify(Uri uri, const sptr &observer) override; + int32_t CleanAllNotify(Uri uri) override; int32_t OnChange(Uri uri, NotifyType notifyType) override; private: diff --git a/services/native/file_access_service/include/file_access_service_ipc_interface_code.h b/services/native/file_access_service/include/file_access_service_ipc_interface_code.h new file mode 100644 index 00000000..1621656a --- /dev/null +++ b/services/native/file_access_service/include/file_access_service_ipc_interface_code.h @@ -0,0 +1,24 @@ +/* + * 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. + */ + +namespace OHOS { +namespace FileAccessFwk { + enum class FileAccessServiceInterfaceCode { + CMD_REGISTER_NOTIFY = 0, + CMD_UNREGISTER_NOTIFY, + CMD_ONCHANGE + }; +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/services/native/file_access_service/include/file_access_service_proxy.h b/services/native/file_access_service/include/file_access_service_proxy.h index cd9bf2dd..64bec4e0 100644 --- a/services/native/file_access_service/include/file_access_service_proxy.h +++ b/services/native/file_access_service/include/file_access_service_proxy.h @@ -18,6 +18,7 @@ #include "ifile_access_service_base.h" #include "iremote_proxy.h" +#include "system_ability_load_callback_stub.h" namespace OHOS { namespace FileAccessFwk { @@ -25,11 +26,23 @@ class FileAccessServiceProxy : public IRemoteProxy { public: explicit FileAccessServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} ~FileAccessServiceProxy() = default; + static sptr GetInstance(); int32_t OnChange(Uri uri, NotifyType notifyType) override; - int32_t RegisterNotify(Uri uri, const sptr &observer, bool notifyForDescendants) override; + int32_t RegisterNotify(Uri uri, bool notifyForDescendants, const sptr &observer) override; int32_t UnregisterNotify(Uri uri, const sptr &observer) override; + class ServiceProxyLoadCallback : public SystemAbilityLoadCallbackStub { + public: + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr &remoteObject) override; + void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; + std::condition_variable proxyConVar_; + std::atomic isLoadSuccess_{false}; + }; + private: + int32_t UnregisterNotifyInternal(MessageParcel &data); + static inline std::mutex proxyMutex_; + static inline sptr serviceProxy_; static inline BrokerDelegator delegator_; }; } // namespace FileAccessFwk diff --git a/services/native/file_access_service/include/file_access_service_stub.h b/services/native/file_access_service/include/file_access_service_stub.h index d992b954..891b692e 100644 --- a/services/native/file_access_service/include/file_access_service_stub.h +++ b/services/native/file_access_service/include/file_access_service_stub.h @@ -28,6 +28,7 @@ class FileAccessServiceStub : public IRemoteStub { public: FileAccessServiceStub(); virtual ~FileAccessServiceStub(); + virtual int32_t CleanAllNotify (Uri uri) = 0; int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; private: diff --git a/services/native/file_access_service/include/holder_manager.h b/services/native/file_access_service/include/holder_manager.h index fb287c46..b7d2da23 100644 --- a/services/native/file_access_service/include/holder_manager.h +++ b/services/native/file_access_service/include/holder_manager.h @@ -110,4 +110,4 @@ private: }; } // namespace FileAccessFwk } // namespace OHOS -#endif // FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_HOLDER_MANAGER_H_ +#endif // FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_HOLDER_MANAGER_H_ \ No newline at end of file diff --git a/services/native/file_access_service/include/ifile_access_service_base.h b/services/native/file_access_service/include/ifile_access_service_base.h index 9f276b6d..6c6022d2 100644 --- a/services/native/file_access_service/include/ifile_access_service_base.h +++ b/services/native/file_access_service/include/ifile_access_service_base.h @@ -29,14 +29,8 @@ public: DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.FileAccessFwk.IFileAccessServiceBase"); public: - enum { - CMD_REGISTER_NOTIFY = 0, - CMD_UNREGISTER_NOTIFY, - CMD_ONCHANGE - }; - virtual int32_t OnChange(Uri uri, NotifyType notifyType) = 0; - virtual int32_t RegisterNotify(Uri uri, const sptr &observer, bool notifyForDescendants) = 0; + virtual int32_t RegisterNotify(Uri uri, bool notifyForDescendants, const sptr &observer) = 0; virtual int32_t UnregisterNotify(Uri uri, const sptr &observer) = 0; }; } // namespace FileAccessFwk diff --git a/services/native/file_access_service/include/observer_callback_proxy.h b/services/native/file_access_service/include/observer_callback_proxy.h index 20a4b060..fc390ba6 100644 --- a/services/native/file_access_service/include/observer_callback_proxy.h +++ b/services/native/file_access_service/include/observer_callback_proxy.h @@ -27,7 +27,7 @@ class ObserverCallbackProxy : public IRemoteProxy { public: explicit ObserverCallbackProxy(const sptr& remote) : IRemoteProxy(remote) {} ~ObserverCallbackProxy() = default; - void OnChange(NotifyMessage ¬ifyMessage) {} override; + void OnChange(NotifyMessage ¬ifyMessage) override; private: static inline BrokerDelegator delegator_; diff --git a/services/native/file_access_service/include/observer_callback_stub.h b/services/native/file_access_service/include/observer_callback_stub.h index 8ebbc8e5..229ba642 100644 --- a/services/native/file_access_service/include/observer_callback_stub.h +++ b/services/native/file_access_service/include/observer_callback_stub.h @@ -18,6 +18,7 @@ #include #include +#include #include "iobserver_callback.h" #include "iremote_stub.h" @@ -30,13 +31,14 @@ namespace FileAccessFwk { class ObserverCallbackStub : public IRemoteStub { public: ObserverCallbackStub() = default; - virtual ~ObserverCallbackStub() = default; - int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; - void OnChange(NotifyMessage ¬ifyMessage) override {} + virtual ~ObserverCallbackStub(); + int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; private: int32_t OnChangeStub(MessageParcel &data, MessageParcel &reply); - int32_t ChooseCodeStub(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); + void InitStubFuncMap(); + using RequestFuncType = int (ObserverCallbackStub::*)(MessageParcel &data, MessageParcel &reply); + std::map stubFuncMap_ = {}; }; } // namespace FileAccessFwk } // namespace OHOS diff --git a/services/native/file_access_service/src/file_access_service.cpp b/services/native/file_access_service/src/file_access_service.cpp index 87e32f83..ad336acc 100644 --- a/services/native/file_access_service/src/file_access_service.cpp +++ b/services/native/file_access_service/src/file_access_service.cpp @@ -22,17 +22,22 @@ #include "hitrace_meter.h" #include "system_ability_definition.h" +using namespace std; namespace OHOS { namespace FileAccessFwk { +namespace { + auto pms = FileAccessService::GetInstance(); + const bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(pms.GetRefPtr()); +} sptr FileAccessService::instance_; -std::mutex FileAccessService::mutex_; +mutex FileAccessService::mutex_; sptr FileAccessService::GetInstance() { if (instance_ != nullptr) { return instance_; } - std::lock_guard lock(mutex_); + lock_guard lock(mutex_); if (instance_ == nullptr) { instance_ = new FileAccessService(); if (instance_ == nullptr) { @@ -70,7 +75,7 @@ void FileAccessService::OnStop() ready_ = false; } -int32_t FileAccessService::Dump(int32_t fd, const std::vector &args) +int32_t FileAccessService::Dump(int32_t fd, const vector &args) { return ERR_OK; } @@ -110,8 +115,7 @@ static bool IsChildUri(const string &comparedUriStr, string &srcUriStr) return false; } -int32_t FileAccessService::RegisterNotify(Uri uri, const sptr &observer, - bool notifyForDescendants) +int32_t FileAccessService::RegisterNotify(Uri uri, bool notifyForDescendants, const sptr &observer) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "RegisterNotify"); shared_ptr obsContext = make_shared(observer); @@ -119,7 +123,7 @@ int32_t FileAccessService::RegisterNotify(Uri uri, const sptr &afterContext) { return obsContext->EqualTo(afterContext); }); - if (code == HolderManager>::CODE_CAN_NOT_FIND) { + if (code == HolderManager>::CODE_CAN_NOT_FIND) { // this is new callback, save this context obsContext->Ref(); code = obsManager_.save(obsContext); @@ -128,7 +132,7 @@ int32_t FileAccessService::RegisterNotify(Uri uri, const sptrRef(); } string uriStr = uri.ToString(); - std::lock_guard lock(nodeMutex_); + lock_guard lock(nodeMutex_); auto iter = relationshipMap_.find(uriStr); shared_ptr obsNode; if (iter != relationshipMap_.end()) { @@ -174,10 +178,10 @@ int32_t FileAccessService::RegisterNotify(Uri uri, const sptr obsNode) { - std::lock_guard lock(nodeMutex_); + lock_guard lock(nodeMutex_); for (auto &[comUri, node] : relationshipMap_) { auto childrenIter = find_if(node->children_.begin(), node->children_.end(), - [obsNode](const std::shared_ptr &obsListNode) { return obsNode == obsListNode; }); + [obsNode](const shared_ptr &obsListNode) { return obsNode == obsListNode; }); if (childrenIter != node->children_.end()) { node->children_.erase(childrenIter); } @@ -190,7 +194,7 @@ void FileAccessService::RemoveRelations(string &uriStr, shared_ptr int FileAccessService::FindUri(const string &uriStr, shared_ptr &outObsNode) { - std::lock_guard lock(nodeMutex_); + lock_guard lock(nodeMutex_); auto iter = relationshipMap_.find(uriStr); if (iter == relationshipMap_.end()) { HILOG_ERROR("Can not find uri"); @@ -200,6 +204,28 @@ int FileAccessService::FindUri(const string &uriStr, shared_ptr &o return ERR_OK; } +int32_t FileAccessService::CleanAllNotify(Uri uri) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "CleanAllNotify"); + string uriStr = uri.ToString(); + shared_ptr obsNode; + if (FindUri(uriStr, obsNode) != ERR_OK) { + HILOG_ERROR("Can not find unregisterNotify uri"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_CAN_NOT_FIND_URI; + } + for (auto code : obsNode->obsCodeList_) { + auto context = obsManager_.get(code); + context->UnRef(); + if (!context->IsValid()) { + obsManager_.release(code); + } + } + RemoveRelations(uriStr, obsNode); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; +} + int32_t FileAccessService::UnregisterNotify(Uri uri, const sptr &observer) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "UnregisterNotify"); @@ -220,7 +246,7 @@ int32_t FileAccessService::UnregisterNotify(Uri uri, const sptr &afterContext) { return obsContext->EqualTo(afterContext); }); - if (code == HolderManager>::CODE_CAN_NOT_FIND) { + if (code == HolderManager>::CODE_CAN_NOT_FIND) { HILOG_ERROR("Can not find observer"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return E_CALLBACK_IS_NOT_REGISTER; @@ -266,15 +292,33 @@ int32_t FileAccessService::OnChange(Uri uri, NotifyType notifyType) StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnChange"); string uriStr = uri.ToString(); shared_ptr node; - if (FindUri(uriStr, node) != ERR_OK) { - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - HILOG_ERROR("Can not find onChange uri"); - return E_CAN_NOT_FIND_URI; - } NotifyMessage notifyMessage; notifyMessage.notifyType_ = notifyType; - std::vector uris {uriStr}; + vector uris {uriStr}; notifyMessage.uris_ = uris; + //When the path is not found, search for its parent path + if (FindUri(uriStr, node) != ERR_OK) { + size_t slashIndex = uriStr.rfind("/"); + if (slashIndex == string::npos) { + HILOG_ERROR("Do not have parent"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_DO_NOT_HAVE_PARENT; + } + string parentUri = uriStr.substr(0, slashIndex); + if (FindUri(parentUri, node) != ERR_OK) { + HILOG_DEBUG("Can not find onChange parent uri"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; + } + if (!node->needChildNote_) { + HILOG_DEBUG("Do not need send onChange message"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; + } + SendListNotify(node->obsCodeList_, notifyMessage); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; + } SendListNotify(node->obsCodeList_, notifyMessage); if ((node->parent_ == nullptr) || (!node->parent_->needChildNote_)) { HILOG_DEBUG("Do not need notify parent"); diff --git a/services/native/file_access_service/src/file_access_service_proxy.cpp b/services/native/file_access_service/src/file_access_service_proxy.cpp index 1f9b976d..88fb15fb 100644 --- a/services/native/file_access_service/src/file_access_service_proxy.cpp +++ b/services/native/file_access_service/src/file_access_service_proxy.cpp @@ -16,16 +16,76 @@ #include "file_access_service_proxy.h" #include "file_access_framework_errno.h" +#include "file_access_service_ipc_interface_code.h" #include "hilog_wrapper.h" #include "hitrace_meter.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" namespace OHOS { namespace FileAccessFwk { +constexpr int LOAD_SA_TIMEOUT_MS = 5000; +sptr FileAccessServiceProxy::GetInstance() +{ + HILOG_INFO("Getinstance"); + std::unique_lock lock(proxyMutex_); + if (serviceProxy_ != nullptr) { + return serviceProxy_; + } + + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + HILOG_ERROR("Samgr is nullptr"); + return nullptr; + } + sptr loadCallback(new ServiceProxyLoadCallback()); + if (loadCallback == nullptr) { + HILOG_ERROR("loadCallback is nullptr"); + return nullptr; + } + int32_t ret = samgr->LoadSystemAbility(FILE_ACCESS_SERVICE_ID, loadCallback); + if (ret != ERR_OK) { + HILOG_ERROR("Failed to Load systemAbility, systemAbilityId:%{pulbic}d, ret code:%{pulbic}d", + FILE_ACCESS_SERVICE_ID, ret); + return nullptr; + } + + auto waitStatus = loadCallback->proxyConVar_.wait_for( + lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS), + [loadCallback]() { return loadCallback->isLoadSuccess_.load(); }); + if (!waitStatus) { + HILOG_ERROR("Load FileAccessService SA timeout"); + return nullptr; + } + return serviceProxy_; +} + +void FileAccessServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess( + int32_t systemAbilityId, + const sptr &remoteObject) +{ + HILOG_INFO("Load FileAccessService SA success,systemAbilityId:%{public}d, remoteObj result:%{private}s", + systemAbilityId, (remoteObject == nullptr ? "false" : "true")); + std::unique_lock lock(proxyMutex_); + serviceProxy_ = iface_cast(remoteObject); + isLoadSuccess_.store(true); + proxyConVar_.notify_one(); +} + +void FileAccessServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + HILOG_INFO("Load FileAccessService SA failed,systemAbilityId:%{public}d", systemAbilityId); + std::unique_lock lock(proxyMutex_); + serviceProxy_ = nullptr; + isLoadSuccess_.store(false); + proxyConVar_.notify_one(); +} + int32_t FileAccessServiceProxy::OnChange(Uri uri, NotifyType notifyType) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnChange"); MessageParcel data; - if (!data.WriteInterfaceToken(FileAccessObserverProxy::GetDescriptor())) { + if (!data.WriteInterfaceToken(FileAccessServiceProxy::GetDescriptor())) { HILOG_ERROR("WriteInterfaceToken failed"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return E_IPCS; @@ -45,7 +105,8 @@ int32_t FileAccessServiceProxy::OnChange(Uri uri, NotifyType notifyType) MessageParcel reply; MessageOption option; - int err = Remote()->SendRequest(CMD_ONCHANGE, data, reply, option); + int err = Remote()->SendRequest(static_cast(FileAccessServiceInterfaceCode::CMD_ONCHANGE), data, reply, + option); if (err != ERR_OK) { HILOG_ERROR("fail to SendRequest. err: %{public}d", err); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); @@ -63,8 +124,8 @@ int32_t FileAccessServiceProxy::OnChange(Uri uri, NotifyType notifyType) return ERR_OK; } -int32_t FileAccessServiceProxy::RegisterNotify(Uri uri, const sptr &observer, - bool notifyForDescendants) +int32_t FileAccessServiceProxy::RegisterNotify(Uri uri, bool notifyForDescendants, + const sptr &observer) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "RegisterNotify"); MessageParcel data; @@ -94,7 +155,8 @@ int32_t FileAccessServiceProxy::RegisterNotify(Uri uri, const sptrSendRequest(CMD_REGISTER_NOTIFY, data, reply, option); + int err = Remote()->SendRequest(static_cast(FileAccessServiceInterfaceCode::CMD_REGISTER_NOTIFY), data, + reply, option); if (err != ERR_OK) { HILOG_ERROR("fail to SendRequest. err: %{public}d", err); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); @@ -112,6 +174,26 @@ int32_t FileAccessServiceProxy::RegisterNotify(Uri uri, const sptrSendRequest(static_cast(FileAccessServiceInterfaceCode::CMD_UNREGISTER_NOTIFY), data, + reply, option); + if (err != ERR_OK) { + HILOG_ERROR("fail to SendRequest. err: %{public}d", err); + return err; + } + + int ret = E_IPCS; + if (!reply.ReadInt32(ret) || ret != ERR_OK) { + HILOG_ERROR("UnregisterNotify operation failed ret : %{public}d", ret); + return ret; + } + + return ERR_OK; +} + int32_t FileAccessServiceProxy::UnregisterNotify(Uri uri, const sptr &observer) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "UnregisterNotify"); @@ -127,31 +209,32 @@ int32_t FileAccessServiceProxy::UnregisterNotify(Uri uri, const sptrAsObject())) { - HILOG_ERROR("fail to WriteRemoteObject observer"); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return E_IPCS; - } - - MessageParcel reply; - MessageOption option; - int err = Remote()->SendRequest(CMD_UNREGISTER_NOTIFY, data, reply, option); - if (err != ERR_OK) { - HILOG_ERROR("fail to SendRequest. err: %{public}d", err); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return err; - } - - int ret = E_IPCS; - if (!reply.ReadInt32(ret) || ret != ERR_OK) { - HILOG_ERROR("UnregisterNotify operation failed ret : %{public}d", ret); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return ret; - } + bool observerNotNull = true; + if (observer != nullptr) { + if (!data.WriteBool(observerNotNull)) { + HILOG_ERROR("fail to WriteBool observerNotNull"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + if (!data.WriteRemoteObject(observer->AsObject())) { + HILOG_ERROR("fail to WriteRemoteObject observer"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + } else { + observerNotNull = false; + if (!data.WriteBool(observerNotNull)) { + HILOG_ERROR("fail to WriteBool observerNotNull"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + } + + // Split the code into two functions for better readability + int32_t result = UnregisterNotifyInternal(data); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return ERR_OK; + return result; } } // namespace FileAccessFwk } // namespace OHOS diff --git a/services/native/file_access_service/src/file_access_service_stub.cpp b/services/native/file_access_service/src/file_access_service_stub.cpp index 5afcde70..ea39ef83 100644 --- a/services/native/file_access_service/src/file_access_service_stub.cpp +++ b/services/native/file_access_service/src/file_access_service_stub.cpp @@ -21,6 +21,7 @@ #include "access_token.h" #include "accesstoken_kit.h" #include "file_access_framework_errno.h" +#include "file_access_service_ipc_interface_code.h" #include "hilog_wrapper.h" #include "hitrace_meter.h" #include "ipc_object_stub.h" @@ -36,9 +37,12 @@ namespace { } FileAccessServiceStub::FileAccessServiceStub() { - stubFuncMap_[CMD_REGISTER_NOTIFY] = &FileAccessServiceStub::CmdRegisterNotify; - stubFuncMap_[CMD_UNREGISTER_NOTIFY] = &FileAccessServiceStub::CmdUnregisterNotify; - stubFuncMap_[CMD_ONCHANGE] = &FileAccessServiceStub::CmdOnChange; + stubFuncMap_[static_cast(FileAccessServiceInterfaceCode::CMD_REGISTER_NOTIFY)] = + &FileAccessServiceStub::CmdRegisterNotify; + stubFuncMap_[static_cast(FileAccessServiceInterfaceCode::CMD_UNREGISTER_NOTIFY)] = + &FileAccessServiceStub::CmdUnregisterNotify; + stubFuncMap_[static_cast(FileAccessServiceInterfaceCode::CMD_ONCHANGE)] = + &FileAccessServiceStub::CmdOnChange; } FileAccessServiceStub::~FileAccessServiceStub() @@ -50,12 +54,6 @@ int32_t FileAccessServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &dat MessageOption &option) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnRemoteRequest"); - if (!CheckCallingPermission(FILE_ACCESS_PERMISSION)) { - HILOG_ERROR("permission error"); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return E_PERMISSION; - } - std::u16string descriptor = FileAccessServiceStub::GetDescriptor(); std::u16string remoteDescriptor = data.ReadInterfaceToken(); if (descriptor != remoteDescriptor) { @@ -69,6 +67,11 @@ int32_t FileAccessServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &dat return (this->*(itFunc->second))(data, reply); } + if (!CheckCallingPermission(FILE_ACCESS_PERMISSION)) { + HILOG_ERROR("permission error"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_PERMISSION; + } FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } @@ -120,7 +123,7 @@ ErrCode FileAccessServiceStub::CmdRegisterNotify(MessageParcel &data, MessagePar } bool notifyForDescendants = data.ReadBool(); - int ret = RegisterNotify(*uri, observer, notifyForDescendants); + int ret = RegisterNotify(*uri, notifyForDescendants, observer); if (!reply.WriteInt32(ret)) { HILOG_ERROR("Parameter RegisterNotify fail to WriteInt32 ret"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); @@ -140,21 +143,27 @@ ErrCode FileAccessServiceStub::CmdUnregisterNotify(MessageParcel &data, MessageP return E_IPCS; } - sptr obj = data.ReadRemoteObject(); - if (obj == nullptr) { - HILOG_INFO("get remote obj fail."); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return E_IPCS; - } - - sptr observer = iface_cast(obj); - if (observer == nullptr) { - HILOG_INFO("get observer fail"); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return E_IPCS; + bool observerNotNull = data.ReadBool(); + int ret = E_IPCS; + if (observerNotNull) { + sptr obj = data.ReadRemoteObject(); + if (obj == nullptr) { + HILOG_INFO("get remote obj fail."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + + sptr observer = iface_cast(obj); + if (observer == nullptr) { + HILOG_INFO("get observer fail"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + ret = UnregisterNotify(*uri, observer); + } else { + ret = CleanAllNotify(*uri); } - int ret = UnregisterNotify(*uri, observer); if (!reply.WriteInt32(ret)) { HILOG_ERROR("Parameter UnregisterNotify fail to WriteInt32 ret"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); diff --git a/services/native/file_access_service/src/observer_callback_proxy.cpp b/services/native/file_access_service/src/observer_callback_proxy.cpp new file mode 100644 index 00000000..bfdfaba0 --- /dev/null +++ b/services/native/file_access_service/src/observer_callback_proxy.cpp @@ -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. + */ + +#include "observer_callback_proxy.h" + +#include "file_access_framework_errno.h" +#include "hilog_wrapper.h" +#include "hitrace_meter.h" +#include "ipc_types.h" +#include "message_option.h" +#include "message_parcel.h" + +namespace OHOS { +namespace FileAccessFwk { +void ObserverCallbackProxy::OnChange(NotifyMessage ¬ifyMessage) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnChange"); + MessageParcel data; + if (!data.WriteInterfaceToken(ObserverCallbackProxy::GetDescriptor())) { + HILOG_ERROR("WriteInterfaceToken failed"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return; + } + + if (!data.WriteParcelable(¬ifyMessage)) { + HILOG_ERROR("fail to WriteParcelable notifyMessage"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return; + } + + MessageParcel reply; + MessageOption option; + int err = Remote()->SendRequest(CMD_ONCHANGE, data, reply, option); + if (err != ERR_OK) { + HILOG_ERROR("fail to SendRequest. err: %{public}d", err); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return; + } + + int ret = E_IPCS; + if (!reply.ReadInt32(ret)) { + HILOG_ERROR("fail to ReadInt32 ret"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return; + } + + if (ret != ERR_OK) { + HILOG_ERROR("OnChange operation failed ret : %{public}d", ret); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return; + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return; +} +} // namespace FileAccessFwk +} // namespace OHOS diff --git a/services/native/file_access_service/src/observer_callback_stub.cpp b/services/native/file_access_service/src/observer_callback_stub.cpp new file mode 100644 index 00000000..733f8cd0 --- /dev/null +++ b/services/native/file_access_service/src/observer_callback_stub.cpp @@ -0,0 +1,87 @@ +/* + * 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 "observer_callback_stub.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "file_access_framework_errno.h" +#include "hilog_wrapper.h" +#include "hitrace_meter.h" +#include "ipc_object_stub.h" +#include "ipc_skeleton.h" +#include "ipc_types.h" +#include "message_parcel.h" + +namespace OHOS { +namespace FileAccessFwk { +ObserverCallbackStub::~ObserverCallbackStub() +{ + stubFuncMap_.clear(); +} + +void ObserverCallbackStub::InitStubFuncMap() +{ + if (stubFuncMap_.size() == 0) { + stubFuncMap_[CMD_ONCHANGE] = &ObserverCallbackStub::OnChangeStub; + } +} + +int32_t ObserverCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, + MessageOption& option) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnRemoteRequest"); + InitStubFuncMap(); + std::u16string descriptor = ObserverCallbackStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_INVALID_STATE; + } + + const auto &itFunc = stubFuncMap_.find(code); + if (itFunc != stubFuncMap_.end()) { + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return (this->*(itFunc->second))(data, reply); + } + + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); +} + +int32_t ObserverCallbackStub::OnChangeStub(MessageParcel &data, MessageParcel &reply) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnChangeStub"); + std::shared_ptr notifyMessage(data.ReadParcelable()); + if (notifyMessage == nullptr) { + HILOG_ERROR("OnChange uri is nullptr"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_URIS; + } + + OnChange(*notifyMessage); + int ret = ERR_OK; + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("Parameter OnChangeStub fail to WriteInt32 ret"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; +} +} +} \ No newline at end of file diff --git a/utils/file_access_framework_errno.h b/utils/file_access_framework_errno.h index 3ee98347..06011da1 100644 --- a/utils/file_access_framework_errno.h +++ b/utils/file_access_framework_errno.h @@ -39,10 +39,12 @@ enum { E_CALLBACK_AND_URI_HAS_NOT_RELATIONS, // Uri and callback do not has relations, can not unregister E_CALLBACK_IS_NOT_REGISTER, // CallBack is not registered, can not unregister E_CAN_NOT_FIND_URI, // Can not find registered uri + E_DO_NOT_HAVE_PARENT, // Do not have parent uri in observerNode + E_LOAD_SA, // load SA failed E_PERMISSION = 201, // Permission verification failed E_PERMISSION_SYS, // is not system app E_COUNT }; } // namespace FileAccessFwk } // namespace OHOS -#endif // FILE_ACCESS_FRAMEWORK_ERRNO_H +#endif // FILE_ACCESS_FRAMEWORK_ERRNO_H \ No newline at end of file -- Gitee From 62ce9ea213399f12082659f3f0449efda6e7ba54 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Thu, 17 Aug 2023 21:25:48 +0800 Subject: [PATCH 05/13] =?UTF-8?q?UserFileService-FileAccessExtAbility-Acce?= =?UTF-8?q?ss=E7=94=A8=E4=BE=8B=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I1a2ed7bf863042c88fa893dc1fd0be1a3f7577f2 --- .../externalfileaccessaccess_fuzzer/BUILD.gn | 70 ++++++++++ .../corpus/init | 16 +++ .../externalfileaccessaccess_fuzzer.cpp | 120 ++++++++++++++++++ .../externalfileaccessaccess_fuzzer.h | 21 +++ .../project.xml | 25 ++++ 5 files changed, 252 insertions(+) create mode 100644 test/fuzztest/externalfileaccessaccess_fuzzer/BUILD.gn create mode 100644 test/fuzztest/externalfileaccessaccess_fuzzer/corpus/init create mode 100644 test/fuzztest/externalfileaccessaccess_fuzzer/externalfileaccessaccess_fuzzer.cpp create mode 100644 test/fuzztest/externalfileaccessaccess_fuzzer/externalfileaccessaccess_fuzzer.h create mode 100644 test/fuzztest/externalfileaccessaccess_fuzzer/project.xml diff --git a/test/fuzztest/externalfileaccessaccess_fuzzer/BUILD.gn b/test/fuzztest/externalfileaccessaccess_fuzzer/BUILD.gn new file mode 100644 index 00000000..29884986 --- /dev/null +++ b/test/fuzztest/externalfileaccessaccess_fuzzer/BUILD.gn @@ -0,0 +1,70 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") +ohos_fuzztest("ExternalFileAccessAccessFuzzTest") { + module_out_path = "filemanagement/user_file_service" + fuzz_config_file = + "${user_file_service_path}/test/fuzztest/externalfileaccessaccess_fuzzer" + include_dirs = [ + "${user_file_service_path}/utils", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/app", + "${ability_runtime_kits_path}/appkit/native/app/include", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/context", + "${user_file_service_path}/interfaces/inner_api/file_access/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${user_file_service_path}/test/fuzztest/externalfileaccessaccess_fuzzer/externalfileaccessaccess_fuzzer.cpp" ] + + deps = [ + "${ability_runtime_path}/frameworks/native/ability/native:abilitykit_native", + "${ability_runtime_path}/frameworks/native/appkit:app_context", + "${access_token_path}/frameworks/accesstoken:accesstoken_communication_adapter_cxx", + "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:app_context", + "ability_runtime:extensionkit_native", + "ability_runtime:napi_common", + "ability_runtime:runtime", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "hitrace:hitrace_meter", + "image_framework:image_native", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":ExternalFileAccessAccessFuzzTest" ] +} diff --git a/test/fuzztest/externalfileaccessaccess_fuzzer/corpus/init b/test/fuzztest/externalfileaccessaccess_fuzzer/corpus/init new file mode 100644 index 00000000..2b595da0 --- /dev/null +++ b/test/fuzztest/externalfileaccessaccess_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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/test/fuzztest/externalfileaccessaccess_fuzzer/externalfileaccessaccess_fuzzer.cpp b/test/fuzztest/externalfileaccessaccess_fuzzer/externalfileaccessaccess_fuzzer.cpp new file mode 100644 index 00000000..dc48b118 --- /dev/null +++ b/test/fuzztest/externalfileaccessaccess_fuzzer/externalfileaccessaccess_fuzzer.cpp @@ -0,0 +1,120 @@ +/* + * 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 "externalfileaccessaccess_fuzzer.h" + +#include +#include +#include +#include + +#include "extension_base.h" +#include "extension_context.h" +#include "message_parcel.h" +#include "file_access_ext_stub.h" +#include "file_access_ext_stub_impl.h" +#include "file_access_ext_ability.h" +#include "js_file_access_ext_ability.h" +#include "js_runtime.h" +#include "securec.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "accesstoken_kit.h" + +namespace OHOS { +using namespace std; +using namespace OHOS; +using namespace FileAccessFwk; +using namespace AbilityRuntime; +constexpr size_t FOO_MAX_LEN = 1024; +constexpr size_t U32_AT_SIZE = 4; + + +enum { + TOKEN_INDEX_ONE = 0, +}; + +void SetNativeToken() +{ + uint64_t tokenId; + const char **perms = new const char *[1]; + perms[TOKEN_INDEX_ONE] = "ohos.permission.FILE_ACCESS_MANAGER"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "ExternalFileAccessAccessFuzzTest"; + tokenId = GetAccessTokenId(&infoInstance); + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool ExternalFileAccessAccessFuzzTest(std::unique_ptr data, size_t size) +{ + SetNativeToken(); + // CMD_ACCESS + uint32_t code = 14; + MessageParcel datas; + datas.WriteInterfaceToken(FileAccessExtStub::GetDescriptor()); + datas.WriteBuffer(data.get(), size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + + auto fileAccessExtAbility = FileAccessExtAbility::Create(nullptr); + auto fileAccessExtAbilitySharePtr = std::shared_ptr(fileAccessExtAbility); + + sptr fileAccessExtStubObj(new (std::nothrow) FileAccessExtStubImpl( + fileAccessExtAbilitySharePtr, nullptr)); + + fileAccessExtStubObj->OnRemoteRequest(code, datas, reply, option); + + fileAccessExtAbility = nullptr; + fileAccessExtStubObj = nullptr; + + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +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::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) { + return 0; + } + + auto str = std::make_unique(size + 1); + (void)memset_s(str.get(), size + 1, 0x00, size + 1); + if (memcpy_s(str.get(), size, data, size) != EOK) { + return 0; + } + + OHOS::ExternalFileAccessAccessFuzzTest(move(str), size); + return 0; +} diff --git a/test/fuzztest/externalfileaccessaccess_fuzzer/externalfileaccessaccess_fuzzer.h b/test/fuzztest/externalfileaccessaccess_fuzzer/externalfileaccessaccess_fuzzer.h new file mode 100644 index 00000000..fb913d16 --- /dev/null +++ b/test/fuzztest/externalfileaccessaccess_fuzzer/externalfileaccessaccess_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FILE_ACCESS_EXTERNAL_FILE_ACCESS_ACCESS_FUZZER_H +#define FILE_ACCESS_EXTERNAL_FILE_ACCESS_ACCESS_FUZZER_H + +#define FUZZ_PROJECT_NAME "externalfileaccessaccess_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/externalfileaccessaccess_fuzzer/project.xml b/test/fuzztest/externalfileaccessaccess_fuzzer/project.xml new file mode 100644 index 00000000..2bb0dbd7 --- /dev/null +++ b/test/fuzztest/externalfileaccessaccess_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + -- Gitee From aa171c8511407e68a939254158ad22ff42ed7953 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Thu, 17 Aug 2023 21:27:19 +0800 Subject: [PATCH 06/13] =?UTF-8?q?UserFileService-FileAccessExtAbility-Crea?= =?UTF-8?q?teFile=E7=94=A8=E4=BE=8B=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I26a025fba4f60316c030210b7c8f2c708b190c79 --- .../BUILD.gn | 69 ++++++++++ .../corpus/init | 16 +++ .../externalfileaccesscreatefile_fuzzer.cpp | 120 ++++++++++++++++++ .../externalfileaccesscreatefile_fuzzer.h | 21 +++ .../project.xml | 25 ++++ 5 files changed, 251 insertions(+) create mode 100644 test/fuzztest/externalfileaccesscreatefile_fuzzer/BUILD.gn create mode 100644 test/fuzztest/externalfileaccesscreatefile_fuzzer/corpus/init create mode 100644 test/fuzztest/externalfileaccesscreatefile_fuzzer/externalfileaccesscreatefile_fuzzer.cpp create mode 100644 test/fuzztest/externalfileaccesscreatefile_fuzzer/externalfileaccesscreatefile_fuzzer.h create mode 100644 test/fuzztest/externalfileaccesscreatefile_fuzzer/project.xml diff --git a/test/fuzztest/externalfileaccesscreatefile_fuzzer/BUILD.gn b/test/fuzztest/externalfileaccesscreatefile_fuzzer/BUILD.gn new file mode 100644 index 00000000..ca2a2dd5 --- /dev/null +++ b/test/fuzztest/externalfileaccesscreatefile_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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") +ohos_fuzztest("ExternalFileAccessCreateFileFuzzTest") { + module_out_path = "filemanagement/user_file_service" + fuzz_config_file = "${user_file_service_path}/test/fuzztest/externalfileaccesscreatefile_fuzzer" + include_dirs = [ + "${user_file_service_path}/utils", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/app", + "${ability_runtime_kits_path}/appkit/native/app/include", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/context", + "${user_file_service_path}/interfaces/inner_api/file_access/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${user_file_service_path}/test/fuzztest/externalfileaccesscreatefile_fuzzer/externalfileaccesscreatefile_fuzzer.cpp" ] + + deps = [ + "${ability_runtime_path}/frameworks/native/ability/native:abilitykit_native", + "${ability_runtime_path}/frameworks/native/appkit:app_context", + "${access_token_path}/frameworks/accesstoken:accesstoken_communication_adapter_cxx", + "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:app_context", + "ability_runtime:extensionkit_native", + "ability_runtime:napi_common", + "ability_runtime:runtime", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "hitrace:hitrace_meter", + "image_framework:image_native", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":ExternalFileAccessCreateFileFuzzTest" ] +} diff --git a/test/fuzztest/externalfileaccesscreatefile_fuzzer/corpus/init b/test/fuzztest/externalfileaccesscreatefile_fuzzer/corpus/init new file mode 100644 index 00000000..2b595da0 --- /dev/null +++ b/test/fuzztest/externalfileaccesscreatefile_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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/test/fuzztest/externalfileaccesscreatefile_fuzzer/externalfileaccesscreatefile_fuzzer.cpp b/test/fuzztest/externalfileaccesscreatefile_fuzzer/externalfileaccesscreatefile_fuzzer.cpp new file mode 100644 index 00000000..c09f4d0e --- /dev/null +++ b/test/fuzztest/externalfileaccesscreatefile_fuzzer/externalfileaccesscreatefile_fuzzer.cpp @@ -0,0 +1,120 @@ +/* + * 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 "externalfileaccesscreatefile_fuzzer.h" + +#include +#include +#include +#include + +#include "extension_base.h" +#include "extension_context.h" +#include "message_parcel.h" +#include "file_access_ext_stub.h" +#include "file_access_ext_stub_impl.h" +#include "file_access_ext_ability.h" +#include "js_file_access_ext_ability.h" +#include "js_runtime.h" +#include "securec.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "accesstoken_kit.h" + +namespace OHOS { +using namespace std; +using namespace OHOS; +using namespace FileAccessFwk; +using namespace AbilityRuntime; +constexpr size_t FOO_MAX_LEN = 1024; +constexpr size_t U32_AT_SIZE = 4; + + +enum { + TOKEN_INDEX_ONE = 0, +}; + +void SetNativeToken() +{ + uint64_t tokenId; + const char **perms = new const char *[1]; + perms[TOKEN_INDEX_ONE] = "ohos.permission.FILE_ACCESS_MANAGER"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "ExternalFileAccessCreateFileFuzzTest"; + tokenId = GetAccessTokenId(&infoInstance); + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool ExternalFileAccessCreateFileFuzzTest(std::unique_ptr data, size_t size) +{ + SetNativeToken(); + // CMD_CREATEILE + uint32_t code = 2; + MessageParcel datas; + datas.WriteInterfaceToken(FileAccessExtStub::GetDescriptor()); + datas.WriteBuffer(data.get(), size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + + auto fileAccessExtAbility = FileAccessExtAbility::Create(nullptr); + auto fileAccessExtAbilitySharePtr = std::shared_ptr(fileAccessExtAbility); + + sptr fileAccessExtStubObj(new (std::nothrow) FileAccessExtStubImpl( + fileAccessExtAbilitySharePtr, nullptr)); + + fileAccessExtStubObj->OnRemoteRequest(code, datas, reply, option); + + fileAccessExtAbility = nullptr; + fileAccessExtStubObj = nullptr; + + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +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::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) { + return 0; + } + + auto str = std::make_unique(size + 1); + (void)memset_s(str.get(), size + 1, 0x00, size + 1); + if (memcpy_s(str.get(), size, data, size) != EOK) { + return 0; + } + + OHOS::ExternalFileAccessCreateFileFuzzTest(move(str), size); + return 0; +} diff --git a/test/fuzztest/externalfileaccesscreatefile_fuzzer/externalfileaccesscreatefile_fuzzer.h b/test/fuzztest/externalfileaccesscreatefile_fuzzer/externalfileaccesscreatefile_fuzzer.h new file mode 100644 index 00000000..297daa9b --- /dev/null +++ b/test/fuzztest/externalfileaccesscreatefile_fuzzer/externalfileaccesscreatefile_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FILE_ACCESS_EXTERNAL_FILE_ACCESS_CREATE_FILE_FUZZER_H +#define FILE_ACCESS_EXTERNAL_FILE_ACCESS_CREATE_FILE_FUZZER_H + +#define FUZZ_PROJECT_NAME "externalfileaccesscreatefile_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/externalfileaccesscreatefile_fuzzer/project.xml b/test/fuzztest/externalfileaccesscreatefile_fuzzer/project.xml new file mode 100644 index 00000000..2bb0dbd7 --- /dev/null +++ b/test/fuzztest/externalfileaccesscreatefile_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + -- Gitee From 86dea56ee01e911f1353143c390c76dec56f69cd Mon Sep 17 00:00:00 2001 From: y30045862 Date: Thu, 17 Aug 2023 21:29:06 +0800 Subject: [PATCH 07/13] =?UTF-8?q?UserFileService-FileAccessExtAbility-Dele?= =?UTF-8?q?te=E7=94=A8=E4=BE=8B=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I5baa6ec711ae43a7eac09cc3cc7c369b16e6e09a --- .../externalfileaccessdelete_fuzzer/BUILD.gn | 70 ++++++++++ .../corpus/init | 16 +++ .../externalfileaccessdelete_fuzzer.cpp | 120 ++++++++++++++++++ .../externalfileaccessdelete_fuzzer.h | 21 +++ .../project.xml | 25 ++++ 5 files changed, 252 insertions(+) create mode 100644 test/fuzztest/externalfileaccessdelete_fuzzer/BUILD.gn create mode 100644 test/fuzztest/externalfileaccessdelete_fuzzer/corpus/init create mode 100644 test/fuzztest/externalfileaccessdelete_fuzzer/externalfileaccessdelete_fuzzer.cpp create mode 100644 test/fuzztest/externalfileaccessdelete_fuzzer/externalfileaccessdelete_fuzzer.h create mode 100644 test/fuzztest/externalfileaccessdelete_fuzzer/project.xml diff --git a/test/fuzztest/externalfileaccessdelete_fuzzer/BUILD.gn b/test/fuzztest/externalfileaccessdelete_fuzzer/BUILD.gn new file mode 100644 index 00000000..52517cc8 --- /dev/null +++ b/test/fuzztest/externalfileaccessdelete_fuzzer/BUILD.gn @@ -0,0 +1,70 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") +ohos_fuzztest("ExternalFileAccessDeleteFuzzTest") { + module_out_path = "filemanagement/user_file_service" + fuzz_config_file = + "${user_file_service_path}/test/fuzztest/externalfileaccessdelete_fuzzer" + include_dirs = [ + "${user_file_service_path}/utils", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/app", + "${ability_runtime_kits_path}/appkit/native/app/include", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/context", + "${user_file_service_path}/interfaces/inner_api/file_access/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${user_file_service_path}/test/fuzztest/externalfileaccessdelete_fuzzer/externalfileaccessdelete_fuzzer.cpp" ] + + deps = [ + "${ability_runtime_path}/frameworks/native/ability/native:abilitykit_native", + "${ability_runtime_path}/frameworks/native/appkit:app_context", + "${access_token_path}/frameworks/accesstoken:accesstoken_communication_adapter_cxx", + "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:app_context", + "ability_runtime:extensionkit_native", + "ability_runtime:napi_common", + "ability_runtime:runtime", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "hitrace:hitrace_meter", + "image_framework:image_native", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":ExternalFileAccessDeleteFuzzTest" ] +} diff --git a/test/fuzztest/externalfileaccessdelete_fuzzer/corpus/init b/test/fuzztest/externalfileaccessdelete_fuzzer/corpus/init new file mode 100644 index 00000000..2b595da0 --- /dev/null +++ b/test/fuzztest/externalfileaccessdelete_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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/test/fuzztest/externalfileaccessdelete_fuzzer/externalfileaccessdelete_fuzzer.cpp b/test/fuzztest/externalfileaccessdelete_fuzzer/externalfileaccessdelete_fuzzer.cpp new file mode 100644 index 00000000..d6afb780 --- /dev/null +++ b/test/fuzztest/externalfileaccessdelete_fuzzer/externalfileaccessdelete_fuzzer.cpp @@ -0,0 +1,120 @@ +/* + * 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 "externalfileaccessdelete_fuzzer.h" + +#include +#include +#include +#include + +#include "extension_base.h" +#include "extension_context.h" +#include "message_parcel.h" +#include "file_access_ext_stub.h" +#include "file_access_ext_stub_impl.h" +#include "file_access_ext_ability.h" +#include "js_file_access_ext_ability.h" +#include "js_runtime.h" +#include "securec.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "accesstoken_kit.h" + +namespace OHOS { +using namespace std; +using namespace OHOS; +using namespace FileAccessFwk; +using namespace AbilityRuntime; +constexpr size_t FOO_MAX_LEN = 1024; +constexpr size_t U32_AT_SIZE = 4; + + +enum { + TOKEN_INDEX_ONE = 0, +}; + +void SetNativeToken() +{ + uint64_t tokenId; + const char **perms = new const char *[1]; + perms[TOKEN_INDEX_ONE] = "ohos.permission.FILE_ACCESS_MANAGER"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "ExternalFileAccessDeleteFuzzTest"; + tokenId = GetAccessTokenId(&infoInstance); + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool ExternalFileAccessDeleteFuzzTest(std::unique_ptr data, size_t size) +{ + SetNativeToken(); + // CMD_DELETE + uint32_t code = 4; + MessageParcel datas; + datas.WriteInterfaceToken(FileAccessExtStub::GetDescriptor()); + datas.WriteBuffer(data.get(), size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + + auto fileAccessExtAbility = FileAccessExtAbility::Create(nullptr); + auto fileAccessExtAbilitySharePtr = std::shared_ptr(fileAccessExtAbility); + + sptr fileAccessExtStubObj(new (std::nothrow) FileAccessExtStubImpl( + fileAccessExtAbilitySharePtr, nullptr)); + + fileAccessExtStubObj->OnRemoteRequest(code, datas, reply, option); + + fileAccessExtAbility = nullptr; + fileAccessExtStubObj = nullptr; + + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +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::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) { + return 0; + } + + auto str = std::make_unique(size + 1); + (void)memset_s(str.get(), size + 1, 0x00, size + 1); + if (memcpy_s(str.get(), size, data, size) != EOK) { + return 0; + } + + OHOS::ExternalFileAccessDeleteFuzzTest(move(str), size); + return 0; +} diff --git a/test/fuzztest/externalfileaccessdelete_fuzzer/externalfileaccessdelete_fuzzer.h b/test/fuzztest/externalfileaccessdelete_fuzzer/externalfileaccessdelete_fuzzer.h new file mode 100644 index 00000000..cc224346 --- /dev/null +++ b/test/fuzztest/externalfileaccessdelete_fuzzer/externalfileaccessdelete_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FILE_ACCESS_EXTERNAL_FILE_ACCESS_DELETE_FUZZER_H +#define FILE_ACCESS_EXTERNAL_FILE_ACCESS_DELETE_FUZZER_H + +#define FUZZ_PROJECT_NAME "externalfileaccessdelete_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/externalfileaccessdelete_fuzzer/project.xml b/test/fuzztest/externalfileaccessdelete_fuzzer/project.xml new file mode 100644 index 00000000..2bb0dbd7 --- /dev/null +++ b/test/fuzztest/externalfileaccessdelete_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + -- Gitee From bfcd42e9164451e76531d3a9350e928f15c21bae Mon Sep 17 00:00:00 2001 From: y30045862 Date: Thu, 17 Aug 2023 21:31:08 +0800 Subject: [PATCH 08/13] =?UTF-8?q?UserFileService-FileAccessExtAbility-GetF?= =?UTF-8?q?ileInfoFromRelativePath=E7=94=A8=E4=BE=8B=20Signed-off-by:=20ya?= =?UTF-8?q?ngjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I732493b89350eefee6208d2c158dd2b0f177cf63 --- .../BUILD.gn | 69 ++++++++++ .../corpus/init | 16 +++ ...cessgetfileinfofromrelativepath_fuzzer.cpp | 120 ++++++++++++++++++ ...accessgetfileinfofromrelativepath_fuzzer.h | 21 +++ .../project.xml | 25 ++++ 5 files changed, 251 insertions(+) create mode 100644 test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/BUILD.gn create mode 100644 test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/corpus/init create mode 100644 test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/externalfileaccessgetfileinfofromrelativepath_fuzzer.cpp create mode 100644 test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/externalfileaccessgetfileinfofromrelativepath_fuzzer.h create mode 100644 test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/project.xml diff --git a/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/BUILD.gn b/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/BUILD.gn new file mode 100644 index 00000000..5117fa82 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") +ohos_fuzztest("ExternalFileAccessGetFileInfoFromRelativePathFuzzTest") { + module_out_path = "filemanagement/user_file_service" + fuzz_config_file = "${user_file_service_path}/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer" + include_dirs = [ + "${user_file_service_path}/utils", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/app", + "${ability_runtime_kits_path}/appkit/native/app/include", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/context", + "${user_file_service_path}/interfaces/inner_api/file_access/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${user_file_service_path}/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/externalfileaccessgetfileinfofromrelativepath_fuzzer.cpp" ] + + deps = [ + "${ability_runtime_path}/frameworks/native/ability/native:abilitykit_native", + "${ability_runtime_path}/frameworks/native/appkit:app_context", + "${access_token_path}/frameworks/accesstoken:accesstoken_communication_adapter_cxx", + "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:app_context", + "ability_runtime:extensionkit_native", + "ability_runtime:napi_common", + "ability_runtime:runtime", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "hitrace:hitrace_meter", + "image_framework:image_native", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":ExternalFileAccessGetFileInfoFromRelativePathFuzzTest" ] +} diff --git a/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/corpus/init b/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/corpus/init new file mode 100644 index 00000000..2b595da0 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/externalfileaccessgetfileinfofromrelativepath_fuzzer.cpp b/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/externalfileaccessgetfileinfofromrelativepath_fuzzer.cpp new file mode 100644 index 00000000..879f9560 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/externalfileaccessgetfileinfofromrelativepath_fuzzer.cpp @@ -0,0 +1,120 @@ +/* + * 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 "externalfileaccessgetfileinfofromrelativepath_fuzzer.h" + +#include +#include +#include +#include + +#include "extension_base.h" +#include "extension_context.h" +#include "message_parcel.h" +#include "file_access_ext_stub.h" +#include "file_access_ext_stub_impl.h" +#include "file_access_ext_ability.h" +#include "js_file_access_ext_ability.h" +#include "js_runtime.h" +#include "securec.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "accesstoken_kit.h" + +namespace OHOS { +using namespace std; +using namespace OHOS; +using namespace FileAccessFwk; +using namespace AbilityRuntime; +constexpr size_t FOO_MAX_LEN = 1024; +constexpr size_t U32_AT_SIZE = 4; + + +enum { + TOKEN_INDEX_ONE = 0, +}; + +void SetNativeToken() +{ + uint64_t tokenId; + const char **perms = new const char *[1]; + perms[TOKEN_INDEX_ONE] = "ohos.permission.FILE_ACCESS_MANAGER"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "ExternalFileAccessGetFileInfoFromRelativePathFuzzTest"; + tokenId = GetAccessTokenId(&infoInstance); + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool ExternalFileAccessGetFileInfoFromRelativePathFuzzTest(std::unique_ptr data, size_t size) +{ + SetNativeToken(); + // CMD_GET_FILEINFO_FROM_RELATIVE_PATH + uint32_t code = 17; + MessageParcel datas; + datas.WriteInterfaceToken(FileAccessExtStub::GetDescriptor()); + datas.WriteBuffer(data.get(), size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + + auto fileAccessExtAbility = FileAccessExtAbility::Create(nullptr); + auto fileAccessExtAbilitySharePtr = std::shared_ptr(fileAccessExtAbility); + + sptr fileAccessExtStubObj(new (std::nothrow) FileAccessExtStubImpl( + fileAccessExtAbilitySharePtr, nullptr)); + + fileAccessExtStubObj->OnRemoteRequest(code, datas, reply, option); + + fileAccessExtAbility = nullptr; + fileAccessExtStubObj = nullptr; + + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +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::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) { + return 0; + } + + auto str = std::make_unique(size + 1); + (void)memset_s(str.get(), size + 1, 0x00, size + 1); + if (memcpy_s(str.get(), size, data, size) != EOK) { + return 0; + } + + OHOS::ExternalFileAccessGetFileInfoFromRelativePathFuzzTest(move(str), size); + return 0; +} diff --git a/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/externalfileaccessgetfileinfofromrelativepath_fuzzer.h b/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/externalfileaccessgetfileinfofromrelativepath_fuzzer.h new file mode 100644 index 00000000..e8e5b9f1 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/externalfileaccessgetfileinfofromrelativepath_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FILE_ACCESS_EXTERNAL_FILE_ACCESS_GET_FILEINFO_RELATIVEPATH_FUZZER_H +#define FILE_ACCESS_EXTERNAL_FILE_ACCESS_GET_FILEINFO_RELATIVEPATH_FUZZER_H + +#define FUZZ_PROJECT_NAME "externalfileaccessgetfileinfofromrelativepath_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/project.xml b/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/project.xml new file mode 100644 index 00000000..2bb0dbd7 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetfileinfofromrelativepath_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + -- Gitee From cb7a04138c01cf914cf5735b91d9670c003d5110 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Thu, 17 Aug 2023 21:32:44 +0800 Subject: [PATCH 09/13] =?UTF-8?q?UserFileService-FileAccessExtAbility-GetF?= =?UTF-8?q?ileInfoFromUri=E7=94=A8=E4=BE=8B=20Signed-off-by:=20yangjingbo1?= =?UTF-8?q?0=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ie0078b0c9e9c8743fc50123af32c9db92a7f0c41 --- .../BUILD.gn | 69 ++++++++++ .../corpus/init | 16 +++ ...nalfileaccessgetfileinfofromuri_fuzzer.cpp | 120 ++++++++++++++++++ ...ernalfileaccessgetfileinfofromuri_fuzzer.h | 21 +++ .../project.xml | 25 ++++ 5 files changed, 251 insertions(+) create mode 100644 test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/BUILD.gn create mode 100644 test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/corpus/init create mode 100644 test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/externalfileaccessgetfileinfofromuri_fuzzer.cpp create mode 100644 test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/externalfileaccessgetfileinfofromuri_fuzzer.h create mode 100644 test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/project.xml diff --git a/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/BUILD.gn b/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/BUILD.gn new file mode 100644 index 00000000..2d63b4d0 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetfileinfofromuri_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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") +ohos_fuzztest("ExternalFileAccessGetFileInfoFromUriFuzzTest") { + module_out_path = "filemanagement/user_file_service" + fuzz_config_file = "${user_file_service_path}/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer" + include_dirs = [ + "${user_file_service_path}/utils", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/app", + "${ability_runtime_kits_path}/appkit/native/app/include", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/context", + "${user_file_service_path}/interfaces/inner_api/file_access/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${user_file_service_path}/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/externalfileaccessgetfileinfofromuri_fuzzer.cpp" ] + + deps = [ + "${ability_runtime_path}/frameworks/native/ability/native:abilitykit_native", + "${ability_runtime_path}/frameworks/native/appkit:app_context", + "${access_token_path}/frameworks/accesstoken:accesstoken_communication_adapter_cxx", + "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:app_context", + "ability_runtime:extensionkit_native", + "ability_runtime:napi_common", + "ability_runtime:runtime", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "hitrace:hitrace_meter", + "image_framework:image_native", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":ExternalFileAccessGetFileInfoFromUriFuzzTest" ] +} diff --git a/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/corpus/init b/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/corpus/init new file mode 100644 index 00000000..2b595da0 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/externalfileaccessgetfileinfofromuri_fuzzer.cpp b/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/externalfileaccessgetfileinfofromuri_fuzzer.cpp new file mode 100644 index 00000000..30d23022 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/externalfileaccessgetfileinfofromuri_fuzzer.cpp @@ -0,0 +1,120 @@ +/* + * 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 "externalfileaccessgetfileinfofromuri_fuzzer.h" + +#include +#include +#include +#include + +#include "extension_base.h" +#include "extension_context.h" +#include "message_parcel.h" +#include "file_access_ext_stub.h" +#include "file_access_ext_stub_impl.h" +#include "file_access_ext_ability.h" +#include "js_file_access_ext_ability.h" +#include "js_runtime.h" +#include "securec.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "accesstoken_kit.h" + +namespace OHOS { +using namespace std; +using namespace OHOS; +using namespace FileAccessFwk; +using namespace AbilityRuntime; +constexpr size_t FOO_MAX_LEN = 1024; +constexpr size_t U32_AT_SIZE = 4; + + +enum { + TOKEN_INDEX_ONE = 0, +}; + +void SetNativeToken() +{ + uint64_t tokenId; + const char **perms = new const char *[1]; + perms[TOKEN_INDEX_ONE] = "ohos.permission.FILE_ACCESS_MANAGER"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "ExternalFileAccessGetFileInfoFromUriFuzzTest"; + tokenId = GetAccessTokenId(&infoInstance); + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool ExternalFileAccessGetFileInfoFromUriFuzzTest(std::unique_ptr data, size_t size) +{ + SetNativeToken(); + // CMD_GET_FILEINFO_FROM_URI + uint32_t code = 16; + MessageParcel datas; + datas.WriteInterfaceToken(FileAccessExtStub::GetDescriptor()); + datas.WriteBuffer(data.get(), size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + + auto fileAccessExtAbility = FileAccessExtAbility::Create(nullptr); + auto fileAccessExtAbilitySharePtr = std::shared_ptr(fileAccessExtAbility); + + sptr fileAccessExtStubObj(new (std::nothrow) FileAccessExtStubImpl( + fileAccessExtAbilitySharePtr, nullptr)); + + fileAccessExtStubObj->OnRemoteRequest(code, datas, reply, option); + + fileAccessExtAbility = nullptr; + fileAccessExtStubObj = nullptr; + + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +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::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) { + return 0; + } + + auto str = std::make_unique(size + 1); + (void)memset_s(str.get(), size + 1, 0x00, size + 1); + if (memcpy_s(str.get(), size, data, size) != EOK) { + return 0; + } + + OHOS::ExternalFileAccessGetFileInfoFromUriFuzzTest(move(str), size); + return 0; +} diff --git a/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/externalfileaccessgetfileinfofromuri_fuzzer.h b/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/externalfileaccessgetfileinfofromuri_fuzzer.h new file mode 100644 index 00000000..8a7e6e67 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/externalfileaccessgetfileinfofromuri_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FILE_ACCESS_EXTERNAL_FILE_ACCESS_GET_FILEINFO_URI_FUZZER_H +#define FILE_ACCESS_EXTERNAL_FILE_ACCESS_GET_FILEINFO_URI_FUZZER_H + +#define FUZZ_PROJECT_NAME "externalfileaccessgetfileinfofromuri_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/project.xml b/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/project.xml new file mode 100644 index 00000000..2bb0dbd7 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetfileinfofromuri_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + -- Gitee From 15626bd4fa1a1c09d437ec17ed5d7f37d4195766 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Thu, 17 Aug 2023 21:34:54 +0800 Subject: [PATCH 10/13] =?UTF-8?q?UserFileService-FileAccessExtAbility-GetR?= =?UTF-8?q?oots=E7=94=A8=E4=BE=8B=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Iea6e8865713a1fe04f5fa9c18c87a020cad26516 --- .../BUILD.gn | 69 ++++++++++ .../corpus/init | 16 +++ .../externalfileaccessgetroots_fuzzer.cpp | 120 ++++++++++++++++++ .../externalfileaccessgetroots_fuzzer.h | 21 +++ .../project.xml | 25 ++++ 5 files changed, 251 insertions(+) create mode 100644 test/fuzztest/externalfileaccessgetroots_fuzzer/BUILD.gn create mode 100644 test/fuzztest/externalfileaccessgetroots_fuzzer/corpus/init create mode 100644 test/fuzztest/externalfileaccessgetroots_fuzzer/externalfileaccessgetroots_fuzzer.cpp create mode 100644 test/fuzztest/externalfileaccessgetroots_fuzzer/externalfileaccessgetroots_fuzzer.h create mode 100644 test/fuzztest/externalfileaccessgetroots_fuzzer/project.xml diff --git a/test/fuzztest/externalfileaccessgetroots_fuzzer/BUILD.gn b/test/fuzztest/externalfileaccessgetroots_fuzzer/BUILD.gn new file mode 100644 index 00000000..baddb4ff --- /dev/null +++ b/test/fuzztest/externalfileaccessgetroots_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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") +ohos_fuzztest("ExternalFileAccessGetRootsFuzzTest") { + module_out_path = "filemanagement/user_file_service" + fuzz_config_file = "${user_file_service_path}/test/fuzztest/externalfileaccessgetroots_fuzzer" + include_dirs = [ + "${user_file_service_path}/utils", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/app", + "${ability_runtime_kits_path}/appkit/native/app/include", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/context", + "${user_file_service_path}/interfaces/inner_api/file_access/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${user_file_service_path}/test/fuzztest/externalfileaccessgetroots_fuzzer/externalfileaccessgetroots_fuzzer.cpp" ] + + deps = [ + "${ability_runtime_path}/frameworks/native/ability/native:abilitykit_native", + "${ability_runtime_path}/frameworks/native/appkit:app_context", + "${access_token_path}/frameworks/accesstoken:accesstoken_communication_adapter_cxx", + "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:app_context", + "ability_runtime:extensionkit_native", + "ability_runtime:napi_common", + "ability_runtime:runtime", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "hitrace:hitrace_meter", + "image_framework:image_native", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":ExternalFileAccessGetRootsFuzzTest" ] +} diff --git a/test/fuzztest/externalfileaccessgetroots_fuzzer/corpus/init b/test/fuzztest/externalfileaccessgetroots_fuzzer/corpus/init new file mode 100644 index 00000000..2b595da0 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetroots_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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/test/fuzztest/externalfileaccessgetroots_fuzzer/externalfileaccessgetroots_fuzzer.cpp b/test/fuzztest/externalfileaccessgetroots_fuzzer/externalfileaccessgetroots_fuzzer.cpp new file mode 100644 index 00000000..efc87b4e --- /dev/null +++ b/test/fuzztest/externalfileaccessgetroots_fuzzer/externalfileaccessgetroots_fuzzer.cpp @@ -0,0 +1,120 @@ +/* + * 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 "externalfileaccessgetroots_fuzzer.h" + +#include +#include +#include +#include + +#include "extension_base.h" +#include "extension_context.h" +#include "message_parcel.h" +#include "file_access_ext_stub.h" +#include "file_access_ext_stub_impl.h" +#include "file_access_ext_ability.h" +#include "js_file_access_ext_ability.h" +#include "js_runtime.h" +#include "securec.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "accesstoken_kit.h" + +namespace OHOS { +using namespace std; +using namespace OHOS; +using namespace FileAccessFwk; +using namespace AbilityRuntime; +constexpr size_t FOO_MAX_LEN = 1024; +constexpr size_t U32_AT_SIZE = 4; + + +enum { + TOKEN_INDEX_ONE = 0, +}; + +void SetNativeToken() +{ + uint64_t tokenId; + const char **perms = new const char *[1]; + perms[TOKEN_INDEX_ONE] = "ohos.permission.FILE_ACCESS_MANAGER"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "ExternalFileAccessGetRootsFuzzTest"; + tokenId = GetAccessTokenId(&infoInstance); + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool ExternalFileAccessGetRootsFuzzTest(std::unique_ptr data, size_t size) +{ + SetNativeToken(); + // CMD_GET_ROOTS + uint32_t code = 13; + MessageParcel datas; + datas.WriteInterfaceToken(FileAccessExtStub::GetDescriptor()); + datas.WriteBuffer(data.get(), size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + + auto fileAccessExtAbility = FileAccessExtAbility::Create(nullptr); + auto fileAccessExtAbilitySharePtr = std::shared_ptr(fileAccessExtAbility); + + sptr fileAccessExtStubObj(new (std::nothrow) FileAccessExtStubImpl( + fileAccessExtAbilitySharePtr, nullptr)); + + fileAccessExtStubObj->OnRemoteRequest(code, datas, reply, option); + + fileAccessExtAbility = nullptr; + fileAccessExtStubObj = nullptr; + + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +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::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) { + return 0; + } + + auto str = std::make_unique(size + 1); + (void)memset_s(str.get(), size + 1, 0x00, size + 1); + if (memcpy_s(str.get(), size, data, size) != EOK) { + return 0; + } + + OHOS::ExternalFileAccessGetRootsFuzzTest(move(str), size); + return 0; +} diff --git a/test/fuzztest/externalfileaccessgetroots_fuzzer/externalfileaccessgetroots_fuzzer.h b/test/fuzztest/externalfileaccessgetroots_fuzzer/externalfileaccessgetroots_fuzzer.h new file mode 100644 index 00000000..120b7bc6 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetroots_fuzzer/externalfileaccessgetroots_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FILE_ACCESS_EXTERNAL_FILE_ACCESS_GET_ROOTS_FUZZER_H +#define FILE_ACCESS_EXTERNAL_FILE_ACCESS_GET_ROOTS_FUZZER_H + +#define FUZZ_PROJECT_NAME "externalfileaccessgetroots_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/externalfileaccessgetroots_fuzzer/project.xml b/test/fuzztest/externalfileaccessgetroots_fuzzer/project.xml new file mode 100644 index 00000000..2bb0dbd7 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetroots_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + -- Gitee From 038ba01fd6411ef89263e1a79bf51c0877323372 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Thu, 17 Aug 2023 21:36:23 +0800 Subject: [PATCH 11/13] =?UTF-8?q?UserFileService-FileAccessExtAbility-GetT?= =?UTF-8?q?humbnail=E7=94=A8=E4=BE=8B=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Id6ef4d76d3e72665bbef0cee423d3dccf7b1e35f --- .../BUILD.gn | 69 ++++++++++ .../corpus/init | 16 +++ .../externalfileaccessgetthumbnail_fuzzer.cpp | 120 ++++++++++++++++++ .../externalfileaccessgetthumbnail_fuzzer.h | 21 +++ .../project.xml | 25 ++++ 5 files changed, 251 insertions(+) create mode 100644 test/fuzztest/externalfileaccessgetthumbnail_fuzzer/BUILD.gn create mode 100644 test/fuzztest/externalfileaccessgetthumbnail_fuzzer/corpus/init create mode 100644 test/fuzztest/externalfileaccessgetthumbnail_fuzzer/externalfileaccessgetthumbnail_fuzzer.cpp create mode 100644 test/fuzztest/externalfileaccessgetthumbnail_fuzzer/externalfileaccessgetthumbnail_fuzzer.h create mode 100644 test/fuzztest/externalfileaccessgetthumbnail_fuzzer/project.xml diff --git a/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/BUILD.gn b/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/BUILD.gn new file mode 100644 index 00000000..488e4830 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetthumbnail_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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") +ohos_fuzztest("ExternalFileAccessGetThumbnailFuzzTest") { + module_out_path = "filemanagement/user_file_service" + fuzz_config_file = "${user_file_service_path}/test/fuzztest/externalfileaccessgetthumbnail_fuzzer" + include_dirs = [ + "${user_file_service_path}/utils", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/app", + "${ability_runtime_kits_path}/appkit/native/app/include", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/context", + "${user_file_service_path}/interfaces/inner_api/file_access/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${user_file_service_path}/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/externalfileaccessgetthumbnail_fuzzer.cpp" ] + + deps = [ + "${ability_runtime_path}/frameworks/native/ability/native:abilitykit_native", + "${ability_runtime_path}/frameworks/native/appkit:app_context", + "${access_token_path}/frameworks/accesstoken:accesstoken_communication_adapter_cxx", + "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:app_context", + "ability_runtime:extensionkit_native", + "ability_runtime:napi_common", + "ability_runtime:runtime", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "hitrace:hitrace_meter", + "image_framework:image_native", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":ExternalFileAccessGetThumbnailFuzzTest" ] +} diff --git a/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/corpus/init b/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/corpus/init new file mode 100644 index 00000000..2b595da0 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/externalfileaccessgetthumbnail_fuzzer.cpp b/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/externalfileaccessgetthumbnail_fuzzer.cpp new file mode 100644 index 00000000..4eb68cb3 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/externalfileaccessgetthumbnail_fuzzer.cpp @@ -0,0 +1,120 @@ +/* + * 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 "externalfileaccessgetthumbnail_fuzzer.h" + +#include +#include +#include +#include + +#include "extension_base.h" +#include "extension_context.h" +#include "message_parcel.h" +#include "file_access_ext_stub.h" +#include "file_access_ext_stub_impl.h" +#include "file_access_ext_ability.h" +#include "js_file_access_ext_ability.h" +#include "js_runtime.h" +#include "securec.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "accesstoken_kit.h" + +namespace OHOS { +using namespace std; +using namespace OHOS; +using namespace FileAccessFwk; +using namespace AbilityRuntime; +constexpr size_t FOO_MAX_LEN = 1024; +constexpr size_t U32_AT_SIZE = 4; + + +enum { + TOKEN_INDEX_ONE = 0, +}; + +void SetNativeToken() +{ + uint64_t tokenId; + const char **perms = new const char *[1]; + perms[TOKEN_INDEX_ONE] = "ohos.permission.FILE_ACCESS_MANAGER"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "ExternalFileAccessGetThumbnailFuzzTest"; + tokenId = GetAccessTokenId(&infoInstance); + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool ExternalFileAccessGetThumbnailFuzzTest(std::unique_ptr data, size_t size) +{ + SetNativeToken(); + // CMD_GET_THUMBNAIL + uint32_t code = 15; + MessageParcel datas; + datas.WriteInterfaceToken(FileAccessExtStub::GetDescriptor()); + datas.WriteBuffer(data.get(), size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + + auto fileAccessExtAbility = FileAccessExtAbility::Create(nullptr); + auto fileAccessExtAbilitySharePtr = std::shared_ptr(fileAccessExtAbility); + + sptr fileAccessExtStubObj(new (std::nothrow) FileAccessExtStubImpl( + fileAccessExtAbilitySharePtr, nullptr)); + + fileAccessExtStubObj->OnRemoteRequest(code, datas, reply, option); + + fileAccessExtAbility = nullptr; + fileAccessExtStubObj = nullptr; + + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +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::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) { + return 0; + } + + auto str = std::make_unique(size + 1); + (void)memset_s(str.get(), size + 1, 0x00, size + 1); + if (memcpy_s(str.get(), size, data, size) != EOK) { + return 0; + } + + OHOS::ExternalFileAccessGetThumbnailFuzzTest(move(str), size); + return 0; +} diff --git a/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/externalfileaccessgetthumbnail_fuzzer.h b/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/externalfileaccessgetthumbnail_fuzzer.h new file mode 100644 index 00000000..eae80c7a --- /dev/null +++ b/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/externalfileaccessgetthumbnail_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FILE_ACCESS_EXTERNAL_FILE_ACCESS_GET_THUMBNAIL_FUZZER_H +#define FILE_ACCESS_EXTERNAL_FILE_ACCESS_GET_THUMBNAIL_FUZZER_H + +#define FUZZ_PROJECT_NAME "externalfileaccessgetthumbnail_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/project.xml b/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/project.xml new file mode 100644 index 00000000..2bb0dbd7 --- /dev/null +++ b/test/fuzztest/externalfileaccessgetthumbnail_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + -- Gitee From 2a8ca7b4689fcb2a6891d51361dbadf548b7f624 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Thu, 17 Aug 2023 21:37:23 +0800 Subject: [PATCH 12/13] =?UTF-8?q?UserFileService-FileAccessExtAbility-List?= =?UTF-8?q?File=E7=94=A8=E4=BE=8B=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ib8d092fac2d4e1e21b5f370f29f84c521bf1cb4a --- .../BUILD.gn | 69 ++++++++++ .../corpus/init | 16 +++ .../externalfileaccesslistfile_fuzzer.cpp | 120 ++++++++++++++++++ .../externalfileaccesslistfile_fuzzer.h | 21 +++ .../project.xml | 25 ++++ 5 files changed, 251 insertions(+) create mode 100644 test/fuzztest/externalfileaccesslistfile_fuzzer/BUILD.gn create mode 100644 test/fuzztest/externalfileaccesslistfile_fuzzer/corpus/init create mode 100644 test/fuzztest/externalfileaccesslistfile_fuzzer/externalfileaccesslistfile_fuzzer.cpp create mode 100644 test/fuzztest/externalfileaccesslistfile_fuzzer/externalfileaccesslistfile_fuzzer.h create mode 100644 test/fuzztest/externalfileaccesslistfile_fuzzer/project.xml diff --git a/test/fuzztest/externalfileaccesslistfile_fuzzer/BUILD.gn b/test/fuzztest/externalfileaccesslistfile_fuzzer/BUILD.gn new file mode 100644 index 00000000..6890b87c --- /dev/null +++ b/test/fuzztest/externalfileaccesslistfile_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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") +ohos_fuzztest("ExternalFileAccessListFileFuzzTest") { + module_out_path = "filemanagement/user_file_service" + fuzz_config_file = "${user_file_service_path}/test/fuzztest/externalfileaccesslistfile_fuzzer" + include_dirs = [ + "${user_file_service_path}/utils", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/app", + "${ability_runtime_kits_path}/appkit/native/app/include", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/context", + "${user_file_service_path}/interfaces/inner_api/file_access/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${user_file_service_path}/test/fuzztest/externalfileaccesslistfile_fuzzer/externalfileaccesslistfile_fuzzer.cpp" ] + + deps = [ + "${ability_runtime_path}/frameworks/native/ability/native:abilitykit_native", + "${ability_runtime_path}/frameworks/native/appkit:app_context", + "${access_token_path}/frameworks/accesstoken:accesstoken_communication_adapter_cxx", + "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:app_context", + "ability_runtime:extensionkit_native", + "ability_runtime:napi_common", + "ability_runtime:runtime", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "hitrace:hitrace_meter", + "image_framework:image_native", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":ExternalFileAccessListFileFuzzTest" ] +} diff --git a/test/fuzztest/externalfileaccesslistfile_fuzzer/corpus/init b/test/fuzztest/externalfileaccesslistfile_fuzzer/corpus/init new file mode 100644 index 00000000..2b595da0 --- /dev/null +++ b/test/fuzztest/externalfileaccesslistfile_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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/test/fuzztest/externalfileaccesslistfile_fuzzer/externalfileaccesslistfile_fuzzer.cpp b/test/fuzztest/externalfileaccesslistfile_fuzzer/externalfileaccesslistfile_fuzzer.cpp new file mode 100644 index 00000000..3ebb65b4 --- /dev/null +++ b/test/fuzztest/externalfileaccesslistfile_fuzzer/externalfileaccesslistfile_fuzzer.cpp @@ -0,0 +1,120 @@ +/* + * 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 "externalfileaccesslistfile_fuzzer.h" + +#include +#include +#include +#include + +#include "extension_base.h" +#include "extension_context.h" +#include "message_parcel.h" +#include "file_access_ext_stub.h" +#include "file_access_ext_stub_impl.h" +#include "file_access_ext_ability.h" +#include "js_file_access_ext_ability.h" +#include "js_runtime.h" +#include "securec.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "accesstoken_kit.h" + +namespace OHOS { +using namespace std; +using namespace OHOS; +using namespace FileAccessFwk; +using namespace AbilityRuntime; +constexpr size_t FOO_MAX_LEN = 1024; +constexpr size_t U32_AT_SIZE = 4; + + +enum { + TOKEN_INDEX_ONE = 0, +}; + +void SetNativeToken() +{ + uint64_t tokenId; + const char **perms = new const char *[1]; + perms[TOKEN_INDEX_ONE] = "ohos.permission.FILE_ACCESS_MANAGER"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "ExternalFileAccessListFileFuzzTest"; + tokenId = GetAccessTokenId(&infoInstance); + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool ExternalFileAccessListFileFuzzTest(std::unique_ptr data, size_t size) +{ + SetNativeToken(); + // CMD_LISTFILE + uint32_t code = 8; + MessageParcel datas; + datas.WriteInterfaceToken(FileAccessExtStub::GetDescriptor()); + datas.WriteBuffer(data.get(), size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + + auto fileAccessExtAbility = FileAccessExtAbility::Create(nullptr); + auto fileAccessExtAbilitySharePtr = std::shared_ptr(fileAccessExtAbility); + + sptr fileAccessExtStubObj(new (std::nothrow) FileAccessExtStubImpl( + fileAccessExtAbilitySharePtr, nullptr)); + + fileAccessExtStubObj->OnRemoteRequest(code, datas, reply, option); + + fileAccessExtAbility = nullptr; + fileAccessExtStubObj = nullptr; + + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +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::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) { + return 0; + } + + auto str = std::make_unique(size + 1); + (void)memset_s(str.get(), size + 1, 0x00, size + 1); + if (memcpy_s(str.get(), size, data, size) != EOK) { + return 0; + } + + OHOS::ExternalFileAccessListFileFuzzTest(move(str), size); + return 0; +} diff --git a/test/fuzztest/externalfileaccesslistfile_fuzzer/externalfileaccesslistfile_fuzzer.h b/test/fuzztest/externalfileaccesslistfile_fuzzer/externalfileaccesslistfile_fuzzer.h new file mode 100644 index 00000000..15fd85f4 --- /dev/null +++ b/test/fuzztest/externalfileaccesslistfile_fuzzer/externalfileaccesslistfile_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FILE_ACCESS_EXTERNAL_FILE_ACCESS_LIST_FILE_FUZZER_H +#define FILE_ACCESS_EXTERNAL_FILE_ACCESS_LIST_FILE_FUZZER_H + +#define FUZZ_PROJECT_NAME "externalfileaccesslistfile_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/externalfileaccesslistfile_fuzzer/project.xml b/test/fuzztest/externalfileaccesslistfile_fuzzer/project.xml new file mode 100644 index 00000000..2bb0dbd7 --- /dev/null +++ b/test/fuzztest/externalfileaccesslistfile_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + -- Gitee From 118363bd656bd2930885e7870c5b7759ed35f988 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Thu, 17 Aug 2023 21:38:24 +0800 Subject: [PATCH 13/13] =?UTF-8?q?UserFileService-FileAccessExtAbility-mkdi?= =?UTF-8?q?r=E7=94=A8=E4=BE=8B=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I8b10d64a352497a7c4dbef89fd75ff91989a43c5 --- .../externalfileaccessmkdir_fuzzer/BUILD.gn | 70 ++++++++++ .../corpus/init | 16 +++ .../externalfileaccessmkdir_fuzzer.cpp | 120 ++++++++++++++++++ .../externalfileaccessmkdir_fuzzer.h | 21 +++ .../project.xml | 25 ++++ 5 files changed, 252 insertions(+) create mode 100644 test/fuzztest/externalfileaccessmkdir_fuzzer/BUILD.gn create mode 100644 test/fuzztest/externalfileaccessmkdir_fuzzer/corpus/init create mode 100644 test/fuzztest/externalfileaccessmkdir_fuzzer/externalfileaccessmkdir_fuzzer.cpp create mode 100644 test/fuzztest/externalfileaccessmkdir_fuzzer/externalfileaccessmkdir_fuzzer.h create mode 100644 test/fuzztest/externalfileaccessmkdir_fuzzer/project.xml diff --git a/test/fuzztest/externalfileaccessmkdir_fuzzer/BUILD.gn b/test/fuzztest/externalfileaccessmkdir_fuzzer/BUILD.gn new file mode 100644 index 00000000..54fcb5d1 --- /dev/null +++ b/test/fuzztest/externalfileaccessmkdir_fuzzer/BUILD.gn @@ -0,0 +1,70 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") +ohos_fuzztest("ExternalFileAccessMkdirFuzzTest") { + module_out_path = "filemanagement/user_file_service" + fuzz_config_file = + "${user_file_service_path}/test/fuzztest/externalfileaccessmkdir_fuzzer" + include_dirs = [ + "${user_file_service_path}/utils", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/app", + "${ability_runtime_kits_path}/appkit/native/app/include", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/context", + "${user_file_service_path}/interfaces/inner_api/file_access/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${user_file_service_path}/test/fuzztest/externalfileaccessmkdir_fuzzer/externalfileaccessmkdir_fuzzer.cpp" ] + + deps = [ + "${ability_runtime_path}/frameworks/native/ability/native:abilitykit_native", + "${ability_runtime_path}/frameworks/native/appkit:app_context", + "${access_token_path}/frameworks/accesstoken:accesstoken_communication_adapter_cxx", + "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:app_context", + "ability_runtime:extensionkit_native", + "ability_runtime:napi_common", + "ability_runtime:runtime", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "hitrace:hitrace_meter", + "image_framework:image_native", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":ExternalFileAccessMkdirFuzzTest" ] +} diff --git a/test/fuzztest/externalfileaccessmkdir_fuzzer/corpus/init b/test/fuzztest/externalfileaccessmkdir_fuzzer/corpus/init new file mode 100644 index 00000000..2b595da0 --- /dev/null +++ b/test/fuzztest/externalfileaccessmkdir_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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/test/fuzztest/externalfileaccessmkdir_fuzzer/externalfileaccessmkdir_fuzzer.cpp b/test/fuzztest/externalfileaccessmkdir_fuzzer/externalfileaccessmkdir_fuzzer.cpp new file mode 100644 index 00000000..4b693043 --- /dev/null +++ b/test/fuzztest/externalfileaccessmkdir_fuzzer/externalfileaccessmkdir_fuzzer.cpp @@ -0,0 +1,120 @@ +/* + * 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 "externalfileaccessmkdir_fuzzer.h" + +#include +#include +#include +#include + +#include "extension_base.h" +#include "extension_context.h" +#include "message_parcel.h" +#include "file_access_ext_stub.h" +#include "file_access_ext_stub_impl.h" +#include "file_access_ext_ability.h" +#include "js_file_access_ext_ability.h" +#include "js_runtime.h" +#include "securec.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "accesstoken_kit.h" + +namespace OHOS { +using namespace std; +using namespace OHOS; +using namespace FileAccessFwk; +using namespace AbilityRuntime; +constexpr size_t FOO_MAX_LEN = 1024; +constexpr size_t U32_AT_SIZE = 4; + + +enum { + TOKEN_INDEX_ONE = 0, +}; + +void SetNativeToken() +{ + uint64_t tokenId; + const char **perms = new const char *[1]; + perms[TOKEN_INDEX_ONE] = "ohos.permission.FILE_ACCESS_MANAGER"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "ExternalFileAccessMkdirFuzzTest"; + tokenId = GetAccessTokenId(&infoInstance); + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} + +bool ExternalFileAccessMkdirFuzzTest(std::unique_ptr data, size_t size) +{ + SetNativeToken(); + // CMD_MKDIR + uint32_t code = 3; + MessageParcel datas; + datas.WriteInterfaceToken(FileAccessExtStub::GetDescriptor()); + datas.WriteBuffer(data.get(), size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + + auto fileAccessExtAbility = FileAccessExtAbility::Create(nullptr); + auto fileAccessExtAbilitySharePtr = std::shared_ptr(fileAccessExtAbility); + + sptr fileAccessExtStubObj(new (std::nothrow) FileAccessExtStubImpl( + fileAccessExtAbilitySharePtr, nullptr)); + + fileAccessExtStubObj->OnRemoteRequest(code, datas, reply, option); + + fileAccessExtAbility = nullptr; + fileAccessExtStubObj = nullptr; + + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +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::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) { + return 0; + } + + auto str = std::make_unique(size + 1); + (void)memset_s(str.get(), size + 1, 0x00, size + 1); + if (memcpy_s(str.get(), size, data, size) != EOK) { + return 0; + } + + OHOS::ExternalFileAccessMkdirFuzzTest(move(str), size); + return 0; +} diff --git a/test/fuzztest/externalfileaccessmkdir_fuzzer/externalfileaccessmkdir_fuzzer.h b/test/fuzztest/externalfileaccessmkdir_fuzzer/externalfileaccessmkdir_fuzzer.h new file mode 100644 index 00000000..111bc329 --- /dev/null +++ b/test/fuzztest/externalfileaccessmkdir_fuzzer/externalfileaccessmkdir_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FILE_ACCESS_EXTERNAL_FILE_ACCESS_MKDIR_FUZZER_H +#define FILE_ACCESS_EXTERNAL_FILE_ACCESS_MKDIR_FUZZER_H + +#define FUZZ_PROJECT_NAME "externalfileaccessmkdir_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/externalfileaccessmkdir_fuzzer/project.xml b/test/fuzztest/externalfileaccessmkdir_fuzzer/project.xml new file mode 100644 index 00000000..2bb0dbd7 --- /dev/null +++ b/test/fuzztest/externalfileaccessmkdir_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + -- Gitee