1. 程式人生 > >Android應用向su申請root許可權,以及Superuser進行授權管理的原理淺析

Android應用向su申請root許可權,以及Superuser進行授權管理的原理淺析

最近研究了好幾天su+Superuser的原始碼,感覺大概梳理通了整個大體的思路框架,mark一下。 

一.su和Suepruser進行root授權的處理流程

對於su命令列程式在對來自Android應用的Root許可權請求處理流程大致如下圖所示(因為快要找工作了,為了節約時間花了一副醜到哭的圖片害羞):

圖中Android應用是申請Root許可權的申請者,su命令列程式時Root許可權擁有者,因su設定了suid位,因此任何執行它的程序都會獲得和它一樣的許可權,這個好像在之前的文章中提到過,其實細節上並非這麼容易,和Superuser使用者許可權管理apk結合起來使用的話還會有一些仲裁的過程。

描述起來就是:

1.Android應用呼叫su程式,來申請root許可權;

2.su啟動LocalSocket服務,LocalSocket的功能和通常我們程序通訊的Socket類似,其實是在本地共享一塊記憶體來實現和本地其他程序之間進行通訊的Socket服務;

3.su命令列程式通過am命令請求顯示Superuser應用的RequestActivity視窗,這個窗口裡面顯示哪個uid的和user的應用申請許可權;

4.Superuser連線到由su程序建立的LocalSocket服務上,至此LocalSocket連線成功,之後程序和Superuser對應的程序可以通過這個LocalSocket來進行資訊傳遞了;

5.LocalSocket的資料通道成功連線庫,su程式通過socket傳遞呼叫者(申請root許可權的Android應用)的一些資訊。

6.Superuser將使用者的仲裁結果資料返回給su程式,如果使用者允許授權則,則 ALLOW root授權,反之DENY。(其中還有使用者在一定時間內未作出選擇的情況,預設為DENY)

這是從Android應用申請root許可權到su和Superuser配合實現使用者選擇後的仲裁的整個過程。

下面我一步一步分析一下這些過程中的細節

二:從su的main函式開始,分析細節

因為su是一個linux命令列程式,故我們首先在Superuser的原始碼的jni目錄下找到su.c檔案,定位到main函式處:

main函式中主要完成了三個主要的工作:

  1.初始化呼叫者資料和效驗
| 1)獲取呼叫者呼叫su命令的命令列引數-from_init函式
| 2)獲取su命令的連結路徑-user_init函式
| 3)獲取呼叫者的名稱 
  2.通過SQLlite資料庫檢查申請“Root授權”的Android應用程式是否還需要進一步效驗
  3.建立LocalSocket服務,並進行相應的資料通訊

由於程式碼比較長,而且聯絡緊密因此在原始碼中我對相應的部分進行了註釋,建議下載後面我給出的註釋後的原始碼來對照理解,如果你懶得下,沒關係,相關的長長的程式碼我給你貼上老 ~~

