2018-09-16 04:16:18 -04:00
|
|
|
/*
|
|
|
|
** Copyright 2018, John Wu (@topjohnwu)
|
|
|
|
** Copyright 2010, Adam Shanks (@ChainsDD)
|
|
|
|
** Copyright 2008, Zinx Verituse (@zinxv)
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "magisk.h"
|
|
|
|
#include "daemon.h"
|
2018-10-04 04:59:51 -04:00
|
|
|
#include "utils.h"
|
2018-09-16 04:16:18 -04:00
|
|
|
#include "su.h"
|
|
|
|
|
|
|
|
#define AM_PATH "/system/bin/app_process", "/system/bin", "com.android.commands.am.Am"
|
|
|
|
|
|
|
|
static char *get_command(const struct su_request *to) {
|
2018-10-04 04:59:51 -04:00
|
|
|
if (to->command[0])
|
2018-09-16 04:16:18 -04:00
|
|
|
return to->command;
|
2018-10-04 04:59:51 -04:00
|
|
|
if (to->shell[0])
|
2018-09-16 04:16:18 -04:00
|
|
|
return to->shell;
|
|
|
|
return DEFAULT_SHELL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void silent_run(char * const args[]) {
|
2018-10-04 04:59:51 -04:00
|
|
|
if (fork_dont_care())
|
2018-09-16 04:16:18 -04:00
|
|
|
return;
|
|
|
|
int zero = open("/dev/zero", O_RDONLY | O_CLOEXEC);
|
2018-10-04 04:59:51 -04:00
|
|
|
xdup2(zero, 0);
|
2018-09-16 04:16:18 -04:00
|
|
|
int null = open("/dev/null", O_WRONLY | O_CLOEXEC);
|
2018-10-04 04:59:51 -04:00
|
|
|
xdup2(null, 1);
|
|
|
|
xdup2(null, 2);
|
2018-09-16 04:16:18 -04:00
|
|
|
setenv("CLASSPATH", "/system/framework/am.jar", 1);
|
|
|
|
execv(args[0], args);
|
|
|
|
PLOGE("exec am");
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2018-10-04 04:59:51 -04:00
|
|
|
static void setup_user(char *user, struct su_info *info) {
|
|
|
|
switch (DB_SET(info, SU_MULTIUSER_MODE)) {
|
2018-10-04 01:49:52 -04:00
|
|
|
case MULTIUSER_MODE_OWNER_ONLY:
|
|
|
|
case MULTIUSER_MODE_OWNER_MANAGED:
|
|
|
|
sprintf(user, "%d", 0);
|
|
|
|
break;
|
|
|
|
case MULTIUSER_MODE_USER:
|
2018-10-04 04:59:51 -04:00
|
|
|
sprintf(user, "%d", info->uid / 100000);
|
2018-10-04 01:49:52 -04:00
|
|
|
break;
|
2018-09-16 04:16:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 04:59:51 -04:00
|
|
|
void app_log(struct su_context *ctx) {
|
2018-09-16 04:16:18 -04:00
|
|
|
char user[8];
|
2018-10-04 04:59:51 -04:00
|
|
|
setup_user(user, ctx->info);
|
2018-09-16 04:16:18 -04:00
|
|
|
|
|
|
|
char fromUid[8];
|
|
|
|
sprintf(fromUid, "%d",
|
2018-10-04 04:59:51 -04:00
|
|
|
DB_SET(ctx->info, SU_MULTIUSER_MODE) == MULTIUSER_MODE_OWNER_MANAGED ?
|
|
|
|
ctx->info->uid % 100000 : ctx->info->uid);
|
2018-09-16 04:16:18 -04:00
|
|
|
|
|
|
|
char toUid[8];
|
2018-10-04 04:59:51 -04:00
|
|
|
sprintf(toUid, "%d", ctx->req.uid);
|
2018-09-16 04:16:18 -04:00
|
|
|
|
|
|
|
char pid[8];
|
2018-10-04 04:59:51 -04:00
|
|
|
sprintf(pid, "%d", ctx->pid);
|
2018-09-16 04:16:18 -04:00
|
|
|
|
|
|
|
char policy[2];
|
2018-10-04 04:59:51 -04:00
|
|
|
sprintf(policy, "%d", ctx->info->access.policy);
|
2018-09-16 04:16:18 -04:00
|
|
|
|
|
|
|
char *cmd[] = {
|
|
|
|
AM_PATH, "broadcast",
|
|
|
|
"-a", "android.intent.action.BOOT_COMPLETED",
|
2018-10-04 04:59:51 -04:00
|
|
|
"-p", DB_STR(ctx->info, SU_MANAGER),
|
2018-09-16 04:16:18 -04:00
|
|
|
"--user", user,
|
|
|
|
"--es", "action", "log",
|
|
|
|
"--ei", "from.uid", fromUid,
|
|
|
|
"--ei", "to.uid", toUid,
|
|
|
|
"--ei", "pid", pid,
|
|
|
|
"--ei", "policy", policy,
|
2018-10-04 04:59:51 -04:00
|
|
|
"--es", "command", get_command(&ctx->req),
|
2018-09-16 04:16:18 -04:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
silent_run(cmd);
|
|
|
|
}
|
|
|
|
|
2018-10-04 04:59:51 -04:00
|
|
|
void app_connect(const char *socket, struct su_info *info) {
|
2018-09-16 04:16:18 -04:00
|
|
|
char user[8];
|
2018-10-04 04:59:51 -04:00
|
|
|
setup_user(user, info);
|
2018-09-16 04:16:18 -04:00
|
|
|
char *cmd[] = {
|
|
|
|
AM_PATH, "broadcast",
|
|
|
|
"-a", "android.intent.action.BOOT_COMPLETED",
|
2018-10-04 04:59:51 -04:00
|
|
|
"-p", DB_STR(info, SU_MANAGER),
|
2018-09-16 04:16:18 -04:00
|
|
|
"--user", user,
|
|
|
|
"--es", "action", "request",
|
|
|
|
"--es", "socket", (char *) socket,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
silent_run(cmd);
|
|
|
|
}
|
|
|
|
|
2018-10-04 04:59:51 -04:00
|
|
|
void socket_send_request(int fd, struct su_info *info) {
|
|
|
|
write_key_token(fd, "uid", info->uid);
|
2018-09-16 04:16:18 -04:00
|
|
|
write_string_be(fd, "eof");
|
|
|
|
}
|
|
|
|
|