1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_OPERATOR_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_OPERATOR_H 18 19 #include <mutex> 20 #include <string> 21 #include <vector> 22 23 #include "aot/aot_args.h" 24 #include "appexecfwk_errors.h" 25 #include "bundle_extractor.h" 26 #include "code_sign_helper.h" 27 #include "installd/installd_constants.h" 28 #include "ipc/check_encryption_param.h" 29 #include "ipc/code_signature_param.h" 30 #include "ipc/extract_param.h" 31 #include "nocopyable.h" 32 33 namespace OHOS { 34 namespace AppExecFwk { 35 using EnforceMetadataProcessForApp = int32_t (*)(const std::unordered_map<std::string, std::string> &, 36 uint32_t, bool &, const int32_t, const bool &); 37 38 class InstalldOperator { 39 public: 40 /** 41 * @brief Check whether a file exist. 42 * @param path Indicates the file path to be checked. 43 * @return Returns true if the file exist; returns false otherwise. 44 */ 45 static bool IsExistFile(const std::string &path); 46 /** 47 * @brief Check whether an AP file exists in the current directory of the file. 48 * @param path Indicates the file path to be checked. 49 * @return Returns true if the file exist; returns false otherwise. 50 */ 51 static bool IsExistApFile(const std::string &path); 52 /** 53 * @brief Check whether a directory exist. 54 * @param path Indicates the directory path to be checked. 55 * @return Returns true if the directory exist; returns false otherwise. 56 */ 57 static bool IsExistDir(const std::string &path); 58 /** 59 * @brief Check whether a directory is empty. 60 * @param dir Indicates the directory path to be checked. 61 * @return Returns true if the directory is empty; returns false otherwise. 62 */ 63 static bool IsDirEmpty(const std::string &dir); 64 /** 65 * @brief Make a new directory including the parent path if not exist. 66 * @param path Indicates the directory path to be checked. 67 * @param isReadByOthers Indicates the directory whether read by other users. 68 * @return Returns true if the directory make successfully; returns false otherwise. 69 */ 70 static bool MkRecursiveDir(const std::string &path, bool isReadByOthers); 71 /** 72 * @brief Delete a directory. 73 * @param path Indicates the directory path to be deleted. 74 * @return Returns true if the directory deleted successfully; returns false otherwise. 75 */ 76 static bool DeleteDir(const std::string &path); 77 /** 78 * @brief Extract the files of a compressed package to a specific directory. 79 * @param srcModulePath Indicates the package file path. 80 * @param targetSoPath so files decompression path. 81 * @param cpuAbi cpuAbi. 82 * @return Returns true if the package extracted successfully; returns false otherwise. 83 */ 84 static bool ExtractFiles(const std::string &sourcePath, const std::string &targetSoPath, 85 const std::string &cpuAbi); 86 87 static bool IsNativeSo(const std::string &entryName, const std::string &cpuAbi); 88 89 static bool ExtractFiles(const ExtractParam &extractParam); 90 static bool ExtractFiles(const std::string hnpPackageInfo, const ExtractParam &extractParam); 91 static void ExtractTargetFile( 92 const BundleExtractor &extractor, 93 const std::string &entryName, 94 const std::string &targetPath, 95 const std::string &cpuAbi, 96 const ExtractFileType &extractFileType = ExtractFileType::SO); 97 static void ExtractTargetHnpFile( 98 const BundleExtractor &extractor, 99 const std::string &entryName, 100 const std::string &targetPath, 101 const ExtractFileType &extractFileType = ExtractFileType::SO); 102 static bool ProcessBundleInstallNative( 103 const std::string &userId, 104 const std::string &hnpRootPath, 105 const std::string &hapPath, 106 const std::string &cpuAbi, 107 const std::string &packageName); 108 static bool ProcessBundleUnInstallNative(const std::string &userId, const std::string &bundleName); 109 110 static bool DeterminePrefix(const ExtractFileType &extractFileType, const std::string &cpuAbi, 111 std::string &prefix); 112 113 static bool DetermineSuffix(const ExtractFileType &extractFileType, std::vector<std::string> &suffixes); 114 115 static bool IsNativeFile( 116 const std::string &entryName, const ExtractParam &extractParam); 117 118 /** 119 * @brief Rename a directory from old path to new path. 120 * @param oldPath Indicates the old path name. 121 * @param newPath Indicates the new path name. 122 * @return Returns true if the directory renamed successfully; returns false otherwise. 123 */ 124 static bool RenameDir(const std::string &oldPath, const std::string &newPath); 125 /** 126 * @brief Change the owner and group ID of a file or directory. 127 * @param filePath Indicates the file or directory path. 128 * @param uid Indicates the uid. 129 * @param uid Indicates the gid. 130 * @return Returns true if changed successfully; returns false otherwise. 131 */ 132 static bool ChangeFileAttr(const std::string &filePath, const int uid, const int gid); 133 /** 134 * @brief Rename a file from old path to new path. 135 * @param oldPath Indicates the old path name. 136 * @param newPath Indicates the new path name. 137 * @return Returns true if the file renamed successfully; returns false otherwise. 138 */ 139 static bool RenameFile(const std::string &oldPath, const std::string &newPath); 140 /** 141 * @brief Check whether a path is valid under a root path. 142 * @param rootDir Indicates the root path name. 143 * @param path Indicates the path to be checked. 144 * @return Returns true if the path is valid successfully; returns false otherwise. 145 */ 146 static bool IsValidPath(const std::string &rootDir, const std::string &path); 147 /** 148 * @brief Check whether a path is valid code path. 149 * @param codePath Indicates the path to be checked. 150 * @return Returns true if the file renamed successfully; returns false otherwise. 151 */ 152 static bool IsValidCodePath(const std::string &codePath); 153 /** 154 * @brief Get the parent directory path of a file. 155 * @param codePath Indicates the file path. 156 * @return Returns the parent directory if get successfully; returns empty string otherwise. 157 */ 158 static std::string GetPathDir(const std::string &path); 159 /** 160 * @brief Delete files in a directory. 161 * @param path Indicates the directory path of the files to be deleted. 162 * @return Returns true if the files deleted successfully; returns false otherwise. 163 */ 164 static bool DeleteFiles(const std::string &dataPath); 165 /** 166 * @brief Delete files in a directory except the directories to be kept. 167 * @param dataPath Indicates the directory path of the files to be deleted. 168 * @param dirsToKeep Indicates the directories to be kept. 169 * @return Returns true if the files deleted successfully; returns false otherwise 170 */ 171 static bool DeleteFilesExceptDirs(const std::string &dataPath, const std::vector<std::string> &dirsToKeep); 172 /** 173 * @brief Make a directory and change the owner and group ID of it. 174 * @param path Indicates the directory path to be made. 175 * @param isReadByOthers Indicates the directory whether read by other users. 176 * @param uid Indicates the uid. 177 * @param uid Indicates the gid. 178 * @return Returns true if directory made successfully; returns false otherwise. 179 */ 180 static bool MkOwnerDir(const std::string &path, bool isReadByOthers, const int uid, const int gid); 181 /** 182 * @brief Make a directory and change the owner and group ID of it. 183 * @param path Indicates the directory path to be made. 184 * @param mode Indicates the directory mode. 185 * @param uid Indicates the uid. 186 * @param uid Indicates the gid. 187 * @return Returns true if directory made successfully; returns false otherwise. 188 */ 189 static bool MkOwnerDir(const std::string &path, int mode, const int uid, const int gid); 190 /** 191 * @brief Get disk usage for dir. 192 * @param dir Indicates the directory. 193 * @param size Indicates the disk size. 194 * @return Returns true if successfully; returns false otherwise. 195 */ 196 static int64_t GetDiskUsage(const std::string &dir, bool isRealPath = false); 197 /** 198 * @brief Traverse all cache directories. 199 * @param currentPath Indicates the current path. 200 * @param cacheDirs Indicates the cache directories. 201 * @return Returns true if successfully; returns false otherwise. 202 */ 203 static void TraverseCacheDirectory(const std::string ¤tPath, std::vector<std::string> &cacheDirs); 204 /** 205 * @brief Get disk usage from path. 206 * @param path Indicates the current path. 207 * @return Returns disk size. 208 */ 209 static int64_t GetDiskUsageFromPath(const std::vector<std::string> &path); 210 211 static bool InitialiseQuotaMounts(); 212 213 static int64_t GetDiskUsageFromQuota(const int32_t uid); 214 215 static bool ScanDir( 216 const std::string &dirPath, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths); 217 218 static bool ScanSoFiles(const std::string &newSoPath, const std::string &originPath, 219 const std::string ¤tPath, std::vector<std::string> &paths); 220 221 static bool CopyFile(const std::string &sourceFile, const std::string &destinationFile); 222 223 static bool CopyFileFast(const std::string &sourcePath, const std::string &destPath); 224 225 static bool ChangeDirOwnerRecursively(const std::string &path, const int uid, const int gid); 226 227 static bool IsDiffFiles(const std::string &entryName, 228 const std::string &targetPath, const std::string &cpuAbi); 229 230 static bool ExtractDiffFiles(const std::string &filePath, const std::string &targetPath, 231 const std::string &cpuAbi); 232 233 static bool ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath, 234 const std::string &newSoPath, int32_t uid); 235 236 static bool ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &fileVec); 237 238 static bool CopyFiles(const std::string &sourceDir, const std::string &destinationDir); 239 240 static bool GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi, 241 std::vector<std::string> &fileNames); 242 243 static bool GetAtomicServiceBundleDataDir(const std::string &bundleName, 244 const int32_t userId, std::vector<std::string> &allPathNames); 245 246 #if defined(CODE_SIGNATURE_ENABLE) 247 static bool PrepareEntryMap(const CodeSignatureParam &codeSignatureParam, 248 const std::vector<std::string> &soEntryFiles, Security::CodeSign::EntryMap &entryMap); 249 static ErrCode PerformCodeSignatureCheck(const CodeSignatureParam &codeSignatureParam, 250 const Security::CodeSign::EntryMap &entryMap); 251 #endif 252 253 static bool VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam); 254 255 #if defined(CODE_ENCRYPTION_ENABLE) 256 static bool EnforceEncryption(std::unordered_map<std::string, std::string> &entryMap, int32_t bundleId, 257 bool &isEncryption, InstallBundleType installBundleType, bool isCompressNativeLibrary); 258 #endif 259 260 static bool CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption); 261 262 static bool CheckHapEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption); 263 264 static bool MoveFiles(const std::string &srcDir, const std::string &desDir, bool isDesDirNeedCreated = false); 265 266 static bool MoveFileOrDir(const std::string &srcPath, const std::string &destPath, mode_t mode); 267 268 static bool MoveFile(const std::string &srcPath, const std::string &destPath); 269 270 static bool ExtractDriverSoFiles(const std::string &srcPath, 271 const std::unordered_multimap<std::string, std::string> &dirMap); 272 273 static bool CopyDriverSoFiles(const std::string &originalDir, const std::string &destinedDir); 274 275 #if defined(CODE_ENCRYPTION_ENABLE) 276 static ErrCode ExtractSoFilesToTmpHapPath(const std::string &hapPath, const std::string &cpuAbi, 277 const std::string &tmpSoPath, int32_t uid); 278 279 static ErrCode ExtractSoFilesToTmpSoPath(const std::string &hapPath, const std::string &realSoFilesPath, 280 const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid); 281 282 static ErrCode DecryptSoFile(const std::string &hapPath, const std::string &tmpHapPath, int32_t uid, 283 uint32_t fileSize, uint32_t offset); 284 285 static ErrCode RemoveEncryptedKey(int32_t uid, const std::vector<std::string> &soList); 286 287 static int32_t CallIoctl(int32_t flag, int32_t associatedFlag, int32_t uid, int32_t &fd); 288 #endif 289 290 static bool GenerateKeyIdAndSetPolicy(int32_t uid, const std::string &bundleName, 291 const int32_t userId, std::string &keyId); 292 293 static bool DeleteKeyId(const std::string &bundleName, const int32_t userId); 294 295 /** 296 * @brief Add file Delete dfx 297 * @param path Indicates the directory path to add dfx. 298 * @return 299 */ 300 static void AddDeleteDfx(const std::string &path); 301 302 /** 303 * @brief Rmv file Delete dfx 304 * @param path Indicates the directory path to add dfx. 305 * @return 306 */ 307 static void RmvDeleteDfx(const std::string &path); 308 309 private: 310 static bool ObtainNativeSoFile(const BundleExtractor &extractor, const std::string &cpuAbi, 311 std::vector<std::string> &soEntryFiles); 312 313 static bool ProcessApplyDiffPatchPath(const std::string &oldSoPath, const std::string &diffFilePath, 314 const std::string &newSoPath, std::vector<std::string> &oldSoFileNames, 315 std::vector<std::string> &diffFileNames); 316 static bool ExtractResourceFiles(const ExtractParam &extractParam, const BundleExtractor &extractor); 317 static bool CheckPathIsSame(const std::string &path, int32_t mode, const int32_t uid, const int32_t gid, 318 bool &isPathExist); 319 #if defined(CODE_ENCRYPTION_ENABLE) 320 static std::mutex encryptionMutex_; 321 static void *encryptionHandle_; 322 static EnforceMetadataProcessForApp enforceMetadataProcessForApp_; 323 static bool OpenEncryptionHandle(); 324 #endif 325 static void FsyncResFile(const std::string &path, const ExtractFileType &extractFileType); 326 }; 327 } // namespace AppExecFwk 328 } // namespace OHOS 329 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_OPERATOR_H 330