转自: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类型的参数:

  1. const DalvikNativeMethod dvm_dalvik_system_Zygote[] = {
  2. {"nativeFork", "()I",
  3. Dalvik_dalvik_system_Zygote_fork },
  4. { "nativeForkAndSpecialize", "(II[II[[ILjava/lang/String;Ljava/lang/String;)I",
  5. Dalvik_dalvik_system_Zygote_forkAndSpecialize },
  6. { "nativeForkSystemServer", "(II[II[[IJJ)I",
  7. Dalvik_dalvik_system_Zygote_forkSystemServer },
  8. { "nativeExecShell", "(Ljava/lang/String;)V",
  9. Dalvik_dalvik_system_Zygote_execShell },
  10. { NULL, NULL, NULL },
  11. }

那么这两个参数是什么呢?继续追一下forkAndSpecialize。

  1. /* native public static int forkAndSpecialize(int uid, int gid,
  2. * int[] gids, int debugFlags, String seInfo, String niceName);
  3. */
  4. static void Dalvik_dalvik_system_Zygote_forkAndSpecialize(const u4* args,
  5. JValue* pResult)
  6. {
  7. pid_t pid;
  8. pid = forkAndSpecializeCommon(args, false);
  9. RETURN_INT(pid);
  10. }

可以看到,增加传入的2个参数一个是seInfo,用于定义新进程的SEAndroid信息,一个是niceName,用于定义新进程名。

在static pid_t forkAndSpecializeCommon(const u4* args, bool isSystemServer)中,其中SEAndroid加入了设置SELinux安全上下文代码段,seInfo和niceName:

  1. #ifdef HAVE_SELINUX
  2. err = setSELinuxContext(uid, isSystemServer, seInfo, niceName);
  3. if (err < 0) {
  4. LOGE("cannot set SELinux context: %s\n", strerror(errno));
  5. dvmAbort();
  6. }
  7. free(seInfo);
  8. free(niceName);
  9. #endif

其中设置SELinux安全上下文方法实现:

  1. #ifdef HAVE_SELINUX
  2. /*
  3. * Set SELinux security context.
  4. *
  5. * Returns 0 on success, -1 on failure.
  6. */
  7. static int setSELinuxContext(uid_t uid, bool isSystemServer,
  8. const char *seInfo, const char *niceName)
  9. {
  10. #ifdef HAVE_ANDROID_OS
  11. return selinux_android_setcontext(uid, isSystemServer, seInfo, niceName);
  12. #else
  13. return 0;
  14. #endif
  15. }
  16. #endif

再往上一层就到了libcore/dalvik/src/main/java/dalvik/system/Zygote.java ,Zygote类的封装,对应forkAndSpecialize方法中添加seInfo和niceName参数传递。

  1. public class Zygote {
  2. ...
  3. public static int forkAndSpecialize(int uid, int gid, int[] gids,
  4. int debugFlags, int[][] rlimits, String seInfo, String niceName) {
  5. preFork();
  6. int pid = nativeForkAndSpecialize(uid, gid, gids, debugFlags, rlimits, seInfo, niceName);
  7. postFork();
  8. return pid;
  9. }
  10. native public static int nativeForkAndSpecialize(int uid, int gid,
  11. int[] gids, int debugFlags, int[][] rlimits, String seInfo, String niceName);
  12. /**
  13. * Forks a new VM instance.
  14. * @deprecated use {@link Zygote#forkAndSpecialize(int, int, int[], int, int[][])}
  15. */
  16. @Deprecated
  17. public static int forkAndSpecialize(int uid, int gid, int[] gids,
  18. boolean enableDebugger, int[][] rlimits) {
  19. int debugFlags = enableDebugger ? DEBUG_ENABLE_DEBUGGER : 0;
  20. return forkAndSpecialize(uid, gid, gids, debugFlags, rlimits, null, null);
  21. }
  22. ...
  23. }

Android应用程序启动流程不再赘述,当建立了ZygoteConnection对象用于socket连接后,接下来就是调用ZygoteConnection.runOnce函数进一步处理了。

源码位置:frameworks/base/core/java/com/android/internal/os/ZygoteConnection.java,其中,SEAndroid增加zygote安全策略函数,在runOnce中调用。

  1. /**
  2. * Applies zygote security policy.
  3. * Based on the credentials of the process issuing a zygote command:
  4. * <ol>
  5. * <li> uid 0 (root) may specify --invoke-with to launch Zygote with a
  6. * wrapper command.
  7. * <li> Any other uid may not specify any invoke-with argument.
  8. * </ul>
  9. *
  10. * @param args non-null; zygote spawner arguments
  11. * @param peer non-null; peer credentials
  12. * @throws ZygoteSecurityException
  13. */
  14. private static void applyInvokeWithSecurityPolicy(Arguments args, Credentials peer,
  15. String peerSecurityContext)
  16. throws ZygoteSecurityException {
  17. int peerUid = peer.getUid();
  18. if (args.invokeWith != null && peerUid != 0) {
  19. throw new ZygoteSecurityException("Peer is not permitted to specify "
  20. + "an explicit invoke-with wrapper command");
  21. }
  22. if (args.invokeWith != null) {
  23. boolean allowed = SELinux.checkSELinuxAccess(peerSecurityContext,
  24. peerSecurityContext,
  25. "zygote",
  26. "specifyinvokewith");
  27. if (!allowed) {
  28. throw new ZygoteSecurityException("Peer is not permitted to specify "
  29. + "an explicit invoke-with wrapper command");
  30. }
  31. }
  32. }
  33. /**
  34. * Applies zygote security policy for SEAndroid information.
  35. *
  36. * @param args non-null; zygote spawner arguments
  37. * @param peer non-null; peer credentials
  38. * @throws ZygoteSecurityException
  39. */
  40. private static void applyseInfoSecurityPolicy(
  41. Arguments args, Credentials peer, String peerSecurityContext)
  42. throws ZygoteSecurityException {
  43. int peerUid = peer.getUid();
  44. if (args.seInfo == null) {
  45. // nothing to check
  46. return;
  47. }
  48. if (!(peerUid == 0 || peerUid == Process.SYSTEM_UID)) {
  49. // All peers with UID other than root or SYSTEM_UID
  50. throw new ZygoteSecurityException(
  51. "This UID may not specify SEAndroid info.");
  52. }
  53. boolean allowed = SELinux.checkSELinuxAccess(peerSecurityContext,
  54. peerSecurityContext,
  55. "zygote",
  56. "specifyseinfo");
  57. if (!allowed) {
  58. throw new ZygoteSecurityException(
  59. "Peer may not specify SEAndroid info");
  60. }
  61. return;
  62. }

