内核安全

Linux 安全缓解机制总结

2020-06-28  本文已影响0人  bsauce

学习资料:

防护研究团队

SELinux (NSA)、AppArmor (OpenSuSE/Ubuntu)、PaX / grsecurity (Spender)、各手机厂商、独立研究(论文)。


1.简介

(1)SELinux

介绍:由美国NSA维护。基于强制访问控制MAC(Mandatory Access Control)实现,基于角色的访问控制——进程只能访问那些在他的任务中所需要文件,简化用户的权限管理,减少系统开销。由NSA编写并设计成内核模块包含到内核中,相应的某些安全相关的应用也被打了SELinux的补丁。

Linux缺点:对于以上这些的不足,防火墙入侵检测系统都是无能为力的。而SELinux则能大幅强化访问权限。

SELinux优点:它通过对于用户,进程权限的最小化,即使受到攻击,进程或者用户权限被夺去,也不会对整个系统造成重大影响。

(2)AppArmor——基于MAC实现

介绍:由OpenSuSE/Ubuntu维护。跟SELinux一样,使用 Linux Security Modules (LSM) 实现。是SELinux的一个备选,SELinux是对文件加标签,AppArmor是对文件路径,配置起来AppArmor更简单,且AppArmor对系统的修改更少。

(3)PaX/grsecurity——采用patch的形式

介绍:开始由老一代黑客Pax维护,后由grsecurity团队维护。采用patch形式加入到linux。


2. 历史

(1)什么是Grsecurity/PaX?

PaX是针对linux kernel的一个加固版本的补丁,它让linux内核的内存页受限于最小权限原则,是这个星球上有史以来最优秀的防御系统级别0day的方案,第1版的设计和实现诞生于2000年,那可是一个没有 ASLR/RELRO/NX/CANARY/FORITY/PIE都没有的年代,这些今天意义上的现代 mitigation技术不管是linux/windows/macosx都多少抄袭和模仿了PaX的设计和实 现,但有很多朋友会问:既然这东东这么厉害,为什么不在linux mainline里? PaX没有进入Linux内核upstream的原因不止一个,甚至有时候都不是纯粹技术本身的问题:

  1. PaX Team并不在意PaX是否进入Linux主干代码,但多年来有很多关心Linux内核安全的人不断的尝试把PaX的代码分拆成小的patch提交给Linux内核社区。

  2. Linux内核社区认为PaX的代码难以维护,而Linux内核社区更喜欢花时间在性能和新功能上,而非安全。

  3. Linux内核社区和Linux基金会受到由各个大厂商的影响,大厂商对于安全的要求取决于他们的客户的需求,如果很多真相不曝光大厂商是不会在意安全性的

  4. 商业公司在意他们的主要利润来源,比如Five-eyes国家(美国,英国,加拿大,澳大利亚和新西兰)的政府项目都会统一采购SELinux的项目,所以大厂商都会花费精力去满足这一需求,这也是SELinux虽然备受争议但是一直有厂商和NSA持续投入的原因。

SELinux也是一个著名的开源MAC(强制访问控制)实现,是由NSA(美国国家安全局)于1990年代末发起的项目,于2000年以GPL自由软件许可证开放源代码,2003年合并到Linux内核中,过去10年中关于是否NSA在其中放后门的争论没有停过,一些人认为应该信任SELinux,因为它是以GPL自由软件许可证公开的源代码,也有人认为它是NSA参与过的项目,所以不应该信任。2013年Snowden曝光棱镜后更多的人极度的不信任NSA,认为NSA有对Android代码植入后门的前科,所以应该怀疑所有NSA积极参与的项目包括SELinux。目前MAC的开源实现里,SELinux主要由RedHat/CentOS/Fedora社区维护,Apparmor主要由OpenSuSE/Ubuntu社区维护,关于SELinux是否应该使用是一个长久争论的话题,个人认为这取决于你的威胁建模,如果你是Five-Eyes阵营你当然应该使用SELinux,如果你是其他阵营比如德国或者中国,或许你应该考虑其他选择。

针对Linux内核的MAC实现都是基于LSM( Linux Security Module)去实现的,LSM利用了一堆CAPABILITY的 机制提供了一些限制用户态程序访问控制的接口,SELinux和Apparmor都是基于LSM开发的,注意LSM并不是一个传统意义上的linux kernel module,至少在2个地方不同于普通module:

  1. 必须在bootloader启动内核时启动,不能在内核加载完后启动。

  2. 不能同时启动2个LSM的实现。

