Android framework 编程之 - Binder调用方UID

注:不要弄混 uid 和 userID 的区别。

1. 获取Binder 调用方UID

java 复制代码
	Binder.getCallingUid

    /**
     * Return the Linux UID assigned to the process that sent you the
     * current transaction that is being processed. This UID can be used with
     * higher-level system services to determine its identity and check
     * permissions. If the current thread is not currently executing an
     * incoming transaction, then its own UID is returned.
     */
    @CriticalNative
    @android.ravenwood.annotation.RavenwoodReplace
    public static final native int getCallingUid();

2. 判断UID 是否是特殊UID

例如判断是否是System UID

java 复制代码
uid == Process.SYSTEM_UID

特殊 UID 的列表

这里仅仅列出一部分

frameworks/base/core/java/android/os/Process.java

java 复制代码
    /**
     * An invalid UID value.
     */
    public static final int INVALID_UID = -1;

    /**
     * Defines the root UID.
     */
    public static final int ROOT_UID = 0;

    /**
     * Defines the UID/GID under which system code runs.
     */
    public static final int SYSTEM_UID = 1000;

    /**
     * Defines the UID/GID under which the telephony code runs.
     */
    public static final int PHONE_UID = 1001;

    /**
     * Defines the UID/GID for the user shell.
     */
    public static final int SHELL_UID = 2000;

将 Binder 调用方 UID 变为本地 UID ,豁免部分权限检查

java 复制代码
	// 将调用标识变为本地进程,相对于System Server 就是 system server 进程
	final long token = Binder.clearCallingIdentity();
	try {
		// 权限检查的是本地进程。
	} finally {
		// 将调用标识变为非本地标识
		Binder.restoreCallingIdentity(token);
	}
相关推荐
fatsheep洋1 小时前
文件上传 --- uploadlabs靶场
android
雨白10 小时前
Android 自定义 View:从绘制基础到实战仪表盘与饼图
android
jiunian_cn10 小时前
【Linux】线程
android·linux·运维·c语言·c++·后端
Frank_HarmonyOS19 小时前
Android MVVM(Model-View-ViewModel)架构
android·架构
新子y1 天前
【操作记录】我的 MNN Android LLM 编译学习笔记记录(一)
android·学习·mnn
lincats1 天前
一步一步学习使用FireMonkey动画(1) 使用动画组件为窗体添加动态效果
android·ide·delphi·livebindings·delphi 12.3·firemonkey
想想吴1 天前
Android.bp 基础
android·安卓·android.bp
写点啥呢1 天前
Android为ijkplayer设置音频发音类型usage
android·音视频·usage·mediaplayer·jikplayer
coder_pig2 天前
🤡 公司Android老项目升级踩坑小记
android·flutter·gradle
死就死在补习班2 天前
Android系统源码分析Input - InputReader读取事件
android