【问题标题】:SELinux rules for i2c files in sysfs in AndroidAndroid sysfs 中 i2c 文件的 SELinux 规则
【发布时间】:2014-12-01 10:31:18
【问题描述】:

我为我的一个 I2C 设备创建了内核驱动程序作为可加载模块。驱动通过new_device文件(/sys/devices/i2c/i2c-0/new_device)实例化在I2C对应文件夹(/sys/devices/i2c/i2c-0/0-0008/)下创建少量sysfs文件。

Lollipop 强制执行 SELinux,因此我需要为需要访问设备 sysfs 文件的应用程序创建规则。这些大多是系统应用程序(它们属于 Android SELinux 中 platform_app 的定义)。问题是任何应用程序域中的应用程序都不允许写入 sysfs 文件:

neverallow { appdomain -bluetooth -nfc }
    sysfs:dir_file_class_set write;

所以我决定专门为我的设备创建文件上下文:

file_context:

/sys/devices/i2c-0/0-0008(/.*)?                     u:object_r:sysfs_mydeviceic:s0

结果很有趣:默认驱动程序文件和文件夹(如 name 和 uevent 等)获得了正确的上下文,但不是由 I2C 驱动程序的 sysfs 部分创建的文件:

root@android:/sys/devices/i2c-0/0-0008 # ls -Z
--w--w--w- root     root              u:object_r:sysfs:s0 data
lrwxrwxrwx root     root              u:object_r:sysfs_mydeviceic:s0 driver -> ../../../bus/i2c/drivers/mydevice
-rw-rw-rw- root     root              u:object_r:sysfs:s0 locked
-r--r--r-- root     root              u:object_r:sysfs_mydeviceic:s0 modalias
-r--r--r-- root     root              u:object_r:sysfs_mydeviceic:s0 name
drwxr-xr-x root     root              u:object_r:sysfs_mydeviceic:s0 power
-rw-rw-rw- root     root              u:object_r:sysfs:s0 protection
-rw-rw-rw- root     root              u:object_r:sysfs:s0 state
lrwxrwxrwx root     root              u:object_r:sysfs_mydeviceic:s0 subsystem -> ../../../bus/i2c
-rw-r--r-- root     root              u:object_r:sysfs_mydeviceic:s0 uevent

我正在寻求如何解决这个问题的帮助:如果我仍然想将 sysfs 上下文转换为 sysfs_mydeviceic 以用于其余文件,那么该怎么做?还是有其他方法可以让应用程序写入 sysfs 文件?

【问题讨论】:

  • 我也在查看解决方案,你找到解决方案了吗?

标签: android linux-kernel i2c selinux sysfs


【解决方案1】:

我在将一些驱动程序移植到 Android 5 时遇到了同样的问题。似乎并非所有 sysfs 文件都放入了正确的 selinux 上下文中。这似乎只发生在通过 new_device 动态实例化的设备上。

我的解决方案是触发 selinux 使用restorecon(8) 恢复文件上下文。 在用于实例化设备的同一脚本中,执行以下命令:

restorecon -r /sys/devices/i2c-2/

-r 标志告诉restorecon 以递归方式工作。如果您愿意,还可以单独列出每个文件。

如果您使用安卓初始化脚本(例如/init.rc),还有另一个可用的命令:

restorecon_recursive /sys/devices/i2c-2/

同样,restorecon 命令只恢复单个文件。 详情请见SEAndroid documentation

【讨论】:

  • 这个解决方案也对我有用!非常感谢。
猜你喜欢
  • 1970-01-01
  • 2021-10-05
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
  • 2013-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多