【Android】SU命令源码解析

Android SU源码解析

  • 代码路径
    system/extras/su/su.cpp
  • 支持命令
    Android su与linux自己的su,在使用上不同。
bash 复制代码
# su [WHO [COMMAND...]]
# Switch to WHO (default 'root') and run the given COMMAND (default sh).
# WHO is a comma-separated list of user, group, and supplementary groups.
# 比如 0(uid) 0(gid) group1,group2,group3
# 0是root用户
su 0 cat /etc/test.txt

源码解析

  • 概览
cpp 复制代码
int main(int argc, char** argv) {
#ifndef IS_MONKEY_VERSION
    uid_t current_uid = getuid();
    if (current_uid != AID_ROOT && AID_SHLL) error(1, 0, "not allowed");
#endif
    
    uid_t uid = 0;
    gid_t git = 0;
    
    if (argv) {
        gid_t gitd[10];
        int gids_count = sizeof(gitds)/size(gids[0]);
        // 解析uid,gid以及gids
        extract_uidgits(*argv,&uid,&gitd, gids, &gitds_count);
        if (gids_count) {
            if (setgourps(gids_count, gids)) {
                error(1, errno, "setgroups failed")
            }
        }
        ++argv;
    }

    if (setgid(gid)) ***
    if (setuid(uid)) ***
    
    setent("PATH", _PATH_DEFPATH, 1);
    unsetenv("IFS");
    
    struct passwd* pw = getpwuid(uid);
    if (pw) {
        setenv("LOGNAME", pw->pw_name,1);
        setenv("USER", pw->pw_name,1);
    } else {
        unsetenv("LOGNAME");
        unsetenv("USER");
    }
    
    char* exec_args[argc+1];
    size_t i = 0;
    for (; *argv != NULL; ++i) {
        exec_args[i] = *argv++;
    }
    
    if (i==0) exec_args[i++]= const_cast<char*>("system/bin/sh");
    exe_args[i] = NULL;
    execvp(exec_args[0], exec_args);
    error(1, errno, "failed to exec %s", exec_args[0]);
}
  • 第一步:检测执行uid,只允许root和shell 用户使用。
cpp 复制代码
#ifndef IS_MONKEY_VERSION
    uid_t current_uid = getuid();
    if (current_uid != AID_ROOT && AID_SHLL) error(1, 0, "not allowed");
#endif
  • 解析UID/GID/GROUPS入参,并设置对应的UID/GID/GROUPS
cpp 复制代码
uid_t uid = 0;
gid_t git = 0;

if (argv) {
     gid_t gitd[10];
     int gids_count = sizeof(gitds)/size(gids[0]);
     // 解析uid,gid以及gids
     extract_uidgits(*argv,&uid,&gitd, gids, &gitds_count);
     if (gids_count) {
         if (setgourps(gids_count, gids)) {
             error(1, errno, "setgroups failed")
         }
     }
     ++argv;
 }
  • 获取用户信息
cpp 复制代码
struct passwd* pw = getpwuid(uid);
if (pw) {
    setenv("LOGNAME", pw->pw_name,1);
    setenv("USER", pw->pw_name,1);
} else {
    unsetenv("LOGNAME");
    unsetenv("USER");
}
  • 解析命令并执行
cpp 复制代码
char* exec_args[argc+1];
size_t i = 0;
for (; *argv != NULL; ++i) {
    exec_args[i] = *argv++;
}

if (i==0) exec_args[i++]= const_cast<char*>("system/bin/sh");
exe_args[i] = NULL;
// 调用execvp执行
execvp(exec_args[0], exec_args);
error(1, errno, "failed to exec %s", exec_args[0]);
  • 例如输入
cpp 复制代码
# 以root(uid) 以root(gid) 执行命令 cat /etc/test.txt
su 0 0 cat /etc/test.txt
相关推荐
雮尘2 小时前
手把手带你玩转Android gRPC:一篇搞定原理、配置与客户端开发
android·前端·grpc
ktl3 小时前
Android 编译加速/优化 80%:一个文件搞定,零侵入零配置
android
alexhilton14 小时前
使用FunctionGemma进行设备端函数调用
android·kotlin·android jetpack
冬奇Lab17 小时前
InputManagerService:输入事件分发与ANR机制
android·源码阅读
张小潇20 小时前
AOSP15 Input专题InputManager源码分析
android·操作系统
RdoZam1 天前
Android-封装基类Activity\Fragment,从0到1记录
android·kotlin
奥陌陌1 天前
android 打印函数调用堆栈
android
用户985120035831 天前
Compose Navigation 3 深度解析(二):基础用法
android·android jetpack
恋猫de小郭1 天前
Android 官方正式官宣 AI 支持 AppFunctions ,Android 官方 MCP 和系统级 OpenClaw 雏形
android·前端·flutter
黄林晴1 天前
Android 17 Beta 2,隐私这把锁又拧紧了
android