NXP iMX8集成SELinux支持

By

Toradex胡珊逢

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

iMX8计算机模块生成支持SELinux 的BSP。


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

----------------------------------

$ cd layers

$ git clone -bdunfell 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

inheritcore-imageselinux-image


SUMMARY ="Toradex Embedded Linux Reference Minimal Image"

DESCRIPTION ="Minimal image without graphical interface that just boots"


$ vi tdx-reference-multimedia-image.bb

requiretdx-reference-minimal-image.bb


SUMMARY ="Toradex Embedded Linux Reference Multimedia Image"

DESCRIPTION ="Image for BSP verification with QT and multimedia features"


inheritpopulate_sdk_qt5selinux-image

----------------------------------


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

----------------------------------

$ MACHINE=apalis-imx8bitbake -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 SELinuxon the system.

# SELINUX= can take one of these threevalues:

#    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 defargssecurity=selinux selinux=1

Apalis iMX8 # saveenv

Apalis iMX8 # reset

----------------------------------


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

----------------------------------

[   0.001418] SELinux:  Initializing.

[   4.652222] SELinux:  Permissionwatch in class filesystem not defined in policy.

[   4.660034] SELinux:  Permissionwatch in class file not defined in policy.

[   4.667029] SELinux:  Permissionwatch_mount in class file not defined in policy.

[   4.674527] SELinux:  Permissionwatch_sb in class file not defined in policy.

[   4.681764] SELinux:  Permissionwatch_with_perm in class file not defined in policy.

[   4.689612] SELinux:  Permissionwatch_reads in class file not defined in policy.

[   4.697116] SELinux:  Permissionwatch in class dir not defined in policy.

[   4.704003] SELinux:  Permissionwatch_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为系统提供更加完善的访问控制,通过制定合适的安全策略,使系统更加安全、可靠地运行。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容