Android 11在libc层拦截I/O调用
需要在Android 11的Libc层拦截系统调用。之前在网络上找到一篇博文,说的是修改下述汇编文件:(以read调用为例)
bionic/libc/arch-arm/syscalls/read.S
博文地址:
经实践,上述方法在Android 10版本是适用的。估计在Android 10以前的版本也适用。
但是,Android 11没有bionic/libc/arch-arm/syscalls/ 文件夹,也没有read.S等汇编文件,需要我们探索新的方法。
查看SYSCALLS.TXT
打开bionic/libc/SYSCALLS.txt,查看里面的注释,里面有这么一句:
This file is used to automatically generate bionic’s system call stubs.
…
…
This file is processed by a python script named gensyscalls.py, run via genrules in Android.bp.
大意是:编译时,会根据这个SYSCALLS.txt来自动生成系统调用的.S文件。
也就是说,在Android 11 中,bionic/libc/arch-arm/syscalls/这个文件夹的汇编文件是自动生成的,且编译时才会生成。
下面以close调用为例说明拦截方法。
1. 修改SYSCALLS.TXT
在SYSCALLS.TXT中找到:
> int __close:close(int) all
修改为:
> int ____close:close(int) all
即加两个下划线。随后编译生成的系统调用函数名便会变为____close(int)
2. 编写__close.cpp
-
在bionic/libc/bionic中新建__close.cpp文件
-
在__close.cpp中添加如下代码:
#include <unistd.h>
#include <sys/__close.h>
//真正的系统调用
extern "C" int ____close(int);
//我们拦截并修改的系统调用
int __close(int f){
/* 此处添加你的代码 */
/* ........................... */
return ____close(f);
}
这样原来调用__close()的程序会进入到我们的__close()函数中。
3. 编写__close.h
- 在bionic/libc/include/sys中新建__close.h文件
- 编写__close.h文件:
#include <linux/unistd.h>
#include <sys/cdefs.h>
#include <sys/stat.h>
__BEGIN_DECLS
extern int __close(int);
__END_DECLS
4. 修改编译脚本
- 打开文件bionic/libc/Android.bp
- 搜索"bionic/open.cpp",来到它附近。这个位置有一堆cpp。
- 在附近一行插入如下(注意加逗号)
"bionic/__close.cpp",