理所当然的,在启动一个新的进程时,frameworks/base/core/java/android/os/Process.java中也会加入SEAndroid信息seInfo。

Android 安全攻防(三): SEAndroid Zygote的更多相关文章

  1. Android trap攻防思路整理

    Android trap攻防                                                                      图/文 h_one 0x01 反 ...

  2. Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复

    Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...

  3. Android反编译(三)之重签名

    Android反编译(三) 之重签名 [目录] 1.原理 2.工具与准备工作 3.操作步骤 4.装X技巧 5.问题 1.原理 1).APK签名的要点 a.所有的应用程序都必须有数字证书 ,Androi ...

  4. 三、Android学习第三天——Activity的布局初步介绍(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 三.Android学习第三天——Activity的布局初步介绍 今天总结下 ...

  5. android基础(三)ContentProvider

    ContentProvider主要用于在不同的应用程序之间实现数据共享,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访问数据的安全性,目前内容提供其实android实现跨 ...

  6. 【转】android 电池(三):android电池系统

    关键词:android电池系统电池系统架构 uevent power_supply驱动 平台信息: 内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV3 ...

  7. android Graphics(三):区域(Range)

    前言:最近几天对画图的研究有些缓慢,项目开始写代码了,只能在晚上空闲的时候捯饬一下自己的东西,今天给大家讲讲区域的相关知识,已经想好后面两篇的内容了,这几天有时间赶紧写出来给大家.有关界面开发的东东内 ...

  8. android Service Activity三种交互方式(付源码)(转)

    android Service Activity三种交互方式(付源码) Android应用服务器OSBeanthread  android Service Binder交互通信实例 最下边有源代码: ...

  9. wemall app商城源码中android按钮的三种响应事件

    wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码中android按 ...

  10. Android For JNI(三)——C的指针,指针变量,指针常见错误,值传递,引用传递,返回多个值

    Android For JNI(三)--C的指针,指针变量,指针常见错误,值传递,引用传递,返回多个值 C中比较难的这一块,大概就是指针了,所以大家还是多翻阅一下资料,当然,如果只是想了解一下,看本篇 ...

随机推荐

  1. IMP-00009: abnormal end of export file解决方案

    一.概述 最近在测试环境的一个oracle数据库上面,使用exp将表导出没有问题,而将导出的文件使用imp导入时却出现了如下错误. IMP-00009: abnormal end of export ...

  2. msyql master thread

    ------------------------------------------------------ 2015-02-10----------------------------------- ...

  3. Python升级PIP

    用pip list的时候.发现最后有两行黄颜色的.提示你可以用‘python -m pip install --upgrade pip’升级你的pip 当时我直接就复制粘贴上去运行了.但是报了一堆红的 ...

  4. 重装windows后如何恢复mysql服务

    如果重装系统后之前的mysql还在的话,可又直接恢复,不需要安装: 注意my.ini文件的位置,应该与bin目录同级: 进入cmd,然后进入你的mysql的bin目录,输入命令:mysqld inst ...

  5. poj 1064 Cable master 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...

  6. 06. Go 语言结构体

    Go语言结构体(struct) Go 语言通过用自定义的方式形成新的类型,结构体是类型中带有成员的复合类型.Go 语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性. Go 语言中的类 ...

  7. C语言解决汉诺塔问题!

    很难受,看了很多资料才明白..... 对这个问题分析,发现思路如下:有n个黄金盘,要先把n-1个弄到B柱上,再把第n个弄到C柱上,然后把n-1个借助A柱弄到C柱上. 实现的函数如下: void f(i ...

  8. 初学Python之爬虫的简单入门

    初学Python之爬虫的简单入门 一.什么是爬虫? 1.简单介绍爬虫   爬虫的全称为网络爬虫,简称爬虫,别名有网络机器人,网络蜘蛛等等. 网络爬虫是一种自动获取网页内容的程序,为搜索引擎提供了重要的 ...

  9. django执行源生的mysql语句

    执行源生sql在python中我们可以借助pymysql模块来进行执行源生sql关于pymysql执行源生sql可以看我的另外一篇博文:pymysql应用 本篇博客只介绍django中执行的源生sql ...

  10. 大话设计模式Python实现- 享元模式

    享元模式(Flyweight Pattern):运用共享技术有效地支持大量细粒度的对象. 下面是一个享元模式的demo: #!/usr/bin/env python # -*- coding:utf- ...