diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp index 3374bb7c8ea864c9db490df04221eb54ee66dce4..6a2e98b85f84783083d330259a0b0a49c844322d 100644 --- a/interfaces/innerkits/native/file_share/src/file_share.cpp +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -250,28 +250,34 @@ int32_t CreateShareFile(const string &uri, uint32_t tokenId, uint32_t flag) return 0; } +static void DelSharePath(const string &delPath) +{ + if (!SandboxHelper::CheckValidPath(delPath)) { + LOGE("DelSharePath, umount path is invalid, path = %{private}s", delPath.c_str()); + return; + } + + if (access(delPath.c_str(), F_OK) == 0) { + if (umount2(delPath.c_str(), MNT_DETACH) != 0) { + LOGE("DelSharePath, umount failed with %{public}d", errno); + } + remove(delPath.c_str()); + } +} + static void UmountDelUris(vector sharePathList, string currentUid, string bundleNameSelf) { + string delPathPrefix = DATA_APP_EL2_PATH + currentUid + SHARE_PATH + bundleNameSelf; for (size_t i = 0; i < sharePathList.size(); i++) { Uri uri(SandboxHelper::Decode(sharePathList[i])); string path = uri.GetPath(); string bundleName = uri.GetAuthority(); - string delRPath = DATA_APP_EL2_PATH + currentUid + SHARE_PATH + bundleNameSelf + SHARE_R_PATH + - bundleName + path; - string delRWPath = DATA_APP_EL2_PATH + currentUid + SHARE_PATH + bundleNameSelf + SHARE_RW_PATH + - bundleName + path; - if (access(delRPath.c_str(), F_OK) == 0) { - if (umount2(delRPath.c_str(), MNT_DETACH) != 0) { - LOGE("UmountdelRPath, umount failed with %{public}d", errno); - } - remove(delRPath.c_str()); - } - if (access(delRWPath.c_str(), F_OK) == 0) { - if (umount2(delRWPath.c_str(), MNT_DETACH) != 0) { - LOGE("UmountdelRWPath, umount failed with %{public}d", errno); - } - remove(delRWPath.c_str()); - } + + string delRPath = delPathPrefix + SHARE_R_PATH + bundleName + path; + DelSharePath(delRPath); + + string delRWPath = delPathPrefix + SHARE_RW_PATH + bundleName + path; + DelSharePath(delRWPath); } } diff --git a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp index c31ff7b264d653b8fa65d3265fe0588fa7e5e4a7..a7f7321a467caec50d1639be27eb09ab80f38f6a 100644 --- a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp +++ b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp @@ -153,6 +153,15 @@ static bool DeleteShareDir(const std::string &PACKAGE_PATH, const std::string &S return result; } +static bool IsValidPath(const std::string &path) +{ + if (path.find("/./") != std::string::npos || + path.find("/../") != std::string::npos) { + return false; + } + return true; +} + static int CreateShareFile(struct HmdfsShareControl &shareControl, const char* file, const std::string &deviceId) { @@ -202,6 +211,11 @@ int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, } const std::string PACKAGE_PATH = GetLowerSharePath(userId, processName); + if (!IsValidPath(PACKAGE_PATH)) { + LOGE("RemoteFileShare::CreateSharePath, GetLowerSharePath failed with %{private}s", PACKAGE_PATH.c_str()); + return EACCES; + } + const std::string LOWER_SHARE_PATH = PACKAGE_PATH + SHARE_PATH_DIR; if (CreateShareDir(PACKAGE_PATH) != 0) return errno; @@ -251,15 +265,6 @@ static int GetDistributedPath(Uri &uri, const int &userId, std::string &distribu return 0; } -static bool IsValidPath(const std::string &path) -{ - if (path.find("/./") != std::string::npos || - path.find("/../") != std::string::npos) { - return false; - } - return true; -} - static std::string GetPhysicalPath(Uri &uri, const std::string &userId) { std::string sandboxPath = uri.GetPath(); diff --git a/test/unittest/file_share_native/file_share_test.cpp b/test/unittest/file_share_native/file_share_test.cpp index b464ef07514f91308a740a9aa5bcbf5b20cb40b8..143f07eb6d61e406699dadc472aaca02bd5ef36d 100644 --- a/test/unittest/file_share_native/file_share_test.cpp +++ b/test/unittest/file_share_native/file_share_test.cpp @@ -234,6 +234,53 @@ namespace { GTEST_LOG_(INFO) << "FileShareTest-end File_share_DeleteShareFile_0006"; } + /** + * @tc.name: File_share_DeleteShareFile_0007 + * @tc.desc: Test function of DeleteShareFile() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileShareTest, File_share_DeleteShareFile_0007, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_DeleteShareFile_0007"; + int32_t uid = -1; + uid = OHOS::IPCSkeleton::GetCallingUid(); + + string bundleNameA = "com.ohos.settingsdata"; + string fileStr = "/data/app/el2/" + to_string(uid) + "/base/" + bundleNameA + "/files/test.txt"; + int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT); + ASSERT_TRUE(fd != -1) << "FileShareTest Create File Failed!"; + string uri = "file://" + bundleNameA + "/data/storage/el2/base/files/test.txt"; + + string bundleNameB = "com.ohos.systemui"; + uint32_t tokenId = AccessTokenKit::GetHapTokenID(uid, bundleNameB, 0); + + int32_t flag = 3; + int32_t ret = CreateShareFile(uri, tokenId, flag); + EXPECT_EQ(ret, E_OK); + + vector sharePathList; + string uriErr = "file://" + bundleNameA + "/data/storage/el2/base/files/abc/../test.txt"; + sharePathList.push_back(uriErr); + ret = DeleteShareFile(tokenId, sharePathList); + EXPECT_EQ(ret, E_OK); + + string sharePath = "/data/service/el2/" + to_string(uid) + "/share/" + bundleNameB + + "/rw/" + bundleNameA + "/data/storage/el2/base/files/test.txt"; + ret = access(sharePath.c_str(), F_OK); + EXPECT_EQ(ret, E_OK); + + sharePathList.push_back(uri); + ret = DeleteShareFile(tokenId, sharePathList); + EXPECT_EQ(ret, E_OK); + + ret = access(sharePath.c_str(), F_OK); + EXPECT_EQ(ret, -1); + GTEST_LOG_(INFO) << "FileShareTest-end File_share_DeleteShareFile_0007"; + } + /** * @tc.name: File_share_GetPhysicalPath_0001 * @tc.desc: Test function of GetPhysicalPath() interface for SUCCESS. diff --git a/test/unittest/remote_file_share/remote_file_share_test.cpp b/test/unittest/remote_file_share/remote_file_share_test.cpp index c9bb30a640b006cf37ab0f4bc02d572249b6b081..19a1cf78b81db5063910b840232a93445599444e 100644 --- a/test/unittest/remote_file_share/remote_file_share_test.cpp +++ b/test/unittest/remote_file_share/remote_file_share_test.cpp @@ -139,6 +139,39 @@ namespace { GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_CreateSharePath_0004"; } + /** + * @tc.name: remote_file_share_test_0005 + * @tc.desc: Test function of CreateSharePath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_CreateSharePath_0005, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_CreateSharePath_0005"; + const string fileStr = "/data/test/remote_file_share_test.txt"; + int fd = open(fileStr.c_str(), O_RDWR); + ASSERT_TRUE(fd != -1) << "RemoteFileShareTest Create File Failed!"; + const int userId = 100; + const string deviceId = "0"; + string sharePath = ""; + char pthreadName[PATH_MAX]; + int ret = pthread_getname_np(pthread_self(), pthreadName, sizeof(pthreadName)); + EXPECT_EQ(ret, E_OK); + string pthreadNameStr = pthreadName; + string errPthreadName = "../test"; + ret = pthread_setname_np(pthread_self(), errPthreadName.c_str()); + EXPECT_EQ(ret, E_OK); + ret = RemoteFileShare::CreateSharePath(fd, sharePath, userId, deviceId); + close(fd); + EXPECT_NE(ret, E_OK); + ret = pthread_setname_np(pthread_self(), pthreadNameStr.c_str()); + EXPECT_EQ(ret, E_OK); + GTEST_LOG_(INFO) << "RemoteFileShareTest Create Share Path " << sharePath; + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_CreateSharePath_0005"; + } + /** * @tc.name: remote_file_share_test_0005 * @tc.desc: Test function of GetDfsUriFromLocal() interface for SUCCESS.