【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
相关推荐
limuyang27 小时前
PDF Viewer KMP(基于 chrome 的 PDFium 内核)
android·kotlin
心平气和量大福大11 小时前
android-控件-搜索框SearchView
android·gitee
2601_9621771311 小时前
【MySQL篇】聚合查询,联合查询
android·java·mysql
壮哥_icon11 小时前
【Android】批量 VectorDrawable 动态分别修改 fillColor 和 strokeColor 的踩坑与终极解决方案
android
齐天qaq12 小时前
Android 系统 APK 分区与权限
android
心平气和量大福大12 小时前
android-控件-多选框CheckBox
android·gitee
pengyu14 小时前
【Kotlin 协程修仙录 · 大乘境 · 初阶】 | 跳出三界:协程在 KMP 与后端开发中的跨平台之道
android·kotlin
XiaoLeisj14 小时前
Android Memory Profiler:堆内存指标、内存抖动与泄漏定位
android·性能优化·内存抖动·内存泄露·memory profiler
未来猫咪花14 小时前
Everything is ViewModel:让状态管理回到对象世界
android·flutter·ios