From c2bb49287a69aeb65d28987e070a833178821a26 Mon Sep 17 00:00:00 2001 From: Nick Chan Date: Tue, 28 Mar 2023 12:43:46 +0800 Subject: [PATCH] plush features --- .gitignore | 1 + Makefile | 4 +-- include/xxd-embedded.h | 13 +++++++++ src/Makefile | 9 ++++++ src/exec_checkra1n.c | 64 ++++++++++++++++++++++++++++++++---------- 5 files changed, 74 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 56b49d6..058486e 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ src/*.dmg.c .idea src/stage2/ src/libcheckra1nhelper.dylib* +src/Pongo.bin diff --git a/Makefile b/Makefile index 52d830b..08b3a0f 100644 --- a/Makefile +++ b/Makefile @@ -74,13 +74,13 @@ clean: $(MAKE) -C docs clean download-deps: - $(MAKE) -C src checkra1n-macos checkra1n-linux-arm64 checkra1n-linux-armel checkra1n-linux-x86 checkra1n-linux-x86_64 checkra1n-kpf-pongo ramdisk.dmg binpack.dmg + $(MAKE) -C src checkra1n-macos checkra1n-linux-arm64 checkra1n-linux-armel checkra1n-linux-x86 checkra1n-linux-x86_64 checkra1n-kpf-pongo ramdisk.dmg binpack.dmg Pongo.bin docs: $(MAKE) -C docs distclean: clean - rm -rf palera1n-* palera1n*.dSYM src/checkra1n-* src/checkra1n-kpf-pongo src/ramdisk.dmg src/binpack.dmg + rm -rf palera1n-* palera1n*.dSYM src/checkra1n-* src/checkra1n-kpf-pongo src/ramdisk.dmg src/binpack.dmg src/Pongo.bin .PHONY: all palera1n clean docs distclean diff --git a/include/xxd-embedded.h b/include/xxd-embedded.h index 21f0000..3e4604a 100644 --- a/include/xxd-embedded.h +++ b/include/xxd-embedded.h @@ -5,17 +5,30 @@ #include #endif +#ifndef NO_CHECKRAIN extern unsigned char checkra1n[]; extern unsigned int checkra1n_len; +#endif +#ifndef NO_KPF extern unsigned char checkra1n_kpf_pongo[]; extern unsigned int checkra1n_kpf_pongo_len; +#endif +#ifndef NO_RAMDISK extern unsigned char ramdisk_dmg[]; extern unsigned int ramdisk_dmg_len; +#endif +#ifndef NO_BINPACK extern unsigned char binpack_dmg[]; extern unsigned int binpack_dmg_len; +#endif + +#ifndef NO_CUSTOM_PONGO +extern unsigned char Pongo_bin[]; +extern unsigned int Pongo_bin_len; +#endif #if defined(__APPLE__) && (TARGET_OS_IPHONE || defined(DEV_BUILD)) extern unsigned char libcheckra1nhelper_dylib[]; diff --git a/src/Makefile b/src/Makefile index 2c7d4fd..283dd77 100644 --- a/src/Makefile +++ b/src/Makefile @@ -37,6 +37,12 @@ else CFLAGS += -DNO_CHECKRAIN endif +ifneq ($(NO_CUSTOM_PONGO),1) +OBJECTS += xxd-Pongo.bin.o +else +CFLAGS += -DNO_CUSTOM_PONGO +endif + ifeq ($(DEV_BUILD),1) OBJECTS += tui_screen_enter_recovery.o tui_screen_enter_dfu.o tui_main.o OBJECTS += tui_screen_jailbreak.o tui_screen_main.o tui_screen_options.o @@ -119,6 +125,9 @@ ramdisk.dmg: binpack.dmg: curl -LO https://cdn.nickchan.lol/palera1n/c-rewrite/deps/binpack.dmg +Pongo.bin: + curl -LO https://cdn.nickchan.lol/palera1n/artifacts/kpf/Pongo.bin + clean: rm -rf *.o palera1n checkra1n checkra1n.c palera1n.dSYM libcheckra1nhelper.dylib diff --git a/src/exec_checkra1n.c b/src/exec_checkra1n.c index 647dfa2..c6b6592 100644 --- a/src/exec_checkra1n.c +++ b/src/exec_checkra1n.c @@ -26,37 +26,65 @@ extern char **environ; #include #include char* pongo_path = NULL; +bool external_pongo = false; char* ext_checkra1n = NULL; #define tmpdir getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR") int exec_checkra1n(void) { LOG(LOG_INFO, "About to execute checkra1n"); - int fd, ret; + int fd_checkra1n, fd_pongo, ret; char* checkra1n_path = NULL; if (ext_checkra1n != NULL) { checkra1n_path = ext_checkra1n; goto checkra1n_exec; } +#ifndef NO_CUSTOM_PONGO + if (pongo_path == NULL) { + pongo_path = malloc(strlen(tmpdir) + 20); + snprintf(pongo_path, strlen(tmpdir) + 20, "%s/Pongo.bin.XXXXXX", tmpdir); + fd_pongo = mkstemp(pongo_path); + if (fd_pongo == -1) { + LOG(LOG_FATAL, "Cannot open temporary file %s: %d (%s)", pongo_path, errno, strerror(errno)); + free(pongo_path); + return -1; + } + ssize_t didWrite_Pongo_bin = write(fd_pongo, Pongo_bin, Pongo_bin_len); + if (didWrite_Pongo_bin != (ssize_t)Pongo_bin_len) { + LOG(LOG_FATAL, "Size written to %s does not match expected: %lld != %d: %d (%s)", pongo_path, didWrite_Pongo_bin, Pongo_bin_len, errno, strerror(errno)); + close(fd_pongo); + unlink(pongo_path); + free(pongo_path); + pongo_path = NULL; + return -1; + } + } else external_pongo = true; +#endif checkra1n_path = malloc(strlen(tmpdir) + 20); snprintf(checkra1n_path, strlen(tmpdir) + 20, "%s/checkra1n.XXXXXX", tmpdir); - fd = mkstemp(checkra1n_path); - if (fd == -1) { - LOG(LOG_FATAL, "Cannot open temporary file: %d (%s)", errno, strerror(errno)); + fd_checkra1n = mkstemp(checkra1n_path); + if (fd_checkra1n == -1) { + LOG(LOG_FATAL, "Cannot open temporary file %s: %d (%s)", checkra1n_path, errno, strerror(errno)); + free(checkra1n_path); + checkra1n_path = NULL; return -1; } - ssize_t didWrite = write(fd, checkra1n, checkra1n_len); - if (didWrite != (ssize_t)checkra1n_len) { - LOG(LOG_FATAL, "Size written does not match expected: %lld != %d: %d (%s)", didWrite, checkra1n_len, errno, strerror(errno)); - close(fd); + ssize_t didWrite_checkra1n = write(fd_checkra1n, checkra1n, checkra1n_len); + if (didWrite_checkra1n != (ssize_t)checkra1n_len) { + LOG(LOG_FATAL, "Size written to %s does not match expected: %lld != %d: %d (%s)", checkra1n_path, didWrite_checkra1n, checkra1n_len, errno, strerror(errno)); + close(fd_checkra1n); unlink(checkra1n_path); + free(checkra1n_path); + checkra1n_path = NULL; return -1; } - close(fd); + close(fd_checkra1n); ret = chmod(checkra1n_path, 0700); if (ret) { LOG(LOG_FATAL, "Cannot chmod %s: %d (%s)", checkra1n_path, errno, strerror(errno)); unlink(checkra1n_path); + free(checkra1n_path); + checkra1n_path = NULL; return -1; } #if defined(__APPLE__) && defined(__arm64__) && (TARGET_OS_IPHONE || defined(FORCE_HELPER)) @@ -76,9 +104,9 @@ int exec_checkra1n(void) { LOG(LOG_FATAL, "Cannot open temporary file: %d (%s)", errno, strerror(errno)); return -1; } - ssize_t didWrite = write(helper_fd, libcheckra1nhelper_dylib, libcheckra1nhelper_dylib_len); - if ((unsigned int)didWrite != libcheckra1nhelper_dylib_len) { - LOG(LOG_FATAL, "Size written does not match expected: %lld != %d: %d (%s)", didWrite, libcheckra1nhelper_dylib_len, errno, strerror(errno)); + ssize_t didWrite_libcheckra1nhelper = write(helper_fd, libcheckra1nhelper_dylib, libcheckra1nhelper_dylib_len); + if ((unsigned int)didWrite_libcheckra1nhelper != libcheckra1nhelper_dylib_len) { + LOG(LOG_FATAL, "Size written does not match expected: %lld != %d: %d (%s)", didWrite_libcheckra1nhelper, libcheckra1nhelper_dylib_len, errno, strerror(errno)); close(helper_fd); unlink(libcheckra1nhelper_dylib_path); return -1; @@ -106,15 +134,22 @@ checkra1n_exec: {}; } else { strncat(args, "R", 0xf); } - if (pongo_path != NULL) strncat(args, "k", 0xf); // keep this at last + if (pongo_path != NULL) { + setenv("CHECKRA1N_OVERRIDE_PONGO", pongo_path, 1); + } + LOG(LOG_VERBOSE5, args); + if (pongo_path != NULL) LOG(LOG_VERBOSE5, pongo_path); pid_t pid; char* checkra1n_argv[] = { checkra1n_path, args, - pongo_path, NULL }; ret = posix_spawn(&pid, checkra1n_path, NULL, NULL, checkra1n_argv, environ); + waitpid(pid, NULL, 0); + if (!external_pongo && pongo_path != NULL) { + unlink(pongo_path); + } if (pongo_path != NULL) free(pongo_path); pongo_path = NULL; if (ret) { @@ -138,6 +173,5 @@ checkra1n_exec: {}; libcheckra1nhelper_dylib_path = NULL; } #endif - waitpid(pid, NULL, 0); return 0; }