Android 安全攻防(三): SEAndroid Zygote
转自:http://blog.csdn.net/yiyaaixuexi/article/details/8495695
在Android系统中,所有的应用程序进程,以及系统服务进程SystemServer都是由Zygote孕育fork出来的。 Zygote的native获取主要研究dalvik/vm/native/dalvik_system_Zygote.cpp,SEAndroid管控应用程序资源存取权限,对于整个dalvik,也正是在此动的手脚。
首先看抛出的DalvikNativeMethod dvm_dalvik_system_Zygote,与原生Android相比,SEAndroid 在 nativeForkAndSpecialize 增加传入了两个String类型的参数:
- const DalvikNativeMethod dvm_dalvik_system_Zygote[] = {
- {"nativeFork", "()I",
- Dalvik_dalvik_system_Zygote_fork },
- { "nativeForkAndSpecialize", "(II[II[[ILjava/lang/String;Ljava/lang/String;)I",
- Dalvik_dalvik_system_Zygote_forkAndSpecialize },
- { "nativeForkSystemServer", "(II[II[[IJJ)I",
- Dalvik_dalvik_system_Zygote_forkSystemServer },
- { "nativeExecShell", "(Ljava/lang/String;)V",
- Dalvik_dalvik_system_Zygote_execShell },
- { NULL, NULL, NULL },
- }
那么这两个参数是什么呢?继续追一下forkAndSpecialize。
- /* native public static int forkAndSpecialize(int uid, int gid,
- * int[] gids, int debugFlags, String seInfo, String niceName);
- */
- static void Dalvik_dalvik_system_Zygote_forkAndSpecialize(const u4* args,
- JValue* pResult)
- {
- pid_t pid;
- pid = forkAndSpecializeCommon(args, false);
- RETURN_INT(pid);
- }
可以看到,增加传入的2个参数一个是seInfo,用于定义新进程的SEAndroid信息,一个是niceName,用于定义新进程名。
在static pid_t forkAndSpecializeCommon(const u4* args, bool isSystemServer)中,其中SEAndroid加入了设置SELinux安全上下文代码段,seInfo和niceName:
- #ifdef HAVE_SELINUX
- err = setSELinuxContext(uid, isSystemServer, seInfo, niceName);
- if (err < 0) {
- LOGE("cannot set SELinux context: %s\n", strerror(errno));
- dvmAbort();
- }
- free(seInfo);
- free(niceName);
- #endif
其中设置SELinux安全上下文方法实现:
- #ifdef HAVE_SELINUX
- /*
- * Set SELinux security context.
- *
- * Returns 0 on success, -1 on failure.
- */
- static int setSELinuxContext(uid_t uid, bool isSystemServer,
- const char *seInfo, const char *niceName)
- {
- #ifdef HAVE_ANDROID_OS
- return selinux_android_setcontext(uid, isSystemServer, seInfo, niceName);
- #else
- return 0;
- #endif
- }
- #endif
再往上一层就到了libcore/dalvik/src/main/java/dalvik/system/Zygote.java ,Zygote类的封装,对应forkAndSpecialize方法中添加seInfo和niceName参数传递。
- public class Zygote {
- ...
- public static int forkAndSpecialize(int uid, int gid, int[] gids,
- int debugFlags, int[][] rlimits, String seInfo, String niceName) {
- preFork();
- int pid = nativeForkAndSpecialize(uid, gid, gids, debugFlags, rlimits, seInfo, niceName);
- postFork();
- return pid;
- }
- native public static int nativeForkAndSpecialize(int uid, int gid,
- int[] gids, int debugFlags, int[][] rlimits, String seInfo, String niceName);
- /**
- * Forks a new VM instance.
- * @deprecated use {@link Zygote#forkAndSpecialize(int, int, int[], int, int[][])}
- */
- @Deprecated
- public static int forkAndSpecialize(int uid, int gid, int[] gids,
- boolean enableDebugger, int[][] rlimits) {
- int debugFlags = enableDebugger ? DEBUG_ENABLE_DEBUGGER : 0;
- return forkAndSpecialize(uid, gid, gids, debugFlags, rlimits, null, null);
- }
- ...
- }
Android应用程序启动流程不再赘述,当建立了ZygoteConnection对象用于socket连接后,接下来就是调用ZygoteConnection.runOnce函数进一步处理了。
源码位置:frameworks/base/core/java/com/android/internal/os/ZygoteConnection.java,其中,SEAndroid增加zygote安全策略函数,在runOnce中调用。
- /**
- * Applies zygote security policy.
- * Based on the credentials of the process issuing a zygote command:
- * <ol>
- * <li> uid 0 (root) may specify --invoke-with to launch Zygote with a
- * wrapper command.
- * <li> Any other uid may not specify any invoke-with argument.
- * </ul>
- *
- * @param args non-null; zygote spawner arguments
- * @param peer non-null; peer credentials
- * @throws ZygoteSecurityException
- */
- private static void applyInvokeWithSecurityPolicy(Arguments args, Credentials peer,
- String peerSecurityContext)
- throws ZygoteSecurityException {
- int peerUid = peer.getUid();
- if (args.invokeWith != null && peerUid != 0) {
- throw new ZygoteSecurityException("Peer is not permitted to specify "
- + "an explicit invoke-with wrapper command");
- }
- if (args.invokeWith != null) {
- boolean allowed = SELinux.checkSELinuxAccess(peerSecurityContext,
- peerSecurityContext,
- "zygote",
- "specifyinvokewith");
- if (!allowed) {
- throw new ZygoteSecurityException("Peer is not permitted to specify "
- + "an explicit invoke-with wrapper command");
- }
- }
- }
- /**
- * Applies zygote security policy for SEAndroid information.
- *
- * @param args non-null; zygote spawner arguments
- * @param peer non-null; peer credentials
- * @throws ZygoteSecurityException
- */
- private static void applyseInfoSecurityPolicy(
- Arguments args, Credentials peer, String peerSecurityContext)
- throws ZygoteSecurityException {
- int peerUid = peer.getUid();
- if (args.seInfo == null) {
- // nothing to check
- return;
- }
- if (!(peerUid == 0 || peerUid == Process.SYSTEM_UID)) {
- // All peers with UID other than root or SYSTEM_UID
- throw new ZygoteSecurityException(
- "This UID may not specify SEAndroid info.");
- }
- boolean allowed = SELinux.checkSELinuxAccess(peerSecurityContext,
- peerSecurityContext,
- "zygote",
- "specifyseinfo");
- if (!allowed) {
- throw new ZygoteSecurityException(
- "Peer may not specify SEAndroid info");
- }
- return;
- }
理所当然的,在启动一个新的进程时,frameworks/base/core/java/android/os/Process.java中也会加入SEAndroid信息seInfo。
Android 安全攻防(三): SEAndroid Zygote的更多相关文章
- Android trap攻防思路整理
Android trap攻防 图/文 h_one 0x01 反 ...
- Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复
Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...
- Android反编译(三)之重签名
Android反编译(三) 之重签名 [目录] 1.原理 2.工具与准备工作 3.操作步骤 4.装X技巧 5.问题 1.原理 1).APK签名的要点 a.所有的应用程序都必须有数字证书 ,Androi ...
- 三、Android学习第三天——Activity的布局初步介绍(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 三.Android学习第三天——Activity的布局初步介绍 今天总结下 ...
- android基础(三)ContentProvider
ContentProvider主要用于在不同的应用程序之间实现数据共享,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访问数据的安全性,目前内容提供其实android实现跨 ...
- 【转】android 电池(三):android电池系统
关键词:android电池系统电池系统架构 uevent power_supply驱动 平台信息: 内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV3 ...
- android Graphics(三):区域(Range)
前言:最近几天对画图的研究有些缓慢,项目开始写代码了,只能在晚上空闲的时候捯饬一下自己的东西,今天给大家讲讲区域的相关知识,已经想好后面两篇的内容了,这几天有时间赶紧写出来给大家.有关界面开发的东东内 ...
- android Service Activity三种交互方式(付源码)(转)
android Service Activity三种交互方式(付源码) Android应用服务器OSBeanthread android Service Binder交互通信实例 最下边有源代码: ...
- wemall app商城源码中android按钮的三种响应事件
wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码中android按 ...
- Android For JNI(三)——C的指针,指针变量,指针常见错误,值传递,引用传递,返回多个值
Android For JNI(三)--C的指针,指针变量,指针常见错误,值传递,引用传递,返回多个值 C中比较难的这一块,大概就是指针了,所以大家还是多翻阅一下资料,当然,如果只是想了解一下,看本篇 ...
随机推荐
- 如何解决eclipse远程服务器上面的Rabbitmq连接超时问题?
1.嗯,问题呢,就是一开始安装好RabbitMQ,练习了一下RabbitMQ的使用,但是呢,过了一段时间,我来复习的时候,发现运行出现下面的错误了.后来想想,是自己学习微服务的时候,修改了/etc/h ...
- C# 波浪线绘制
波浪线效果如上 界面绘制操作 private Point? _startPoint = null; private void ContainerCanvas_OnPreviewMouseLeftBut ...
- Ubuntu Server中怎样卸载keepalived
场景 在Ubuntu Server中进行安装keepalived ,如果安装过程中出现纰漏,想要重新安装keepalived或者就是想直接卸载keepalived. 我们在安装keepalived时指 ...
- JS基础语法---函数也是一种数据类型
1. 如何获取某个变量的类型? typeof 2. 函数是有数据类型 ,数据类型:是function function f1() { console.log("我是函数"); } ...
- Dynamics CRM - js中用webapi基于fetchxml查询遇到的问题 -- Invalid URI: The Uri scheme is too long.
最近用WebApi做基于Fetchxml的查询的时候,遇到一个很蛋疼的报错:Invalid URI: The Uri scheme is too long. 检查了整个URL,也没发现有什么问题. - ...
- CentOS7 安装frp与开机启动
1. 下载frp程序文件 https://github.com/fatedier/frp/releases 2. 解压文件 下载后解压到自己的目录,我这里解压到/usr/local/frp: 3. 添 ...
- DataPump遭遇ORA-06512&ORA-39080&ORA-01403错误案例
最近使用数据泵(DataPump)比较多,遇到了奇奇怪怪的问题,似乎Apply了补丁PSU 10.2.0.5.180717后,DataPump的问题就格外多.如下所示: expdp system/xx ...
- hidraw设备简要分析
关键词:hid.hidraw.usbhid.hidp等等. 下面首先介绍hidraw设备主要用途,然后简要分析hidraw设备驱动(但是不涉及到相关USB/Bluwtooth驱动),最后分析用户空间接 ...
- HTML5常见的取值与单位
HTML5常见的取值与单位 长度单位包括 相对长度单位包括:em, ex, ch, rem, vw, vh, vmax, vmin 绝对长度单位包括:cm, mm, q, in, pt, pc ...
- RabbitMQ学习笔记(六、RabbitMQ进阶)
目录: 性能 存储机制 内存及磁盘告警 性能: 影响RabbitMQ性能的因素有很多,主要的分为硬件性能与软件性能. )硬件性能:如网络.内存.CPU等等. )软件性能:消息持久化.消息确认.路由算法 ...