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

Commit dd300a96 authored by Hung Nguyen's avatar Hung Nguyen
Browse files

Remove zram_swap file after zram writeback setup

/data/per_boot/zram_swap is created for zram writeback but it doesn't
need to exist for zram writeback to work once the loop device is
successfully created. This will also make sure only zram driver has
access to the writeback file and improve security.

Bug: 390004071
Test: manual, verify /data/per_boot is empty after zram setup from
swapon_all

Change-Id: Ifa4755baad090cf34d26e7d572431b6345412043
parent 3b73c8f8
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@
#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/properties.h>
#include <android-base/scopeguard.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
@@ -97,6 +98,7 @@
using android::base::Basename;
using android::base::GetBoolProperty;
using android::base::GetUintProperty;
using android::base::make_scope_guard;
using android::base::Realpath;
using android::base::SetProperty;
using android::base::StartsWith;
@@ -1988,9 +1990,12 @@ static bool PrepareZramBackingDevice(off64_t size) {
        PERROR << "Cannot open target path: " << file_path;
        return false;
    }

    // Always unlink zram_swap file to prevent file system access.
    auto unlink_zram_swap_guard = make_scope_guard([] { unlink(file_path); });

    if (fallocate(target_fd.get(), 0, 0, size) < 0) {
        PERROR << "Cannot truncate target path: " << file_path;
        unlink(file_path);
        return false;
    }