Jellybean MR2 及更高版本的大多数设备都将在其设备上启用 SELinux,但如果您与 OEM 合作或进行平台工作,这可能不一定正确。
我用来验证的方法是使用 getenforce shell 命令:
public boolean isSeLinuxEnforcing() {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec("getenforce");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line);
}
} catch (Exception e) {
Log.e(TAG, "OS does not support getenforce");
// If getenforce is not available to the device, assume the device is not enforcing
e.printStackTrace();
return false;
}
String response = output.toString();
if ("Enforcing".equals(response)) {
return true;
} else if ("Permissive".equals(response)) {
return false;
} else {
Log.e(TAG, "getenforce returned unexpected value, unable to determine selinux!");
// If getenforce is modified on this device, assume the device is not enforcing
return false;
}
}
似乎大多数设备仅在未以强制状态运行时才为 selinux 编写系统属性。您还可以检查属性:ro.boot.selinux以查看内核是否在当前构建中传递了许可参数。