转自: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。

最新文章

  1. python——进程基础
  2. 关于float高度塌陷问题
  3. Nginx问题定位之监控进程异常退出
  4. 如何在服务器上搭建git服务器
  5. 项目差异class文件提取--&gt;上线用
  6. [转]Hive:简单查询不启用Mapreduce job而启用Fetch task
  7. GDB 使用大法
  8. spring mvc视频
  9. 【POJ 1639】 Picnic Planning (最小k度限制生成树)
  10. Web App 图片上传编辑器
  11. PCB的整个加工流程
  12. mobilize扁平化的fullPage.js类工具使用心得
  13. 在vc中使用xtremetoolkit界面库-----安装及环境配置
  14. Spring.Net+NHibenate+Asp.Net Mvc+Easyui框架
  15. jqGrid数据表格
  16. Servlet中的跳转(redirect和forward)
  17. spring boot + vue + element-ui全栈开发入门——前端编辑数据对话框
  18. 继承中的prototype与_proto_
  19. HTTP一、HTTP介绍与套接字
  20. [P2671][NOIP2015]求和 (数论)

热门文章

  1. 分布式的cap原理
  2. Java Virtual Machine (JVM), Difference JDK, JRE &amp; JVM – Core Java
  3. Redis实战(一)Redis简介及环境安装(Windows)
  4. Java生鲜电商平台-订单模块状态机架构设计
  5. HTTP中的Accept-Encoding、Content-Encoding、Transfer-Encoding、Content-Type
  6. SAP 不支持交货单中同一个物料多个行项目HU与序列号组合发货场景
  7. Android 上下文菜单 PopupMenu
  8. Zookeeper学习(一)
  9. [20191119]探究ipcs命令输出2.txt
  10. 微信小程序之上传图片和图片预览