int main(int argc, char *argv[]) {
    // Sanitize all secure environment variables (from linker_environ.c in AOSP linker).
    /* The same list than GLibc at this point */
    static const char* const unsec_vars[] = {
        "GCONV_PATH",
        "GETCONF_DIR",
        "HOSTALIASES",
        "LD_AUDIT",
        "LD_DEBUG",
        "LD_DEBUG_OUTPUT",
        "LD_DYNAMIC_WEAK",
        "LD_LIBRARY_PATH",
        "LD_ORIGIN_PATH",
        "LD_PRELOAD",
        "LD_PROFILE",
        "LD_SHOW_AUXV",
        "LD_USE_LOAD_BIAS",
        "LOCALDOMAIN",
        "LOCPATH",
        "MALLOC_TRACE",
        "MALLOC_CHECK_",
        "NIS_PATH",
        "NLSPATH",
        "RESOLV_HOST_CONF",
        "RES_OPTIONS",
        "TMPDIR",
        "TZDIR",
        "LD_AOUT_LIBRARY_PATH",
        "LD_AOUT_PRELOAD",
        // not listed in linker, used due to system() call
        "IFS",
    };
    const char* const* cp   = unsec_vars;
    const char* const* endp = cp + sizeof(unsec_vars)/sizeof(unsec_vars[0]);
    while (cp < endp) {
        unsetenv(*cp);
        cp++;
    }

    /*
     * set LD_LIBRARY_PATH if the linker has wiped out it due to we're suid.
     * This occurs on Android 4.0+
     */
    setenv("LD_LIBRARY_PATH", "/vendor/lib:/system/lib", 0);

    LOGD("su invoked.");
//  第一階段主要是進行一些初始化和效驗
// stx 這個結構體定義了三個成員:from、to和user 表示su呼叫的上下文
    struct su_context ctx = {
        .from = {
            .pid = -1,
            .uid = 0,
            .bin = "",
            .args = "",
            .name = "",
        },
        .to = {
            .uid = AID_ROOT,
            .login = 0,
            .keepenv = 0,
            .shell = NULL,
            .command = NULL,
            .argv = argv,
            .argc = argc,
            .optind = 0,
            .name = "",
        },
        .user = {
            .android_user_id = 0,
            .multiuser_mode = MULTIUSER_MODE_OWNER_ONLY,
            .database_path = REQUESTOR_DATA_PATH REQUESTOR_DATABASE_PATH,
            .base_path = REQUESTOR_DATA_PATH REQUESTOR
        },
    };
    struct stat st;
    int c, socket_serv_fd, fd;//LocalSocket的控制代碼
    char buf[64], *result;
    policy_t dballow;
    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' },
        { NULL, 0, NULL, 0 },
    };

    while ((c = getopt_long(argc, argv, "+c:hlmps:Vvu", long_opts, NULL)) != -1) {
        switch(c) {
        case 'c':
            ctx.to.shell = DEFAULT_SHELL;
            ctx.to.command = optarg;
            break;
        case 'h':
            usage(EXIT_SUCCESS);
            break;
        case 'l':
            ctx.to.login = 1;
            break;
        case 'm':
        case 'p':
            ctx.to.keepenv = 1;
            break;
        case 's':
            ctx.to.shell = optarg;
            break;
        case 'V':
            printf("%d\n", VERSION_CODE);
            exit(EXIT_SUCCESS);
        case 'v':
            printf("%s\n", VERSION);
            exit(EXIT_SUCCESS);
        case 'u':
            switch (get_multiuser_mode()) {
            case MULTIUSER_MODE_USER:
                printf("%s\n", MULTIUSER_VALUE_USER);
                break;
            case MULTIUSER_MODE_OWNER_MANAGED:
                printf("%s\n", MULTIUSER_VALUE_OWNER_MANAGED);
                break;
            case MULTIUSER_MODE_OWNER_ONLY:
                printf("%s\n", MULTIUSER_VALUE_OWNER_ONLY);
                break;
            case MULTIUSER_MODE_NONE:
                printf("%s\n", MULTIUSER_VALUE_NONE);
                break;
            }
            exit(EXIT_SUCCESS);
        default:
            /* Bionic getopt_long doesn't terminate its error output by newline */
            fprintf(stderr, "\n");
            usage(2);
        }
    }
    if (optind < argc && !strcmp(argv[optind], "-")) {
        ctx.to.login = 1;
        optind++;
    }
    /* username or uid */
    if (optind < argc && strcmp(argv[optind], "--")) {
        struct passwd *pw;
        pw = getpwnam(argv[optind]);
        if (!pw) {
            char *endptr;

            /* It seems we shouldn't do this at all */
            errno = 0;
            ctx.to.uid = strtoul(argv[optind], &endptr, 10);
            if (errno || *endptr) {
                LOGE("Unknown id: %s\n", argv[optind]);
                fprintf(stderr, "Unknown id: %s\n", argv[optind]);
                exit(EXIT_FAILURE);
            }
        } else {
            ctx.to.uid = pw->pw_uid;
            if (pw->pw_name)
                strncpy(ctx.to.name, pw->pw_name, sizeof(ctx.to.name));
        }
        optind++;
    }
    if (optind < argc && !strcmp(argv[optind], "--")) {
        optind++;
    }
    ctx.to.optind = optind;

    su_ctx = &ctx;
    
    
    //  初始化from呼叫者的資訊,主要是呼叫者的使用者ID
    if (from_init(&ctx.from) < 0) {
        deny(&ctx);
    }
        
    read_options(&ctx);
    user_init(&ctx);

    // the latter two are necessary for stock ROMs like note 2 which do dumb things with su, or crash otherwise
    if (ctx.from.uid == AID_ROOT) {//如果Android應用已經是root許可權,就直接允許其獲取root許可權
        LOGD("Allowing root/system/radio.");
        allow(&ctx);
    }
    // 校驗superuser是否安裝。
    // verify superuser is installed
    if (stat(ctx.user.base_path, &st) < 0) {
        // send to market (disabled, because people are and think this is hijacking their su)
        // if (0 == strcmp(JAVA_PACKAGE_NAME, REQUESTOR))
        //     silent_run("am start -d http://www.clockworkmod.com/superuser/install.html -a android.intent.action.VIEW");
        PLOGE("stat %s", ctx.user.base_path);
        deny(&ctx);
    }



    // always allow if this is the superuser uid
    // superuser needs to be able to reenable itself when disabled...
    if (ctx.from.uid == st.st_uid) {//如果呼叫者就是Superuser,那麼直接授予root許可權。
        allow(&ctx);
    }

    // check if superuser is disabled completely
    if (access_disabled(&ctx.from)) {
        LOGD("access_disabled");
        deny(&ctx);
    }

    // autogrant shell at this point
    if (ctx.from.uid == AID_SHELL) {
        LOGD("Allowing shell.");
        allow(&ctx);
    }

    // deny if this is a non owner request and owner mode only
    if (ctx.user.multiuser_mode == MULTIUSER_MODE_OWNER_ONLY && ctx.user.android_user_id != 0) {
        deny(&ctx);
    }

    ctx.umask = umask(027);
    // 在/dev目錄下建立一個用於LocalSocket快取的目錄,LocalSocket實際通過記憶體來傳遞資料
    int ret = mkdir(REQUESTOR_CACHE_PATH, 0770);
    if (chown(REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid)) {
        PLOGE("chown (%s, %ld, %ld)", REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid);
        deny(&ctx);
    }

    if (setgroups(0, NULL)) {
        PLOGE("setgroups");
        deny(&ctx);
    }
    if (setegid(st.st_gid)) {
        PLOGE("setegid (%lu)", st.st_gid);
        deny(&ctx);
    }
    if (seteuid(st.st_uid)) {
        PLOGE("seteuid (%lu)", st.st_uid);
        deny(&ctx);
    }
    //  第二階段:檢查申請“Root授權”的Android應用程式是否還需要進一步效驗
    dballow = database_check(&ctx);//核對資料庫,如果資料庫中已經顯示授予Root許可權就直接授予,拒絕過就直接拒絕
	//database_check檔案是在db.c檔案中實現的
    switch (dballow) {
        case INTERACTIVE:
            break;
        case ALLOW:
            LOGD("db allowed");
            allow(&ctx);    /* never returns */
        case DENY:
        default:
            LOGD("db denied");
            deny(&ctx);        /* never returns too */
    }
    //  第三階段:建立LocalSocket服務,並進行相應的資料通訊
    socket_serv_fd = socket_create_temp(ctx.sock_path, sizeof(ctx.sock_path));//根據dev下的路徑建立LocalSocket服務
    LOGD(ctx.sock_path);
    if (socket_serv_fd < 0) {//如果LocalSocket建立失敗就直接拒絕授權
        deny(&ctx);
    }

    signal(SIGHUP, cleanup_signal);
    signal(SIGPIPE, cleanup_signal);
    signal(SIGTERM, cleanup_signal);
    signal(SIGQUIT, cleanup_signal);
    signal(SIGINT, cleanup_signal);
    signal(SIGABRT, cleanup_signal);

    if (send_request(&ctx) < 0) { //通過am命令向Superuser傳送命令,請求顯示RequestActivity視窗
        deny(&ctx);
    }

    atexit(cleanup);

    fd = socket_accept(socket_serv_fd);//等待Superuser的連線  
	//一點Superuser已經連線到su命令建立的LocalSocket上,資料傳輸就不必再使用am命令了,直接通過資料流傳輸即可
    if (fd < 0) {
        deny(&ctx);
    }
    if (socket_send_request(fd, &ctx)) {//連線成功後,通過已連線的Socket向Superuser傳送呼叫者的資訊
        deny(&ctx);
    }
    if (socket_receive_result(fd, buf, sizeof(buf))) {//接收使用者在Superuser的選擇視窗中進行的授權選擇
        deny(&ctx);
    }

    close(fd);
    close(socket_serv_fd);
    socket_cleanup(&ctx);

    result = buf;

