gpt4 book ai didi

安卓框架 : System service does not get permission(s)

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:51 25 4
gpt4 key购买 nike

我在 Android 框架中添加了一个系统服务(不是应用程序)(因此在 system_process 中运行)。通过 Binder.getCallingUid() 我可以确定调用进程/应用程序。到目前为止,一切都很好。但是,如果我的服务尝试使用其他系统服务(例如 LocationManager),则会抛出 SecurityException,因为 LocationManager 认为它是由调用我的服务的原始应用调用的。

据我了解,系统服务默认拥有所有权限,所以不应该是这样吧?

From programming4.us/Mobile/1304.aspx: Binder services are free to make other binder calls, but these calls always occur with the service’s own identity (UID and PID) and not the identity of the caller.

这里有一些代码来说明问题:

public class MyService implements IInterface {

public IBinder asBinder() {
return mBinder;
}

private final IMyService.Stub mBinder = new IMyService.Stub() {

public void doSomething() {
int uid = Binder.getCallingUid(); // uid of the calling app
int myUid = Process.myUid(); // my uid == 1000
...
try {
ILocationManager lm = ILocationManager.Stub.asInterface(ServiceManager.getService("location"));
Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} catch (Exception e) {
SecurityException is thrown. Requires ACCESS_FINE_LOCATION
}
}

};
}

在此先感谢您的帮助或意见!

最佳答案

我自己还没有尝试过(但是,我会在星期一来,因为我目前正面临您所描述的情况):Binder 有两个方法“clearCallingIdentity”和“restoreCallingIdentity”,它们可能对我们。如果我对文档的解释正确,那么我认为它们是这样工作的。

第一种方法清除传入 IPC 的身份,以便在您访问 LocationManager 时使用您自己的身份而不是调用者的身份。它返回一个 long 值,您随后将其传递给第二个方法以恢复调用者身份。这应该使您能够通过 LM 的权限检查,因为您将有效地从 system_process(即 LM 所在的同一进程)调用它。

关于安卓框架 : System service does not get permission(s),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4865199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com