Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 038a19ba authored by Alex Buynytskyy's avatar Alex Buynytskyy
Browse files

More robust app data and user data removal.

1. rename the folder, so any new files will end up in the renamed
   folder.
   this also greatly reduces chances that app will be able to create new files.
2. delete the renamed folder
3. provide an api to cleanup renamed/deleted folders on system startup

Bug: 162757029
Test: atest installd_service_test installd_cache_test installd_utils_test installd_dexopt_test installd_otapreopt_test installd_file_test
Change-Id: If1c209d49675f7fa9df60b6136588e3b0a7786e5
parent 32ac30f7
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ package {
cc_defaults {
    name: "installd_defaults",

    cpp_std: "c++2a",
    cflags: [
        "-Wall",
        "-Werror",
@@ -41,6 +42,7 @@ cc_defaults {
        "libbinder",
        "libcrypto",
        "libcutils",
        "libext2_uuid",
        "liblog",
        "liblogwrap",
        "libprocessgroup",
@@ -239,6 +241,8 @@ cc_library_static {

cc_binary {
    name: "otapreopt",

    cpp_std: "c++2a",
    cflags: [
        "-Wall",
        "-Werror",
@@ -268,6 +272,7 @@ cc_binary {
        "libbase",
        "libcrypto",
        "libcutils",
        "libext2_uuid",
        "liblog",
        "liblogwrap",
        "libprocessgroup",
+23 −11
Original line number Diff line number Diff line
@@ -968,13 +968,13 @@ binder::Status InstalldNativeService::destroyAppData(const std::optional<std::st
    binder::Status res = ok();
    if (flags & FLAG_STORAGE_CE) {
        auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode);
        if (delete_dir_contents_and_dir(path) != 0) {
        if (rename_delete_dir_contents_and_dir(path) != 0) {
            res = error("Failed to delete " + path);
        }
    }
    if (flags & FLAG_STORAGE_DE) {
        auto path = create_data_user_de_package_path(uuid_, userId, pkgname);
        if (delete_dir_contents_and_dir(path) != 0) {
        if (rename_delete_dir_contents_and_dir(path) != 0) {
            res = error("Failed to delete " + path);
        }
        if ((flags & FLAG_CLEAR_APP_DATA_KEEP_ART_PROFILES) == 0) {
@@ -1005,16 +1005,15 @@ binder::Status InstalldNativeService::destroyAppData(const std::optional<std::st
            }

            auto path = StringPrintf("%s/Android/data/%s", extPath.c_str(), pkgname);
            if (delete_dir_contents_and_dir(path, true) != 0) {
            if (rename_delete_dir_contents_and_dir(path, true) != 0) {
                res = error("Failed to delete contents of " + path);
            }

            path = StringPrintf("%s/Android/media/%s", extPath.c_str(), pkgname);
            if (delete_dir_contents_and_dir(path, true) != 0) {
            if (rename_delete_dir_contents_and_dir(path, true) != 0) {
                res = error("Failed to delete contents of " + path);
            }
            path = StringPrintf("%s/Android/obb/%s", extPath.c_str(), pkgname);
            if (delete_dir_contents_and_dir(path, true) != 0) {
            if (rename_delete_dir_contents_and_dir(path, true) != 0) {
                res = error("Failed to delete contents of " + path);
            }
        }
@@ -1562,27 +1561,27 @@ binder::Status InstalldNativeService::destroyUserData(const std::optional<std::s
    binder::Status res = ok();
    if (flags & FLAG_STORAGE_DE) {
        auto path = create_data_user_de_path(uuid_, userId);
        if (delete_dir_contents_and_dir(path, true) != 0) {
        if (rename_delete_dir_contents_and_dir(path, true) != 0) {
            res = error("Failed to delete " + path);
        }
        if (uuid_ == nullptr) {
            path = create_data_misc_legacy_path(userId);
            if (delete_dir_contents_and_dir(path, true) != 0) {
            if (rename_delete_dir_contents_and_dir(path, true) != 0) {
                res = error("Failed to delete " + path);
            }
            path = create_primary_cur_profile_dir_path(userId);
            if (delete_dir_contents_and_dir(path, true) != 0) {
            if (rename_delete_dir_contents_and_dir(path, true) != 0) {
                res = error("Failed to delete " + path);
            }
        }
    }
    if (flags & FLAG_STORAGE_CE) {
        auto path = create_data_user_ce_path(uuid_, userId);
        if (delete_dir_contents_and_dir(path, true) != 0) {
        if (rename_delete_dir_contents_and_dir(path, true) != 0) {
            res = error("Failed to delete " + path);
        }
        path = findDataMediaPath(uuid, userId);
        if (delete_dir_contents_and_dir(path, true) != 0) {
        if (rename_delete_dir_contents_and_dir(path, true) != 0) {
            res = error("Failed to delete " + path);
        }
    }
@@ -3324,5 +3323,18 @@ binder::Status InstalldNativeService::migrateLegacyObbData() {
    return ok();
}

binder::Status InstalldNativeService::cleanupDeletedDirs(const std::optional<std::string>& uuid) {
    const char* uuid_cstr = uuid ? uuid->c_str() : nullptr;
    const auto users = get_known_users(uuid_cstr);
    for (auto userId : users) {
        auto ce_path = create_data_user_ce_path(uuid_cstr, userId);
        auto de_path = create_data_user_de_path(uuid_cstr, userId);

        find_and_delete_renamed_deleted_dirs_under_path(ce_path);
        find_and_delete_renamed_deleted_dirs_under_path(de_path);
    }
    return ok();
}

}  // namespace installd
}  // namespace android
+2 −0
Original line number Diff line number Diff line
@@ -188,6 +188,8 @@ public:

    binder::Status migrateLegacyObbData();

    binder::Status cleanupDeletedDirs(const std::optional<std::string>& uuid);

private:
    std::recursive_mutex mLock;
    std::unordered_map<userid_t, std::weak_ptr<std::shared_mutex>> mUserIdLock;
+2 −0
Original line number Diff line number Diff line
@@ -129,6 +129,8 @@ interface IInstalld {

    void migrateLegacyObbData();

    void cleanupDeletedDirs(@nullable @utf8InCpp String uuid);

    const int FLAG_STORAGE_DE = 0x1;
    const int FLAG_STORAGE_CE = 0x2;
    const int FLAG_STORAGE_EXTERNAL = 0x4;
+19 −57
Original line number Diff line number Diff line
@@ -8,46 +8,47 @@ package {
    default_applicable_licenses: ["frameworks_native_license"],
}

cc_test {
    name: "installd_utils_test",
cc_defaults {
    name: "installd_tests_defaults",
    test_suites: ["device-tests"],
    clang: true,
    srcs: ["installd_utils_test.cpp"],
    cpp_std: "c++2a",
    cflags: [
        "-Wall",
        "-Werror",
    ],
    shared_libs: [
        "libbase",
        "libutils",
        "libcutils",
        "libext2_uuid",
        "libutils",
    ],
    static_libs: [
        "liblog",
    ],
}

cc_test {
    name: "installd_utils_test",
    defaults: ["installd_tests_defaults"],
    srcs: ["installd_utils_test.cpp"],
    static_libs: [
        "libasync_safe",
        "libdiskusage",
        "libinstalld",
        "liblog",
    ],
    test_config: "installd_utils_test.xml",
}

cc_test {
    name: "installd_cache_test",
    test_suites: ["device-tests"],
    clang: true,
    defaults: ["installd_tests_defaults"],
    srcs: ["installd_cache_test.cpp"],
    cflags: [
        "-Wall",
        "-Werror",
    ],
    shared_libs: [
        "libbase",
        "libbinder",
        "libcrypto",
        "libcutils",
        "libprocessgroup",
        "libselinux",
        "libutils",
        "server_configurable_flags",
    ],
    static_libs: [
@@ -55,7 +56,6 @@ cc_test {
        "libdiskusage",
        "libinstalld",
        "libziparchive",
        "liblog",
        "liblogwrap",
    ],
    test_config: "installd_cache_test.xml",
@@ -78,21 +78,13 @@ cc_test {

cc_test {
    name: "installd_service_test",
    test_suites: ["device-tests"],
    clang: true,
    defaults: ["installd_tests_defaults"],
    srcs: ["installd_service_test.cpp"],
    cflags: [
        "-Wall",
        "-Werror",
    ],
    shared_libs: [
        "libbase",
        "libbinder",
        "libcrypto",
        "libcutils",
        "libprocessgroup",
        "libselinux",
        "libutils",
        "packagemanager_aidl-cpp",
        "server_configurable_flags",
    ],
@@ -101,7 +93,6 @@ cc_test {
        "libdiskusage",
        "libinstalld",
        "libziparchive",
        "liblog",
        "liblogwrap",
    ],
    test_config: "installd_service_test.xml",
@@ -124,28 +115,19 @@ cc_test {

cc_test {
    name: "installd_dexopt_test",
    test_suites: ["device-tests"],
    clang: true,
    defaults: ["installd_tests_defaults"],
    srcs: ["installd_dexopt_test.cpp"],
    cflags: [
        "-Wall",
        "-Werror",
    ],
    shared_libs: [
        "libbase",
        "libbinder",
        "libcrypto",
        "libcutils",
        "libprocessgroup",
        "libselinux",
        "libutils",
        "server_configurable_flags",
    ],
    static_libs: [
        "libasync_safe",
        "libdiskusage",
        "libinstalld",
        "liblog",
        "liblogwrap",
        "libziparchive",
        "libz",
@@ -170,41 +152,21 @@ cc_test {

cc_test {
    name: "installd_otapreopt_test",
    test_suites: ["device-tests"],
    clang: true,
    defaults: ["installd_tests_defaults"],
    srcs: ["installd_otapreopt_test.cpp"],
    cflags: [
        "-Wall",
        "-Werror",
    ],
    shared_libs: [
        "libbase",
        "libcutils",
        "libutils",
        "server_configurable_flags",
    ],
    static_libs: [
        "liblog",
        "libotapreoptparameters",
    ],
}

cc_test {
    name: "installd_file_test",
    test_suites: ["device-tests"],
    clang: true,
    defaults: ["installd_tests_defaults"],
    srcs: ["installd_file_test.cpp"],
    cflags: [
        "-Wall",
        "-Werror",
    ],
    shared_libs: [
        "libbase",
        "libcutils",
        "libutils",
    ],
    static_libs: [
        "libinstalld",
        "liblog",
    ],
}
Loading