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
This commit is contained in:
Yi-Yo Chiang 2022-12-13 01:00:34 +08:00 committed by Yi-yo Chiang
parent e0877535ab
commit b0e6c82a86

View file

@ -116,8 +116,7 @@ std::vector<const std::string> OverlayMountPoints() {
// For non-A/B devices prefer cache backing storage if
// kPreferCacheBackingStorageProp property set.
if (!IsABDevice() &&
android::base::GetBoolProperty(kPreferCacheBackingStorageProp, false) &&
if (!IsABDevice() && android::base::GetBoolProperty(kPreferCacheBackingStorageProp, false) &&
android::base::GetIntProperty("ro.vendor.api_level", -1) < __ANDROID_API_T__) {
return {kCacheMountPoint, kScratchMountPoint};
}
@ -332,8 +331,14 @@ static inline bool KernelSupportsUserXattrs() {
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.
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);
if (candidate.empty()) return "";
auto ret = kLowerdirOption + mount_point + "," + kUpperdirOption + candidate + kUpperName +
@ -344,14 +349,14 @@ std::string fs_mgr_get_overlayfs_options(const std::string& mount_point) {
if (KernelSupportsUserXattrs()) {
ret += ",userxattr";
}
for (const auto& flag : android::base::Split(entry.fs_options, ",")) {
if (android::base::StartsWith(flag, "context=")) {
ret += "," + flag;
}
}
return ret;
}
const std::string fs_mgr_mount_point(const std::string& mount_point) {
if ("/"s != mount_point) return mount_point;
return "/system";
}
constexpr char kOverlayfsFileContext[] = "u:object_r:overlayfs_file:s0";
class AutoSetFsCreateCon final {
@ -710,8 +715,9 @@ std::vector<mount_info> ReadMountinfoFromFile(const std::string& path) {
return info;
}
bool fs_mgr_overlayfs_mount(const std::string& mount_point) {
auto options = fs_mgr_get_overlayfs_options(mount_point);
bool fs_mgr_overlayfs_mount(const FstabEntry& entry) {
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;
auto retval = true;
@ -1346,7 +1352,7 @@ bool fs_mgr_overlayfs_mount_all(Fstab* fstab) {
scratch_can_be_mounted = false;
TryMountScratch();
}
ret &= fs_mgr_overlayfs_mount(mount_point);
ret &= fs_mgr_overlayfs_mount(entry);
}
return ret;
}