在我们的 Android Service 应用程序中,我们使用 JNI 来调用共享库的 C++ 代码。在此代码中,我们调用 C“系统”命令来执行另一个进程。该代码在所有 Android 操作系统上都能完美运行,但现在在 Android 8 Oreo 上失败。 是否有任何新的安全性或其他限制阻止执行系统命令?
更多数据:
此“c”代码已从我的共享库成功执行:
int error = system("ls -l/sdcard/tmp >>/sdcard/tmp/log.txt");
文件 log.txt 按预期填充,错误 = 0;
但是此命令始终失败,并出现意外错误代码 (40704):
int error = system("/system/bin/7za t/sdcard/tmp/Camera.7z >>/sdcard/tmp/log.txt");
我可以从 shell(“adb shell”)执行此命令。 7za 是一个兼容的 Android 应用程序,它没有可写和可执行段(readelf --program-headers -W libBadFlags.so | grep WE)。
我尝试在已 root 的 Android 设备上的多个位置使用不同的权限找到 7za,但仍然遇到相同的问题。
请您参考如下方法:
Native libraries
In apps targeting Android 8.0 (API level 26), native libraries no longer load if they contain any load segment that is both writable and executable. Some apps might stop working because of this change if they have native libraries with incorrect load segments. This is a security-hardening measure.
有关详细信息,请参阅 Writable and Executable Segments .
Linker changes are tied to the API level that an app targets. If there is a linker change at the targeted API level, the app cannot load the library. If you are targeting an API level lower than the API level where the linker change occurs, logcat shows a warning.
我猜你需要在 Mainfest.xml
中设置 android:extractNativeLibs="false"
- 至少文档是这么说的;所以这可能值得一试......和/或 native 库的 fragment 需要以不同的方式排列。
这在某种程度上是一个相当理论化的问题,除非有某种可用的错误消息,在启用 native 调试器时应该抛出该错误消息。