#define SOCKET_RESPONSE    "socket:" // socket:ALLOW
    if (strncmp(result, SOCKET_RESPONSE, sizeof(SOCKET_RESPONSE) - 1))
        LOGW("SECURITY RISK: Requestor still receives credentials in intent");
    else
        result += sizeof(SOCKET_RESPONSE) - 1;//讓result指標指向“Socket:”後面的資料
    if (!strcmp(result, "DENY")) {
        deny(&ctx);
    } else if (!strcmp(result, "ALLOW")) {
        allow(&ctx);
    } else {
        LOGE("unknown response from Superuser Requestor: %s", result);
        deny(&ctx);
    }
}
/*到現在為止,我們已經瞭解了su命令和Superuser在授權root許可權時,su端的工作原理,
但是su.c檔案中的兩個函式allow()和deny()的具體實現我們還沒有看到,這兩個函式才是
最終允許或拒絕root許可權授權的關鍵/*
這裡面主要涉及到兩個初始化函式from_init和user_init,以及一個描述su呼叫上下文的結構體su_context。

main函式中首先進行的是呼叫者資訊的初始化工作即from_init函式的功能

static int from_init(struct su_initiator *from) {
    char path[PATH_MAX], exe[PATH_MAX];
    char args[4096], *argv0, *argv_rest;//argv_rest指向真正的su命令列引數的資訊
    int fd;
    ssize_t len;
    int i;
    int err;

    from->uid = getuid();//獲取實際的使用者ID,用於標識Android App(呼叫者)
    from->pid = getppid();//獲取呼叫程序的父程序ID

    /* Get the command line */
    snprintf(path, sizeof(path), "/proc/%u/cmdline", from->pid);//獲得su呼叫時的命令列引數
    fd = open(path, O_RDONLY);
    if (fd < 0) {
        PLOGE("Opening command line");
        return -1;
    }
    len = read(fd, args, sizeof(args));
    err = errno;
    close(fd);
    if (len < 0 || len == sizeof(args)) {
        PLOGEV("Reading command line", err);
        return -1;
    }
    //  su \0  a  \0  b  \0 c
    //  su \0  a  ' ' b  ' ' c \0   (\0表示字串結束)
    argv0 = args;   // 指向了第一個命令列引數,也就是su
    argv_rest = NULL;
    for (i = 0; i < len; i++) { //len表示總體的命令列長度
        if (args[i] == '\0') {//從第一個 \0開始,獲取命令列引數到argv_rest
            if (!argv_rest) {
                argv_rest = &args[i+1];
            } else {
                args[i] = ' ';
            }
        }
    }
    args[len] = '\0';

    if (argv_rest) {
        strncpy(from->args, argv_rest, sizeof(from->args));//將命令列引數放入呼叫者資訊from->args中
        from->args[sizeof(from->args)-1] = '\0';
    } else {
        from->args[0] = '\0';
    }

    /* If this isn't app_process, use the real path instead of argv[0] */
    snprintf(path, sizeof(path), "/proc/%u/exe", from->pid);
    len = readlink(path, exe, sizeof(exe));
    if (len < 0) {
        PLOGE("Getting exe path");
        return -1;
    }
    exe[len] = '\0';
    if (strcmp(exe, "/system/bin/app_process")) {
        argv0 = exe;
    }

    strncpy(from->bin, argv0, sizeof(from->bin));
    from->bin[sizeof(from->bin)-1] = '\0';

    struct passwd *pw;
    pw = getpwuid(from->uid);
    if (pw && pw->pw_name) {
        strncpy(from->name, pw->pw_name, sizeof(from->name));
    }

    return 0;
}
其中包括獲得呼叫者執行su時的命令列引數uid等操作。對照註釋看吧。

