Commit e16d604d authored by topjohnwu's avatar topjohnwu

Implement Magic Mount

parent d3d5703f
......@@ -22,9 +22,7 @@ LOCAL_SRC_FILES := \
daemon/daemon.c \
daemon/socket_trans.c \
daemon/log_monitor.c \
daemon/post_fs.c \
daemon/post_fs_data.c \
daemon/late_start.c \
daemon/bootstages.c \
magiskhide/magiskhide.c \
magiskhide/hide_daemon.c \
magiskhide/proc_monitor.c \
......
This diff is collapsed.
......@@ -31,8 +31,6 @@ static void *request_handler(void *args) {
int client = *((int *) args);
free(args);
client_request req = read_int(client);
char *s;
int pid, status, code;
switch (req) {
case LAUNCH_MAGISKHIDE:
launch_magiskhide(client);
......@@ -67,11 +65,7 @@ static void *request_handler(void *args) {
late_start(client);
break;
case TEST:
s = read_string(client);
LOGI("%s\n", s);
free(s);
write_int(client, 0);
close(client);
// test(client);
break;
}
return NULL;
......@@ -94,7 +88,7 @@ static void *large_sepol_patch(void *args) {
LOGD("sepol: Starting large patch thread\n");
// Patch su to everything
sepol_allow("su", ALL, ALL, ALL);
dump_policydb("/sys/fs/selinux/load");
dump_policydb(SELINUX_LOAD);
LOGD("sepol: Large patch done\n");
destroy_policydb();
return NULL;
......@@ -117,11 +111,14 @@ void start_daemon() {
xsetsid();
setcon("u:r:su:s0");
umask(022);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// Patch selinux with medium patch before we do anything
load_policydb("/sys/fs/selinux/policy");
load_policydb(SELINUX_POLICY);
sepol_med_rules();
dump_policydb("/sys/fs/selinux/load");
dump_policydb(SELINUX_LOAD);
// Continue the larger patch in another thread, we will need to join later
pthread_create(&sepol_patch, NULL, large_sepol_patch, NULL);
......
/* late_start.c - late_start service actions
*/
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "magisk.h"
#include "daemon.h"
#include "utils.h"
void late_start(int client) {
LOGI("** late_start service mode running\n");
// ack
write_int(client, 0);
close(client);
// Wait till the full patch is done
pthread_join(sepol_patch, NULL);
// Run scripts after full patch, most reliable way to run scripts
exec_common_script("service");
}
/* post_fs.c - post-fs actions
*/
#include <unistd.h>
#include "magisk.h"
#include "utils.h"
#include "daemon.h"
void post_fs(int client) {
LOGI("** post-fs mode running\n");
// ack
write_int(client, 0);
// TODO: Simple bind mounts
close(client);
unblock:
unblock_boot_process();
}
/* post_fs_data.c - post-fs-data actions
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <linux/loop.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include "magisk.h"
#include "utils.h"
#include "daemon.h"
#include "resetprop.h"
static char *loopsetup(const char *img) {
char device[20];
struct loop_info64 info;
int i, lfd, ffd;
memset(&info, 0, sizeof(info));
// First get an empty loop device
for (i = 0; i <= 7; ++i) {
sprintf(device, "/dev/block/loop%d", i);
lfd = xopen(device, O_RDWR);
if (ioctl(lfd, LOOP_GET_STATUS64, &info) == -1)
break;
close(lfd);
}
if (i == 8) return NULL;
ffd = xopen(img, O_RDWR);
if (ioctl(lfd, LOOP_SET_FD, ffd) == -1)
return NULL;
strcpy((char *) info.lo_file_name, img);
ioctl(lfd, LOOP_SET_STATUS64, &info);
return strdup(device);
}
static void *start_magisk_hide(void *args) {
// Setup default error handler for thread
err_handler = exit_thread;
launch_magiskhide(-1);
return NULL;
}
char *mount_image(const char *img, const char *target) {
char *device = loopsetup(img);
if (device)
xmount(device, target, "ext4", 0, NULL);
return device;
}
void post_fs_data(int client) {
// ack
write_int(client, 0);
close(client);
if (!check_data())
goto unblock;
LOGI("** post-fs-data mode running\n");
LOGI("* Mounting magisk.img\n");
// Mounting magisk image
char *magiskimg = mount_image("/data/magisk.img", "/magisk");
free(magiskimg);
// TODO: Magic Mounts, modules etc.
// Run common scripts
exec_common_script("post-fs-data");
// Start magiskhide if enabled
char *hide_prop = getprop("persist.magisk.hide");
if (hide_prop) {
if (strcmp(hide_prop, "1") == 0) {
pthread_t thread;
xpthread_create(&thread, NULL, start_magisk_hide, NULL);
}
free(hide_prop);
}
unblock:
unblock_boot_process();
}
......@@ -10,12 +10,6 @@
#include <pthread.h>
#include <android/log.h>
#ifndef MAGISK_VER_CODE
#define MAGISK_VER_CODE 99999
#endif
#ifndef MAGISK_VERSION
#define MAGISK_VERSION VER_DEBUG
#endif
#define MAGISK_VER_STR xstr(MAGISK_VERSION) ":MAGISK"
#define str(a) #a
......@@ -30,6 +24,11 @@
#define ARG_MAX 4096
#endif
#define SELINUX_PATH "/sys/fs/selinux/"
#define SELINUX_ENFORCE SELINUX_PATH "enforce"
#define SELINUX_POLICY SELINUX_PATH "policy"
#define SELINUX_LOAD SELINUX_PATH "load"
// Global handler for PLOGE
extern __thread void (*err_handler)(void);
......
......@@ -283,7 +283,7 @@ static void cpio_extract(const char *entry, const char *filename, struct vector
static void cpio_backup(const char *orig, struct vector *v) {
struct vector o_body, *o = &o_body, bak;
cpio_file *m, *n, *dir, *rem;
char chk1[21], chk2[21], buf[PATH_MAX];
char buf[PATH_MAX];
int res, doBak;
dir = xcalloc(sizeof(*dir), 1);
......
......@@ -60,8 +60,10 @@ void launch_magiskhide(int client) {
if (init_resetprop())
goto error;
if (setprop2("persist.magisk.hide", "1", 0))
if (client != -1) {
if (setprop("persist.magisk.hide", "1"))
goto error;
}
hide_sensitive_props();
......
......@@ -5,8 +5,6 @@
#define HIDELIST "/magisk/.core/magiskhide/hidelist"
#define DUMMYPATH "/dev/magisk/dummy"
#define ENFORCE_FILE "/sys/fs/selinux/enforce"
#define POLICY_FILE "/sys/fs/selinux/policy"
typedef enum {
HIDE_ERROR = -1,
......
......@@ -29,15 +29,15 @@ static int isMocked = 0;
void manage_selinux() {
if (isMocked) return;
char val[1];
int fd = xopen(ENFORCE_FILE, O_RDONLY);
int fd = xopen(SELINUX_ENFORCE, O_RDONLY);
xxread(fd, val, 1);
close(fd);
// Permissive
if (val[0] == '0') {
LOGI("hide_daemon: Permissive detected, hide the state\n");
chmod(ENFORCE_FILE, 0640);
chmod(POLICY_FILE, 0440);
chmod(SELINUX_ENFORCE, 0640);
chmod(SELINUX_POLICY, 0440);
isMocked = 1;
}
}
......
Subproject commit 3e106a9dc53400b2dde2e80cff277a5552a447ac
Subproject commit feec3e82558b1640e2deb88fde19e50fe589bbcb
......@@ -35,7 +35,8 @@ static void usage() {
" post-fs, post-fs-data, service\n"
"\n"
"Options:\n"
" -v print client and daemon version\n"
" -c print client version\n"
" -v print daemon version\n"
" -V print daemon version code\n"
"\n"
"Supported applets:\n"
......@@ -57,12 +58,14 @@ int main(int argc, char *argv[]) {
else arg = argv[0];
if (strcmp(arg, "magisk") == 0) {
if (argc < 2) usage();
if (strcmp(argv[1], "-v") == 0) {
printf("Client: %s\n", MAGISK_VER_STR);
if (strcmp(argv[1], "-c") == 0) {
printf("%s\n", MAGISK_VER_STR);
return 0;
} else if (strcmp(argv[1], "-v") == 0) {
int fd = connect_daemon();
write_int(fd, CHECK_VERSION);
char *v = read_string(fd);
printf("Daemon: %s\n", v);
printf("%s\n", v);
free(v);
return 0;
} else if (strcmp(argv[1], "-V") == 0) {
......@@ -92,10 +95,11 @@ int main(int argc, char *argv[]) {
return read_int(fd);
} else if (strcmp(argv[1], "--test") == 0) {
// Temporary testing entry
int fd = connect_daemon();
write_int(fd, TEST);
write_string(fd, argv[2]);
return read_int(fd);
// int fd = connect_daemon();
// write_int(fd, TEST);
// return read_int(fd);
// test();
return 0;
} else {
// It's calling applets
--argc;
......
......@@ -151,7 +151,7 @@ int setprop(const char *name, const char *value) {
return setprop2(name, value, 1);
}
int setprop2(const char *name, const char *value, int trigger) {
int setprop2(const char *name, const char *value, const int trigger) {
int ret;
char *check = getprop(name);
......@@ -190,7 +190,7 @@ int deleteprop(const char *name) {
return 0;
}
int read_prop_file(const char* filename) {
int read_prop_file(const char* filename, const int trigger) {
PRINT_D("resetprop: Load prop file [%s]\n", filename);
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
......@@ -223,7 +223,7 @@ int read_prop_file(const char* filename) {
if ( ((pch == NULL) || (i >= (pch - line))) || (pch >= line + read - 1) ) continue;
// Separate the string
*pch = '\0';
setprop(line + i, pch + 1);
setprop2(line + i, pch + 1, trigger);
}
free(line);
fclose(fp);
......@@ -234,7 +234,7 @@ int resetprop_main(int argc, char *argv[]) {
int del = 0, file = 0, trigger = 1;
int exp_arg = 2, stdout_bak, null;
int exp_arg = 2;
char *name, *value, *filename;
if (argc < 3) {
......@@ -283,7 +283,7 @@ int resetprop_main(int argc, char *argv[]) {
return -1;
if (file) {
return read_prop_file(filename);
return read_prop_file(filename, trigger);
} else if (del) {
return deleteprop(name);
} else {
......
......@@ -10,9 +10,10 @@ extern "C" {
int init_resetprop();
int setprop(const char *name, const char *value);
int setprop2(const char *name, const char *value, int trigger);
int setprop2(const char *name, const char *value, const int trigger);
char *getprop(const char *name);
int deleteprop(const char *name);
int read_prop_file(const char* filename, const int trigger);
#ifdef __cplusplus
}
......
......@@ -9,6 +9,7 @@
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
......@@ -238,10 +239,9 @@ int run_command(int *fd, const char *path, char *const argv[]) {
xdup2(sv[0], STDOUT_FILENO);
xdup2(sv[0], STDERR_FILENO);
} else {
int null = open("/dev/null", O_RDWR);
xdup2(null, STDIN_FILENO);
xdup2(null, STDOUT_FILENO);
xdup2(null, STDERR_FILENO);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
execv(path, argv);
......@@ -249,29 +249,33 @@ int run_command(int *fd, const char *path, char *const argv[]) {
return -1;
}
#define MAGISK_CORE "/magisk/.core/"
void exec_common_script(const char* stage) {
DIR *dir;
struct dirent *entry;
char buf[PATH_MAX];
snprintf(buf, sizeof(buf), MAGISK_CORE "%s.d", stage);
if (!(dir = opendir(buf)))
return;
while ((entry = xreaddir(dir))) {
if (entry->d_type == DT_REG) {
snprintf(buf, sizeof(buf), MAGISK_CORE "%s.d/%s", stage, entry->d_name);
if (access(buf, X_OK) == -1)
continue;
LOGI("%s.d: exec [%s]\n", stage, entry->d_name);
char *const command[] = { "sh", buf, NULL };
int pid = run_command(NULL, "/system/bin/sh", command);
if (pid != -1)
waitpid(pid, NULL, 0);
int mkdir_p(const char *pathname, mode_t mode) {
char *path = strdup(pathname), *p;
errno = 0;
for (p = path + 1; *p; ++p) {
if (*p == '/') {
*p = '\0';
if (mkdir(path, mode) == -1) {
if (errno != EEXIST)
return -1;
}
*p = '/';
}
}
if (mkdir(path, mode) == -1) {
if (errno != EEXIST)
return -1;
}
free(path);
return 0;
}
closedir(dir);
int bind_mount(const char *from, const char *to) {
int ret = xmount(from, to, NULL, MS_BIND, NULL);
LOGD("bind_mount: %s -> %s\n", from, to);
return ret;
}
int open_new(const char *filename) {
return xopen(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
}
......@@ -61,6 +61,7 @@ int xmkdir(const char *pathname, mode_t mode);
void *xmmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset);
ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count);
int xmkdir_p(const char *pathname, mode_t mode);
// misc.c
......@@ -79,6 +80,8 @@ void unlock_blocks();
void unblock_boot_process();
void setup_sighandlers(void (*handler)(int));
int run_command(int *fd, const char *path, char *const argv[]);
void exec_common_script(const char* stage);
int mkdir_p(const char *pathname, mode_t mode);
int bind_mount(const char *from, const char *to);
int open_new(const char *filename);
#endif
......@@ -21,11 +21,11 @@ void vec_deep_destroy(struct vector *v);
#define vec_entry(v) (v)->data
/* Usage: vec_for_each(vector *v, void *e) */
#define vec_for_each(v, e) \
e = (v)->data[0]; \
for (size_t _ = 0; _ < (v)->size; ++_, e = (v)->data[_])
e = v ? (v)->data[0] : NULL; \
for (size_t _ = 0; v && _ < (v)->size; ++_, e = (v)->data[_])
#define vec_for_each_r(v, e) \
e = (v)->data[(v)->size - 1]; \
for (size_t _ = (v)->size; _ > 0; --_, e = (v)->data[_ - 1])
e = v ? (v)->data[(v)->size - 1] : NULL; \
for (size_t _ = (v)->size; v && _ > 0; --_, e = (v)->data[_ - 1])
#endif
\ No newline at end of file
......@@ -298,4 +298,10 @@ ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count) {
return ret;
}
int xmkdir_p(const char *pathname, mode_t mode) {
int ret = mkdir_p(pathname, mode);
if (ret == -1) {
PLOGE("mkdir_p %s", pathname);
}
return ret;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment