Magisk/activity.c

109 lines
2.2 KiB
C
Raw Normal View History

/*
2017-01-31 21:59:48 +00:00
** Copyright 2017, 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 <paths.h>
#include <stdio.h>
#include <stdarg.h>
2017-04-14 19:21:31 +00:00
#include "magisk.h"
#include "su.h"
/* intent actions */
#define ACTION_REQUEST "start", "-n", REQUESTOR "/" REQUESTOR_PREFIX ".RequestActivity"
#define ACTION_NOTIFY "start", "-n", REQUESTOR "/" REQUESTOR_PREFIX ".NotifyActivity"
#define ACTION_RESULT "broadcast", "-n", REQUESTOR "/" REQUESTOR_PREFIX ".SuReceiver"
#define AM_PATH "/system/bin/app_process", "/system/bin", "com.android.commands.am.Am"
2017-04-14 19:21:31 +00:00
static void silent_run(char* const args[]) {
set_identity(0);
2017-05-26 18:40:12 +00:00
if (fork())
2017-04-14 19:21:31 +00:00
return;
int zero = open("/dev/zero", O_RDONLY | O_CLOEXEC);
dup2(zero, 0);
int null = open("/dev/null", O_WRONLY | O_CLOEXEC);
dup2(null, 1);
dup2(null, 2);
setenv("CLASSPATH", "/system/framework/am.jar", 1);
execv(args[0], args);
PLOGE("exec am");
_exit(EXIT_FAILURE);
}
2017-05-26 18:40:12 +00:00
static void setup_user(struct su_context *ctx, char* user) {
switch (ctx->user.multiuser_mode) {
case MULTIUSER_MODE_OWNER_ONLY: /* Should already be denied if not owner */
case MULTIUSER_MODE_OWNER_MANAGED:
sprintf(user, "%d", 0);
break;
case MULTIUSER_MODE_USER:
sprintf(user, "%d", ctx->user.android_user_id);
break;
2017-04-14 19:21:31 +00:00
}
}
2017-04-14 19:21:31 +00:00
void app_send_result(struct su_context *ctx, policy_t policy) {
2017-05-26 18:40:12 +00:00
char fromUid[16];
sprintf(fromUid, "%d", ctx->from.uid);
2017-04-14 19:21:31 +00:00
2017-05-26 18:40:12 +00:00
char toUid[16];
2017-04-14 19:21:31 +00:00
sprintf(toUid, "%d", ctx->to.uid);
2017-05-26 18:40:12 +00:00
char pid[16];
2017-04-14 19:21:31 +00:00
sprintf(pid, "%d", ctx->from.pid);
2017-05-26 18:40:12 +00:00
char user[16];
setup_user(ctx, user);
2017-04-14 19:21:31 +00:00
char *result_command[] = {
AM_PATH,
ACTION_RESULT,
"--ei",
"from.uid",
2017-05-26 18:40:12 +00:00
fromUid,
2017-04-14 19:21:31 +00:00
"--ei",
"to.uid",
toUid,
"--ei",
"pid",
pid,
"--es",
"command",
get_command(&ctx->to),
"--es",
"action",
policy == ALLOW ? "allow" : "deny",
2017-05-26 18:40:12 +00:00
"--user",
2017-04-14 19:21:31 +00:00
user,
NULL
};
silent_run(result_command);
}
2017-04-14 19:21:31 +00:00
void app_send_request(struct su_context *ctx) {
char user[64];
2017-05-26 18:40:12 +00:00
setup_user(ctx, user);
2017-04-14 19:21:31 +00:00
char *request_command[] = {
AM_PATH,
ACTION_REQUEST,
"--es",
"socket",
ctx->sock_path,
2017-05-26 18:40:12 +00:00
"--user",
2017-04-14 19:21:31 +00:00
user,
NULL
};
silent_run(request_command);
}