Detect volume down key combo for safe mode

It is possible that a module is breaking the device so bad that zygote
cannot even be started. In this case, system_server cannot start and
detect the safe mode key combo, set the persist property, and reboot.

Also on old Android versions, the system directly goes to safe mode
after detecting a key combo without rebooting, defeating the purpose of
Magisk's safe mode protection if we only check for the persist property.

Directly adding key combo check natively in magiskd allows us to enter
Magisk safe mode before the system is even aware of it.
This commit is contained in:
topjohnwu
2020-05-19 04:53:16 -07:00
parent 3c04dab472
commit e02e46d0fc
4 changed files with 96 additions and 48 deletions

View File

@@ -48,10 +48,8 @@ static bool check_key_combo() {
constexpr const char *name = "/event";
for (int minor = 64; minor < 96; ++minor) {
if (mknod(name, S_IFCHR | 0444, makedev(13, minor))) {
PLOGE("mknod");
if (xmknod(name, S_IFCHR | 0444, makedev(13, minor)))
continue;
}
int fd = open(name, O_RDONLY | O_CLOEXEC);
unlink(name);
if (fd < 0)
@@ -60,17 +58,15 @@ static bool check_key_combo() {
ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(bitmask)), bitmask);
if (test_bit(KEY_VOLUMEUP, bitmask))
events.push_back(fd);
else
close(fd);
}
if (events.empty())
return false;
run_finally fin([&]() -> void {
for (const int &fd : events)
close(fd);
});
run_finally fin([&]{ std::for_each(events.begin(), events.end(), close); });
// Return true if volume key up is hold for more than 3 seconds
// Return true if volume up key is held for more than 3 seconds
int count = 0;
for (int i = 0; i < 500; ++i) {
for (const int &fd : events) {