2018-09-16 04:16:18 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2019-02-10 03:57:51 -05:00
|
|
|
#include <daemon.h>
|
|
|
|
#include <utils.h>
|
|
|
|
|
2018-09-16 04:16:18 -04:00
|
|
|
#include "su.h"
|
|
|
|
|
2019-07-07 00:31:49 -07:00
|
|
|
using namespace std;
|
|
|
|
|
2019-05-13 02:01:10 -07:00
|
|
|
bool CONNECT_BROADCAST;
|
|
|
|
|
2019-04-10 23:35:31 -04:00
|
|
|
#define START_ACTIVITY \
|
2019-01-26 14:53:49 -05:00
|
|
|
"/system/bin/app_process", "/system/bin", "com.android.commands.am.Am", \
|
2019-04-10 23:35:31 -04:00
|
|
|
"start", "-n", nullptr, "--user", nullptr, "-f", "0x18000020", "-a"
|
|
|
|
|
|
|
|
// 0x18000020 = FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_MULTIPLE_TASK|FLAG_INCLUDE_STOPPED_PACKAGES
|
2018-09-16 04:16:18 -04:00
|
|
|
|
2019-05-13 02:01:10 -07:00
|
|
|
#define START_BROADCAST \
|
|
|
|
"/system/bin/app_process", "/system/bin", "com.android.commands.am.Am", \
|
|
|
|
"broadcast", "-n", nullptr, "--user", nullptr, "-f", "0x00000020", \
|
|
|
|
"-a", "android.intent.action.REBOOT", "--es", "action"
|
|
|
|
|
|
|
|
// 0x00000020 = FLAG_INCLUDE_STOPPED_PACKAGES
|
|
|
|
|
2019-04-29 21:26:43 -04:00
|
|
|
static inline const char *get_command(const 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;
|
|
|
|
}
|
|
|
|
|
2019-07-07 00:31:49 -07:00
|
|
|
static inline void get_user(char *user, const su_info *info) {
|
2019-04-10 23:35:31 -04:00
|
|
|
sprintf(user, "%d",
|
|
|
|
info->cfg[SU_MULTIUSER_MODE] == MULTIUSER_MODE_USER
|
|
|
|
? info->uid / 100000
|
|
|
|
: 0);
|
|
|
|
}
|
|
|
|
|
2019-07-07 00:31:49 -07:00
|
|
|
static inline void get_uid(char *uid, const su_info *info) {
|
2019-04-10 23:35:31 -04:00
|
|
|
sprintf(uid, "%d",
|
|
|
|
info->cfg[SU_MULTIUSER_MODE] == MULTIUSER_MODE_OWNER_MANAGED
|
|
|
|
? info->uid % 100000
|
|
|
|
: info->uid);
|
|
|
|
}
|
|
|
|
|
2019-07-07 00:31:49 -07:00
|
|
|
static void exec_am_cmd(const char **args, const su_info *info) {
|
2019-01-26 14:53:49 -05:00
|
|
|
char component[128];
|
2019-05-13 02:01:10 -07:00
|
|
|
sprintf(component, "%s/%s", info->str[SU_MANAGER].data(), args[3][0] == 'b' ? "a.h" : "a.m");
|
2019-04-10 23:35:31 -04:00
|
|
|
char user[8];
|
|
|
|
get_user(user, info);
|
|
|
|
|
|
|
|
/* Fill in dynamic arguments */
|
2019-02-02 01:27:51 +08:00
|
|
|
args[5] = component;
|
2019-04-10 23:35:31 -04:00
|
|
|
args[7] = user;
|
|
|
|
|
2019-01-26 14:53:49 -05:00
|
|
|
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 04:16:18 -04:00
|
|
|
}
|
|
|
|
|
2019-05-13 02:01:10 -07:00
|
|
|
#define LOG_BODY \
|
|
|
|
"log", \
|
|
|
|
"--ei", "from.uid", fromUid, \
|
|
|
|
"--ei", "to.uid", toUid, \
|
|
|
|
"--ei", "pid", pid, \
|
|
|
|
"--ei", "policy", policy, \
|
2019-07-07 00:31:49 -07:00
|
|
|
"--es", "command", get_command(&ctx.req), \
|
|
|
|
"--ez", "notify", ctx.info->access.notify ? "true" : "false", \
|
2019-05-13 02:01:10 -07:00
|
|
|
nullptr
|
|
|
|
|
2019-07-07 00:31:49 -07:00
|
|
|
void app_log(const su_context &ctx) {
|
2018-09-16 04:16:18 -04:00
|
|
|
char fromUid[8];
|
2019-07-07 00:31:49 -07:00
|
|
|
get_uid(fromUid, ctx.info.get());
|
2018-09-16 04:16:18 -04:00
|
|
|
|
|
|
|
char toUid[8];
|
2019-07-07 00:31:49 -07:00
|
|
|
sprintf(toUid, "%d", ctx.req.uid);
|
2018-09-16 04:16:18 -04:00
|
|
|
|
|
|
|
char pid[8];
|
2019-07-07 00:31:49 -07:00
|
|
|
sprintf(pid, "%d", ctx.pid);
|
2018-09-16 04:16:18 -04:00
|
|
|
|
|
|
|
char policy[2];
|
2019-07-07 00:31:49 -07:00
|
|
|
sprintf(policy, "%d", ctx.info->access.policy);
|
2018-09-16 04:16:18 -04:00
|
|
|
|
2019-05-13 02:01:10 -07:00
|
|
|
if (CONNECT_BROADCAST) {
|
|
|
|
const char *cmd[] = { START_BROADCAST, LOG_BODY };
|
2019-07-07 00:31:49 -07:00
|
|
|
exec_am_cmd(cmd, ctx.info.get());
|
2019-05-13 02:01:10 -07:00
|
|
|
} else {
|
|
|
|
const char *cmd[] = { START_ACTIVITY, LOG_BODY };
|
2019-07-07 00:31:49 -07:00
|
|
|
exec_am_cmd(cmd, ctx.info.get());
|
2019-05-13 02:01:10 -07:00
|
|
|
}
|
2018-09-16 04:16:18 -04:00
|
|
|
}
|
|
|
|
|
2019-05-13 02:01:10 -07:00
|
|
|
#define NOTIFY_BODY \
|
|
|
|
"notify", \
|
|
|
|
"--ei", "from.uid", fromUid, \
|
|
|
|
"--ei", "policy", policy, \
|
|
|
|
nullptr
|
|
|
|
|
2019-07-07 00:31:49 -07:00
|
|
|
void app_notify(const su_context &ctx) {
|
2018-10-27 22:06:24 -04:00
|
|
|
char fromUid[8];
|
2019-07-07 00:31:49 -07:00
|
|
|
get_uid(fromUid, ctx.info.get());
|
2018-10-27 22:06:24 -04:00
|
|
|
|
|
|
|
char policy[2];
|
2019-07-07 00:31:49 -07:00
|
|
|
sprintf(policy, "%d", ctx.info->access.policy);
|
2018-10-27 22:06:24 -04:00
|
|
|
|
2019-05-13 02:01:10 -07:00
|
|
|
if (CONNECT_BROADCAST) {
|
|
|
|
const char *cmd[] = { START_BROADCAST, NOTIFY_BODY };
|
2019-07-07 00:31:49 -07:00
|
|
|
exec_am_cmd(cmd, ctx.info.get());
|
2019-05-13 02:01:10 -07:00
|
|
|
} else {
|
|
|
|
const char *cmd[] = { START_ACTIVITY, NOTIFY_BODY };
|
2019-07-07 00:31:49 -07:00
|
|
|
exec_am_cmd(cmd, ctx.info.get());
|
2019-05-13 02:01:10 -07:00
|
|
|
}
|
|
|
|
|
2018-10-27 22:06:24 -04:00
|
|
|
}
|
|
|
|
|
2019-07-07 00:31:49 -07:00
|
|
|
void app_connect(const char *socket, const shared_ptr<su_info> &info) {
|
2018-11-04 03:38:06 -05:00
|
|
|
const char *cmd[] = {
|
2019-04-10 23:35:31 -04:00
|
|
|
START_ACTIVITY, "request",
|
2018-11-04 03:38:06 -05:00
|
|
|
"--es", "socket", socket,
|
|
|
|
nullptr
|
2018-09-16 04:16:18 -04:00
|
|
|
};
|
2019-07-07 00:31:49 -07:00
|
|
|
exec_am_cmd(cmd, info.get());
|
2019-05-13 02:01:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void broadcast_test() {
|
|
|
|
su_info info;
|
|
|
|
get_db_settings(info.cfg);
|
|
|
|
get_db_strings(info.str);
|
|
|
|
validate_manager(info.str[SU_MANAGER], 0, &info.mgr_st);
|
|
|
|
|
|
|
|
const char *cmd[] = { START_BROADCAST, "test", nullptr };
|
|
|
|
exec_am_cmd(cmd, &info);
|
2018-09-16 04:16:18 -04:00
|
|
|
}
|
|
|
|
|
2019-07-07 00:31:49 -07:00
|
|
|
void socket_send_request(int fd, const shared_ptr<su_info> &info) {
|
2018-10-04 04:59:51 -04:00
|
|
|
write_key_token(fd, "uid", info->uid);
|
2018-09-16 04:16:18 -04:00
|
|
|
write_string_be(fd, "eof");
|
|
|
|
}
|