Some code adjustments

This commit is contained in:
topjohnwu 2019-02-24 23:09:34 -05:00
parent 63b18246d8
commit c9fa8118d1
15 changed files with 65 additions and 95 deletions

View File

@ -284,7 +284,7 @@ static bool patch_sepolicy() {
// Force init to load /sepolicy
uint8_t *addr;
size_t size;
mmap_rw("/init", (void **) &addr, &size);
mmap_rw("/init", addr, size);
for (int i = 0; i < size; ++i) {
if (memcmp(addr + i, SPLIT_PLAT_CIL, sizeof(SPLIT_PLAT_CIL) - 1) == 0) {
memcpy(addr + i + sizeof(SPLIT_PLAT_CIL) - 4, "xxx", 3);
@ -327,7 +327,7 @@ static void decompress_ramdisk() {
return;
uint8_t *buf;
size_t sz;
mmap_ro(ramdisk_xz, (void **) &buf, &sz);
mmap_ro(ramdisk_xz, buf, sz);
int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC);
unxz(fd, buf, sz);
munmap(buf, sz);
@ -362,7 +362,7 @@ static void patch_socket_name(const char *path) {
uint8_t *buf;
char name[sizeof(MAIN_SOCKET)];
size_t size;
mmap_rw(path, (void **) &buf, &size);
mmap_rw(path, buf, size);
for (int i = 0; i < size; ++i) {
if (memcmp(buf + i, MAIN_SOCKET, sizeof(MAIN_SOCKET)) == 0) {
gen_rand_str(name, sizeof(name));

View File

@ -47,7 +47,7 @@ boot_img::~boot_img() {
#define UNSUPP_RET 1
#define CHROME_RET 2
int boot_img::parse_file(const char *image) {
mmap_ro(image, (void **) &map_addr, &map_size);
mmap_ro(image, map_addr, map_size);
fprintf(stderr, "Parsing boot image: [%s]\n", image);
for (uint8_t *head = map_addr; head < map_addr + map_size; ++head) {
switch (check_fmt(head, map_size)) {
@ -332,7 +332,7 @@ void repack(const char* orig_image, const char* out_image) {
if (COMPRESSED(boot.k_fmt)) {
size_t raw_size;
void *kernel_raw;
mmap_ro(KERNEL_FILE, &kernel_raw, &raw_size);
mmap_ro(KERNEL_FILE, kernel_raw, raw_size);
boot.hdr->kernel_size = compress(boot.k_fmt, fd, kernel_raw, raw_size);
munmap(kernel_raw, raw_size);
} else {
@ -355,7 +355,7 @@ void repack(const char* orig_image, const char* out_image) {
if (COMPRESSED(boot.r_fmt)) {
size_t cpio_size;
void *cpio;
mmap_ro(RAMDISK_FILE, &cpio, &cpio_size);
mmap_ro(RAMDISK_FILE, cpio, cpio_size);
boot.hdr->ramdisk_size = compress(boot.r_fmt, fd, cpio, cpio_size);
munmap(cpio, cpio_size);
} else {
@ -397,7 +397,7 @@ void repack(const char* orig_image, const char* out_image) {
// Map output image as rw
munmap(boot.map_addr, boot.map_size);
mmap_rw(out_image, (void **) &boot.map_addr, &boot.map_size);
mmap_rw(out_image, boot.map_addr, boot.map_size);
// MTK headers
if (boot.flags & MTK_KERNEL) {

View File

@ -1,11 +1,13 @@
#include <unistd.h>
#include <sys/mman.h>
extern "C" {
#include <libfdt.h>
}
#include <utils.h>
#include "magiskboot.h"
#include "format.h"
static void print_props(const void *fdt, int node, int depth) {
int prop;
@ -45,7 +47,7 @@ static void dtb_dump(const char *file) {
size_t size ;
uint8_t *dtb, *fdt;
fprintf(stderr, "Loading dtbs from [%s]\n", file);
mmap_ro(file, (void **) &dtb, &size);
mmap_ro(file, dtb, size);
// Loop through all the dtbs
int dtb_num = 0;
for (int i = 0; i < size; ++i) {
@ -65,11 +67,12 @@ static void dtb_patch(const char *file, int patch) {
uint8_t *dtb, *fdt;
fprintf(stderr, "Loading dtbs from [%s]\n", file);
if (patch)
mmap_rw(file, (void **) &dtb, &size);
mmap_rw(file, dtb, size);
else
mmap_ro(file, (void **) &dtb, &size);
mmap_ro(file, dtb, size);
// Loop through all the dtbs
int dtb_num = 0, found = 0;
int dtb_num = 0;
bool found = false;
for (int i = 0; i < size; ++i) {
if (memcmp(dtb + i, DTB_MAGIC, 4) == 0) {
fdt = dtb + i;
@ -85,11 +88,11 @@ static void dtb_patch(const char *file, int patch) {
void *dup = xmalloc(value_size);
memcpy(dup, value, value_size);
memset(value, 0, value_size);
found |= patch_verity(&dup, &value_size, 1);
found |= patch_verity(&dup, &value_size);
memcpy(value, dup, value_size);
free(dup);
} else {
found |= patch_verity(&value, &value_size, 0);
found |= patch_verity(&value, &value_size, false);
}
}
}

View File

@ -20,7 +20,7 @@ void hexpatch(const char *image, const char *from, const char *to) {
int patternsize = strlen(from) / 2, patchsize = strlen(to) / 2;
size_t filesize;
uint8_t *file, *pattern, *patch;
mmap_rw(image, (void **) &file, &filesize);
mmap_rw(image, file, filesize);
pattern = (uint8_t *) xmalloc(patternsize);
patch = (uint8_t *) xmalloc(patchsize);
hex2byte((uint8_t *) from, pattern);

View File

@ -1,10 +1,7 @@
#ifndef _MAGISKBOOT_H_
#define _MAGISKBOOT_H_
#pragma once
#include <sys/types.h>
#include "format.h"
#define KERNEL_FILE "kernel"
#define RAMDISK_FILE "ramdisk.cpio"
#define SECOND_FILE "second"
@ -21,7 +18,5 @@ int cpio_commands(int argc, char *argv[]);
int dtb_commands(const char *cmd, int argc, char *argv[]);
// Pattern
int patch_verity(void **buf, uint32_t *size, int patch);
bool patch_verity(void **buf, uint32_t *size, bool patch = true);
void patch_encryption(void **buf, uint32_t *size);
#endif

View File

@ -125,7 +125,7 @@ int main(int argc, char *argv[]) {
uint8_t sha1[SHA_DIGEST_SIZE];
void *buf;
size_t size;
mmap_ro(argv[2], &buf, &size);
mmap_ro(argv[2], buf, size);
SHA_hash(buf, size, sha1);
for (int i = 0; i < SHA_DIGEST_SIZE; ++i)
printf("%02x", sha1[i]);

View File

@ -31,8 +31,9 @@ static int check_encryption_pattern(const char *s) {
return -1;
}
int patch_verity(void **buf, uint32_t *size, int patch) {
int skip, src_size = *size, found = 0;
bool patch_verity(void **buf, uint32_t *size, bool patch) {
int skip, src_size = *size;
bool found = false;
char *src = (char *) *buf, *patched = patch ? (char *) xcalloc(src_size, 1) : nullptr;
for (int read = 0, write = 0; read < src_size; ++read, ++write) {
if ((skip = check_verity_pattern(src + read)) > 0) {
@ -43,7 +44,7 @@ int patch_verity(void **buf, uint32_t *size, int patch) {
fprintf(stderr, "Found pattern [%.*s]\n", skip, src + read);
}
read += skip;
found = 1;
found = true;
}
if (patch)
patched[write] = src[read];

View File

@ -45,7 +45,7 @@ void magisk_cpio::patch(bool keepverity, bool keepforceencrypt) {
str_contains(cur->first, "fstab") && S_ISREG(cur->second->mode);
if (!keepverity) {
if (fstab) {
patch_verity(&cur->second->data, &cur->second->filesize, 1);
patch_verity(&cur->second->data, &cur->second->filesize);
} else if (cur->first == "verity_key") {
rm(cur);
continue;

View File

@ -157,7 +157,7 @@ static void pb_getprop(read_cb_t *read_cb) {
props.properties.arg = read_cb;
pb_byte_t *buf;
size_t size;
mmap_ro(PERSISTENT_PROPERTY_DIR "/persistent_properties", (void **) &buf, &size);
mmap_ro(PERSISTENT_PROPERTY_DIR "/persistent_properties", buf, size);
pb_istream_t stream = pb_istream_from_buffer(buf, size);
pb_decode(&stream, PersistentProperties_fields, &props);
munmap(buf, size);

View File

@ -1,6 +1,7 @@
#pragma once
#include <stdio.h>
#include <syscall.h>
#ifndef PR_SET_VMA
#define PR_SET_VMA 0x53564d41
@ -9,6 +10,5 @@
#define PR_SET_VMA_ANON_NAME 0
#endif
#define getline __getline
#define fsetxattr __fsetxattr
#define fsetxattr(...) syscall(__NR_fsetxattr, __VA_ARGS__)
extern "C" ssize_t __getline(char **, size_t *, FILE *);
extern "C" int __fsetxattr(int, const char *, const void *, size_t, int);

View File

@ -26,8 +26,6 @@
* SUCH DAMAGE.
*/
#include "system_properties/prop_area.h"
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
@ -41,6 +39,8 @@
#include <async_safe/log.h>
#include "system_properties/prop_area.h"
constexpr size_t PA_SIZE = 128 * 1024;
constexpr uint32_t PROP_AREA_MAGIC = 0x504f5250;
constexpr uint32_t PROP_AREA_VERSION = 0xfc6ed0ab;

View File

@ -152,7 +152,7 @@ void cpio::output(OutStream &out) {
cpio_rw::cpio_rw(const char *filename) {
char *buf;
size_t sz;
mmap_ro(filename, (void **) &buf, &sz);
mmap_ro(filename, buf, sz);
fprintf(stderr, "Loading cpio: [%s]\n", filename);
load_cpio(buf, sz);
munmap(buf, sz);
@ -172,7 +172,7 @@ void cpio_rw::insert(cpio_entry *e) {
void cpio_rw::add(mode_t mode, const char *name, const char *file) {
void *buf;
size_t sz;
mmap_ro(file, &buf, &sz);
mmap_ro(file, buf, sz);
auto e = new cpio_entry(name, S_IFREG | mode);
e->filesize = sz;
e->data = xmalloc(sz);
@ -242,7 +242,7 @@ void cpio_rw::load_cpio(char *buf, size_t sz) {
}
cpio_mmap::cpio_mmap(const char *filename) {
mmap_ro(filename, (void **) &buf, &sz);
mmap_ro(filename, buf, sz);
fprintf(stderr, "Loading cpio: [%s]\n", filename);
size_t pos = 0;
cpio_newc_header *header;

View File

@ -309,24 +309,22 @@ void fclone_attr(int sourcefd, int targetfd) {
fsetattr(targetfd, &a);
}
static void _mmap(int rw, const char *filename, void **buf, size_t *size) {
void *__mmap(const char *filename, size_t *size, bool rw) {
struct stat st;
void *buf;
int fd = xopen(filename, (rw ? O_RDWR : O_RDONLY) | O_CLOEXEC);
fstat(fd, &st);
if (S_ISBLK(st.st_mode))
ioctl(fd, BLKGETSIZE64, size);
else
*size = st.st_size;
*buf = *size > 0 ? xmmap(nullptr, *size, PROT_READ | (rw ? PROT_WRITE : 0), MAP_SHARED, fd, 0) : nullptr;
buf = *size > 0 ? xmmap(nullptr, *size, PROT_READ | (rw ? PROT_WRITE : 0), MAP_SHARED, fd, 0) : nullptr;
close(fd);
return buf;
}
void mmap_ro(const char *filename, void **buf, size_t *size) {
_mmap(0, filename, buf, size);
}
void mmap_rw(const char *filename, void **buf, size_t *size) {
_mmap(1, filename, buf, size);
*buf = __mmap(filename, size, false);
}
void fd_full_read(int fd, void **buf, size_t *size) {
@ -359,25 +357,6 @@ void full_read_at(int dirfd, const char *filename, void **buf, size_t *size) {
close(fd);
}
void stream_full_read(int fd, void **buf, size_t *size) {
size_t cap = 1 << 20;
uint8_t tmp[1 << 20];
*buf = xmalloc(cap);
ssize_t read;
*size = 0;
while (1) {
read = xread(fd, tmp, sizeof(tmp));
if (read <= 0)
break;
if (*size + read > cap) {
cap *= 2;
*buf = realloc(*buf, cap);
}
memcpy((uint8_t *) *buf + *size, tmp, read);
*size += read;
}
}
void write_zero(int fd, size_t size) {
size_t pos = lseek(fd, 0, SEEK_CUR);
ftruncate(fd, pos + size);

View File

@ -1,8 +1,7 @@
/* util.h - Header for all utility functions
*/
#ifndef _UTILS_H_
#define _UTILS_H_
#pragma once
#include <stdio.h>
#include <dirent.h>
@ -18,8 +17,6 @@ extern "C" {
#define UID_SHELL (get_shell_uid())
#define UID_ROOT 0
#define UID_SYSTEM (get_system_uid())
#define UID_RADIO (get_radio_uid())
// xwrap.cpp
@ -52,7 +49,6 @@ ssize_t xsendmsg(int sockfd, const struct msghdr *msg, int flags);
ssize_t xrecvmsg(int sockfd, struct msghdr *msg, int flags);
int xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
int xsocketpair(int domain, int type, int protocol, int sv[2]);
int xstat(const char *pathname, struct stat *buf);
int xlstat(const char *pathname, struct stat *buf);
int xdup2(int oldfd, int newfd);
@ -71,8 +67,7 @@ int xrename(const char *oldpath, const char *newpath);
int xmkdir(const char *pathname, mode_t mode);
int xmkdirs(const char *pathname, mode_t mode);
int xmkdirat(int dirfd, const char *pathname, mode_t mode);
void *xmmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset);
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);
pid_t xfork();
int xpoll(struct pollfd *fds, nfds_t nfds, int timeout);
@ -80,8 +75,6 @@ int xpoll(struct pollfd *fds, nfds_t nfds, int timeout);
// misc.cpp
unsigned get_shell_uid();
unsigned get_system_uid();
unsigned get_radio_uid();
int fork_dont_care();
int fork_no_zombie();
void gen_rand_str(char *buf, int len);
@ -91,11 +84,9 @@ void set_nice_name(const char *name);
#define getline __getline
#define getdelim __getdelim
#define fsetxattr __fsetxattr
ssize_t __getline(char **lineptr, size_t *n, FILE *stream);
ssize_t __getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
int __fsetxattr(int fd, const char *name, const void *value, size_t size, int flags);
// file.cpp
@ -129,11 +120,9 @@ int fsetattr(int fd, struct file_attr *a);
void fclone_attr(int sourcefd, int targetfd);
void clone_attr(const char *source, const char *target);
void mmap_ro(const char *filename, void **buf, size_t *size);
void mmap_rw(const char *filename, void **buf, size_t *size);
void fd_full_read(int fd, void **buf, size_t *size);
void full_read(const char *filename, void **buf, size_t *size);
void full_read_at(int dirfd, const char *filename, void **buf, size_t *size);
void stream_full_read(int fd, void **buf, size_t *size);
void write_zero(int fd, size_t size);
#ifdef __cplusplus
@ -180,6 +169,31 @@ private:
// file.cpp
void file_readline(const char *filename, const std::function<bool (std::string_view&)> &fn, bool trim = false);
void *__mmap(const char *filename, size_t *size, bool rw);
template <typename B>
void mmap_ro(const char *filename, B &buf, size_t &sz) {
buf = (B) __mmap(filename, &sz, false);
}
template <typename B, typename L>
void mmap_ro(const char *filename, B &buf, L &sz) {
size_t __sz;
buf = (B) __mmap(filename, &__sz, false);
sz = __sz;
}
template <typename B>
void mmap_rw(const char *filename, B &buf, size_t &sz) {
buf = (B) __mmap(filename, &sz, true);
}
template <typename B, typename L>
void mmap_rw(const char *filename, B &buf, L &sz) {
size_t __sz;
buf = (B) __mmap(filename, &__sz, true);
sz = __sz;
}
// misc.cpp
@ -217,5 +231,3 @@ int exec_command_sync(Args &&...args) {
bool ends_with(const std::string_view &s1, const std::string_view &s2);
#endif
#endif

View File

@ -24,22 +24,6 @@ unsigned get_shell_uid() {
return ppwd->pw_uid;
}
unsigned get_system_uid() {
struct passwd* ppwd = getpwnam("system");
if (nullptr == ppwd)
return 1000;
return ppwd->pw_uid;
}
unsigned get_radio_uid() {
struct passwd* ppwd = getpwnam("radio");
if (nullptr == ppwd)
return 1001;
return ppwd->pw_uid;
}
int fork_dont_care() {
int pid = xfork();
if (pid) {
@ -163,10 +147,6 @@ ssize_t __getline(char **lineptr, size_t *n, FILE *stream) {
return __getdelim(lineptr, n, '\n', stream);
}
int __fsetxattr(int fd, const char *name, const void *value, size_t size, int flags) {
return (int) syscall(__NR_fsetxattr, fd, name, value, size, flags);
}
int exec_command(exec_t &exec) {
int pipefd[2] = {-1, -1}, outfd = -1;