main函式中第二個主要完成的工作就是:檢查申請“Root”授權的Android應用程式是否還需要進一步的效驗。然後在本地SQLite資料庫中進行查詢,如果資料庫中顯示授予root許可權就直接

授予root許可權,拒絕過就直接拒絕,如果沒有記錄就進入到下一階段,和Superuser進行通訊,然後Superuser接收到使用者的選擇後將選擇資料傳回到su程式中來決定是否授予新申請root許可權的Android應用Roo許可權。

main中完成的第三個主要的主要工作就是建立本地LocalSocket服務,等待Superuser的連線,當連線成功後傳遞呼叫者資訊即初始化後的su_context結構體,Superuser在收到呼叫者的資訊後顯示出來讓使用者仲裁是否授予許可權,最後將使用者選擇回傳到su程式,su根據結果來執行deny()或者allow()函式。

三:允許或拒絕root許可權授權的關鍵allow()函式和deny()函式

到現在為止,我們已經瞭解了su命令和Superuser在授權root許可權時,su端的工作原理,但是su.c檔案中的兩個函式allow()和deny()的具體實現我們還沒有看到,這兩個函式才是
最終允許或拒絕root許可權授權的關鍵:

static __attribute__ ((noreturn)) void deny(struct su_context *ctx) {
    char *cmd = get_command(&ctx->to);

    int send_to_app = 1;

    // no need to log if called by root
    if (ctx->from.uid == AID_ROOT)
        send_to_app = 0;

    // dumpstate (which logs to logcat/shell) will spam the crap out of the system with su calls
    if (strcmp("/system/bin/dumpstate", ctx->from.bin) == 0)
        send_to_app = 0;

    if (send_to_app)
        send_result(ctx, DENY);

    LOGW("request rejected (%u->%u %s)", ctx->from.uid, ctx->to.uid, cmd);
    fprintf(stderr, "%s\n", strerror(EACCES));
    exit(EXIT_FAILURE);
}

