Add magisk daemon

This commit is contained in:
topjohnwu 2017-04-08 07:37:43 +08:00
parent a223f6056e
commit 054a1e5ea4
10 changed files with 394 additions and 18 deletions

View File

@ -7,6 +7,7 @@ LOCAL_SHARED_LIBRARIES := libsqlite libselinux
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/utils \
$(LOCAL_PATH)/daemon \
$(LOCAL_PATH)/selinux/libselinux/include \
$(LOCAL_PATH)/selinux/libsepol/include \
$(LOCAL_PATH)/sqlite3
@ -17,6 +18,8 @@ LOCAL_SRC_FILES := \
utils/vector.c \
utils/xwrap.c \
utils/log_monitor.c \
daemon/daemon.c \
daemon/socket_trans.c \
magiskhide/magiskhide.c \
magiskhide/hide_daemon.c \
magiskhide/proc_monitor.c \
@ -26,13 +29,13 @@ LOCAL_SRC_FILES := \
magiskpolicy/utils.c \
resetprop/resetprop.cpp \
resetprop/libc_logging.cpp \
resetprop/system_properties.cpp \
su/su.c \
su/daemon.c \
su/activity.c \
su/db.c \
su/utils.c \
su/pts.c
resetprop/system_properties.cpp
# su/su.c \
# su/daemon.c \
# su/activity.c \
# su/db.c \
# su/utils.c \
# su/pts.c
LOCAL_CFLAGS := -Wno-implicit-exception-spec-mismatch
LOCAL_LDLIBS := -llog

88
jni/daemon/daemon.c Normal file
View File

@ -0,0 +1,88 @@
/* daemon.c - Magisk Daemon
*
* Start the daemon and wait for requests
* Connect the daemon and send requests through sockets
*/
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include "magisk.h"
#include "utils.h"
#include "daemon.h"
static void request_handler(int client) {
/* TODO: Put all function entrypoints here
it'll currently just log the input string */
char *s = read_string(client);
close(client);
LOGI("%s\n", s);
free(s);
}
/* Setup the address and return socket fd */
static int setup_socket(struct sockaddr_un *sun) {
int fd = xsocket(AF_LOCAL, SOCK_STREAM, 0);
if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
PLOGE("fcntl FD_CLOEXEC");
}
memset(sun, 0, sizeof(*sun));
sun->sun_family = AF_LOCAL;
memcpy(sun->sun_path, REQUESTOR_DAEMON_PATH, REQUESTOR_DAEMON_PATH_LEN);
return fd;
}
void start_daemon() {
// Launch the daemon, create new session, set proper context
if (getuid() != AID_ROOT || getgid() != AID_ROOT) {
PLOGE("daemon requires root. uid/gid not root");
}
switch (fork()) {
case -1:
PLOGE("fork");
case 0:
break;
default:
return;
}
xsetsid();
xsetcon("u:r:su:s0");
struct sockaddr_un sun;
int fd = setup_socket(&sun);
xbind(fd, (struct sockaddr*) &sun, sizeof(sun));
xlisten(fd, 10);
// Change process name
strcpy(argv0, "magisk_daemon");
// Loop forever to listen to requests
while(1) {
request_handler(xaccept(fd, NULL, NULL));
}
}
/* Connect the daemon, and return a socketfd */
int connect_daemon() {
struct sockaddr_un sun;
int fd = setup_socket(&sun);
if (connect(fd, (struct sockaddr*) &sun, sizeof(sun))) {
/* If we cannot access the daemon, we start the daemon
* since there is no clear entry point when the daemon should be started
*/
start_daemon();
do {
// Wait for 10ms
usleep(10000);
} while (connect(fd, (struct sockaddr*) &sun, sizeof(sun)));
}
return fd;
}

10
jni/daemon/daemon.h Normal file
View File

@ -0,0 +1,10 @@
/* daemon.h - Utility functions for daemon-client communication
*/
// socket_trans.c
int recv_fd(int sockfd);
void send_fd(int sockfd, int fd);
int read_int(int fd);
void write_int(int fd, int val);
char* read_string(int fd);
void write_string(int fd, const char* val);

146
jni/daemon/socket_trans.c Normal file
View File

