如何在android 4.4.4 及更高版本中将 SELinux 设置为 0 或宽容模式?

How to set SELinux to 0 or permissive mode in android 4.4.4 and above?

我想在 android 4.4.4(及更高版本,如果可能)上将 SELinux(安全增强 Linux)模式设置为许可或 (0)。我使用以下命令:root 下的 setenforce 0setenforce permissivesetenforce Permissive(我的设备已获得 root 权限)。但是 getenforce 的输出总是 Enforcing。现在我已经厌倦了这个问题。

谁能给我一个解决方案?提前致谢。

根据您的设备的 root 方式和 Android ROM,您的 运行ning 将决定您如何禁用它。首先要尝试的是:

adb shell su 0 setenforce 0

这不同于:

adb shell setenforce 0

su 上的执行导致域从 shell(无法设置)转换到 su 域(可以调用 setenforce)。例如,运行:

$ adb shell id -Z
context=u:r:shell:s0

相比于:

$ adb shell su 0 id -Z
context=u:r:su:s0

这可能由于三个原因而失败:

  1. 您没有 su 可执行文件
  2. su 可执行文件的标签有误
  3. su 域规则未编译到引导映像中

要更正问题 2,您可以(假设 adb 是 root):

adb remount
adb shell chcon /system/xbin/su u:object_r:su_exec:s0

这可能会失败,这可能表示问题 3。要解决问题 3,您需要重新编译包含 su 策略文件的 boot.img。如果您正在编译 AOSP,只需使用您设备的 userdebug 或 eng 变体。

另一种方法是从 init.c 中删除功能,并且与问题 3 一样,需要重新编译 boot.img。进入 system/core/init/init.c(或 .cpp)并删除对 security_setenforce().

的所有调用

此外,XDA 有一个应用程序可以帮助自动执行此过程并使其更容易,但是,我无法评价该应用程序的质量: http://www.xda-developers.com/easily-change-your-android-selinux-mode/

Apparently Google 已经从他们的许多 Stock 内核中删除了 CONFIG_SECURITY_SELINUX_DEVELOP 内核标志。因此,William(下文)提到的标准技巧可能不起作用。这些设备的一个示例是 Samsung Note 4 (SM-N910F) 运行 AOS 4.4.4。

上面的 link 指出:

CONFIG_SECURITY_SELINUX_DEVELOP aka global permissive mode, is useful for when you are first developing device-specific policy for a board (add 'androidboot.selinux=permissive' to BOARD_KERNEL_CMDLINE). It also permits transient setenforce 0 in -userdebug or -eng builds, which can be helpful for developers.

If the bootloader is locked, then you can't modify the kernel cmdline

"另外,init程序中处理的代码 androidboot.selinux= 选项仅在 -userdebug-eng 构建中编译,因此即使除了引导加载程序锁定,您也不能使用 androidboot.selinux=permissive-user 版本上。”

检查您的构建类型的方法是:

$ getprop ro.build.type
user

必须 ROOT 了!不确定这是否适用于 KitKat(它应该)但我在我的 Nexus 6 上使用它。运行 在终端或 ADB Shell:

之后
su
mount -o remount,rw /system
mkdir /system/su.d
echo "#!/system/bin/sh" > /system/su.d/permissive.sh
echo "echo 0 > /sys/fs/selinux/enforce" >> /system/su.d/permissive.sh
chmod 755 /system/su.d/permissive.sh

重启后检查:

su
/system/bin/getenforce