2018-09-16 10:16:18 +02: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>
|
|
|
|
|
2019-02-10 09:57:51 +01:00
|
|
|
#include <magisk.h>
|
|
|
|
#include <daemon.h>
|
|
|
|
#include <utils.h>
|
|
|
|
|
2018-09-16 10:16:18 +02:00
|
|
|
#include "su.h"
|
|
|
|
|
2019-01-26 20:53:49 +01:00
|
|
|
#define BROADCAST_BOOT_COMPLETED \
|
|
|
|
"/system/bin/app_process", "/system/bin", "com.android.commands.am.Am", \
|
|
|
|
"broadcast", nullptr, nullptr, "-a", "android.intent.action.BOOT_COMPLETED", \
|
|
|
|
"-f", "0x00000020"
|
2018-09-16 10:16:18 +02:00
|
|
|
|
2018-12-28 09:03:23 +01:00
|
|
|
static inline const char *get_command(const struct su_request *to) {
|
2018-10-04 10:59:51 +02:00
|
|
|
if (to->command[0])
|
2018-09-16 10:16:18 +02:00
|
|
|
return to->command;
|
2018-10-04 10:59:51 +02:00
|
|
|
if (to->shell[0])
|
2018-09-16 10:16:18 +02:00
|
|
|
return to->shell;
|
|
|
|
return DEFAULT_SHELL;
|
|
|
|
}
|
|
|
|
|
2019-01-26 20:53:49 +01:00
|
|
|
static void silent_run(const char **args, struct su_info *info) {
|
|
|
|
char component[128];
|
|
|
|
if (SDK_INT >= 22) {
|
|
|
|
args[4] = "-p";
|
|
|
|
args[5] = info->str[SU_MANAGER];
|
|
|
|
} else {
|
|
|
|
sprintf(component, "%s/a.h", info->str[SU_MANAGER]);
|
|
|
|
args[4] = "-n";
|
|
|
|
args[5] = component;
|
|
|
|
}
|
|
|
|
exec_t exec {
|
|
|
|
.pre_exec = []() -> void {
|
|
|
|
int null = xopen("/dev/null", O_WRONLY | O_CLOEXEC);
|
|
|
|
dup2(null, STDOUT_FILENO);
|
|
|
|
dup2(null, STDERR_FILENO);
|
|
|
|
setenv("CLASSPATH", "/system/framework/am.jar", 1);
|
|
|
|
},
|
|
|
|
.fork = fork_dont_care,
|
|
|
|
.argv = args
|
|
|
|
};
|
|
|
|
exec_command(exec);
|
2018-09-16 10:16:18 +02:00
|
|
|
}
|
|
|
|
|
2018-10-04 10:59:51 +02:00
|
|
|
static void setup_user(char *user, struct su_info *info) {
|
2018-11-05 00:24:08 +01:00
|
|
|
switch (info->cfg[SU_MULTIUSER_MODE]) {
|
2018-10-04 07:49:52 +02:00
|
|
|
case MULTIUSER_MODE_OWNER_ONLY:
|
|
|
|
case MULTIUSER_MODE_OWNER_MANAGED:
|
|
|
|
sprintf(user, "%d", 0);
|
|
|
|
break;
|
|
|
|
case MULTIUSER_MODE_USER:
|
2018-10-04 10:59:51 +02:00
|
|
|
sprintf(user, "%d", info->uid / 100000);
|
2018-10-04 07:49:52 +02:00
|
|
|
break;
|
2018-09-16 10:16:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 10:59:51 +02:00
|
|
|
void app_log(struct su_context *ctx) {
|
2018-09-16 10:16:18 +02:00
|
|
|
char user[8];
|
2018-10-04 10:59:51 +02:00
|
|
|
setup_user(user, ctx->info);
|
2018-09-16 10:16:18 +02:00
|
|
|
|
|
|
|
char fromUid[8];
|
|
|
|
sprintf(fromUid, "%d",
|
2018-11-05 00:24:08 +01:00
|
|
|
ctx->info->cfg[SU_MULTIUSER_MODE] == MULTIUSER_MODE_OWNER_MANAGED ?
|
2018-10-04 10:59:51 +02:00
|
|
|
ctx->info->uid % 100000 : ctx->info->uid);
|
2018-09-16 10:16:18 +02:00
|
|
|
|
|
|
|
char toUid[8];
|
2018-10-04 10:59:51 +02:00
|
|
|
sprintf(toUid, "%d", ctx->req.uid);
|
2018-09-16 10:16:18 +02:00
|
|
|
|
|
|
|
char pid[8];
|
2018-10-04 10:59:51 +02:00
|
|
|
sprintf(pid, "%d", ctx->pid);
|
2018-09-16 10:16:18 +02:00
|
|
|
|
|
|
|
char policy[2];
|
2018-10-04 10:59:51 +02:00
|
|
|
sprintf(policy, "%d", ctx->info->access.policy);
|
2018-09-16 10:16:18 +02:00
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
const char *cmd[] = {
|
2019-01-26 20:53:49 +01:00
|
|
|
BROADCAST_BOOT_COMPLETED,
|
2018-09-16 10:16:18 +02:00
|
|
|
"--user", user,
|
|
|
|
"--es", "action", "log",
|
|
|
|
"--ei", "from.uid", fromUid,
|
|
|
|
"--ei", "to.uid", toUid,
|
|
|
|
"--ei", "pid", pid,
|
|
|
|
"--ei", "policy", policy,
|
2018-10-04 10:59:51 +02:00
|
|
|
"--es", "command", get_command(&ctx->req),
|
2018-10-28 03:06:24 +01:00
|
|
|
"--ez", "notify", ctx->info->access.notify ? "true" : "false",
|
2018-11-04 09:38:06 +01:00
|
|
|
nullptr
|
2018-09-16 10:16:18 +02:00
|
|
|
};
|
2019-01-26 20:53:49 +01:00
|
|
|
silent_run(cmd, ctx->info);
|
2018-09-16 10:16:18 +02:00
|
|
|
}
|
|
|
|
|
2018-10-28 03:06:24 +01:00
|
|
|
void app_notify(struct su_context *ctx) {
|
|
|
|
char user[8];
|
|
|
|
setup_user(user, ctx->info);
|
|
|
|
|
|
|
|
char fromUid[8];
|
|
|
|
sprintf(fromUid, "%d",
|
2018-11-05 00:24:08 +01:00
|
|
|
ctx->info->cfg[SU_MULTIUSER_MODE] == MULTIUSER_MODE_OWNER_MANAGED ?
|
2018-10-28 03:06:24 +01:00
|
|
|
ctx->info->uid % 100000 : ctx->info->uid);
|
|
|
|
|
|
|
|
char policy[2];
|
|
|
|
sprintf(policy, "%d", ctx->info->access.policy);
|
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
const char *cmd[] = {
|
2019-01-26 20:53:49 +01:00
|
|
|
BROADCAST_BOOT_COMPLETED,
|
|
|
|
"--user", user,
|
|
|
|
"--es", "action", "notify",
|
|
|
|
"--ei", "from.uid", fromUid,
|
|
|
|
"--ei", "policy", policy,
|
|
|
|
nullptr
|
2018-10-28 03:06:24 +01:00
|
|
|
};
|
2019-01-26 20:53:49 +01:00
|
|
|
silent_run(cmd, ctx->info);
|
2018-10-28 03:06:24 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 10:59:51 +02:00
|
|
|
void app_connect(const char *socket, struct su_info *info) {
|
2018-09-16 10:16:18 +02:00
|
|
|
char user[8];
|
2018-10-04 10:59:51 +02:00
|
|
|
setup_user(user, info);
|
2019-01-26 20:53:49 +01:00
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
const char *cmd[] = {
|
2019-01-26 20:53:49 +01:00
|
|
|
BROADCAST_BOOT_COMPLETED,
|
2018-09-16 10:16:18 +02:00
|
|
|
"--user", user,
|
|
|
|
"--es", "action", "request",
|
2018-11-04 09:38:06 +01:00
|
|
|
"--es", "socket", socket,
|
|
|
|
nullptr
|
2018-09-16 10:16:18 +02:00
|
|
|
};
|
2019-01-26 20:53:49 +01:00
|
|
|
silent_run(cmd, info);
|
2018-09-16 10:16:18 +02:00
|
|
|
}
|
|
|
|
|
2018-10-04 10:59:51 +02:00
|
|
|
void socket_send_request(int fd, struct su_info *info) {
|
|
|
|
write_key_token(fd, "uid", info->uid);
|
2018-09-16 10:16:18 +02:00
|
|
|
write_string_be(fd, "eof");
|
|
|
|
}
|