# 地址空间布局随机化

**地址空间布局随机化**（ASLR）是一种内存保护技术：操作系统在程序每次运行时，将进程的栈、堆、库和可执行代码放置在不可预测的地址上，使利用内存破坏漏洞的攻击者无法把漏洞利用所需的地址硬编码。Apple 的安全指南概括了这一目标：通过让内存地址和偏移量不可预测，漏洞利用代码就无法硬编码这些值。<sup>[1](https://help.apple.com/pdf/security/en_US/apple-platform-security-guide.pdf)</sup> ASLR 是现代操作系统中部署最广泛的内存安全缓解措施之一，在大多数当前 Linux 发行版上默认启用。<sup>[2](https://documentation.ubuntu.com/security/security-features/process-memory/aslr/)</sup>

| 关键事实 | 详情 |
|---|---|
| 随机化的对象 | 栈、堆、mmap 基址（共享库与 vDSO），以及对位置无关可执行文件而言的代码起始地址<sup>[3](https://docs.kernel.org/admin-guide/sysctl/kernel.html?highlight=randomize_va_space)</sup> |
| Linux 控制方式 | 通过 `/proc/sys/kernel/randomize_va_space` 全系统控制：0 关闭，1 部分（栈、mmap 基址、vDSO），2 完全（增加堆）；当 `CONFIG_COMPAT_BRK` 被禁用时，2 为默认值<sup>[3](https://docs.kernel.org/admin-guide/sysctl/kernel.html?highlight=randomize_va_space)</sup><sup> • </sup><sup>[2](https://documentation.ubuntu.com/security/security-features/process-memory/aslr/)</sup> |
| 分工 | 内核随机化初始栈和堆的布局；用户空间 ELF 加载器随机化可执行文件及其共享库<sup>[2](https://documentation.ubuntu.com/security/security-features/process-memory/aslr/)</sup> |
| 32 位熵 | 32 位系统上有效可用的仅 16 位（Linux 上为 8 位、256 个值），暴力破解可在毫秒级攻破<sup>[4](https://www.mdpi.com/2076-3417/12/13/6702)</sup> |
| 64 位 Linux 熵 | 在 64 位 Ubuntu 系统上实测，栈、堆和代码段的熵约为 16 至 19 位<sup>[5](https://jis.athabascau.ca/index.php/jis/article/view/490)</sup> |
| 内核变体 | KASLR 在每次启动时重定位内核的物理和虚拟基址（`CONFIG_RANDOMIZE_BASE`）；Ubuntu 自 16.10 起默认启用<sup>[6](https://www.kernel.org/doc/html/latest/security/self-protection.html?highlight=kaslr)</sup><sup> • </sup><sup>[7](https://documentation.ubuntu.com/security/security-features/kernel-protections/)</sup> |
| 已知弱点 | 内存泄露可击败 ASLR：一旦泄露一个地址，布局即可被推导出来，因此可靠的漏洞利用通常除内存破坏漏洞外还需要一个信息泄露漏洞<sup>[5](https://jis.athabascau.ca/index.php/jis/article/view/490)</sup> |

## 工作原理

进程的虚拟地址空间被划分为具有常规用途的多个区域：可执行代码及其静态数据、运行时增长的堆、由 `mmap` 创建的内存映射（共享库就落在这里），以及栈。在没有随机化的情况下，这些区域从由 ABI 确定的固定且众所周知的地址开始，因此越过缓冲区末尾写入数据的攻击者确切知道该覆盖哪个返回地址或函数指针。ASLR 在创建进程时给每个区域的基址加上一个随机偏移量，对可执行文件、映射区和栈区分别独立选取。<sup>[8](https://web.stanford.edu/~blp/papers/asrandom.pdf)</sup>

随机化由内核和用户空间共同实现：内核随机化初始进程布局，即栈和堆，而用户空间 ELF 加载器随机化可执行文件及其共享库的位置。<sup>[2](https://documentation.ubuntu.com/security/security-features/process-memory/aslr/)</sup> 随机化可执行文件自身的代码要求二进制文件以位置无关可执行文件（PIE）形式构建，因为普通可执行文件包含链接时固定的绝对地址。在 macOS 和 iOS 上，编译器提供了这一支持：要求使用位置无关代码，且针对 macOS 10.7 及以后或 iOS 4.3 及以后的可执行文件，必要标志已默认启用。<sup>[9](https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/BufferOverflows.html)</sup>

ASLR 补充而非替代其他缓解措施。它提高了返回 libc（return-to-libc）和面向返回的编程（ROP）攻击的门槛，这类攻击重用已知地址处的现有代码；数据执行防止则另行阻止注入的 shellcode 在栈或堆上运行。<sup>[10](https://arxiv.org/abs/1008.4099)</sup> PaX 项目——一套 [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)补丁，其 ASLR 设计文档可追溯至 2003 年——为 ELF 二进制文件和动态库开创了这一技术，并在加载和链接时随机化栈、堆、代码和 mmap 段的基址。<sup>[8](https://web.stanford.edu/~blp/papers/asrandom.pdf)</sup><sup> • </sup><sup>[11](https://jenda.hrach.eu/f2/pax-docs/index.dis.html)</sup>

## Linux 上的配置

在 Linux 上，ASLR 内建于内核，由 `/proc/sys/kernel/randomize_va_space` 参数全系统控制。<sup>[12](https://docs.oracle.com/en/operating-systems/oracle-linux/6/security/ol_aslr_sec.html)</sup> 内核文档定义了三个值：<sup>[3](https://docs.kernel.org/admin-guide/sysctl/kernel.html?highlight=randomize_va_space)</sup>

- **0** 关闭进程地址空间随机化。这是不支持该特性的架构上以及使用 `norandmaps` 参数启动的内核上的默认值。
- **1** 随机化 mmap 基址、栈和 vDSO 页的地址，这意味着共享库在随机地址加载；对于以 PIE 链接的二进制文件，代码起始地址也被随机化。这是启用 `CONFIG_COMPAT_BRK` 时的默认值。
- **2** 额外启用堆随机化。这是禁用 `CONFIG_COMPAT_BRK` 时的默认值，也是大多数现代系统上的设置。<sup>[2](https://documentation.ubuntu.com/security/security-features/process-memory/aslr/)</sup>

堆的例外是出于兼容性：某些遗留应用程序，如 1996 年的古老版本 libc.so.5，假定 `brk` 区域紧接代码加 bss 的末尾开始，当该区域起始地址被随机化时它们就会出问题。内核文档指出，没有已知的非遗留应用程序会因此出问题，因此完全随机化对大多数系统是安全的。<sup>[3](https://docs.kernel.org/admin-guide/sysctl/kernel.html?highlight=randomize_va_space)</sup> Ubuntu 的历史记录指出，对 `brk` 相对于可执行文件内存区域的偏移量进行随机化的功能在内核 2.6.26 中加入，出现在 Ubuntu 8.10 中。<sup>[13](https://documentation.ubuntu.com/security/security-features/process-memory/)</sup>

管理员可以通过写入 procfs 文件或使用 `sysctl -w kernel.randomize_va_space=1` 临时改变该值，并通过 `/etc/sysctl.d/` 中的文件使其持久化。<sup>[2](https://documentation.ubuntu.com/security/security-features/process-memory/aslr/)</sup> 单个程序及其子进程可以使用 `setarch uname -m -R program` 在禁用随机化的情况下运行，这在调试或研究漏洞利用时常用。<sup>[12](https://docs.oracle.com/en/operating-systems/oracle-linux/6/security/ol_aslr_sec.html)</sup>

## 平台实现

**Windows。** 链接器标志 `/DYNAMICBASE` 将映像标记为可重定位，`/HIGHENTROPYVA` 表示支持高熵 64 位 ASLR，让操作系统使用随机化的 64 位虚拟地址重定位映像的各个段。`/HIGHENTROPYVA` 对 64 位映像默认启用，要求同时使用 `/DYNAMICBASE` 和 `/LARGEADDRESSAWARE`，且在 32 位映像上被忽略。<sup>[14](https://learn.microsoft.com/en-us/cpp/build/reference/highentropyva?view=msvc-170)</sup> Windows 漏洞防护增加了强制 ASLR，强制重定位未使用 `/DYNAMICBASE` 编译的映像，以及自下而上 ASLR，随机化包括堆、栈、TEB 和 PEB 在内的虚拟内存分配，对 64 位进程可选更大的方差。<sup>[15](https://github.com/MicrosoftDocs/defender-docs/blob/public/defender-endpoint/customize-exploit-protection.md)</sup>

**Apple 平台。** macOS 和 iOS 在每次运行时尽可能为栈、堆、库、框架和可执行代码选择不同的位置。<sup>[9](https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/BufferOverflows.html)</sup> 在 iOS、iPadOS 和 visionOS 上，内置应用在启动时使用 ASLR 随机化所有内存区域，Xcode 自动以 ASLR 支持编译第三方程序；文档将操纵栈和库地址的返回 libc 攻击列为随机化使之更难实施的攻击类型，尤其是在多台设备之间。<sup>[16](https://support.apple.com/en-gb/guide/security/sec15bfe098e/web)</sup>

**内核自身。** 内核地址空间布局随机化（KASLR）将同样的思路应用于操作系统：它在每次启动时随机化内核的基址，因此两台机器或同一设备的连续重启不太可能共享同一基址，迫使漏洞利用在针对内核符号之前先发现基址。<sup>[7](https://documentation.ubuntu.com/security/security-features/kernel-protections/)</sup> 其范围与用户空间 ASLR 不同：用户空间随机化在每个进程启动时应用，而 KASLR 在每次启动时整体应用于整个内核。<sup>[7](https://documentation.ubuntu.com/security/security-features/kernel-protections/)</sup> 内核通过 `CONFIG_RANDOMIZE_BASE` 实现它，在启动时重定位内核的物理和虚拟基址。<sup>[6](https://www.kernel.org/doc/html/latest/security/self-protection.html?highlight=kaslr)</sup>

## 有效性与局限

ASLR 提供的安全性以熵位衡量，即攻击者必须猜测的随机位数。其数量受架构限制。在 32 位 x86 上，PaX ASLR 为可执行文件和映射区提供 16 位随机性，为栈提供 24 位，其中映射区偏移量上限为 16 位，因为改变高位会破坏大型 `mmap` 调用，而低位必须遵守页对齐。<sup>[8](https://web.stanford.edu/~blp/papers/asrandom.pdf)</sup> 更一般地说，由于对齐限制，32 位版本的 ASLR 只能多样化 16 至 24 个地址位。<sup>[17](https://ietresearch.onlinelibrary.wiley.com/doi/10.1049/iet-ifs.2018.5142)</sup> 一份关于缓冲区溢出缓解措施的调查将实际数字定为 32 位系统上 16 个有效位，或在 Linux 上 8 位（256 个值），小到暴力破解可在毫秒内成功。<sup>[4](https://www.mdpi.com/2076-3417/12/13/6702)</sup> 在 64 位 Ubuntu 上，实验测量发现栈、堆和代码段的熵约为 16 至 19 位。<sup>[5](https://jis.athabascau.ca/index.php/jis/article/view/490)</sup>

**暴力破解与泄露。** 斯坦福大学牵头团队的经典分析表明，去随机化攻击可将任何标准的缓冲区溢出漏洞利用转化为对随机化系统有效的利用，只是更慢。针对 32 位 x86 上的 PaX，暴力破解 16 位 mmap 偏移量最多需要 65,536 次探测，平均 32,768 次，而且由于 PaX 在进程的整个生命周期内固定每个偏移量（包括派生出的子进程），获知一个服务器子进程的布局就揭示了所有子进程的布局。<sup>[8](https://web.stanford.edu/~blp/papers/asrandom.pdf)</sup> 同一分析发现，在初始布局之后重新随机化，无论频率如何，对暴力破解最多只增加 1 位安全性，这表明精力最好花在增加熵上。<sup>[8](https://web.stanford.edu/~blp/papers/asrandom.pdf)</sup> 互补的发现是，当内存地址被泄露时，ASLR 的保护被完全抵消：一次泄露即可击败该方案，这就是为什么针对 ASLR 的可靠漏洞利用通常除内存破坏漏洞外还需要一个信息泄露漏洞。<sup>[5](https://jis.athabascau.ca/index.php/jis/article/view/490)</sup>

**实现弱点。** 2016 年 Black Hat Asia 上，当时隶属于西苏格兰大学的安全研究员 Hector Marco Gisbert 和 Ismael Ripoll 的研究指出了 Linux 和 PaX ASLR 的四个弱点：熵太少、地址分布不均匀、对象之间的相关性，以及跨进程的布局继承。他们的概念验证利用相关性弱点在不到一秒内在 64 位 Linux 上绕过了完全 ASLR，并提出了一种重新设计的方案 ASLR-NG。<sup>[18](https://research-portal.uws.ac.uk/en/publications/exploiting-linux-and-pax-aslrs-weaknesses-on-32-bit-and-64-bit-sy/)</sup> 2024 年 ACM CCS 一项测量实际实现的研究发现，Linux 发行版提供稳健的随机化，而 Windows 和 macOS 往往未能充分随机化可执行代码和库等关键区域，并且检测到 Linux 5.18 之后库的熵显著减少。<sup>[19](https://arxiv.org/abs/2408.15107)</sup> 在 [Apple M1](https://www.edgepedia.org/zh/apple-m1) 芯片上原生运行的 macOS 上，实测库的启动时熵为 12.2 位，可执行文件的运行时熵为 11.5 位，部分原因是 16 KB 页强制 14 位对齐。<sup>[19](https://arxiv.org/abs/2408.15107)</sup>

**无泄露的攻击。** 在 EuroS&P 2018 上提出的位置无关代码重用攻击表明，高级攻击者即使完全没有信息泄露，也能实施实用的代码重用攻击，方法是依赖代码片段的相对而非绝对位置，并通过相对内存写入用小偏移量修补基本的 ROP 载荷。<sup>[20](https://research.vu.nl/en/publications/position-independent-code-reuse-on-the-effectiveness-of-aslr-in-t/)</sup> 微架构侧信道是另一条途径：硬件结构可以泄露 ASLR 所依赖的随机化地址，这促使了协同设计的防御措施，如 Oreo，它在地址转换前从虚拟地址中剥离随机化位，使侧信道无法观察到它们。<sup>[21](https://arxiv.org/abs/2412.07135)</sup>

**内核侧的局限。** 由于架构约束，Linux 将 KASLR 范围限制为 32 位（例如 x86-64 的 32 位立即操作数），当指针泄露时使内核暴露于暴力 ROP 攻击；Adelie 研究项目通过使用位置无关代码持续重新随机化内核模块来解决这一问题。<sup>[22](https://arxiv.org/abs/2201.08378)</sup>

## References

1. [Address Space Layout Randomization (ASLR), Apple Platform Security Guide, Glossary](https://help.apple.com/pdf/security/en_US/apple-platform-security-guide.pdf)
2. [Address Space Layout Randomization (ASLR), Ubuntu security documentation](https://documentation.ubuntu.com/security/security-features/process-memory/aslr/)
3. [Documentation for /proc/sys/kernel/, randomize_va_space, The Linux Kernel documentation](https://docs.kernel.org/admin-guide/sysctl/kernel.html?highlight=randomize_va_space)
4. [An In-Depth Survey of Bypassing Buffer Overflow Mitigation Techniques, Applied Sciences (MDPI)](https://www.mdpi.com/2076-3417/12/13/6702)
5. [The Predictable Unpredictable: Undermining Linux ASLR Through Memory Disclosure, Journal of Integrated Studies](https://jis.athabascau.ca/index.php/jis/article/view/490)
6. [Kernel Self-Protection, The Linux Kernel documentation](https://www.kernel.org/doc/html/latest/security/self-protection.html?highlight=kaslr)
7. [Kernel protections, Ubuntu security documentation](https://documentation.ubuntu.com/security/security-features/kernel-protections/)
8. [On the Effectiveness of Address-Space Randomization (Shacham, Page, Pfaff, Loo, Lockwood, Jiang), Stanford](https://web.stanford.edu/~blp/papers/asrandom.pdf)
9. [Avoiding Buffer Overflows and Underflows, Apple Secure Coding Guide](https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/BufferOverflows.html)
10. [Security Mitigations for Return-Oriented Programming Attacks, arXiv:1008.4099](https://arxiv.org/abs/1008.4099)
11. [Documentation for the PaX project](https://jenda.hrach.eu/f2/pax-docs/index.dis.html)
12. [Address Space Layout Randomization, Oracle Linux 6 Security Guide](https://docs.oracle.com/en/operating-systems/oracle-linux/6/security/ol_aslr_sec.html)
13. [Process and memory protections, Ubuntu security documentation](https://documentation.ubuntu.com/security/security-features/process-memory/)
14. [/HIGHENTROPYVA, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/build/reference/highentropyva?view=msvc-170)
15. [Customize exploit protection, Microsoft Defender documentation](https://github.com/MicrosoftDocs/defender-docs/blob/public/defender-endpoint/customize-exploit-protection.md)
16. [Security of runtime process in iOS, iPadOS and visionOS, Apple Support](https://support.apple.com/en-gb/guide/security/sec15bfe098e/web)
17. [Dynamic reencryption of return addresses, IET Information Security](https://ietresearch.onlinelibrary.wiley.com/doi/10.1049/iet-ifs.2018.5142)
18. [Exploiting Linux and PaX ASLR's weaknesses on 32-bit and 64-bit systems (Marco Gisbert & Ripoll, Black Hat Asia 2016), UWS Academic Portal](https://research-portal.uws.ac.uk/en/publications/exploiting-linux-and-pax-aslrs-weaknesses-on-32-bit-and-64-bit-sy/)
19. [The Illusion of Randomness: An Empirical Analysis of Address Space Layout Randomization Implementations, arXiv:2408.15107 (ACM CCS 2024)](https://arxiv.org/abs/2408.15107)
20. [Position-Independent Code Reuse: On the Effectiveness of ASLR in the Absence of Information Disclosure (EuroS&P 2018), Vrije Universiteit Amsterdam](https://research.vu.nl/en/publications/position-independent-code-reuse-on-the-effectiveness-of-aslr-in-t/)
21. [Oreo: Protecting ASLR Against Microarchitectural Attacks, arXiv:2412.07135 (NDSS 2025)](https://arxiv.org/abs/2412.07135)
22. [Adelie: Continuous Address Space Layout Re-randomization for Linux Drivers, arXiv:2201.08378 (ASPLOS 2022)](https://arxiv.org/abs/2201.08378)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 网络与安全 › Network defense and threats › Firewalls and perimeter defense

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/address-space-layout-randomization

English original: https://www.edgepedia.org/address-space-layout-randomization