但PaX Team是一群old school security hackers,他们认为LSM一方面打破了 “security as a whole”的哲学,另外一方面对于内核漏洞没有防御能力,虽然在早年Linux内核社区以及大厂商不管是刻意还是无意的想要掩盖这一点,但时间证明PaX Team是正确的。其 实当人们谈到Gnu/Linux安全性比windows/OSX更好时,其实未必,至少linux内核社区并没有把安全性放在首位,Linus Torvalds从来都不是太care安全问题,不是吗?

PaX从一开始就主要关注如何防御和检测memory corruption,PaX由PaX team维护,Grsecurity主要包括了RBAC(基于角色的访问控制)和一系列对PaX的改进(包括抗爆破,ASLR的抗信息泄漏,不允许文件系统级别的任意代码执行等),Grsecurity主要由Spender维护,最近几年这2组Patch都合并到了一起发布,所以我们都称这组补丁为Grsecurity/PaX或者PaX/Grsecurity。

Grsecurity包括了很多简单的自动化特性,比如RBAC系统可以通过学习模式(你可以选择基于进程/用户或者整个系统)来自动化创建规则,这些规则都是人类可读的纯文本格式(Shawn:SELinux是使用难以审计的二进制的格式),报错信息有助于根据攻击类型来制定相应的规则。

(2)对比

SELinux:最强大但最复杂的

AppArmor:比SELinux更简单的配置/管理

grsecurity:由于自动培训的简单配置,比访问控制更多的功能

Feature SELinux AppArmor grsecurity
Automated No (audit2allow and system-config-selinux) Yes (Yast wizard) Yes (auto traning / gradm)
Powerful policy setup Yes (very complex) Yes Yes
Default and recommended integration CentOS / RedHat / Debian Suse / OpenSuse Any Linux distribution
Training and vendor support Yes (Redhat) Yes (Novell) No (community forum and lists)
Recommend for Advanced user New / advanced user New users
Feature Pathname based system does not require labelling or relabelling filesystem Attaches labels to all files, processes and objects ACLs

(3)KSPP(Kernel Self Protection Project)

KSPP目标:由于很多保护机制已经存在很久了,如PaX/grsecurity和一些论文,KSPP目标就是将这些保护机制整合到linux本身,实现内核自保护。

PaX/Grsecurity的test patch于2017年4月26日关闭公开下载。Linux基金会是导致商业用户,个人用户和社区用户失去访问test patch的权利的罪魁祸首。原因如下:

(4)Now

PaX的思路的确非常的震撼,那 都是10多年前的设计和实现,在这个一天云计算一天雾计算的年代,虽然关注本 质的黑客越来越少,但地下精神并未死去,PaX Team就是一个活生生的例证,相 反,不少old school黑客都坚信其实old school的数量并没有减少,至少我个人 相信这是真的…Phrack没死,Grsecurity/PaX没死,DNFWAH也没死,希望更多的 黑客分享自己的hacking之旅。

Phrack is not dead, Grsecurity/PaX is not dead, DNFWAH is not dead, The Underground spirit is not dead…..If they were, that’d be on us!

=——————————————————————–=

To one of the most respected old school communities: Grsecurity/PaX. We/I salute you!!!


3.防御机制

(1)Linux Kernel Defence Map

linux-kernel-defence-map2.jpg

说明:

防御机制:

(2)KSPP项目

