Magisk/native/jni/su/su.c

260 lines
6.4 KiB
C
Raw Normal View History

/*
2017-04-14 21:21:31 +02:00
* Copyright 2017, John Wu (@topjohnwu)
* Copyright 2015, Pierre-Hugues Husson <phh@phh.me>
* Copyright 2010, Adam Shanks (@ChainsDD)
* Copyright 2008, Zinx Verituse (@zinxv)
*/
/* su.c - The main function running in the daemon
*/
2017-07-07 19:12:47 +02:00
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
2017-04-14 21:21:31 +02:00
#include <string.h>
#include <unistd.h>
#include <getopt.h>
2017-04-14 21:21:31 +02:00
#include <fcntl.h>
#include <pwd.h>
2017-04-14 21:21:31 +02:00
#include <errno.h>
#include <signal.h>
2017-07-07 19:12:47 +02:00
#include <sched.h>
#include <libgen.h>
#include <sys/types.h>
2017-04-14 21:21:31 +02:00
#include <sys/stat.h>
#include <selinux/selinux.h>
2017-04-14 21:21:31 +02:00
#include "magisk.h"
#include "daemon.h"
#include "utils.h"
2017-04-14 21:21:31 +02:00
#include "su.h"
struct su_context *su_ctx;
2017-04-14 21:21:31 +02:00
static void usage(int status) {
FILE *stream = (status == EXIT_SUCCESS) ? stdout : stderr;
fprintf(stream,
2017-07-08 17:50:47 +02:00
"MagiskSU v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ")\n\n"
"Usage: su [options] [-] [user [argument...]]\n\n"
2017-04-14 21:21:31 +02:00
"Options:\n"
" -c, --command COMMAND pass COMMAND to the invoked shell\n"
" -h, --help display this help message and exit\n"
" -, -l, --login pretend the shell to be a login shell\n"
" -m, -p,\n"
" --preserve-environment preserve the entire environment\n"
2017-04-14 21:21:31 +02:00
" -s, --shell SHELL use SHELL instead of the default " DEFAULT_SHELL "\n"
" -v, --version display version number and exit\n"
" -V display version code and exit\n"
2017-07-07 19:12:47 +02:00
" -mm, -M,\n"
" --mount-master force run in the global mount namespace\n");
exit2(status);
}
2017-04-14 21:21:31 +02:00
static char *concat_commands(int argc, char *argv[]) {
char command[ARG_MAX];
command[0] = '\0';
for (int i = optind - 1; i < argc; ++i) {
if (command[0])
2017-04-14 21:21:31 +02:00
sprintf(command, "%s %s", command, argv[i]);
else
strcpy(command, argv[i]);
2017-04-14 21:21:31 +02:00
}
return strdup(command);
}
static void populate_environment() {
2017-04-14 21:21:31 +02:00
struct passwd *pw;
if (su_ctx->to.keepenv)
2017-04-14 21:21:31 +02:00
return;
pw = getpwuid(su_ctx->to.uid);
2017-04-14 21:21:31 +02:00
if (pw) {
setenv("HOME", pw->pw_dir, 1);
if (su_ctx->to.shell)
setenv("SHELL", su_ctx->to.shell, 1);
2017-04-14 21:21:31 +02:00
else
setenv("SHELL", DEFAULT_SHELL, 1);
if (su_ctx->to.login || su_ctx->to.uid) {
2017-04-14 21:21:31 +02:00
setenv("USER", pw->pw_name, 1);
setenv("LOGNAME", pw->pw_name, 1);
}
}
}
2017-09-15 09:23:50 +02:00
void set_identity(unsigned uid) {
/*
* Set effective uid back to root, otherwise setres[ug]id will fail
* if uid isn't root.
*/
if (seteuid(0)) {
PLOGE("seteuid (root)");
}
if (setresgid(uid, uid, uid)) {
PLOGE("setresgid (%u)", uid);
}
if (setresuid(uid, uid, uid)) {
PLOGE("setresuid (%u)", uid);
}
}
2017-04-14 21:21:31 +02:00
static __attribute__ ((noreturn)) void allow() {
char* argv[] = { NULL, NULL, NULL, NULL };
2017-04-14 21:21:31 +02:00
if (su_ctx->to.login)
argv[0] = "-";
else
argv[0] = basename(su_ctx->to.shell);
2017-04-14 21:21:31 +02:00
if (su_ctx->to.command) {
argv[1] = "-c";
argv[2] = su_ctx->to.command;
2017-04-14 21:21:31 +02:00
}
// Setup shell
umask(022);
populate_environment();
2017-04-14 21:21:31 +02:00
set_identity(su_ctx->to.uid);
2017-09-15 09:23:50 +02:00
if (su_ctx->info->access.notify || su_ctx->info->access.log)
app_log();
execvp(su_ctx->to.shell, argv);
fprintf(stderr, "Cannot execute %s: %s\n", su_ctx->to.shell, strerror(errno));
2017-04-14 21:21:31 +02:00
PLOGE("exec");
exit(EXIT_FAILURE);
}
2017-04-14 21:21:31 +02:00
static __attribute__ ((noreturn)) void deny() {
if (su_ctx->info->access.notify || su_ctx->info->access.log)
app_log();
LOGW("su: request rejected (%u->%u)", su_ctx->info->uid, su_ctx->to.uid);
2017-04-14 21:21:31 +02:00
fprintf(stderr, "%s\n", strerror(EACCES));
exit(EXIT_FAILURE);
}
__attribute__ ((noreturn)) void exit2(int status) {
// Handle the pipe, or the daemon will get stuck
if (su_ctx->pipefd[0] >= 0) {
2018-06-26 11:33:16 +02:00
xwrite(su_ctx->pipefd[1], &su_ctx->info->access.policy, sizeof(policy_t));
2017-10-28 10:03:39 +02:00
close(su_ctx->pipefd[0]);
close(su_ctx->pipefd[1]);
}
exit(status);
2017-01-30 19:51:22 +01:00
}
2017-04-14 21:21:31 +02:00
int su_daemon_main(int argc, char **argv) {
int c;
2017-04-14 21:21:31 +02:00
struct option long_opts[] = {
{ "command", required_argument, NULL, 'c' },
{ "help", no_argument, NULL, 'h' },
{ "login", no_argument, NULL, 'l' },
{ "preserve-environment", no_argument, NULL, 'p' },
{ "shell", required_argument, NULL, 's' },
{ "version", no_argument, NULL, 'v' },
{ "context", required_argument, NULL, 'z' },
2017-07-07 19:12:47 +02:00
{ "mount-master", no_argument, NULL, 'M' },
2017-04-14 21:21:31 +02:00
{ NULL, 0, NULL, 0 },
};
2017-07-07 19:12:47 +02:00
while ((c = getopt_long(argc, argv, "c:hlmps:Vvuz:M", long_opts, NULL)) != -1) {
2017-04-14 21:21:31 +02:00
switch (c) {
case 'c':
su_ctx->to.command = concat_commands(argc, argv);
2017-04-14 21:21:31 +02:00
optind = argc;
break;
case 'h':
usage(EXIT_SUCCESS);
break;
case 'l':
su_ctx->to.login = 1;
2017-04-14 21:21:31 +02:00
break;
case 'm':
case 'p':
su_ctx->to.keepenv = 1;
2017-04-14 21:21:31 +02:00
break;
case 's':
su_ctx->to.shell = optarg;
2017-04-14 21:21:31 +02:00
break;
case 'V':
2017-04-18 15:09:53 +02:00
printf("%d\n", MAGISK_VER_CODE);
exit2(EXIT_SUCCESS);
2017-04-14 21:21:31 +02:00
case 'v':
2017-04-18 15:09:53 +02:00
printf("%s\n", MAGISKSU_VER_STR);
exit2(EXIT_SUCCESS);
2017-04-14 21:21:31 +02:00
case 'z':
// Do nothing, placed here for legacy support :)
break;
2017-07-07 19:12:47 +02:00
case 'M':
su_ctx->info->dbs.v[SU_MNT_NS] = NAMESPACE_MODE_GLOBAL;
2017-07-07 19:12:47 +02:00
break;
2017-04-14 21:21:31 +02:00
default:
/* Bionic getopt_long doesn't terminate its error output by newline */
fprintf(stderr, "\n");
usage(2);
}
}
if (optind < argc && strcmp(argv[optind], "-") == 0) {
su_ctx->to.login = 1;
2017-04-14 21:21:31 +02:00
optind++;
}
/* username or uid */
if (optind < argc) {
2017-04-14 21:21:31 +02:00
struct passwd *pw;
pw = getpwnam(argv[optind]);
if (pw)
su_ctx->to.uid = pw->pw_uid;
else
su_ctx->to.uid = atoi(argv[optind]);
2017-04-14 21:21:31 +02:00
optind++;
}
2017-07-07 19:12:47 +02:00
// Handle namespaces
switch (su_ctx->info->dbs.v[SU_MNT_NS]) {
2017-07-07 19:12:47 +02:00
case NAMESPACE_MODE_GLOBAL:
LOGD("su: use global namespace\n");
break;
case NAMESPACE_MODE_REQUESTER:
LOGD("su: use namespace of pid=[%d]\n", su_ctx->pid);
if (switch_mnt_ns(su_ctx->pid)) {
LOGD("su: setns failed, fallback to isolated\n");
2018-07-13 16:10:24 +02:00
xunshare(CLONE_NEWNS);
2017-07-07 19:12:47 +02:00
}
break;
case NAMESPACE_MODE_ISOLATE:
LOGD("su: use new isolated namespace\n");
2018-07-13 16:10:24 +02:00
xunshare(CLONE_NEWNS);
2017-07-07 19:12:47 +02:00
break;
}
// Change directory to cwd
chdir(su_ctx->cwd);
if (su_ctx->pipefd[0] >= 0) {
// Create random socket
struct sockaddr_un addr;
int sockfd = create_rand_socket(&addr);
2017-04-14 21:21:31 +02:00
// Connect Magisk Manager
app_connect(addr.sun_path + 1);
int fd = socket_accept(sockfd, 60);
2017-04-14 21:21:31 +02:00
socket_send_request(fd);
su_ctx->info->access.policy = read_int_be(fd);
2017-04-14 21:21:31 +02:00
close(fd);
close(sockfd);
2017-04-14 21:21:31 +02:00
// Report the policy to main daemon
xwrite(su_ctx->pipefd[1], &su_ctx->info->access.policy, sizeof(policy_t));
2017-10-28 10:03:39 +02:00
close(su_ctx->pipefd[0]);
close(su_ctx->pipefd[1]);
}
2017-04-14 21:21:31 +02:00
su_ctx->info->access.policy == ALLOW ? allow() : deny();
}