Android 11 support ๐ŸŽ‰

This commit is contained in:
topjohnwu 2020-04-19 02:35:28 -07:00
parent c9255ab31b
commit dfbd1305b3
3 changed files with 30 additions and 33 deletions

View File

@ -57,9 +57,9 @@ static void (*android_update_LD_LIBRARY_PATH)(const char *ld_library_path);
} }
#ifdef __LP64__ #ifdef __LP64__
constexpr char apex_path[] = "/apex/com.android.runtime/lib64:"; constexpr char apex_path[] = "/apex/com.android.runtime/lib64:/apex/com.android.art/lib64:";
#else #else
constexpr char apex_path[] = "/apex/com.android.runtime/lib:"; constexpr char apex_path[] = "/apex/com.android.runtime/lib:/apex/com.android.art/lib:";
#endif #endif
static int dl_init = 0; static int dl_init = 0;

View File

@ -2,6 +2,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <vector> #include <vector>
#include <libgen.h>
#include <magisk.hpp> #include <magisk.hpp>
#include <magiskpolicy.hpp> #include <magiskpolicy.hpp>
@ -21,8 +22,9 @@ using namespace std;
static vector<raw_data> rc_list; static vector<raw_data> rc_list;
static void patch_init_rc(FILE *rc, const char *tmp_dir) { static void patch_init_rc(const char *src, const char *dest, const char *tmp_dir) {
file_readline("/init.rc", [=](string_view line) -> bool { FILE *rc = xfopen(dest, "we");
file_readline(src, [=](string_view line) -> bool {
// Do not start vaultkeeper // Do not start vaultkeeper
if (str_contains(line, "start vaultkeeper")) { if (str_contains(line, "start vaultkeeper")) {
LOGD("Remove vaultkeeper\n"); LOGD("Remove vaultkeeper\n");
@ -53,6 +55,9 @@ static void patch_init_rc(FILE *rc, const char *tmp_dir) {
gen_rand_str(bc_svc, sizeof(bc_svc)); gen_rand_str(bc_svc, sizeof(bc_svc));
LOGD("Inject magisk services: [%s] [%s] [%s]\n", pfd_svc, ls_svc, bc_svc); LOGD("Inject magisk services: [%s] [%s] [%s]\n", pfd_svc, ls_svc, bc_svc);
fprintf(rc, magiskrc, tmp_dir, pfd_svc, ls_svc, bc_svc); fprintf(rc, magiskrc, tmp_dir, pfd_svc, ls_svc, bc_svc);
fclose(rc);
clone_attr(src, dest);
} }
static void load_overlay_rc(const char *overlay) { static void load_overlay_rc(const char *overlay) {
@ -98,11 +103,7 @@ void RootFSInit::setup_rootfs() {
mv_path("/overlay.d", "/"); mv_path("/overlay.d", "/");
} }
// Patch init.rc patch_init_rc("/init.rc", "/init.p.rc", "/sbin");
FILE *rc = xfopen("/init.p.rc", "we");
patch_init_rc(rc, "/sbin");
fclose(rc);
clone_attr("/init.rc", "/init.p.rc");
rename("/init.p.rc", "/init.rc"); rename("/init.p.rc", "/init.rc");
// Create hardlink mirror of /sbin to /root // Create hardlink mirror of /sbin to /root
@ -196,9 +197,7 @@ static string magic_mount_list;
static void magic_mount(const string &sdir, const string &ddir = "") { static void magic_mount(const string &sdir, const string &ddir = "") {
auto dir = xopen_dir(sdir.data()); auto dir = xopen_dir(sdir.data());
for (dirent *entry; (entry = readdir(dir.get()));) { for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
string src = sdir + "/" + entry->d_name; string src = sdir + "/" + entry->d_name;
string dest = ddir + "/" + entry->d_name; string dest = ddir + "/" + entry->d_name;
if (access(dest.data(), F_OK) == 0) { if (access(dest.data(), F_OK) == 0) {
@ -215,10 +214,11 @@ static void magic_mount(const string &sdir, const string &ddir = "") {
} }
} }
#define ROOTMIR MIRRDIR "/system_root" #define ROOTMIR MIRRDIR "/system_root"
#define ROOTBLK BLOCKDIR "/system_root" #define ROOTBLK BLOCKDIR "/system_root"
#define MONOPOLICY "/sepolicy" #define MONOPOLICY "/sepolicy"
#define LIBSELINUX "/system/" LIBNAME "/libselinux.so" #define LIBSELINUX "/system/" LIBNAME "/libselinux.so"
#define NEW_INITRC "/system/etc/init/hw/init.rc"
void SARBase::patch_rootdir() { void SARBase::patch_rootdir() {
char tmp_dir[16]; char tmp_dir[16];
@ -229,9 +229,8 @@ void SARBase::patch_rootdir() {
p = tmp_dir + sprintf(tmp_dir, "%s", "/sbin"); p = tmp_dir + sprintf(tmp_dir, "%s", "/sbin");
sepol = "/sbin/.se"; sepol = "/sbin/.se";
} else { } else {
strcpy(tmp_dir, "/dev/"); p = tmp_dir + sprintf(tmp_dir, "%s", "/dev/");
p = tmp_dir + 5; p += gen_rand_str(p, 8);
p += gen_rand_str(p, 10);
xmkdir(tmp_dir, 0); xmkdir(tmp_dir, 0);
sepol = "/dev/.se"; sepol = "/dev/.se";
} }
@ -257,12 +256,9 @@ void SARBase::patch_rootdir() {
// Patch init // Patch init
raw_data init; raw_data init;
file_attr attr;
bool redirect = false; bool redirect = false;
int src = xopen("/init", O_RDONLY | O_CLOEXEC); int src = xopen("/init", O_RDONLY | O_CLOEXEC);
fd_full_read(src, init.buf, init.sz); fd_full_read(src, init.buf, init.sz);
fgetattr(src, &attr);
close(src);
uint8_t *eof = init.buf + init.sz; uint8_t *eof = init.buf + init.sz;
for (uint8_t *p = init.buf; p < eof;) { for (uint8_t *p = init.buf; p < eof;) {
if (memcmp(p, SPLIT_PLAT_CIL, sizeof(SPLIT_PLAT_CIL)) == 0) { if (memcmp(p, SPLIT_PLAT_CIL, sizeof(SPLIT_PLAT_CIL)) == 0) {
@ -281,14 +277,14 @@ void SARBase::patch_rootdir() {
xmkdir(ROOTOVL, 0); xmkdir(ROOTOVL, 0);
int dest = xopen(ROOTOVL "/init", O_CREAT | O_WRONLY | O_CLOEXEC); int dest = xopen(ROOTOVL "/init", O_CREAT | O_WRONLY | O_CLOEXEC);
xwrite(dest, init.buf, init.sz); xwrite(dest, init.buf, init.sz);
fsetattr(dest, &attr); fclone_attr(src, dest);
close(src);
close(dest); close(dest);
if (!redirect) { if (!redirect) {
// init is dynamically linked, need to patch libselinux // init is dynamically linked, need to patch libselinux
raw_data lib; raw_data lib;
full_read(LIBSELINUX, lib.buf, lib.sz); full_read(LIBSELINUX, lib.buf, lib.sz);
getattr(LIBSELINUX, &attr);
eof = lib.buf + lib.sz; eof = lib.buf + lib.sz;
for (uint8_t *p = lib.buf; p < eof; ++p) { for (uint8_t *p = lib.buf; p < eof; ++p) {
if (memcmp(p, MONOPOLICY, sizeof(MONOPOLICY)) == 0) { if (memcmp(p, MONOPOLICY, sizeof(MONOPOLICY)) == 0) {
@ -297,12 +293,11 @@ void SARBase::patch_rootdir() {
break; break;
} }
} }
xmkdir(ROOTOVL "/system", 0755); xmkdirs(dirname(ROOTOVL LIBSELINUX), 0755);
xmkdir(ROOTOVL "/system/" LIBNAME, 0755);
dest = xopen(ROOTOVL LIBSELINUX, O_CREAT | O_WRONLY | O_CLOEXEC); dest = xopen(ROOTOVL LIBSELINUX, O_CREAT | O_WRONLY | O_CLOEXEC);
xwrite(dest, lib.buf, lib.sz); xwrite(dest, lib.buf, lib.sz);
fsetattr(dest, &attr);
close(dest); close(dest);
clone_attr(LIBSELINUX, ROOTOVL LIBSELINUX);
} }
// sepolicy // sepolicy
@ -333,10 +328,13 @@ void SARBase::patch_rootdir() {
} }
// Patch init.rc // Patch init.rc
FILE *rc = xfopen(ROOTOVL "/init.rc", "we"); if (access("/init.rc", F_OK) == 0) {
patch_init_rc(rc, tmp_dir); patch_init_rc("/init.rc", ROOTOVL "/init.rc", tmp_dir);
fclose(rc); } else {
clone_attr("/init.rc", ROOTOVL "/init.rc"); // Android 11's new init.rc
xmkdirs(dirname(ROOTOVL NEW_INITRC), 0755);
patch_init_rc(NEW_INITRC, ROOTOVL NEW_INITRC, tmp_dir);
}
// Mount rootdir // Mount rootdir
magic_mount(ROOTOVL); magic_mount(ROOTOVL);

View File

@ -71,10 +71,9 @@ void ForcedFirstStageInit::prepare() {
// Move stuffs for next stage // Move stuffs for next stage
xmkdir(FSR "/system", 0755); xmkdir(FSR "/system", 0755);
xmkdir(FSR "/system/bin", 0755); xmkdir(FSR "/system/bin", 0755);
rename("/init", FSR "/system/bin/init"); rename("/init" /* magiskinit */, FSR "/system/bin/init");
symlink("/system/bin/init", FSR "/init"); symlink("/system/bin/init", FSR "/init");
xmkdir(FSR "/.backup", 0); rename("/.backup", FSR "/.backup");
rename("/.backup/.magisk", FSR "/.backup/.magisk");
rename("/overlay.d", FSR "/overlay.d"); rename("/overlay.d", FSR "/overlay.d");
} }
@ -90,7 +89,7 @@ void FirstStageInit::prepare() {
// Move stuffs for next stage // Move stuffs for next stage
xmkdir("/system", 0755); xmkdir("/system", 0755);
xmkdir("/system/bin", 0755); xmkdir("/system/bin", 0755);
rename("/init", "/system/bin/init"); rename("/init" /* magiskinit */ , "/system/bin/init");
rename("/.backup/init", "/init"); rename("/.backup/init", "/init");
} }