Blog:
用 Yocto 生成支持 SELinux 的BSP

Monday, March 1, 2021

SELinux 是 Security-Enhanced Linux 的简称,它是为 Linux 提供安全子系统的内核模块。其主要作用是控制进程对资源的访问,在基于用户权限的 DAC 之外对进程提供更加精细的强制访问控制( MAC)。在常见的 Linux 发行版中 SELinux 功能可以通过安装相应的软件来开启,但在嵌入式 Linux 设备上往往需要重新生成 BSP。本文接下来将介绍如何使用 Yocto 为 Apalis iMX8 计算机模块生成支持 SELinux 的 BSP。

Yocto 编译环境搭建可以参考这里。如果无法直接下载 repo 文件,也可以使用国内镜像。配置完成后,下载用于编译 SELinux 的 meta-selinux layer。Linux BSP 5.x 使用 dunfell 分支,用 git 命令下载相同版本的 meta-selinux。

$ cd layers
$ git clone -b dunfell git://git.yoctoproject.org/meta-selinux


在 conf/local.conf 文件的结尾添加以下内容。

DISTRO_FEATURES_append = " xattrs pam selinux"
PREFERRED_PROVIDER_virtual/refpolicy ?= "refpolicy-minimum"
IMAGE_INSTALL_append = " packagegroup-core-full-cmdline packagegroup-core-selinux"


Toradex 提供精简地 minimal console 和包含 Qt5 和 gstreamer 等的 multimedia 两种参考镜像,配置文件位于 layers/meta-toradex-demos/recipes-images/images 目录。根据需要,在 tdx-reference-minimal-image.bb 或者 tdx-reference-multimedia-image.bb 的 inherit 后面添加 selinux-image。

$ vi tdx-reference-minimal-image.bb
inherit core-image selinux-image

SUMMARY = "Toradex Embedded Linux Reference Minimal Image"
DESCRIPTION = "Minimal image without graphical interface that just boots"

$ vi tdx-reference-multimedia-image.bb
require tdx-reference-minimal-image.bb

SUMMARY = "Toradex Embedded Linux Reference Multimedia Image"
DESCRIPTION = "Image for BSP verification with QT and multimedia features"

inherit populate_sdk_qt5 selinux-image


默认的 Linux 内核配置是没有添加 SELinux 支持,因此在生成 BSP 之前做相应的修改。

$ MACHINE=apalis-imx8 bitbake -c menuconfig virtual/kernel


在“General setup“ 和 ”Security options“ 中开启以下功能。

CONFIG_AUDIT=y
CONFIG_SECURITYFS=y
CONFIG_SECURITY_NETWORK=y
CONFIG_SECURITY_SELINUX=y
CONFIG_SECURITY_SELINUX_BOOTPARAM=y
CONFIG_SECURITY_SELINUX_DISABLE=y
CONFIG_SECURITY_SELINUX_DEVELOP=y
CONFIG_SECURITY_SELINUX_AVC_STATS=y
CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE=0
CONFIG_DEFAULT_SECURITY_SELINUX=y


最后编译所需的镜像,这里使用 minimal console 镜像。

$ MACHINE=apalis-imx8 bitbake tdx-reference-minimal-image


生成的 BSP 使用 Toradex Easy Installer 安装到 Apalis iMX8 计算机模块上。

开机启动后将 SELinux 配置为 permissive 模式,由于没有配置任何策略,采用默认的 enforcing 模式会阻止进程对相关资源文件的访问,从而影响启动。所以在配置相应策略前,permissive 模式仅记录进程的操作但不进行阻止。

root@apalis-imx8:~# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=permissive

root@apalis-imx8:~# sync


保存配置后重启。在调试串口中按空格进入 U-Boot 命令模式,运行下面命令。

Apalis iMX8 # setenv defargs security=selinux selinux=1
Apalis iMX8 # saveenv
Apalis iMX8 # reset


重启后可以看到 SELinux 相关日志,完成首次初始化后系统会自动重启。

[    0.001418] SELinux:  Initializing.
[    4.652222] SELinux:  Permission watch in class filesystem not defined in policy.
[    4.660034] SELinux:  Permission watch in class file not defined in policy.
[    4.667029] SELinux:  Permission watch_mount in class file not defined in policy.
[    4.674527] SELinux:  Permission watch_sb in class file not defined in policy.
[    4.681764] SELinux:  Permission watch_with_perm in class file not defined in policy.
[    4.689612] SELinux:  Permission watch_reads in class file not defined in policy.
[    4.697116] SELinux:  Permission watch in class dir not defined in policy.
[    4.704003] SELinux:  Permission watch_mount in class dir not defined in policy.


进入系统后查看 SELinux 运行状态。

root@apalis-imx8:~# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             minimum
Current mode:                   permissive
Mode from config file:          permissive
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Memory protection checking:     actual (secure)
Max kernel policy version:      31


至此用户可以根据项目需要创建对应的安全策略,并修改 /etc/selinux/config 使其生效。

总结

SELinux 为系统提供更加完善的访问控制,通过制定合适的安全策略,使系统更加安全、可靠地运行。

Author: 胡珊逢,FAE,韬睿(上海)

Leave a comment

Please login to leave a comment!
Have a Question?