# 安全增强型 Linux

**安全增强型 Linux（SELinux）** 是一组集成到 [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)中的强制访问控制（MAC）机制，最初由美国国家安全局（NSA）开发，作为 Flask 安全架构的一种实现。<sup>[1](https://www.usenix.org/legacy/events/usenix01/freenix01/full_papers/loscocco/loscocco_html/index.html)</sup> 标准 Linux 的自主访问控制（DAC）让文件所有者决定谁可以使用文件，而 SELinux 则让系统范围的策略来做决定，因此即使是拥有 root 权限运行的进程也受到管理员所定义规则的约束。<sup>[2](https://source.android.com/docs/security/features/selinux?authuser=531)</sup> 它是主流的 Linux 安全模块，在[红帽企业 Linux](https://www.edgepedia.org/zh/red-hat-enterprise-linux) 和 Fedora 等发行版以及 Android 操作系统中默认随附并启用。<sup>[3](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html-single/using_selinux/index)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | NSA 在 Linux 中实现 Flask 灵活 MAC 架构的原型，在 2001 年 USENIX 年度技术会议上发表<sup>[1](https://www.usenix.org/legacy/events/usenix01/freenix01/full_papers/loscocco/loscocco_html/index.html)</sup> |
| 内核集成 | 在 NSA 于 2001 年 3 月在 2.5 Linux 内核峰会上做演示之后，通过 Linux 安全模块（LSM）框架集成<sup>[4](https://kernel.org/doc/html/v5.11/security/lsm.html)</sup> |
| 安全模型 | 实现强制访问控制（MAC）、多级安全（MLS）和多类别安全（MCS）<sup>[5](https://docs.redhat.com/documentation/en-us/red_hat_enterprise_linux_atomic_host/7/html/overview_of_containers_in_red_hat_systems/introduction_to_linux_containers)</sup> |
| 决策规则 | 默认拒绝：加载的策略未明确允许的每一次访问都会被拒绝<sup>[2](https://source.android.com/docs/security/features/selinux?authuser=531)</sup> |
| 上下文格式 | 四个字段：SELinux 用户、角色、类型和安全级别；类型按惯例以 `_t` 结尾<sup>[3](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html-single/using_selinux/index)</sup> |
| 运行模式 | 强制（拒绝并记录）、宽容（仅记录）和禁用<sup>[3](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html-single/using_selinux/index)</sup> |
| 主要部署 | 红帽企业 Linux（默认强制模式）和 Android（自 Android 5.0 起全面强制）<sup>[3](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html-single/using_selinux/index)</sup><sup> • </sup><sup>[2](https://source.android.com/docs/security/features/selinux?authuser=531)</sup> |

## 起源与内核集成

NSA 与 Secure Computing Corporation 合作开发了 Flask——一种旨在克服传统强制访问控制局限性的灵活 MAC 架构，随后在 Linux 内核中实现了该架构，即 SELinux 原型，使这项技术能够为更广泛的社区所用，并推动安全操作系统的进一步研究。<sup>[1](https://www.usenix.org/legacy/events/usenix01/freenix01/full_papers/loscocco/loscocco_html/index.html)</sup> Flask 的底层设计由 Secure Computing Corporation 的 雷·斯宾塞、NSA 的斯蒂芬·斯莫利和彼得·洛斯考科 以及[犹他大学](https://www.edgepedia.org/zh/university-of-utah)的合作者共同描述，要求每一个安全决策都必须查询安全策略，并将强制执行直接集成到系统中提供服务的组件之中。<sup>[6](https://www.cs.columbia.edu/~locasto/projects/spcl/docs/research/spencer98flask.pdf)</sup>

2001 年 3 月，NSA 在 2.5 Linux 内核峰会上展示了 SELinux。当时 SELinux 作为独立的内核补丁存在，其他几个安全项目也各自维护着自己的补丁。对此，[林纳斯·托瓦兹](https://www.edgepedia.org/zh/linus-torvalds)描述了他愿意接纳入主流内核的安全框架类型，Linux 安全模块（LSM）框架遂由多个项目——包括 SELinux、Immunix、SGI 和 Janus——联合开发而成。<sup>[4](https://kernel.org/doc/html/v5.11/security/lsm.html)</sup> 尽管名称中有“模块”一词，LSM 却并不是一种可加载模块机制：安全模块在内核编译时选定，并可在启动时通过 `security=` 或 `lsm=` 命令行参数选择，且最多只有一个“主”MAC 模块处于活动状态，如 SELinux、Smack、Tomoyo 或 AppArmor。<sup>[7](https://www.kernel.org/doc/html/latest/admin-guide/LSM/index.html)</sup> SELinux 也可以在启动时通过 `selinux=0` 参数禁用，该参数的默认值为 1（启用）。<sup>[8](https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html?highlight=rootflags)</sup>

## 强制执行的工作方式

SELinux 为每个进程和系统资源附加一个安全标签，称为 SELinux 上下文。<sup>[9](https://access.redhat.com/documentation/ml-in/red_hat_enterprise_linux/9/pdf/using_selinux/red_hat_enterprise_linux-9-using_selinux-en-us.pdf)</sup> 一个上下文有四个字段：用户、角色、类型和安全级别。类型是大多数策略规则所使用的字段；进程类型也称为域，类型按惯例以 `_t` 结尾，例如 Apache Web 服务器进程的 `httpd_t` 和 `/var/www/html/` 下文件的 `httpd_sys_content_t`。<sup>[3](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html-single/using_selinux/index)</sup> 诸如 `ALLOW apache_process apache_log:FILE READ;` 这样的规则读作：Apache 进程可以读取其日志文件。<sup>[10](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/pdf/using_selinux/Red_Hat_Enterprise_Linux-10-Using_SELinux-en-US.pdf)</sup>

内核侧架构有五个核心部分：发起动作的主体、了解并执行某资源类型动作的对象管理器、决定策略是否允许该请求的安全服务器、安全策略本身，以及缓存安全服务器决策以提高性能的访问向量缓存（AVC）。<sup>[11](https://github.com/SELinuxProject/selinux-notebook/blob/main/src/core_components.md?plain=1)</sup> SELinux 通过一种特定风格的“类型强制”来执行访问控制，其中所有主体和对象都携带一个类型标识符，策略规则正是使用这些标识符。<sup>[12](https://github.com/SELinuxProject/selinux-notebook/blob/main/src/type_enforcement.md)</sup>

有两条顺序规则在实践中很重要。SELinux 策略规则在 DAC 规则之后检查：如果 DAC 已经拒绝访问，则不会评估 SELinux 规则，也不会记录 SELinux 拒绝。反过来，即使 DAC 会允许，SELinux 的拒绝也会阻止访问，因为除非规则明确授予，策略会拒绝所有交互。<sup>[3](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html-single/using_selinux/index)</sup>

## 模式与管理

SELinux 以三种模式之一运行。在强制模式下——默认且推荐的状态——加载的策略在全系统范围内强制执行。在宽容模式下，系统为对象打标签并记录本应被拒绝的操作，但不阻止任何操作，这对策略开发和调试很有用。禁用模式不被鼓励，因为系统会停止为文件等持久对象打标签，导致日后重新启用变得困难。<sup>[3](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html-single/using_selinux/index)</sup> `setenforce` 工具在强制和宽容之间切换，但重启后不保持；永久更改需通过 `/etc/selinux/config` 或内核参数进行，`getenforce` 报告当前模式。<sup>[3](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html-single/using_selinux/index)</sup>

策略及其配置位于 `/etc/selinux` 下，其中 `SELINUXTYPE` 条目指定活动策略的名称，例如 `targeted`。<sup>[11](https://github.com/SELinuxProject/selinux-notebook/blob/main/src/core_components.md?plain=1)</sup> 策略是模块化的：基础策略包含强制性定义，附加模块——通常每个应用程序或服务一个——被编译、链接并加载到安全服务器中。<sup>[11](https://github.com/SELinuxProject/selinux-notebook/blob/main/src/core_components.md?plain=1)</sup> 当应用程序因策略缺少规则而被拒绝访问时，管理员可以用 `audit2allow` 工具生成本地模块，不过红帽警告说，请求重大安全权限也可能表明应用程序已被攻破。<sup>[3](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html-single/using_selinux/index)</sup>

用户空间工具链由 SELinux 项目维护：将文本策略语言转换为内核格式的策略编译工具，`semodule` 和 `semanage` 等策略管理工具，`audit2why` 和 `audit2allow` 等开发辅助工具，供 D-Bus 和 systemd 等感知 SELinux 的应用程序使用的 `libselinux` 库，以及 `setenforce` 和 `restorecon` 等实用工具。<sup>[13](https://github.com/SELinuxProject/selinux/wiki)</sup> 在 RHEL 中，systemd 守护进程本身充当 SELinux 访问管理器，在授权服务控制操作之前检查调用进程和单元文件的标签。<sup>[3](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html-single/using_selinux/index)</sup>

## 策略来源

NSA 最初的示例策略如果缺乏对强制执行机制和策略语言的深入了解，就难以理解、开发和维护，编写策略被描述为关于 SELinux 的最大抱怨。由 Tresys Technology 发起的参考策略项目利用模块化、分层、封装和抽象重新组织了该示例策略，它也是当今几乎所有在用 SELinux 示例策略的基础。<sup>[14](https://github.com/SELinuxProject/refpolicy/blob/main/doc/WHITEPAPER.md)</sup> 它现在是用于构建 Linux SELinux 策略的标准策略源，支持受限守护进程、MLS/MCS 策略和完全锁定的系统；Fedora 将其作为 `selinux-policy-targeted`（默认）、`selinux-policy-minimum` 和 `selinux-policy-mls` 软件包分发。<sup>[15](https://github.com/SELinuxProject/selinux-notebook/blob/main/src/reference_policy.md)</sup>

## 部署

**红帽企业 Linux** 自 2.6 内核系列起就与 NSA 合作集成了 SELinux，使用 LSM 框架。<sup>[16](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/5/html/deployment_guide/ch-selinux)</sup> 默认情况下，所有 Linux 用户（包括管理员）都映射到不受限的 SELinux 用户 `unconfined_u`；将他们分配给受限用户会收紧对其进程可执行操作的控制。<sup>[3](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html-single/using_selinux/index)</sup>

**Android** 使用 SELinux 对所有进程实施 MAC，包括以 root 权限运行的进程。SELinux 基于默认拒绝运行，Android 以强制模式随附它，其策略在 Android 开源项目中通用。Android 4.3 引入了宽容版本，Android 4.4 要求对选定的系统域强制执行，Android 5.0 及更高版本在 60 多个域中全面强制执行 SELinux；后续版本逐步收紧策略，包括在 Android 6.0 中改善用户隔离和减少 `/proc` 访问。<sup>[2](https://source.android.com/docs/security/features/selinux?authuser=531)</sup>

**容器与虚拟化** 依赖 SELinux 提供仅靠命名空间和 cgroup 无法提供的隔离层。命名空间和 cgroup 隔离进程并限制资源使用，但它们本身无法阻止恶意进程突破容器。SELinux 通过基于 libvirt 构建的 sVirt 项目，防止容器内的 root 进程干扰容器外的进程，使用 Docker 创建的容器会自动从策略中分配一个 SELinux 上下文。如果主机上的 SELinux 被禁用或处于宽容模式，容器的隔离就不够安全。<sup>[5](https://docs.redhat.com/documentation/en-us/red_hat_enterprise_linux_atomic_host/7/html/overview_of_containers_in_red_hat_systems/introduction_to_linux_containers)</sup>

## 性能

LSM 钩子的放置对文件操作带来了可测量的开销。一项在 Linux v5.3 上的测量研究发现，当内核集成了 SELinux 钩子但策略强制执行被禁用时，打开文件显示出约 87% 的性能下降，而 Linux v2.4.2 上为 27%；作者将差异归因于策略强制执行和钩子放置两方面，因为多年来授权钩子的数量几乎翻了一番。<sup>[17](https://arxiv.org/abs/2101.11611)</sup> 2001 年最初的 SELinux 论文也报告了原型的微基准和宏基准性能数据，使性能评估从一开始就成为项目的一部分。<sup>[1](https://www.usenix.org/legacy/events/usenix01/freenix01/full_papers/loscocco/loscocco_html/index.html)</sup>

## 与 AppArmor 的比较

SELinux 和 AppArmor 是两种广泛部署的 Linux 安全模块，两者都对应用程序进行隔离以限制攻击者可利用的攻击面，但它们的工作方式不同。<sup>[18](https://www.techtarget.com/it-infrastructure/tip/Compare-two-Linux-security-modules-SELinux-vs-AppArmor)</sup> AppArmor 应用从用户空间加载的以任务为中心的配置文件，没有配置文件的任务不受限制地运行，等同于标准 Linux DAC 权限；SELinux 则相反，为每个主体和对象打标签，并拒绝策略不允许的任何操作。<sup>[19](https://kernel.org/doc/html/latest/admin-guide/LSM/apparmor.html)</sup><sup> • </sup><sup>[2](https://source.android.com/docs/security/features/selinux?authuser=531)</sup> 一项比较 SELinux、AppArmor 和 FBAC-LSM 模型的可用性研究发现，专业知识有限的最终用户难以有效使用现有机制，凸显了两种主流方法的一个已知局限。<sup>[20](https://researchportal.murdoch.edu.au/esploro/outputs/journalArticle/Empowering-end-users-to-confine-their/991005540115207891)</sup>

## References

1. [Peter Loscocco and Stephen Smalley (2001). Integrating Flexible Support for Security Policies into the Linux Operating System. USENIX Annual Technical Conference, FREENIX Track.](https://www.usenix.org/legacy/events/usenix01/freenix01/full_papers/loscocco/loscocco_html/index.html)
2. [Security-Enhanced Linux in Android, Android Open Source Project](https://source.android.com/docs/security/features/selinux?authuser=531)
3. [Using SELinux, Red Hat Enterprise Linux 10 documentation](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html-single/using_selinux/index)
4. [Linux Security Modules: General Security Hooks for Linux, kernel.org documentation](https://kernel.org/doc/html/v5.11/security/lsm.html)
5. [Introduction to Linux Containers, Red Hat Enterprise Linux Atomic Host 7 documentation](https://docs.redhat.com/documentation/en-us/red_hat_enterprise_linux_atomic_host/7/html/overview_of_containers_in_red_hat_systems/introduction_to_linux_containers)
6. [Ray Spencer, Stephen Smalley, Peter Loscocco, Mike Hibler, David Andersen, Jay Lepreau (1999). The Flask Security Architecture: System Support for Diverse Security Policies.](https://www.cs.columbia.edu/~locasto/projects/spcl/docs/research/spencer98flask.pdf)
7. [Linux Security Module Usage, kernel.org documentation](https://www.kernel.org/doc/html/latest/admin-guide/LSM/index.html)
8. [The kernel's command-line parameters, kernel.org documentation](https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html?highlight=rootflags)
9. [Using SELinux, Red Hat Enterprise Linux 9 documentation (PDF)](https://access.redhat.com/documentation/ml-in/red_hat_enterprise_linux/9/pdf/using_selinux/red_hat_enterprise_linux-9-using_selinux-en-us.pdf)
10. [Using SELinux, Red Hat Enterprise Linux 10 documentation (PDF)](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/pdf/using_selinux/Red_Hat_Enterprise_Linux-10-Using_SELinux-en-US.pdf)
11. [Core SELinux Components, The SELinux Notebook](https://github.com/SELinuxProject/selinux-notebook/blob/main/src/core_components.md?plain=1)
12. [Type Enforcement, The SELinux Notebook](https://github.com/SELinuxProject/selinux-notebook/blob/main/src/type_enforcement.md)
13. [SELinux Userspace wiki, SELinux Project](https://github.com/SELinuxProject/selinux/wiki)
14. [Christopher J. PeBenito, Frank Mayer, Karl MacMillan. Reference Policy for Security Enhanced Linux (whitepaper)](https://github.com/SELinuxProject/refpolicy/blob/main/doc/WHITEPAPER.md)
15. [The Reference Policy, The SELinux Notebook](https://github.com/SELinuxProject/selinux-notebook/blob/main/src/reference_policy.md)
16. [Introduction to SELinux, Red Hat Enterprise Linux 5 Deployment Guide](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/5/html/deployment_guide/ch-selinux)
17. [Analyzing the Overhead of Filesystem Protection Using Linux Security Modules (arXiv:2101.11611)](https://arxiv.org/abs/2101.11611)
18. [Compare two Linux security modules: SELinux vs. AppArmor, TechTarget](https://www.techtarget.com/it-infrastructure/tip/Compare-two-Linux-security-modules-SELinux-vs-AppArmor)
19. [AppArmor, kernel.org documentation](https://kernel.org/doc/html/latest/admin-guide/LSM/apparmor.html)
20. [Z.C. Schreuders, T. McGill, C. Payne (2011). Empowering end users to confine their own applications: The results of a usability study comparing SELinux, AppArmor and FBAC-LSM. ACM Transactions on Information and System Security 14(2).](https://researchportal.murdoch.edu.au/esploro/outputs/journalArticle/Empowering-end-users-to-confine-their/991005540115207891)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Unix-like and Linux › Unix, BSD, and kernel internals

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/security-enhanced-linux

English original: https://www.edgepedia.org/security-enhanced-linux
