問題は、SuperSU にsu
標準の GNU バイナリと互換性のないバイナリが含まれていることsu
です。
NDK を使用して Android 用にコンパイルsu
し、バイナリを置き換えると、問題なく動作します。
次のコードを使用できます。
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
int main (int ac, char **av, char **env) {
int iret;
if (ac == 1) { // Run /bin/bash as root
iret = setuid(0);
if (iret == -1) {
fprintf(stderr, "This binary is u-s, use chmod u+s %s as root.\n", av[0]);
exit(-1);
}
strcpy(av[0], "/bin/bash");
iret = execve(av[0], av, env);
if (iret == -1) {
fprintf(stderr, "Can't execute a shell.\n");
exit(-1);
}
} else {
struct passwd *pswd = getpwnam(av[1]);
//printf("%d\n", pswd->pw_uid);
if (pswd == NULL) {
fprintf(stderr, "User %s does not exist.\n", av[1]);
exit(-1);
}
iret = setuid(pswd->pw_uid);
if (iret == -1) {
fprintf(stderr, "This binary is u-s, use chmod u+s %s as root.\n", av[0]);
exit(-1);
}
iret = execve(av[2], (char **)&av[2], env);
if (iret == -1) {
fprintf(stderr, "%s\n", strerror(errno));
exit(-1);
}
}
return 0;
}
コンパイルして、所有者をルートに変更します。次に root で chmod u+s ./su を実行します。最後に、通常のユーザーとしてコマンドを実行します。
$ ./su システム /bin/bash -c "ls -l /"