Generalize unxz

This commit is contained in:
topjohnwu 2023-09-21 05:47:21 -07:00
parent 15e13a8d8b
commit 46275b90c2
4 changed files with 24 additions and 23 deletions

View File

@ -12,12 +12,13 @@
using namespace std; using namespace std;
bool unxz(int fd, const uint8_t *buf, size_t size) { bool unxz(out_stream &strm, rust::Slice<const uint8_t> bytes) {
uint8_t out[8192]; uint8_t out[8192];
xz_crc32_init(); xz_crc32_init();
size_t size = bytes.size();
struct xz_dec *dec = xz_dec_init(XZ_DYNALLOC, 1 << 26); struct xz_dec *dec = xz_dec_init(XZ_DYNALLOC, 1 << 26);
struct xz_buf b = { struct xz_buf b = {
.in = buf, .in = bytes.data(),
.in_pos = 0, .in_pos = 0,
.in_size = size, .in_size = size,
.out = out, .out = out,
@ -29,20 +30,11 @@ bool unxz(int fd, const uint8_t *buf, size_t size) {
ret = xz_dec_run(dec, &b); ret = xz_dec_run(dec, &b);
if (ret != XZ_OK && ret != XZ_STREAM_END) if (ret != XZ_OK && ret != XZ_STREAM_END)
return false; return false;
write(fd, out, b.out_pos); strm.write(out, b.out_pos);
b.out_pos = 0; b.out_pos = 0;
} while (b.in_pos != size); } while (b.in_pos != size);
return true; return true;
}
static int dump_bin(const uint8_t *buf, size_t sz, const char *path, mode_t mode) {
int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
if (fd < 0)
return 1;
if (!unxz(fd, buf, sz))
return 1;
close(fd);
return 0;
} }
void restore_ramdisk_init() { void restore_ramdisk_init() {
@ -59,10 +51,6 @@ void restore_ramdisk_init() {
} }
} }
int dump_preload(const char *path, mode_t mode) {
return dump_bin(init_ld_xz, sizeof(init_ld_xz), path, mode);
}
class RecoveryInit : public BaseInit { class RecoveryInit : public BaseInit {
public: public:
using BaseInit::BaseInit; using BaseInit::BaseInit;

View File

@ -1,4 +1,5 @@
#include <base.hpp> #include <base.hpp>
#include <stream.hpp>
#include "init-rs.hpp" #include "init-rs.hpp"
@ -26,12 +27,11 @@ struct BootConfig {
extern std::vector<std::string> mount_list; extern std::vector<std::string> mount_list;
int magisk_proxy_main(int argc, char *argv[]); int magisk_proxy_main(int argc, char *argv[]);
bool unxz(int fd, const uint8_t *buf, size_t size); bool unxz(out_stream &strm, rust::Slice<const uint8_t> bytes);
void load_kernel_info(BootConfig *config); void load_kernel_info(BootConfig *config);
bool check_two_stage(); bool check_two_stage();
const char *backup_init(); const char *backup_init();
void restore_ramdisk_init(); void restore_ramdisk_init();
int dump_preload(const char *path, mode_t mode);
/*************** /***************
* Base classes * Base classes

View File

@ -167,14 +167,16 @@ static void extract_files(bool sbin) {
mmap_data magisk(m32); mmap_data magisk(m32);
unlink(m32); unlink(m32);
int fd = xopen("magisk32", O_WRONLY | O_CREAT, 0755); int fd = xopen("magisk32", O_WRONLY | O_CREAT, 0755);
unxz(fd, magisk.buf(), magisk.sz()); fd_channel ch(fd);
unxz(ch, magisk);
close(fd); close(fd);
} }
if (access(m64, F_OK) == 0) { if (access(m64, F_OK) == 0) {
mmap_data magisk(m64); mmap_data magisk(m64);
unlink(m64); unlink(m64);
int fd = xopen("magisk64", O_WRONLY | O_CREAT, 0755); int fd = xopen("magisk64", O_WRONLY | O_CREAT, 0755);
unxz(fd, magisk.buf(), magisk.sz()); fd_channel ch(fd);
unxz(ch, magisk);
close(fd); close(fd);
xsymlink("./magisk64", "magisk"); xsymlink("./magisk64", "magisk");
} else { } else {
@ -184,7 +186,8 @@ static void extract_files(bool sbin) {
mmap_data stub(stub_xz); mmap_data stub(stub_xz);
unlink(stub_xz); unlink(stub_xz);
int fd = xopen("stub.apk", O_WRONLY | O_CREAT, 0); int fd = xopen("stub.apk", O_WRONLY | O_CREAT, 0);
unxz(fd, stub.buf(), stub.sz()); fd_channel ch(fd);
unxz(ch, stub);
close(fd); close(fd);
} }
} }

View File

@ -2,7 +2,7 @@
#include <magisk.hpp> #include <magisk.hpp>
#include <sepolicy.hpp> #include <sepolicy.hpp>
#include <base.hpp> #include <embed.hpp>
#include "init.hpp" #include "init.hpp"
@ -38,6 +38,16 @@ void MagiskInit::patch_sepolicy(const char *in, const char *out) {
} }
} }
static void dump_preload() {
int fd = xopen("/dev/preload.so", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if (fd < 0)
return;
fd_channel ch(fd);
if (!unxz(ch, byte_view(init_ld_xz, sizeof(init_ld_xz))))
return;
close(fd);
}
#define MOCK_COMPAT SELINUXMOCK "/compatible" #define MOCK_COMPAT SELINUXMOCK "/compatible"
#define MOCK_LOAD SELINUXMOCK "/load" #define MOCK_LOAD SELINUXMOCK "/load"
#define MOCK_ENFORCE SELINUXMOCK "/enforce" #define MOCK_ENFORCE SELINUXMOCK "/enforce"
@ -50,7 +60,7 @@ bool MagiskInit::hijack_sepolicy() {
// This meant that instead of going through convoluted methods trying to alter // This meant that instead of going through convoluted methods trying to alter
// and block init's control flow, we can just LD_PRELOAD and replace the // and block init's control flow, we can just LD_PRELOAD and replace the
// security_load_policy function with our own implementation. // security_load_policy function with our own implementation.
dump_preload("/dev/preload.so", 0644); dump_preload();
setenv("LD_PRELOAD", "/dev/preload.so", 1); setenv("LD_PRELOAD", "/dev/preload.so", 1);
} }