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

Commit b0e6c82a authored by Yi-Yo Chiang's avatar Yi-Yo Chiang Committed by Yi-yo Chiang
Browse files

overlayfs: Mount overlay with context= fs_options if original mount has it

Say we have mount configurations as follows:
  mount <dev> /mnt_point -t ext4 -o ro,context=<se_file_context>
  mount overlay /mnt_point2 -t overlay -o lowerdir=/mnt_point,upperdir=...

Overlayfs driver doesn't forward the overridden file context from
/mnt_point to /mnt_point2, thus the same file (same inode) would have
different file context when accessed via /mnt_point and /mnt_point2.

This change makes adb remount to mount filesystem overlays with context=
option if the overlaid mountpoint has it too. This makes the files under
context= mountpoint to retain the same file context after remount.

Also run clang-format on the whole file to fix some formatting issues.

Bug: 243501054
Test: adb remount && check file context with "ls -Z"
Change-Id: Ie4815604c56f1ce81b755cd0569b6577bd5f575f
parent e0877535
Loading
Loading
Loading
Loading
+17 −11
Original line number Original line Diff line number Diff line
@@ -116,8 +116,7 @@ std::vector<const std::string> OverlayMountPoints() {


    // For non-A/B devices prefer cache backing storage if
    // For non-A/B devices prefer cache backing storage if
    // kPreferCacheBackingStorageProp property set.
    // kPreferCacheBackingStorageProp property set.
    if (!IsABDevice() &&
    if (!IsABDevice() && android::base::GetBoolProperty(kPreferCacheBackingStorageProp, false) &&
        android::base::GetBoolProperty(kPreferCacheBackingStorageProp, false) &&
        android::base::GetIntProperty("ro.vendor.api_level", -1) < __ANDROID_API_T__) {
        android::base::GetIntProperty("ro.vendor.api_level", -1) < __ANDROID_API_T__) {
        return {kCacheMountPoint, kScratchMountPoint};
        return {kCacheMountPoint, kScratchMountPoint};
    }
    }
@@ -332,8 +331,14 @@ static inline bool KernelSupportsUserXattrs() {
    return major > 5 || (major == 5 && minor >= 15);
    return major > 5 || (major == 5 && minor >= 15);
}
}


const std::string fs_mgr_mount_point(const std::string& mount_point) {
    if ("/"s != mount_point) return mount_point;
    return "/system";
}

// default options for mount_point, returns empty string for none available.
// default options for mount_point, returns empty string for none available.
std::string fs_mgr_get_overlayfs_options(const std::string& mount_point) {
std::string fs_mgr_get_overlayfs_options(const FstabEntry& entry) {
    const auto mount_point = fs_mgr_mount_point(entry.mount_point);
    auto candidate = fs_mgr_get_overlayfs_candidate(mount_point);
    auto candidate = fs_mgr_get_overlayfs_candidate(mount_point);
    if (candidate.empty()) return "";
    if (candidate.empty()) return "";
    auto ret = kLowerdirOption + mount_point + "," + kUpperdirOption + candidate + kUpperName +
    auto ret = kLowerdirOption + mount_point + "," + kUpperdirOption + candidate + kUpperName +
@@ -344,12 +349,12 @@ std::string fs_mgr_get_overlayfs_options(const std::string& mount_point) {
    if (KernelSupportsUserXattrs()) {
    if (KernelSupportsUserXattrs()) {
        ret += ",userxattr";
        ret += ",userxattr";
    }
    }
    return ret;
    for (const auto& flag : android::base::Split(entry.fs_options, ",")) {
        if (android::base::StartsWith(flag, "context=")) {
            ret += "," + flag;
        }
        }

    }
const std::string fs_mgr_mount_point(const std::string& mount_point) {
    return ret;
    if ("/"s != mount_point) return mount_point;
    return "/system";
}
}


constexpr char kOverlayfsFileContext[] = "u:object_r:overlayfs_file:s0";
constexpr char kOverlayfsFileContext[] = "u:object_r:overlayfs_file:s0";
@@ -710,8 +715,9 @@ std::vector<mount_info> ReadMountinfoFromFile(const std::string& path) {
    return info;
    return info;
}
}


bool fs_mgr_overlayfs_mount(const std::string& mount_point) {
bool fs_mgr_overlayfs_mount(const FstabEntry& entry) {
    auto options = fs_mgr_get_overlayfs_options(mount_point);
    const auto mount_point = fs_mgr_mount_point(entry.mount_point);
    const auto options = fs_mgr_get_overlayfs_options(entry);
    if (options.empty()) return false;
    if (options.empty()) return false;


    auto retval = true;
    auto retval = true;
@@ -1346,7 +1352,7 @@ bool fs_mgr_overlayfs_mount_all(Fstab* fstab) {
            scratch_can_be_mounted = false;
            scratch_can_be_mounted = false;
            TryMountScratch();
            TryMountScratch();
        }
        }
        ret &= fs_mgr_overlayfs_mount(mount_point);
        ret &= fs_mgr_overlayfs_mount(entry);
    }
    }
    return ret;
    return ret;
}
}