From ed567a66b52f31a5ef0b3793bdf4beee8d8bdc15 Mon Sep 17 00:00:00 2001 From: hahahahahayesyeseys <1145018948@qq.com> Date: Wed, 17 Jul 2024 17:20:34 +0800 Subject: [PATCH 1/6] 0717 --- .gitee/PULL_REQUEST_TEMPLATE.zh-CN.md | 71 ----------------- .../js/src/mod_fs/properties/listfile.cpp | 67 +++++++++++++++- .../kits/js/src/mod_fs/properties/open.cpp | 79 +++++++++++++++++-- 3 files changed, 136 insertions(+), 81 deletions(-) delete mode 100644 .gitee/PULL_REQUEST_TEMPLATE.zh-CN.md diff --git a/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md b/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md deleted file mode 100644 index 44f2fb5e5..000000000 --- a/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md +++ /dev/null @@ -1,71 +0,0 @@ -**Description:** - -**Issue number:** - -**Test & Result:** - -**CodeCheck:** - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
类型自检项自检结果
多线程相关在类的成员变量中定义了vector/map/list等容器类型,且在多个成员函数中有操作时,需要加锁保护自检结果:
定义全局变量,在多个函数中都有操作时,需要加锁保护自检结果:
内存相关调用外部接口时,确认是否对返回值做了判断,尤其外部接口返回了nullptr的情况,避免进程崩溃自检结果:
调用安全函数时,如memcpy_s等,是否检查其返回值自检结果:
检查函数中是否涉及了内存或资源申请(如文件句柄),注意每个异常退出流程,是否都已经将资源释放(推荐使用RAII)自检结果:
隐式内存分配场景:realpath、ReadParcelable序列化、cJSON相关函数时等,需主动释放或使用智能指针 自检结果:
校验外部输入使用nlohmann:json解析外部输入时,需判断参数类型是否符合预期自检结果:
所有外部输入均不可信,需判断外部输入是否直接作为内存分配的大小,数组下标、循环条件、SQL查询等自检结果:
外部输入的路径不可信,需使用realpath做标准化处理,并判断路径的合法性自检结果:
外部输入包括对外提供的接口,IPC的proxy/stub接口,序列化/反序列化接口等自检结果:
数学运算代码中是否混合了加减乘除等运算,需检查是否可能导致整数溢出或符号翻转自检结果:
需检查代码是否有高精度数字转换为低精度的操作,如果必须,建议使用C++安全类型转换接口自检结果:
秘钥相关如变量临时保存了口令、秘钥等,需要在使用完成后及时清空(内存memset掉)自检结果:
权限相关作为系统服务对外提供了接口,是否做了权限保护和校验(如需要),只允许申请了权限的应用访问自检结果:
内核对外提供了设备节点,是否做了权限保护,只允许特定的系统服务访问自检结果:
内核操作是否有mmap操作,并使用remap_pfn_range进行地址映射时,校验起始地址是否是用户态输入且没有做合法性校验 自检结果:
是否有copy_from_user,并对外部输入的数据做了长度校验,以防止缓冲区溢出自检结果:
是否有使用copy_to_user,并在返回到用户态时,对数据做了完整初始化,或使用memset情况后再赋值自检结果:
\ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 777297995..27dfe4f2c 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -326,13 +326,72 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succPath, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath(); - if (!succPath) { - HILOGE("Invalid path"); + // get path type:arraybuffer || string + napi_valuetype valueType; + napi_status status = napi_typeof(env, funcArg[NARG_POS::FIRST], &valueType); + + if (status != napi_ok) { NError(EINVAL).ThrowErr(env); return nullptr; } - if (!GetOptionArg(env, funcArg, g_optionArgs, string(path.get()))) { + std::string pathStr; + if(valueType==napi_string){//string + auto [succPath, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath(); + if (!succPath) { + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + pathStr = std::string(path.get()); + } else if(valueType==napi_object){//arraybuffer + bool isArrayBuffer=false; + status = napi_is_arraybuffer(env, funcArg[NARG_POS::FIRST], &isArrayBuffer); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + if(!IsArrayBuffer) { + HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + size_t arrayBufferLength; + void* arrayBufferData; + //get [arraybuffer pointer] and its [length] to transfer to string + status = napi_get_arraybuffer_info(env, funcArg[NARG_POS::FIRST], &arrayBufferData, &arrayBufferLength); + + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + //create reference to assert arraybuffer alive while being used + napi_ref arrayBufferRef; + + status = napi_create_reference(env, funcArg[NARG_POS::FIRST], 1, &arrayBufferRef); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + //get path + pathStr = std::string(static_cast(data), length); + + // delete referrence + status = napi_delete_reference(env, arrayBufferRef); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + } else { + HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + if (!GetOptionArg(env, funcArg, g_optionArgs, pathStr)) { HILOGE("Invalid options"); NError(EINVAL).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 81efa8112..3d0d19d26 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -229,17 +229,84 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath(); - if (!succPath) { - HILOGE("Invalid path"); + // get path type:arraybuffer || string + napi_valuetype valueType; + napi_status status = napi_typeof(env, funcArg[NARG_POS::FIRST], &valueType); + + if (status != napi_ok) { NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succMode, mode] = GetJsFlags(env, funcArg); - if (!succMode) { + + //init path + std::string pathStr; + if(valueType==napi_string){//string + auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath(); + if (!succPath) { + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto [succMode, mode] = GetJsFlags(env, funcArg); + if (!succMode) { + return nullptr; + } + pathStr = std::string(path.get()); + + } + else if(valueType==napi_object){//arraybuffer + bool isArrayBuffer=false; + status = napi_is_arraybuffer(env, funcArg[NARG_POS::FIRST], &isArrayBuffer); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + if(!IsArrayBuffer) { + HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succMode, mode] = GetJsFlags(env, funcArg); + if (!succMode) { + return nullptr; + } + + //get [arraybuffer pointer] and its [length] to transfer to string + size_t arrayBufferLength; + void* arrayBufferData; + status = napi_get_arraybuffer_info(env, funcArg[NARG_POS::FIRST], &arrayBufferData, &arrayBufferLength); + + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + //create reference to assert arraybuffer alive while being used + napi_ref arrayBufferRef; + + status = napi_create_reference(env, funcArg[NARG_POS::FIRST], 1, &arrayBufferRef); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + //get path + pathStr = std::string(static_cast(data), length); + + // delete referrence + status = napi_delete_reference(env, arrayBufferRef); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + } else { + HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); + NError(EINVAL).ThrowErr(env); return nullptr; } - string pathStr(path.get()); + #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) if (pathStr.find("://") != string::npos) { auto [res, uriStr] = OpenFileByUri(pathStr, mode); -- Gitee From e2804d7ba678227b4cdf1a621ab63b952bbe1ed2 Mon Sep 17 00:00:00 2001 From: hahahahahayesyeseys <1145018948@qq.com> Date: Thu, 18 Jul 2024 09:44:54 +0800 Subject: [PATCH 2/6] add arraybuffer to open and listfile Signed-off-by: hahahahahayesyeseys <1145018948@qq.com> --- interfaces/kits/js/src/mod_fs/properties/listfile.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 27dfe4f2c..81b2eda88 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -329,7 +329,6 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) // get path type:arraybuffer || string napi_valuetype valueType; napi_status status = napi_typeof(env, funcArg[NARG_POS::FIRST], &valueType); - if (status != napi_ok) { NError(EINVAL).ThrowErr(env); return nullptr; @@ -355,17 +354,14 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - size_t arrayBufferLength; void* arrayBufferData; //get [arraybuffer pointer] and its [length] to transfer to string status = napi_get_arraybuffer_info(env, funcArg[NARG_POS::FIRST], &arrayBufferData, &arrayBufferLength); - if (status != napi_ok) { NError(EINVAL).ThrowErr(env); return nullptr; } - //create reference to assert arraybuffer alive while being used napi_ref arrayBufferRef; -- Gitee From 5f2fece43a16ebb5034c2bc48774a3aecbfe5b15 Mon Sep 17 00:00:00 2001 From: hahahahahayesyeseys <1145018948@qq.com> Date: Thu, 18 Jul 2024 10:01:05 +0800 Subject: [PATCH 3/6] add arraybuffer to open and listfile Signed-off-by: hahahahahayesyeseys <1145018948@qq.com> --- interfaces/kits/js/src/mod_fs/properties/listfile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 81b2eda88..912673f17 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -361,7 +361,7 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) if (status != napi_ok) { NError(EINVAL).ThrowErr(env); return nullptr; - } + } //create reference to assert arraybuffer alive while being used napi_ref arrayBufferRef; -- Gitee From 85840d6ed921a7bfdbcd200ef2fbad80dfee3644 Mon Sep 17 00:00:00 2001 From: hahahahahayesyeseys <1145018948@qq.com> Date: Thu, 18 Jul 2024 10:01:28 +0800 Subject: [PATCH 4/6] 1145018948@qq.com Signed-off-by: hahahahahayesyeseys <1145018948@qq.com> --- interfaces/kits/js/src/mod_fs/properties/listfile.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 912673f17..7d96930fb 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -364,7 +364,6 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) } //create reference to assert arraybuffer alive while being used napi_ref arrayBufferRef; - status = napi_create_reference(env, funcArg[NARG_POS::FIRST], 1, &arrayBufferRef); if (status != napi_ok) { NError(EINVAL).ThrowErr(env); -- Gitee From 56f9a1db71b3cdfe01478e0cf688a1f0594aa36e Mon Sep 17 00:00:00 2001 From: hahahahahayesyeseys <1145018948@qq.com> Date: Thu, 18 Jul 2024 10:04:27 +0800 Subject: [PATCH 5/6] 1145018948@qq.com Signed-off-by: hahahahahayesyeseys <1145018948@qq.com> --- interfaces/kits/js/src/mod_fs/properties/listfile.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 7d96930fb..6c7b7f0ac 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -369,7 +369,6 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - //get path pathStr = std::string(static_cast(data), length); -- Gitee From b1c7b1fb85997ac500ed5fa7e36dc69280fdd092 Mon Sep 17 00:00:00 2001 From: hahahahahayesyeseys <1145018948@qq.com> Date: Thu, 18 Jul 2024 10:07:15 +0800 Subject: [PATCH 6/6] add arraybuffer to open and listfile Signed-off-by: hahahahahayesyeseys <1145018948@qq.com> --- interfaces/kits/js/src/mod_fs/properties/listfile.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 6c7b7f0ac..808627853 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -363,6 +363,7 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) return nullptr; } //create reference to assert arraybuffer alive while being used + napi_ref arrayBufferRef; status = napi_create_reference(env, funcArg[NARG_POS::FIRST], 1, &arrayBufferRef); if (status != napi_ok) { -- Gitee