init: snapuserd: Fix ranges for mlock()
It cannot be assumed that file mappings in /proc/<pid>/maps will be
completely backed by the underlying file. [1]
Use MappedFileSize() to deduce the correct ranges for the mlock()
calls when locking system pages in the OTA path.
While at it also clean up the some unreachable code (mlockall()),
and improve error logging.
[1] SIGBUS at https://man7.org/linux/man-pages/man2/mmap.2.html#RETURN_VALUE
Test: Incremental OTA
Bug: 324952273
Change-Id: Ia2ab150e1b8de8c638f5b1acc1de83deb7ac5cff
Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
diff --git a/init/snapuserd_transition.cpp b/init/snapuserd_transition.cpp
index 3a78343..dea7af9 100644
--- a/init/snapuserd_transition.cpp
+++ b/init/snapuserd_transition.cpp
@@ -195,22 +195,20 @@
return;
}
auto start = reinterpret_cast<const void*>(map.start);
- auto len = map.end - map.start;
+ uint64_t len = android::procinfo::MappedFileSize(map);
if (!len) {
return;
}
+
if (mlock(start, len) < 0) {
- LOG(ERROR) << "mlock failed, " << start << " for " << len << " bytes.";
+ PLOG(ERROR) << "\"" << map.name << "\": mlock(" << start << ", " << len
+ << ") failed: pgoff = " << map.pgoff;
ok = false;
}
};
if (!android::procinfo::ReadProcessMaps(getpid(), callback) || !ok) {
- LOG(FATAL) << "Could not process /proc/" << getpid() << "/maps file for init, "
- << "falling back to mlockall().";
- if (mlockall(MCL_CURRENT) < 0) {
- LOG(FATAL) << "mlockall failed";
- }
+ LOG(FATAL) << "Could not process /proc/" << getpid() << "/maps file for init";
}
}