{"id":1311,"date":"2019-07-17T17:07:36","date_gmt":"2019-07-18T00:07:36","guid":{"rendered":"https:\/\/outflux.net\/blog\/?p=1311"},"modified":"2022-05-05T20:30:44","modified_gmt":"2022-05-06T03:30:44","slug":"security-things-in-linux-v5-2","status":"publish","type":"post","link":"https:\/\/outflux.net\/blog\/archives\/2019\/07\/17\/security-things-in-linux-v5-2\/","title":{"rendered":"security things in Linux v5.2"},"content":{"rendered":"<p>Previously: <a href=\"\/blog\/archives\/2019\/05\/27\/security-things-in-linux-v5-1\/\">v5.1<\/a>.<\/p>\n<p>Linux kernel <a href=\"https:\/\/lore.kernel.org\/lkml\/CAHk-=whtW3FdruS-q2zehJPSan1SKtHoFHKX28A3J_1gfTFUMw@mail.gmail.com\/\">v5.2 was released<\/a> last week! Here are some security-related things I found interesting:<\/p>\n<p><strong id=\"v5.2-freelist-randomization\">page allocator freelist randomization<\/strong><br \/>\nWhile the SLUB and SLAB allocator <a href=\"\/blog\/archives\/2016\/10\/03\/security-things-in-linux-v4-7\/\">freelists have been randomized for a while now<\/a>, the overarching page allocator itself wasn&#8217;t. This meant that anything doing allocation outside of the <code>kmem_cache<\/code>\/<code>kmalloc()<\/code> would have deterministic placement in memory. This is bad both for security and for some cache management cases. Dan Williams <a href=\"https:\/\/git.kernel.org\/linus\/e900a918b0984ec8f2eb150b8477a47b75d17692\">implemented this randomization under CONFIG_SHUFFLE_PAGE_ALLOCATOR<\/a> now, which provides additional uncertainty to memory layouts, though at a rather low granularity of 4MB (see <code>SHUFFLE_ORDER<\/code>). Also note that this feature needs to be enabled at boot time with <code>page_alloc.shuffle=1<\/code> unless you have direct-mapped memory-side-cache (you can check the state at <code>\/sys\/module\/page_alloc\/parameters\/shuffle<\/code>).<\/p>\n<p><strong id=\"v5.2-var-init\">stack variable initialization with Clang<\/strong><br \/>\nAlexander Potapenko <a href=\"https:\/\/git.kernel.org\/linus\/709a972efb01efaeb97cad1adc87fe400119c8ab\">added support via CONFIG_INIT_STACK_ALL<\/a> for Clang&#8217;s <code>-ftrivial-auto-var-init=pattern<\/code> option that enables <a href=\"https:\/\/reviews.llvm.org\/D54604\">automatic initialization of stack variables<\/a>. This provides even greater coverage than the prior <a href=\"\/blog\/archives\/2019\/05\/27\/security-things-in-linux-v5-1\/\">GCC plugin for stack variable initialization<\/a>, as Clang&#8217;s implementation also covers variables not passed by reference. (In theory, the kernel build should still warn about these instances, but even if they exist, Clang will initialize them.) Another notable difference between the GCC plugins and Clang&#8217;s implementation is that Clang initializes with a repeating 0xAA byte pattern, rather than zero. (Though this changes under certain situations, like for 32-bit pointers which are initialized with 0x000000AA.) As with the GCC plugin, the benefit is that the entire class of uninitialized stack variable flaws goes away.<\/p>\n<p><strong>Kernel Userspace Access Prevention on powerpc<\/strong><br \/>\nLike SMAP on x86 and PAN on ARM, Michael Ellerman and Russell Currey have landed support for disallowing access to userspace without explicit markings in the kernel <a href=\"https:\/\/git.kernel.org\/linus\/890274c2dc4c0a57ae5a12d6a76fa6d05b599d98\">(KUAP) on Power9<\/a> and later PPC CPUs under <code>CONFIG_PPC_RADIX_MMU=y<\/code> (which is the default). This is the continuation of the execute protection <a href=\"\/blog\/archives\/2017\/02\/27\/security-things-in-linux-v4-10\/\">(KUEP) in v4.10<\/a>. Now if an attacker tries to trick the kernel into any kind of unexpected access from userspace (not just executing code), the kernel will fault.<\/p>\n<p><strong>Microarchitectural Data Sampling mitigations on x86<\/strong><br \/>\nAnother set of cache memory side-channel attacks came to light, and were consolidated together under the name <a href=\"https:\/\/www.kernel.org\/doc\/html\/latest\/admin-guide\/hw-vuln\/mds.html\">Microarchitectural Data Sampling<\/a> (MDS). MDS is weaker than other cache side-channels (less control over target address), but memory contents can still be exposed. Much like <a href=\"https:\/\/www.kernel.org\/doc\/html\/latest\/admin-guide\/hw-vuln\/l1tf.html\">L1TF<\/a>, when one&#8217;s threat model includes untrusted code running under Symmetric Multi Threading (SMT: more logical cores than physical cores), the only full mitigation is to disable hyperthreading (boot with &#8220;<code>nosmt<\/code>&#8220;). For all the other variations of the MDS family, Andi Kleen (and others) <a href=\"https:\/\/git.kernel.org\/linus\/ed5194c2732c8084af9fd159c146ea92bf137128\">implemented various flushing mechanisms<\/a> to avoid cache leakage.<\/p>\n<p><strong id=\"v5.2-userfaultfd\">unprivileged userfaultfd sysctl knob<\/strong><br \/>\nBoth FUSE and userfaultfd provide attackers with a way to stall a kernel thread in the middle of memory accesses from userspace by initiating an access on an unmapped page. While FUSE is usually behind some kind of access controls, userfaultfd hadn&#8217;t been. To avoid various <a href=\"https:\/\/cyseclabs.com\/blog\/cve-2016-6187-heap-off-by-one-exploit\">heap grooming<\/a> and <a href=\"https:\/\/duasynt.com\/blog\/linux-kernel-heap-spray\">heap spraying<\/a> techniques for exploiting Use-after-Free flaws, Peter Xu added the new &#8220;<code><a href=\"https:\/\/git.kernel.org\/linus\/cefdca0a86be517bc390fc4541e3674b8e7803b0\">vm.unprivileged_userfaultfd<\/a><\/code>&#8221; sysctl knob to disallow unprivileged access to the userfaultfd syscall.<\/p>\n<p><strong>temporary mm for text poking on x86<\/strong><br \/>\nThe kernel regularly performs self-modification with things like <code>text_poke()<\/code> (during stuff like alternatives, ftrace, etc). Before, this was done with fixed mappings (&#8220;fixmap&#8221;) where a specific fixed address at the high end of memory was used to map physical pages as needed. However, this resulted in some temporal risks: other CPUs could write to the fixmap, or there might be stale TLB entries on removal that other CPUs might still be able to write through to change the target contents. Instead, Nadav Amit has created a <a href=\"https:\/\/git.kernel.org\/linus\/b3fd8e83ada0d51b71a84297480187e2d40e5ded\">separate memory map for kernel text writes<\/a>, as if the kernel is trying to make writes to userspace. This mapping ends up staying local to the current CPU, and the <a href=\"https:\/\/git.kernel.org\/linus\/4fc19708b165c1c152fa1f12f6600e66184b7786\">poking address is randomized<\/a>, unlike the old fixmap.<\/p>\n<p><strong>ongoing: implicit fall-through removal<\/strong><br \/>\nGustavo A. R. Silva is nearly done with <a href=\"https:\/\/git.kernel.org\/pub\/scm\/linux\/kernel\/git\/torvalds\/linux.git\/log\/?h=v5.2&#038;qt=grep&#038;q=expect.*fall.*through\">marking<\/a> (and <a href=\"https:\/\/git.kernel.org\/pub\/scm\/linux\/kernel\/git\/torvalds\/linux.git\/log\/?h=v5.2&#038;qt=grep&#038;q=missing+break\">fixing<\/a>) all the <a href=\"https:\/\/outflux.net\/blog\/archives\/2019\/03\/12\/security-things-in-linux-v5-0\/\">implicit fall-through cases<\/a> in the kernel. Based on the <a href=\"https:\/\/lore.kernel.org\/lkml\/20190506180521.GA30749@embeddedor\/\">pull request from Gustavo<\/a>, it looks very much like v5.3 will see <code>-Wimplicit-fallthrough<\/code> added to the global build flags and then this class of bug should stay extinct in the kernel.<\/p>\n<p><strong>CLONE_PIDFD added<\/strong><br \/>\nChristian Brauner added the new <a href=\"https:\/\/git.kernel.org\/linus\/b3e5838252665ee4cfa76b82bdf1198dca81e5be\"><code>CLONE_PIDFD<\/code> flag<\/a> to the <code>clone()<\/code> system call, which complements the <a href=\"\/blog\/archives\/2019\/05\/27\/security-things-in-linux-v5-1\/\">pidfd work in v5.1<\/a> so that programs can now gain a handle for a process ID right at <code>fork()<\/code> (actually <code>clone()<\/code>) time, instead of needing to get the handle from <code>\/proc<\/code> after process creation. With signals and forking now enabled, the next major piece (already in linux-next) will be adding <code>P_PIDFD<\/code> to the <code>waitid()<\/code> system call, and common process management can be done entirely with pidfd.<\/p>\n<p><strong>Other things<\/strong><br \/>\n<a href=\"https:\/\/twitter.com\/a13xp0p0v\/status\/1164935830319828992\">Alexander Popov pointed out some more v5.2 features<\/a> that I missed in this blog post. I&#8217;m repeating them here, with some minor edits\/clarifications. Thank you Alexander!<\/p>\n<ul>\n<li><a href=\"https:\/\/git.kernel.org\/linus\/8f147727030bf9e81331ab9b8f42d4611bb6a3d9\">x86-64 IRQ\/exception\/debug stack now has guard pages<\/a> to detect stack overflows immediately and deterministically from those contexts.<\/li>\n<li><a href=\"https:\/\/git.kernel.org\/linus\/134fca9063ad4851de767d1768180e5dede9a881\"><core>mincore(2)<\/code> is made more conservative<\/a> to avoid information exposures about memory cache states and similar.<\/li>\n<li><a href=\"https:\/\/git.kernel.org\/linus\/6ec62961e6de9506e8b8620dc19897d8cfd41c2e\">x86&#8217;s <code>objtool<\/code> can now validate callers of uaccess helpers to make sure SMAP is not left disabled<\/a>.<\/li>\n<li><a href=\"https:\/\/git.kernel.org\/linus\/2edb16efc899f9c232e2d880930b855e4cf55df4\">powerpc\/32 now supports KASAN<\/a>.<\/li>\n<li><a href=\"https:\/\/git.kernel.org\/linus\/453d87f6a8aed827f5ebb1708a4cea458fd68d23\">powerpc now warns if W+X pages are found on boot<\/a>.<\/li>\n<\/ul>\n<p><em>Edit: added CLONE_PIDFD notes, as reminded by Christian Brauner. :)<\/em><br \/>\n<em>Edit: added Alexander Popov&#8217;s notes<\/em><\/p>\n<p>That&#8217;s it for now; let me know if you think I should add anything here. We&#8217;re almost to -rc1 for <a href=\"\/blog\/archives\/2019\/11\/14\/security-things-in-linux-v5-3\/\">v5.3<\/a>!<\/p>\n<p style='text-align:left'>&copy; 2019 &#8211; 2022, <a href=\"https:\/\/outflux.net\/blog\/\">Kees Cook<\/a>. This work is licensed under a <a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\">Creative Commons Attribution-ShareAlike 4.0 License<\/a>.<br \/><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><img decoding=\"async\" alt=\"CC BY-SA 4.0\" style=\"border-width:0\" src=\"https:\/\/i.creativecommons.org\/l\/by-sa\/4.0\/88x31.png\" \/><\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously: v5.1. Linux kernel v5.2 was released last week! Here are some security-related things I found interesting: page allocator freelist randomization While the SLUB and SLAB allocator freelists have been randomized for a while now, the overarching page allocator itself wasn&#8217;t. This meant that anything doing allocation outside of the kmem_cache\/kmalloc() would have deterministic placement [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,20,18,21,6,14,19],"tags":[],"_links":{"self":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/1311"}],"collection":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/comments?post=1311"}],"version-history":[{"count":30,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/1311\/revisions"}],"predecessor-version":[{"id":1591,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/1311\/revisions\/1591"}],"wp:attachment":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/media?parent=1311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/categories?post=1311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/tags?post=1311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}