Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 1 | #include <ctype.h> |
| 2 | #include <errno.h> |
| 3 | #include <fcntl.h> |
| 4 | #include <getopt.h> |
| 5 | #include <limits.h> |
| 6 | #include <linux/input.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <sys/reboot.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <time.h> |
| 13 | #include <unistd.h> |
| 14 | |
| 15 | #include <sys/wait.h> |
| 16 | #include <sys/limits.h> |
| 17 | #include <dirent.h> |
| 18 | #include <sys/stat.h> |
| 19 | |
| 20 | #include <signal.h> |
| 21 | #include <sys/wait.h> |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <stdio.h> |
| 25 | #include <errno.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <sys/mount.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 31 | |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 32 | #include <limits.h> |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 33 | |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 34 | #include <cutils/properties.h> |
| 35 | |
| 36 | #define RECOVERY_MODE_FILE "/data/.recovery_mode" |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 37 | #define UPDATE_BINARY BOARD_HIJACK_RECOVERY_PATH"/update-binary" |
| 38 | #define UPDATE_PACKAGE BOARD_HIJACK_RECOVERY_PATH"/recovery.zip" |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 39 | |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 40 | int |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 41 | exec_and_wait(char** argp) |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 42 | { |
| 43 | pid_t pid; |
| 44 | sig_t intsave, quitsave; |
| 45 | sigset_t mask, omask; |
| 46 | int pstat; |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 47 | |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 48 | sigemptyset(&mask); |
| 49 | sigaddset(&mask, SIGCHLD); |
| 50 | sigprocmask(SIG_BLOCK, &mask, &omask); |
| 51 | switch (pid = vfork()) { |
| 52 | case -1: /* error */ |
| 53 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 54 | return(-1); |
| 55 | case 0: /* child */ |
| 56 | sigprocmask(SIG_SETMASK, &omask, NULL); |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 57 | execve(argp[0], argp, environ); |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 58 | _exit(127); |
| 59 | } |
| 60 | |
| 61 | intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN); |
| 62 | quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN); |
| 63 | pid = waitpid(pid, (int *)&pstat, 0); |
| 64 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 65 | (void)bsd_signal(SIGINT, intsave); |
| 66 | (void)bsd_signal(SIGQUIT, quitsave); |
| 67 | return (pid == -1 ? -1 : pstat); |
| 68 | } |
| 69 | |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 70 | typedef char* string; |
| 71 | |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 72 | int main(int argc, char** argv) { |
| 73 | char* hijacked_executable = argv[0]; |
| 74 | struct stat info; |
| 75 | |
| 76 | if (NULL != strstr(hijacked_executable, "hijack")) { |
| 77 | // no op |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 78 | if (argc >= 2) { |
| 79 | if (strcmp("sh", argv[1]) == 0) { |
| 80 | return ash_main(argc - 1, argv + 1); |
| 81 | } |
| 82 | if (strcmp("mount", argv[1]) == 0) { |
| 83 | printf("mount!\n"); |
| 84 | return mount_main(argc - 1, argv + 1); |
| 85 | } |
| 86 | if (strcmp("umount", argv[1]) == 0) { |
| 87 | printf("umount!\n"); |
| 88 | return umount_main(argc - 1, argv + 1); |
| 89 | } |
| 90 | } |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 91 | printf("Hijack!\n"); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | char cmd[PATH_MAX]; |
| 96 | if (0 == stat(RECOVERY_MODE_FILE, &info)) { |
| 97 | remove(RECOVERY_MODE_FILE); |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 98 | sprintf(cmd, "%s 2 0 %s", UPDATE_BINARY, UPDATE_PACKAGE); |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 99 | |
| 100 | char* remount_root_args[] = { "/system/bin/hijack", "mount", "-orw,remount", "/", NULL }; |
| 101 | //mount_main(3, remount_root_args); |
| 102 | //__system("/system/bin/hijack mount -orw,remount /"); |
| 103 | exec_and_wait(remount_root_args); |
| 104 | |
| 105 | mkdir("/preinstall", S_IRWXU); |
| 106 | mkdir("/tmp", S_IRWXU); |
| 107 | mkdir("/res", S_IRWXU); |
| 108 | mkdir("/res/images", S_IRWXU); |
| 109 | remove("/etc"); |
| 110 | mkdir("/etc", S_IRWXU); |
| 111 | rename("/sbin/adbd", "/sbin/adbd.old"); |
| 112 | property_set("ctl.stop", "runtime"); |
| 113 | property_set("ctl.stop", "zygote"); |
| 114 | property_set("persist.service.adb.enable", "1"); |
| 115 | |
| 116 | char* mount_preinstall_args[] = { "/system/bin/hijack", "mount", "/dev/block/preinstall", "/preinstall", NULL }; |
| 117 | //mount_main(3, mount_preinstall_args); |
| 118 | //__system("/system/bin/hijack mount /dev/block/preinstall /preinstall"); |
| 119 | exec_and_wait(mount_preinstall_args); |
| 120 | |
| 121 | char* umount_args[] = { "/system/bin/hijack", "umount", "-l", "/system", NULL }; |
| 122 | exec_and_wait(umount_args); |
| 123 | |
| 124 | char* updater_args[] = { UPDATE_BINARY, "2", "0", UPDATE_PACKAGE, NULL }; |
| 125 | return exec_and_wait(updater_args); |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 128 | char real_executable[PATH_MAX]; |
| 129 | sprintf(real_executable, "%s.bin", hijacked_executable); |
| 130 | string* argp = (string*)malloc(sizeof(string) * (argc + 1)); |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 131 | int i; |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 132 | for (i = 0; i < argc; i++) { |
| 133 | argp[i]=argv[i]; |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 134 | } |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 135 | argp[argc] = NULL; |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 136 | |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 137 | argp[0] = real_executable; |
| 138 | |
| 139 | return exec_and_wait(argp); |
Koushik Dutta | d632c0d | 2010-08-16 09:22:18 -0700 | [diff] [blame] | 140 | } |