@ -0,0 +1,146 @@
/* socket_trans.c - Functions to transfer data through socket
*/
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "magisk.h"
#include "utils.h"
#include "daemon.h"
/*
* Receive a file descriptor from a Unix socket.
* Contributed by @mkasick
*
* Returns the file descriptor on success, or -1 if a file
* descriptor was not actually included in the message
*
* On error the function terminates by calling exit(-1)
*/
int recv_fd(int sockfd) {
// Need to receive data from the message, otherwise don't care about it.
char iovbuf;
struct iovec iov = {
.iov_base = &iovbuf,
.iov_len = 1,
};
char cmsgbuf[CMSG_SPACE(sizeof(int))];
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cmsgbuf,
.msg_controllen = sizeof(cmsgbuf),
};
xrecvmsg(sockfd, &msg, MSG_WAITALL);
// Was a control message actually sent?
switch (msg.msg_controllen) {
case 0:
// No, so the file descriptor was closed and won't be used.
return -1;
case sizeof(cmsgbuf):
// Yes, grab the file descriptor from it.
break;
default:
goto error;
}
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg == NULL ||
cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
cmsg->cmsg_level != SOL_SOCKET ||
cmsg->cmsg_type != SCM_RIGHTS) {
error:
LOGE("unable to read fd");
exit(-1);
}
return *(int *)CMSG_DATA(cmsg);
}
/*
* Send a file descriptor through a Unix socket.
* Contributed by @mkasick
*
* On error the function terminates by calling exit(-1)
*
* fd may be -1, in which case the dummy data is sent,
* but no control message with the FD is sent.
*/
void send_fd(int sockfd, int fd) {
// Need to send some data in the message, this will do.
struct iovec iov = {
.iov_base = "",
.iov_len = 1,
};
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
};
char cmsgbuf[CMSG_SPACE(sizeof(int))];
if (fd != -1) {
// Is the file descriptor actually open?
if (fcntl(fd, F_GETFD) == -1) {
if (errno != EBADF) {
PLOGE("unable to send fd");
}
// It's closed, don't send a control message or sendmsg will EBADF.
} else {
// It's open, send the file descriptor in a control message.
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
*(int *)CMSG_DATA(cmsg) = fd;
}
}
xsendmsg(sockfd, &msg, 0);
}
int read_int(int fd) {
int val;
xxread(fd, &val, sizeof(int));
return val;
}
void write_int(int fd, int val) {
xwrite(fd, &val, sizeof(int));
}
char* read_string(int fd) {
int len = read_int(fd);
if (len > PATH_MAX || len < 0) {
LOGE("invalid string length %d", len);
exit(1);
}
char* val = xmalloc(sizeof(char) * (len + 1));
xxread(fd, val, len);
val[len] = '\0';
return val;
}
void write_string(int fd, const char* val) {
int len = strlen(val);
write_int(fd, len);
xwrite(fd, val, len);
}

View File

@ -11,6 +11,14 @@
#include <string.h>
#include <android/log.h>
#define AID_SHELL (get_shell_uid())
#define AID_ROOT 0
#define AID_SYSTEM (get_system_uid())
#define AID_RADIO (get_radio_uid())
#define REQUESTOR_DAEMON_PATH "\0MAGISK"
#define REQUESTOR_DAEMON_PATH_LEN 7
#define LOG_TAG "Magisk"
#ifdef DEBUG
@ -35,7 +43,7 @@ extern char *argv0; /* For changing process name */
// Multi-call entrypoints
int magiskhide_main(int argc, char *argv[]);
int magiskpolicy_main(int argc, char *argv[]);
int su_main(int argc, char *argv[]);
// int su_main(int argc, char *argv[]);
#ifdef __cplusplus
extern "C" {
@ -45,6 +53,13 @@ int resetprop_main(int argc, char *argv[]);
}
#endif
/*****************
* Magisk Daemon *
*****************/
void start_daemon();
int connect_daemon();
/**************
* MagiskHide *
**************/

View File

@ -55,6 +55,8 @@ void hide_daemon() {
}
close(pipefd[1]);
// Set the process name
strcpy(argv0, "magiskhide_daemon");
int pid, fd;

View File