漏洞类型:

  1. stack overflow and exhaustion

    示例:https://jon.oberheide.org/files/half-nelson.c

    防护:

    • stack canaries, e.g. gcc's -fstack-protector (v2.6.30) and -fstack- protector-strong (v3.14), best-effort CONFIG selected for compiler
    • guard pages (e.g. GRKERNSEC_KSTACKOVERFLOW)
      • vmap stack (v4.9 x86, v4.14 arm64), removal of thread_info from stack (v4.9 x86, v4.10 arm64)
    • alloca checking (e.g. PAX_MEMORY_STACKLEAK): Alexander Popov 进出内核空间时对进程内核栈的数据进行擦除。
    • shadow stacks (e.g. Clang SafeStack,将原生栈分为两个区域,尝试保护栈上的关键数据:一个安全栈,用于控制流信息以及仅以安全方式访问的数据(通过静态分析确定)。一个用于保存其他数据的非安全栈。这两个栈位于进程地址空间中不同的内存区域,因此防止了非安全栈的溢出破坏安全栈。)
  2. integer over/underflow

    示例:

    防护:

    • check for refcount overflow (v4.11) (e.g. PAX_REFCOUNT,针对引用计数溢出的加固。实现包括两个部分,一部分是探测引用计数的溢出,一部分是溢出发生时的异常处理。)
      • refcount_t conversions: Elena Reshetova, Peter Zijlstra, Hans Liljestrand, David Windsor
    • compiler plugin to detect multiplication overflows at runtime (e.g. PAX_SIZE_OVERFLOW, Clang -fsanitize=integer)
  3. buffer overflows

    示例:http://blog.includesecurity.com/2014/06/exploit-walkthrough-cve-2014-0196-pty-kernel-race-condition.html

    防护:

    • runtime validation of copy_{to,from}_user() buffer sizes (e.g. PAX_USERCOPY)
      • CONFIG_HARDENED_USERCOPY (v4.8)
      • Usercopy whitelisting and slab segregation: David Windsor
    • metadata validation (e.g. glibc's heap protections)
      • linked-list hardening (based on grsecurity) CONFIG_DEBUG_LIST (v4.10),
      • heap freelist obfuscation (based on grsecurity) CONFIG_SLUB_HARDENED (v4.14)
      • Heap canaries: Daniel Micay
      • Intel MPX: Hans Liljestrand, Elena Reshetova
    • FORTIFY_SOURCE (inspired by glibc), check str/mem() sizes at compile- and run-time
      • CONFIG_FORTIFY_SOURCE (v4.13)
      • Intra-object checking: Daniel Micay
  4. format string injection

    示例:http://www.openwall.com/lists/oss-security/2013/06/06/13

    防护:

    • Drop %n entirely (v3.13)
    • detect non-const format strings at compile time (e.g. gcc's -Wformat- security, or better plugin)
    • detect non-const format strings at run time (e.g. memory location checking done with glibc's -D_FORITY_SOURCE=2)
    • (Can we get rid of %p? Stay tuned...)
  5. kernel pointer exposure

    示例:

    防护:

    • kptr_restrict sysctl (v2.6.38) too weak: requires dev opt-in
    • remove visibility to kernel symbols (e.g. GRKERNSEC_HIDESYM)
    • obfuscate output of %p (v4.15): in dmesg, seq_file, user buffers, etc (e.g. GRKERNSEC_HIDESYM + PAX_USERCOPY)
  6. uninitialized variables

    示例:https://outflux.net/slides/2011/defcon/kernel-exploitation.pdf

    防护:

    • GCC plugin, stackleak: clear kernel stack between system calls (from PAX_MEMORY_STACKLEAK): Alexander Popov
    • GCC plugin, structleak: instrument compiler to fully initialize all structures (from PAX_MEMORY_STRUCTLEAK): (__user v4.11, by-reference v4.14)
  7. use-after-free

    示例:http://perception-point.io/2016/01/14/analysis-and-exploitation-of-a-linux-kernel-vulnerability-cve-2016-0728/

    防护:

    • clearing memory on free can stop attacks where there is no reallocation control (e.g. PAX_MEMORY_SANITIZE)
      • Zero poisoning (v4.6)
    • segregating memory used by the kernel and by userspace can stop attacks where this boundary is crossed (e.g. PAX_USERCOPY)
    • randomizing heap allocations or using quarantines can frustrate the reallocation efforts the attack needs to perform (e.g. OpenBSD malloc)
      • Freelist randomization (SLAB: v4.7, SLUB: v4.8)

利用方法:

  1. finding the kernel

    示例:https://github.com/jonoberheide/ksymhunter 也包含内核指针泄露

    防护:

    • hide symbols and kernel pointers (see “Kernel pointer exposure”)
    • kernel ASLR
      • text/modules base: x86 (v3.14), arm64 (v4.6), MIPS (v4.7), ARM: Ard Biesheuvel
      • memory: arm64 (v4.6), x86 (v4.8)
      • PIE: arm64 (v4.6), x86: Thomas Garnier
    • runtime randomization of kernel functions
    • executable-but-not-readable memory
      • Initial support: x86 (v4.6), arm64 (v4.9), needs real hardware and kernel support
    • per-build structure layout randomization (e.g. GRKERNSEC_RANDSTRUCT)
      • manual (v4.13), automatic (v4.14)
  2. direct kernel overwrite

    示例:

    防护:

    • Executable memory cannot be writable (CONFIG_STRICT_KERNEL_RWX)
      • s390, parisc: forever ago
      • x86: v3.18 (more completely)
      • ARM: v3.19
      • arm64: v4.0
      • powerpc: v4.13
  3. function pointer overwrite

    示例:

    防护:

    • read-only function tables (e.g. PAX_CONSTIFY_PLUGIN)
    • make sensitive targets that need one-time or occasional updates only writable during updates (e.g. PAX_KERNEXEC):
      • __ro_after_init (v4.6)
      • write-once memory: Igor Stoppa
    • struct timer_list .data field removal (v4.15)
  4. userspace execution

    示例:以上都是

    防护:

    • hardware segregation: SMEP (x86), PXN (ARM, arm64)
    • emulated memory segregation via page table swap, PCID, etc (e.g. PAX_MEMORY_UDEREF):
      • Domains (ARM: v4.3)
      • TTBR0 (arm64: v4.10)
      • PTI (x86: v4.15)
    • compiler instrumentation to set high bit on function calls
  5. userspace data access

    示例:

    防护:

    • hardware segregation: SMAP (x86), PAN (ARM, arm64)
    • emulated memory segregation via page table swap, PCID, etc (e.g. PAX_MEMORY_UDEREF):
      • Domains (ARM: v4.3)
      • TTBR0 (arm64: v4.10)
      • PCID (x86): Andy Lutomirski
    • eXclusive Page Frame Ownership: Tycho Andersen, Juerg Haefliger
  6. reused code chunks——ROP/JOP

    示例:http://vulnfactory.org/research/h2hc-remote.pdf

    防护:

各版本更新:

  1. v4.10
    • PAN emulation, arm64
    • thread_info relocated off stack, arm64
    • Linked list hardening
    • RNG seeding from UEFI, arm64
    • W^X detection, arm64
  2. v4.11
    • refcount_t infrastructure
    • 2 refcount_t conversions
    • read-only usermodehelper
    • structleak plugin (__user mode)
  3. v4.12
    • 57 refcount_t conversions
    • read-only and fixed-location GDT, x86
    • usercopy consolidation
    • read-only LSM structures
    • KASLR enabled by default, x86
    • stack canary expanded to bit-width of host
    • stack/heap gap expanded
  4. v4.13
    • 65 refcount_t conversions
    • CONFIG_REFCOUNT_FULL
    • CONFIG_FORTIFY_SOURCE
    • randstruct plugin (manual mode)
    • ELF_ET_DYN_BASE lowered
  5. v4.14
    • 3 refcount_t conversions (bikeshedding stall)
    • randstruct plugin (automatic mode)
    • SLUB freelist pointer obfuscation
    • structleak plugin (by-reference mode)
    • VMAP_STACK, arm64
    • set_fs() removal progress
    • set_fs() balance detection, x86, arm64, arm
  6. v4.15
    • PTI
    • retpoline
    • 35 refcount_t conversions (32 remaining)
    • struct timer_list .data field removal
    • fast refcount overflow protection, x86 (also in v4.14 -stable)
    • %p hashing
  7. Maybe in v4.16
    • 32 refcount_t conversions?
    • usercopy whitelisting
    • CONFIG_CC_STACKPROTECTOR_AUTO
  8. Future
    • stackleak plugin
    • eXclusive Page Frame Owner
    • KASLR, arm
    • SMAP emulation, x86
    • brute force detection
    • write-rarely memory
    • Link-Time Optimization
    • Clang plugins
    • Control Flow Integrity
    • integer overflow detection
    • VLA removal (-Werror=vla)
    • per-task stack canary, non-x86
    • per-CPU page tables
    • read-only page tables
    • hardened slab allocator
    • hypervisor magic :)

后续的更新可以看看https://outflux.net/blog/,上面有每个linux版本的安全更新说明。

参考资料:

PaX的技术考古之旅

PaX的技术考古之旅——学习

Brad Spengler采访——Linux 内核社区是数字军火商、斯拉夫兵工厂甚至 NSA 的最爱 Linux Torvalds

PaX与KSPP矛盾_HardenedLinux: The way to the Ark

KSPP-Linux内核自防护项目的初始文档

wiki-SELinux

wiki-AppArmor

wiki-grsecurity

wiki-Kernel Self Protection Project

百度百科-SELinux

百度百科-AppArmor

Grsecurity/Appendix/Grsecurity and PaX Configuration Options

PaX/Grsecurity配置选项

上一篇 下一篇

猜你喜欢

热点阅读