【问题标题】:Android Binder clearing caller identityAndroid Binder 清除调用者身份
【发布时间】:2013-04-07 00:31:16
【问题描述】:

最近看了Android的源码,发现在做一些Binder的IPC调用时,总是调用一对方法。我阅读了评论,但我无法清楚地知道根本原因。配对方法如下:

final long origId = Binder.clearCallingIdentity();

//other local method.

Binder.restoreCallingIdentity(origId);

有谁知道这对方法的作用是什么?好像和权限有关。

【问题讨论】:

    标签: android android-source android-binder


    【解决方案1】:

    虽然问题比较老了,但是除了官方的方法描述之外,还是值得多放点细节的。


    除了(或连同)IPC,Binder 框架在Android 中的关键作用是安全性。

    每个Binder 事务在调用进程(caller)的身份(PID 和 UID)下运行,以便被调用进程(callee)可以检查调用进程的权限,并决定请求的方法是否可以执行。

    如果这样的事务需要(临时)在 callee's 身份下运行,则可以清除 caller 身份,然后通过调用Binder.clearCallingIdentity()Binder.restoreCallingIdentity(long) 分别。在调用之间,将检查被调用者的权限。

    作为示例考虑系统服务(AOSP 位置:/frameworks/base/services/java/com/android/server)。运行在system_server 进程UID=1000 中的服务可以暂时清除调用者的身份以通过权限检查。

    【讨论】:

      【解决方案2】:

      我认为我不能比官方 API 中的描述更好地回答:http://developer.android.com/reference/android/os/Binder.html

      public static final long clearCallingIdentity ()

      重置当前线程上传入 IPC 的标识。如果在处理传入呼叫时,您将调用可能是您的进程本地的其他对象的接口并且需要对传入它们的调用进行权限检查(因此他们将检查您自己的权限),这将很有用本地进程,而不是最初调用您的任何进程)。

      【讨论】:

      • 如果 AIDL 调用和 clearCallingIdentity() 在不同的线程上怎么办? clearCallingIdentity() 的行为如何?
      猜你喜欢
      • 1970-01-01
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      • 1970-01-01
      • 2010-12-14
      • 1970-01-01
      • 2014-10-20
      相关资源
      最近更新 更多