@ -3,6 +3,7 @@
#include "utils.h"
#include "magisk.h"
#include "daemon.h"
char magiskbuf[BUF_SIZE];
char *argv0;
@ -23,6 +24,11 @@ int main(int argc, char *argv[]) {
} else if (strcmp(argv[1], "--install") == 0) {
// TODO: Install symlinks
return 0;
} else if (strcmp(argv[1], "--test") == 0) {
// Temporary testing entry
int fd = connect_daemon();
write_string(fd, argv[2]);
return 0;
} else {
// It's calling applets
--argc;
@ -33,7 +39,7 @@ int main(int argc, char *argv[]) {
// Applets
if (strcmp(arg, "su") == 0) {
return su_main(argc, argv);
// return su_main(argc, argv);
} else if (strcmp(arg, "magiskpolicy") == 0) {
return magiskpolicy_main(argc, argv);
} else if (strcmp(arg, "resetprop") == 0) {

View File

@ -10,6 +10,7 @@
#include <sys/types.h>
#include "magisk.h"
#include "utils.h"
int check_data() {

View File

@ -4,11 +4,12 @@
#ifndef _UTILS_H_
#define _UTILS_H_
#include <sys/types.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "magisk.h"
#include "vector.h"
// xwrap.c
@ -21,10 +22,18 @@ int xpipe(int pipefd[2]);
int xsetns(int fd, int nstype);
DIR *xopendir(const char *name);
struct dirent *xreaddir(DIR *dirp);
// vector.c
#include "vector.h"
pid_t xsetsid();
int xsetcon(char *context);
int xsocket(int domain, int type, int protocol);
int xbind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int xconnect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int xlisten(int sockfd, int backlog);
int xaccept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
void *xmalloc(size_t size);
void *xcalloc(size_t nmemb, size_t size);
void *xrealloc(void *ptr, size_t size);
ssize_t xsendmsg(int sockfd, const struct msghdr *msg, int flags);
ssize_t xrecvmsg(int sockfd, struct msghdr *msg, int flags);
// log_monitor.c

View File

@ -14,15 +14,18 @@
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <selinux/selinux.h>
#include "magisk.h"
#include "utils.h"
FILE *xfopen(const char *pathname, const char *mode) {
FILE *fp = fopen(pathname, mode);
if (fp == NULL) {
PLOGE("fopen");
PLOGE("fopen: %s", pathname);
}
return fp;
}
@ -30,7 +33,7 @@ FILE *xfopen(const char *pathname, const char *mode) {
int xopen(const char *pathname, int flags) {
int fd = open(pathname, flags);
if (fd < 0) {
PLOGE("open");
PLOGE("open: %s", pathname);
}
return fd;
}
@ -76,7 +79,7 @@ int xsetns(int fd, int nstype) {
DIR *xopendir(const char *name) {
DIR *d = opendir(name);
if (d == NULL) {
PLOGE("opendir");
PLOGE("opendir: %s", name);
}
return d;
}
@ -90,3 +93,96 @@ struct dirent *xreaddir(DIR *dirp) {
return e;
}
pid_t xsetsid() {
pid_t pid = setsid();
if (pid == -1) {
PLOGE("setsid");
}
return pid;
}
int xsetcon(char *context) {
if (setcon(context) == -1) {
PLOGE("setcon: %s", context);
}
return 0;
}
int xsocket(int domain, int type, int protocol) {
int fd = socket(domain, type, protocol);
if (fd == -1) {
PLOGE("socket");
}
return fd;
}
int xbind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
if (bind(sockfd, addr, addrlen) == -1) {
PLOGE("bind");
}
return 0;
}
int xconnect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
if (connect(sockfd, addr, addrlen) == -1) {
PLOGE("bind");
}
return 0;
}
int xlisten(int sockfd, int backlog) {
if (listen(sockfd, backlog) == -1) {
PLOGE("listen");
}
return 0;
}
int xaccept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
int fd = accept(sockfd, addr, addrlen);
if (fd == -1) {
PLOGE("accept");
}
return fd;
}
void *xmalloc(size_t size) {
void *p = malloc(size);
if (p == NULL) {
PLOGE("malloc");
}
return p;
}
void *xcalloc(size_t nmemb, size_t size) {
void *p = calloc(nmemb, size);
if (p == NULL) {
PLOGE("calloc");
}
return p;
}
void *xrealloc(void *ptr, size_t size) {
void *p = realloc(ptr, size);
if (p == NULL) {
PLOGE("realloc");
}
return p;
}
ssize_t xsendmsg(int sockfd, const struct msghdr *msg, int flags) {
int sent = sendmsg(sockfd, msg, flags);
if (sent == -1) {
PLOGE("sendmsg");
}
return sent;
}
ssize_t xrecvmsg(int sockfd, struct msghdr *msg, int flags) {
int rec = recvmsg(sockfd, msg, flags);
if (rec == -1) {
PLOGE("recvmsg");
}
return rec;
}