static __attribute__ ((noreturn)) void allow(struct su_context *ctx) {
    char *arg0;
    int argc, err;

    umask(ctx->umask);
    int send_to_app = 1;

    // no need to log if called by root
    if (ctx->from.uid == AID_ROOT)
        send_to_app = 0;

    // dumpstate (which logs to logcat/shell) will spam the crap out of the system with su calls
    if (strcmp("/system/bin/dumpstate", ctx->from.bin) == 0)
        send_to_app = 0;

    if (send_to_app)
        send_result(ctx, ALLOW);//向Superuser回發信息,表明授權成功,這個函式在activity.c中。
		//在socket連線關閉時,是通過am命令傳遞資訊到Superuser的。

    char *binary;
    argc = ctx->to.optind;
    if (ctx->to.command) {
        binary = ctx->to.shell;
        ctx->to.argv[--argc] = ctx->to.command;
        ctx->to.argv[--argc] = "-c";
    }
    else if (ctx->to.shell) {
        binary = ctx->to.shell;
    }
    else {
        if (ctx->to.argv[argc]) {
            binary = ctx->to.argv[argc++];
        }
        else {
            binary = DEFAULT_SHELL;
        }
    }

    arg0 = strrchr (binary, '/');
    arg0 = (arg0) ? arg0 + 1 : binary;
    if (ctx->to.login) {
        int s = strlen(arg0) + 2;
        char *p = malloc(s);

        if (!p)
            exit(EXIT_FAILURE);

        *p = '-';
        strcpy(p + 1, arg0);
        arg0 = p;
    }

    populate_environment(ctx);
    set_identity(ctx->to.uid);

#define PARG(arg)                                    \
    (argc + (arg) < ctx->to.argc) ? " " : "",                    \
    (argc + (arg) < ctx->to.argc) ? ctx->to.argv[argc + (arg)] : ""

    LOGD("%u %s executing %u %s using binary %s : %s%s%s%s%s%s%s%s%s%s%s%s%s%s",
            ctx->from.uid, ctx->from.bin,
            ctx->to.uid, get_command(&ctx->to), binary,
            arg0, PARG(0), PARG(1), PARG(2), PARG(3), PARG(4), PARG(5),
            (ctx->to.optind + 6 < ctx->to.argc) ? " ..." : "");

    ctx->to.argv[--argc] = arg0;
    execvp(binary, ctx->to.argv + argc);
    err = errno;
    PLOGE("exec");
    fprintf(stderr, "Cannot execute %s: %s\n", binary, strerror(err));
    exit(EXIT_FAILURE);
}

其中
execvp(binary, ctx->to.argv + argc);//這一句才是前面各種效驗仲裁的允許獲得root許可權的最終核心的執行程式碼。
我在這兒就不一行一行的講解了,其實有的細節部分要結合其他檔案中的內容進行理解,大家有興趣的話可以閱讀原始碼。我有時間進行更深刻的理解的話,會持續更新的。也歡迎大家和我交流。

Superuser原始碼下載連結:http://download.csdn.net/detail/koozxcv/9487931