到目前为止,我已经尝试了官方网站和其他在线资源上提供的几种方法。比如通过执行
getenforce
命令得到结果。
private fun isSELinuxEnabled(): () -> Boolean {
return {
val process = Runtime.getRuntime().exec(arrayOf("getenforce"))
val reader = BufferedReader(InputStreamReader(process.inputStream))
val status = reader.readLine()
process.waitFor()
Log.d(TAG, "isSELinuxEnabled(): $status")
status == "enforcing"
}
}
我还尝试使用以下 JNI 来获取状态:
FILE *file = fopen("/sys/fs/selinux/enforce", "r");
if (file == NULL) {
return (*env)->NewStringUTF(env, "Unknown"); // Return "Unknown" if the file cannot be opened
}
char status[10];
fgets(status, sizeof(status), file);
fclose(file);
if (strcmp(status, "1\n") == 0) {
return (*env)->NewStringUTF(env, "Enforcing");
} else {
return (*env)->NewStringUTF(env, "Permissive");
}
此外,还可以使用一些属性,例如
ro.boot.selinux
和 ro.build.selinux
来获取 SELinux 状态。到目前为止,没有运气,没有一个设备产生任何结果,所有方法都返回 null 或未知值,并且从不强制或许可,即使我使用 adb shell getenfoce
验证启用了强制。
adb 命令在我系统的终端中工作,但即使从应用程序执行该命令,也会产生相同的结果。
各位,这是我最迫切的问题:有没有可靠的方法可以使用 JNI 或纯 Kotlin/Java 来获取 Android 中 SELinux 的状态?
我使用Java Reflection API来获取系统状态信息,并且我已经验证该方法适用于最新的API版本,即API 34。
public static String getviarefSelinuxFlag(Context ctx) {
try {
Class<?> cls = Class.forName("android.os.SELinux");
Object newInstance = cls.getConstructor(Context.class).newInstance(ctx);
Method method = cls.getMethod("isSELinuxEnforced");
Method method1 = cls.getMethod("isSELinuxEnabled");
Boolean SELinuxEnforced = (Boolean) method.invoke(null);
Boolean SELinuxEnabled = (Boolean) method1.invoke(null);
return "isSELinuxEnforced : " + SELinuxEnforced + "\nisSELinuxEnabled : " + SELinuxEnabled;
} catch (Exception ignored) {
return "Method not working";
}
}