userspace/su: add ndk compatible su from kernelnosu
Co-authored-by: nampud <nampud@users.noreply.github.com>
This commit is contained in:
@@ -1,11 +1,54 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sys/prctl.h>
|
#include <sys/prctl.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/xattr.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <termios.h>
|
||||||
|
|
||||||
// This is a simple example. If you want a full-featured "su", please use "/data/adb/ksud debug su".
|
#define KERNEL_SU_OPTION 0xDEADBEEF
|
||||||
int main(){
|
#define CMD_GRANT_ROOT 0
|
||||||
int32_t result = 0;
|
#define CMD_ENABLE_SU 15
|
||||||
prctl(0xdeadbeef, 0, 0, 0, &result);
|
|
||||||
system("/system/bin/sh");
|
int main(int argc, char **argv, char **envp) {
|
||||||
return 0;
|
unsigned long result = 0;
|
||||||
|
|
||||||
|
if (argc >= 2 && strcmp(argv[1], "--disable-sucompat") == 0) {
|
||||||
|
prctl(KERNEL_SU_OPTION, CMD_ENABLE_SU, 0L, 0L, (unsigned long)&result);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
prctl(KERNEL_SU_OPTION, CMD_GRANT_ROOT, 0L, 0L, (unsigned long)&result);
|
||||||
|
if (result != KERNEL_SU_OPTION) {
|
||||||
|
const char *error = "Access Denied: sucompat not permitted\n";
|
||||||
|
write(STDERR_FILENO, error, strlen(error));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct termios term;
|
||||||
|
if (ioctl(STDIN_FILENO, TCGETS, &term) == 0) {
|
||||||
|
char tty_path[PATH_MAX];
|
||||||
|
ssize_t len = readlink("/proc/self/fd/0", tty_path, sizeof(tty_path) - 1);
|
||||||
|
if (len > 0) {
|
||||||
|
tty_path[len] = '\0';
|
||||||
|
const char *selinux_ctx = "u:object_r:devpts:s0";
|
||||||
|
setxattr(tty_path, "security.selinux", selinux_ctx, strlen(selinux_ctx) + 1, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *default_args[] = { "/system/bin/su", NULL };
|
||||||
|
if (argc < 1 || !argv) {
|
||||||
|
argv = (char **)default_args;
|
||||||
|
} else {
|
||||||
|
argv[0] = "/system/bin/su";
|
||||||
|
}
|
||||||
|
|
||||||
|
execve("/data/adb/ksud", argv, envp);
|
||||||
|
|
||||||
|
const char *error = "Error: Failed to execve /data/adb/ksud\n";
|
||||||
|
write(STDERR_FILENO, error, strlen(error));
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user