Magisk/native/jni/su/connect.cpp

120 lines
2.6 KiB
C++
Raw Normal View History

#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>
#include "su.h"
#define START_ACTIVITY \
"/system/bin/app_process", "/system/bin", "com.android.commands.am.Am", \
"start", "-n", nullptr, "--user", nullptr, "-f", "0x18000020", "-a"
// 0x18000020 = FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_MULTIPLE_TASK|FLAG_INCLUDE_STOPPED_PACKAGES
2019-04-30 03:26:43 +02:00
static inline const char *get_command(const su_request *to) {
2018-10-04 10:59:51 +02:00
if (to->command[0])
return to->command;
2018-10-04 10:59:51 +02:00
if (to->shell[0])
return to->shell;
return DEFAULT_SHELL;
}
2019-04-30 03:26:43 +02:00
static inline void get_user(char *user, su_info *info) {
sprintf(user, "%d",
info->cfg[SU_MULTIUSER_MODE] == MULTIUSER_MODE_USER
? info->uid / 100000
: 0);
}
2019-04-30 03:26:43 +02:00
static inline void get_uid(char *uid, su_info *info) {
sprintf(uid, "%d",
info->cfg[SU_MULTIUSER_MODE] == MULTIUSER_MODE_OWNER_MANAGED
? info->uid % 100000
: info->uid);
}
2019-04-30 03:26:43 +02:00
static void silent_run(const char **args, su_info *info) {
char component[128];
sprintf(component, "%s/a.m", info->str[SU_MANAGER].data());
char user[8];
get_user(user, info);
/* Fill in dynamic arguments */
2019-02-01 18:27:51 +01:00
args[5] = component;
args[7] = user;
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);
}
2019-04-30 03:26:43 +02:00
void app_log(su_context *ctx) {
char fromUid[8];
get_uid(fromUid, ctx->info);
char toUid[8];
2018-10-04 10:59:51 +02:00
sprintf(toUid, "%d", ctx->req.uid);
char pid[8];
2018-10-04 10:59:51 +02:00
sprintf(pid, "%d", ctx->pid);
char policy[2];
2018-10-04 10:59:51 +02:00
sprintf(policy, "%d", ctx->info->access.policy);
2018-11-04 09:38:06 +01:00
const char *cmd[] = {
START_ACTIVITY, "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
};
silent_run(cmd, ctx->info);
}
2019-04-30 03:26:43 +02:00
void app_notify(su_context *ctx) {
2018-10-28 03:06:24 +01:00
char fromUid[8];
get_uid(fromUid, ctx->info);
2018-10-28 03:06:24 +01:00
char policy[2];
sprintf(policy, "%d", ctx->info->access.policy);
2018-11-04 09:38:06 +01:00
const char *cmd[] = {
START_ACTIVITY, "notify",
"--ei", "from.uid", fromUid,
"--ei", "policy", policy,
nullptr
2018-10-28 03:06:24 +01:00
};
silent_run(cmd, ctx->info);
2018-10-28 03:06:24 +01:00
}
2019-04-30 03:26:43 +02:00
void app_connect(const char *socket, su_info *info) {
2018-11-04 09:38:06 +01:00
const char *cmd[] = {
START_ACTIVITY, "request",
2018-11-04 09:38:06 +01:00
"--es", "socket", socket,
nullptr
};
silent_run(cmd, info);
}
2019-04-30 03:26:43 +02:00
void socket_send_request(int fd, su_info *info) {
2018-10-04 10:59:51 +02:00
write_key_token(fd, "uid", info->uid);
write_string_be(fd, "eof");
}