【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
相关推荐
世界尽头与你1 分钟前
Android Root 检测原理
android·安全·网络安全·渗透测试
jctech34 分钟前
让「小程序」跑起真正的原生代码:ComboNative 多进程原生小程序运行时 · Alpha 首发
android·设计模式·架构
tmacfrank35 分钟前
Android 17 重点新特性介绍
android
YM52e1 小时前
鸿蒙 Flutter 渐变效果详解:LinearGradient、RadialGradient、SweepGradient
android·学习·flutter·华为·harmonyos·鸿蒙
-SOLO-2 小时前
Android 内存压力工具
android
雅客李2 小时前
2026云手机延迟数据测评 安卓云手机远程操控实测哪个最好
android·智能手机
YM52e3 小时前
鸿蒙Flutter Card组件:Material设计风格卡片
android·学习·flutter·华为·harmonyos·鸿蒙
Xzaveir16 小时前
不要用一个状态表示“号码已认证”:企业号码身份的四域模型
android·人工智能
(Charon)17 小时前
【C++】手写 MySQL 连接池(二):同步连接的获取、加锁与释放
android
zhangjin112018 小时前
AOSP下载
android