Magisk/native/jni/su/activity.c

142 lines
3.4 KiB
C
Raw Normal View History

/*
2017-01-31 22:59:48 +01: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 <stdio.h>
2017-04-14 21:21:31 +02:00
#include "magisk.h"
#include "su.h"
/* intent actions */
2017-10-28 10:03:39 +02:00
#define ACTION_REQUEST "%s/" REQUESTOR_PREFIX ".RequestActivity"
#define ACTION_RESULT "%s/" REQUESTOR_PREFIX ".SuReceiver"
#define AM_PATH "/system/bin/app_process", "/system/bin", "com.android.commands.am.Am"
2017-09-15 09:23:50 +02:00
static char *get_command(const struct su_request *to) {
if (to->command)
return to->command;
if (to->shell)
return to->shell;
return DEFAULT_SHELL;
}
2017-04-14 21:21:31 +02:00
static void silent_run(char* const args[]) {
set_identity(0);
2017-05-26 20:40:12 +02:00
if (fork())
2017-04-14 21:21:31 +02: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-27 19:28:18 +02:00
static int setup_user(struct su_context *ctx, char* user) {
switch (ctx->info->dbs.v[SU_MULTIUSER_MODE]) {
2017-05-26 20:40:12 +02:00
case MULTIUSER_MODE_OWNER_ONLY: /* Should already be denied if not owner */
case MULTIUSER_MODE_OWNER_MANAGED:
sprintf(user, "%d", 0);
2017-09-15 12:01:31 +02:00
return ctx->info->uid / 100000;
2017-05-26 20:40:12 +02:00
case MULTIUSER_MODE_USER:
2017-09-15 12:01:31 +02:00
sprintf(user, "%d", ctx->info->uid / 100000);
2017-05-26 20:40:12 +02:00
break;
2017-04-14 21:21:31 +02:00
}
2017-05-27 19:28:18 +02:00
return 0;
}
2017-04-14 21:21:31 +02:00
void app_send_result(struct su_context *ctx, policy_t policy) {
2017-05-26 20:40:12 +02:00
char fromUid[16];
if (ctx->info->dbs.v[SU_MULTIUSER_MODE] == MULTIUSER_MODE_OWNER_MANAGED)
sprintf(fromUid, "%d", ctx->info->uid % 100000);
else
sprintf(fromUid, "%d", ctx->info->uid);
2017-04-14 21:21:31 +02:00
2017-05-26 20:40:12 +02:00
char toUid[16];
2017-04-14 21:21:31 +02:00
sprintf(toUid, "%d", ctx->to.uid);
2017-05-26 20:40:12 +02:00
char pid[16];
2017-05-31 21:19:45 +02:00
sprintf(pid, "%d", ctx->pid);
2017-04-14 21:21:31 +02:00
2017-05-26 20:40:12 +02:00
char user[16];
2017-05-27 19:28:18 +02:00
int notify = setup_user(ctx, user);
2017-04-14 21:21:31 +02:00
2017-10-28 10:03:39 +02:00
char activity[128];
2018-06-13 19:44:16 +02:00
sprintf(activity, ACTION_RESULT, ctx->info->str.s[SU_MANAGER]);
2017-10-28 10:03:39 +02:00
2017-05-27 19:28:18 +02:00
// Send notice to manager, enable logging
2017-04-14 21:21:31 +02:00
char *result_command[] = {
2017-10-28 10:03:39 +02:00
AM_PATH, "broadcast", "-n",
activity,
"--user", user,
"--ei", "mode", "0",
"--ei", "from.uid", fromUid,
"--ei", "to.uid", toUid,
"--ei", "pid", pid,
"--es", "command", get_command(&ctx->to),
"--es", "action", policy == ALLOW ? "allow" : "deny",
2017-04-14 21:21:31 +02:00
NULL
};
silent_run(result_command);
2017-05-27 19:28:18 +02:00
// Send notice to user (if needed) to create toasts
if (notify) {
sprintf(fromUid, "%d", ctx->info->uid);
2017-05-27 19:28:18 +02:00
sprintf(user, "%d", notify);
char *notify_command[] = {
2017-10-28 10:03:39 +02:00
AM_PATH, "broadcast", "-n",
activity,
"--user", user,
"--ei", "mode", "1",
"--ei", "from.uid", fromUid,
"--es", "action", policy == ALLOW ? "allow" : "deny",
2017-05-27 19:28:18 +02:00
NULL
};
silent_run(notify_command);
}
}
2017-04-14 21:21:31 +02:00
void app_send_request(struct su_context *ctx) {
2017-05-27 19:28:18 +02:00
char user[16];
int notify = setup_user(ctx, user);
2017-04-14 21:21:31 +02:00
2017-10-28 10:03:39 +02:00
char activity[128];
2018-06-13 19:44:16 +02:00
sprintf(activity, ACTION_REQUEST, ctx->info->str.s[SU_MANAGER]);
2017-10-28 10:03:39 +02:00
2017-04-14 21:21:31 +02:00
char *request_command[] = {
2017-10-28 10:03:39 +02:00
AM_PATH, "start", "-n",
activity,
"--user", user,
"--es", "socket", ctx->sock_path,
2017-10-28 10:03:39 +02:00
"--ez", "timeout", notify ? "false" : "true",
2017-04-14 21:21:31 +02:00
NULL
};
silent_run(request_command);
2017-05-27 19:28:18 +02:00
// Send notice to user to tell them root is managed by owner
if (notify) {
sprintf(user, "%d", notify);
2018-06-13 19:44:16 +02:00
sprintf(activity, ACTION_RESULT, ctx->info->str.s[SU_MANAGER]);
2017-05-27 19:28:18 +02:00
char *notify_command[] = {
2017-10-28 10:03:39 +02:00
AM_PATH, "broadcast", "-n",
activity,
"--user", user,
"--ei", "mode", "2",
2017-05-27 19:28:18 +02:00
NULL
};
silent_run(notify_command);
}
}