{"id":938,"date":"2016-10-04T17:26:19","date_gmt":"2016-10-05T00:26:19","guid":{"rendered":"https:\/\/outflux.net\/blog\/?p=938"},"modified":"2017-08-10T13:09:28","modified_gmt":"2017-08-10T20:09:28","slug":"security-things-in-linux-v4-8","status":"publish","type":"post","link":"https:\/\/outflux.net\/blog\/archives\/2016\/10\/04\/security-things-in-linux-v4-8\/","title":{"rendered":"security things in Linux v4.8"},"content":{"rendered":"<p>Previously: <a href=\"\/blog\/archives\/2016\/10\/03\/security-things-in-linux-v4-7\/\">v4.7<\/a>. Here are a bunch of security things I&#8217;m excited about in Linux v4.8:<\/p>\n<p><strong>SLUB freelist ASLR<\/strong><\/p>\n<p>Thomas Garnier continued his <a href=\"\/blog\/archives\/2016\/10\/03\/security-things-in-linux-v4-7\/\">freelist randomization<\/a> work by adding <a href=\"https:\/\/git.kernel.org\/linus\/210e7a43fa905bccafa9bb5966fba1d71f33eb8b\">SLUB support<\/a>.<\/p>\n<p><strong>x86_64 KASLR text base offset physical\/virtual decoupling<\/strong><\/p>\n<p>On x86_64, to implement the KASLR text base offset, the physical memory location of the kernel was randomized, which resulted in the virtual address being offset as well. Due to how the kernel&#8217;s &#8220;-2GB&#8221; addressing works (<a href=\"http:\/\/man7.org\/linux\/man-pages\/man1\/gcc.1.html#OPTIONS\">gcc<\/a>&#8216;s &#8220;<code>-mcmodel=kernel<\/code>&#8220;), it wasn&#8217;t possible to randomize the physical location beyond the 2GB limit, leaving any additional physical memory unused as a randomization target. In order to decouple the physical and virtual location of the kernel (to make physical address exposures less valuable to attackers), the physical location of the kernel needed to be randomized <em>separately<\/em> from the virtual location. This required a lot of work for handling very large addresses spanning terabytes of address space. Yinghai Lu, Baoquan He, and I landed a <a href=\"https:\/\/git.kernel.org\/linus\/8391c73c96f28d4e8c40fd401fd0c9c04391b44a\">series of patches<\/a> that ultimately did this (and in the process fixed some other bugs too). This expands the <em>physical<\/em> offset entropy to roughly <code>$physical_memory_size_of_system \/ 2MB<\/code> bits.<\/p>\n<p><strong>x86_64 KASLR memory base offset<\/strong><\/p>\n<p>Thomas Garnier rolled out KASLR to the kernel&#8217;s various statically located memory ranges, randomizing their locations with <a href=\"https:\/\/git.kernel.org\/linus\/0483e1fa6e09d4948272680f691dccb1edb9677f\">CONFIG_RANDOMIZE_MEMORY<\/a>. One of the more notable things randomized is the physical memory mapping, which is a <a href=\"https:\/\/github.com\/n3k\/CansecWest2016_Getting_Physical_Extreme_Abuse_of_In tel_Based_Paging_Systems\/blob\/master\/Presentation\/CanSec2016_Presentation.pdf\">known target for attacks<\/a>. Also randomized is the vmalloc area, which makes attacks against targets vmalloced during boot (which tend to always end up in the same location on a given system) are now harder to locate. (The vmemmap region randomization accidentally missed the v4.8 window and will appear in v4.9.)<\/p>\n<p><strong>x86_64 KASLR with hibernation<\/strong><\/p>\n<p>Rafael Wysocki (with Thomas Garnier, Borislav Petkov, Yinghai Lu, Logan Gunthorpe, and myself) worked on a <a href=\"https:\/\/git.kernel.org\/linus\/65c0554b73c920023cc8998802e508b798113b46\">number<\/a> <a href=\"https:\/\/git.kernel.org\/linus\/c226fab474291e3c6ac5fa30a2b0778acc311e61\">of<\/a> <a href=\"https:\/\/git.kernel.org\/linus\/e4630fdd47637168927905983205d7b7c5c08c09\">fixes<\/a> to hibernation code that, even without KASLR, were coincidentally exposed by the <a href=\"\/blog\/archives\/2016\/09\/27\/security-things-in-linux-v4-4\/\">earlier W^X fix<\/a>. With that original problem fixed, then memory KASLR exposed more problems. I&#8217;m very grateful everyone was able to help out fixing these, especially Rafael and Thomas. It&#8217;s a hard place to debug. The bottom line, now, is that hibernation and KASLR are <a href=\"https:\/\/git.kernel.org\/linus\/65fe935dd2387a4faf15314c73f5e6d31ef0217e\">no longer mutually exclusive<\/a>.<\/p>\n<p><strong>gcc plugin infrastructure<\/strong><\/p>\n<p>Emese Revfy ported the PaX\/Grsecurity <a href=\"https:\/\/git.kernel.org\/linus\/6b90bd4ba40b38dc13c2782469c1c77e4ed79915\">gcc plugin infrastructure<\/a> to upstream. If you want to perform compiler-based magic on kernel builds, now it&#8217;s much easier with <code>CONFIG_GCC_PLUGINS<\/code>! The plugins live in <code>scripts\/gcc-plugins\/<\/code>. Current plugins are a short example called &#8220;Cyclic Complexity&#8221; which just emits the complexity of functions as they&#8217;re compiled, and &#8220;Sanitizer Coverage&#8221; which provides the same functionality as gcc&#8217;s recent &#8220;<a href=\"https:\/\/lwn.net\/Articles\/677764\/\">-fsanitize-coverage=trace-pc<\/a>&#8221; but back through gcc 4.5. Another notable detail about this work is that it was the first Linux kernel security work funded by Linux Foundation&#8217;s <a href=\"https:\/\/www.coreinfrastructure.org\/grants\">Core Infrastructure Initiative<\/a>. I&#8217;m looking forward to more plugins!<\/p>\n<p>If you&#8217;re on Debian or Ubuntu, the required gcc plugin headers are available via the <code>gcc-$N-plugin-dev<\/code> package (and similarly for all cross-compiler packages).<\/p>\n<p><strong>hardened usercopy<\/strong><\/p>\n<p>Along with work from Rik van Riel, Laura Abbott, Casey Schaufler, and many other folks doing testing on the <a href=\"http:\/\/www.openwall.com\/lists\/kernel-hardening\/\">KSPP mailing list<\/a>, I ported part of PAX_USERCOPY (the basic runtime bounds checking) to upstream as <a href=\"https:\/\/git.kernel.org\/linus\/f5509cc18daa7f82bcc553be70df2117c8eedc16\">CONFIG_HARDENED_USERCOPY<\/a>. One of the interface boundaries between the kernel and user-space are the <code>copy_to_user()<\/code>\/<code>copy_from_user()<\/code> family of functions. Frequently, the size of a copy is known at compile-time (&#8220;built-in constant&#8221;), so there&#8217;s not much benefit in checking those sizes (hardened usercopy avoids these cases). In the case of dynamic sizes, hardened usercopy checks for 3 areas of memory: slab allocations, stack allocations, and kernel text. Direct kernel text copying is simply disallowed. Stack copying is allowed as long as it is entirely contained by the current stack memory range (and on x86, only if it does not include the saved stack frame and instruction pointers). For slab allocations (e.g. those allocated through <code>kmem_cache_alloc()<\/code> and the <code>kmalloc()<\/code>-family of functions), the copy size is compared against the size of the object being copied. For example, if <code>copy_from_user()<\/code> is writing to a structure that was allocated as size 64, but the copy gets tricked into trying to write 65 bytes, hardened usercopy will catch it and kill the process.<\/p>\n<p>For testing hardened usercopy, <a href=\"https:\/\/git.kernel.org\/linus\/6c3521400c345fa2575a6f5b212c215db38c5d93\">lkdtm gained several new tests<\/a>: USERCOPY_HEAP_SIZE_TO, USERCOPY_HEAP_SIZE_FROM, USERCOPY_STACK_FRAME_TO,<br \/>\nUSERCOPY_STACK_FRAME_FROM, USERCOPY_STACK_BEYOND, and USERCOPY_KERNEL. Additionally, USERCOPY_HEAP_FLAG_TO and USERCOPY_HEAP_FLAG_FROM were added to test what will be coming next for hardened usercopy: flagging slab memory as &#8220;safe for copy to\/from user-space&#8221;, effectively whitelisting certainly slab caches, as done by PAX_USERCOPY. This further reduces the scope of what&#8217;s allowed to be copied to\/from, since most kernel memory is not intended to ever be exposed to user-space. Adding this logic will require some reorganization of usercopy code to add some new APIs, as PAX_USERCOPY&#8217;s approach to handling special-cases is to add bounce-copies (copy from slab to stack, then copy to userspace) as needed, which is unlikely to be acceptable upstream.<\/p>\n<p><strong>seccomp reordered after ptrace<\/strong><\/p>\n<p>By its original design, seccomp filtering happened before ptrace so that seccomp-based ptracers (i.e. <code>SECCOMP_RET_TRACE<\/code>) could explicitly bypass seccomp filtering and force a desired syscall. Nothing actually used this feature, and as it turns out, it&#8217;s not compatible with process launchers that install seccomp filters (e.g. systemd, lxc) since as long as the <code>ptrace<\/code> and <code>fork<\/code> syscalls are allowed (and <code>fork<\/code> is needed for any sensible container environment), a process could spawn a tracer to help bypass a filter by injecting syscalls. After Andy Lutomirski convinced me that ordering ptrace first does not change the attack surface of a running process (unless <em>all<\/em> syscalls are blacklisted, the entire ptrace attack surface will always be exposed), I <a href=\"https:\/\/git.kernel.org\/linus\/93e35efb8de45393cf61ed07f7b407629bf698ea\">rearranged things<\/a>. Now there is no (expected) way to bypass seccomp filters, and containers with seccomp filters can allow ptrace again.<\/p>\n<p><em>Edit: missed this next feature when I originally posted<\/em><\/p>\n<p><strong>NX stack and heap on MIPS<\/strong><\/p>\n<p>Other architectures have had a non-executable stack and heap for a while now, and now <a href=\"https:\/\/git.kernel.org\/linus\/1a770b85c1f1c1ee37afd7cef5237ffc4c970f04\">MIPS has caught up<\/a>, thanks to Paul Barton. The primary reason for the delay was finding a way to cleanly deal with branch delay slot instructions which needed a place to write instructions. Traditionally this was on the stack, but now it&#8217;s <a href=\"https:\/\/git.kernel.org\/linus\/432c6bacbd0c16ec210c43da411ccc3855c4c010\">handled with a per-mm page<\/a>.<\/p>\n<p>That&#8217;s it for v4.8! The merge window is open for <a href=\"\/blog\/archives\/2016\/12\/12\/security-things-in-linux-v4-9\/\">v4.9<\/a>&#8230;<\/p>\n<p style='text-align:left'>&copy; 2016 &#8211; 2017, <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: v4.7. Here are a bunch of security things I&#8217;m excited about in Linux v4.8: SLUB freelist ASLR Thomas Garnier continued his freelist randomization work by adding SLUB support. x86_64 KASLR text base offset physical\/virtual decoupling On x86_64, to implement the KASLR text base offset, the physical memory location of the kernel was randomized, which [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,18,21,6,14,19],"tags":[],"_links":{"self":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/938"}],"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=938"}],"version-history":[{"count":14,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/938\/revisions"}],"predecessor-version":[{"id":1040,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/938\/revisions\/1040"}],"wp:attachment":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/media?parent=938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/categories?post=938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/tags?post=938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}