【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 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台2 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone2 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
哈基米南北2 天前
ViewRootImpl 事件分发责任链分析
源码
致远ccc2 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo2 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077002 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼2 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone2 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen2 天前
属性动画原理与高级动画实现
android·kotlin·canvas