{"id":1171,"date":"2018-06-14T16:23:56","date_gmt":"2018-06-14T23:23:56","guid":{"rendered":"https:\/\/outflux.net\/blog\/?p=1171"},"modified":"2018-10-18T13:56:10","modified_gmt":"2018-10-18T20:56:10","slug":"security-things-in-linux-v4-17","status":"publish","type":"post","link":"https:\/\/outflux.net\/blog\/archives\/2018\/06\/14\/security-things-in-linux-v4-17\/","title":{"rendered":"security things in Linux v4.17"},"content":{"rendered":"<p>Previously: <a href=\"\/blog\/archives\/2018\/04\/12\/security-things-in-linux-v4-16\/\">v4.16<\/a>.<\/p>\n<p>Linux kernel <a href=\"https:\/\/lkml.org\/lkml\/2018\/6\/3\/142\">v4.17 was released last week<\/a>, and here are some of the security things I think are interesting:<\/p>\n<p><strong>Jailhouse hypervisor<\/strong><\/p>\n<p>Jan Kiszka <a href=\"https:\/\/git.kernel.org\/linus\/cea061e455c88312b86142e68c8fc5b8e1294ca2\">landed<\/a> <a href=\"https:\/\/github.com\/siemens\/jailhouse\">Jailhouse hypervisor<\/a> support, which uses static partitioning (i.e. no resource over-committing), where the root &#8220;cell&#8221; spawns new jails by shrinking its own CPU\/memory\/etc resources and hands them over to the new jail. There&#8217;s a nice write-up of the hypervisor <a href=\"https:\/\/lwn.net\/Articles\/578295\/\">on LWN from 2014<\/a>.<\/p>\n<p><strong>Sparc ADI<\/strong><\/p>\n<p>Khalid Aziz <a href=\"https:\/\/git.kernel.org\/linus\/4608f064532c28c0ea3c03fe26a3a5909852811a\">landed<\/a> the userspace support for Sparc <a href=\"https:\/\/git.kernel.org\/pub\/scm\/linux\/kernel\/git\/torvalds\/linux.git\/tree\/Documentation\/sparc\/adi.txt\">Application Data Integrity<\/a> (ADI or SSM: Silicon Secured Memory), which is the hardware memory coloring (tagging) feature in Sparc M7. I&#8217;d love to see this extended into the kernel itself, as it would kill linear overflows between allocations, since the base pointer being used is tagged to belong to only a certain allocation (sized to a multiple of cache lines). Any attempt to increment beyond, into memory with a different tag, raises an exception. Enrico Perla has some great write-ups on <a href=\"https:\/\/lazytyped.blogspot.com\/2016\/12\/hardening-allocators-with-adi.html\">using ADI in allocators<\/a> and a <a href=\"https:\/\/lazytyped.blogspot.com\/2016\/12\/hardware-buffer-overflow-defenses.html\">comparison of ADI to Intel&#8217;s MPX<\/a>.<\/p>\n<p><strong>new kernel stacks cleared on fork<\/strong><\/p>\n<p>It was possible that old memory contents would live in a new process&#8217;s kernel stack. While normally not visible, &#8220;uninitialized&#8221; memory read flaws or read overflows could expose these contents (especially stuff &#8220;deeper&#8221; in the stack that may never get overwritten for the life of the process). To avoid this, I made sure that <a href=\"https:\/\/git.kernel.org\/linus\/e01e80634ecdde1dd113ac43b3adad21b47f3957\">new stacks were always zeroed<\/a>. Oddly, this &#8220;priming&#8221; of the cache appeared to actually improve performance, though it was mostly in the noise.<\/p>\n<p><strong>MAP_FIXED_NOREPLACE<\/strong><\/p>\n<p>As part of further defense in depth against attacks like <a href=\"https:\/\/blog.qualys.com\/securitylabs\/2017\/06\/19\/the-stack-clash\">Stack Clash<\/a>, Michal Hocko <a href=\"https:\/\/git.kernel.org\/linus\/a4ff8e8620d3f4f50ac4b41e8067b7d395056843\">created <code>MAP_FIXED_NOREPLACE<\/code><\/a>. The regular <code>MAP_FIXED<\/code> has a subtle behavior not normally noticed (but used by some, so it couldn&#8217;t just be fixed): it will <em>replace<\/em> any overlapping portion of a pre-existing mapping. This means the kernel would silently overlap the stack into mmap or text regions, since <code>MAP_FIXED<\/code> was being used to build a new process&#8217;s memory layout. Instead, <code>MAP_FIXED_NOREPLACE<\/code> has all the features of <code>MAP_FIXED<\/code> <em>without<\/em> the replacement behavior: it will fail if a pre-existing mapping overlaps with the newly requested one. The ELF loader has been switched to use <code>MAP_FIXED_NOREPLACE<\/code>, and it&#8217;s available to userspace too, for similar use-cases.<\/p>\n<p><strong>pin stack limit during exec<\/strong><\/p>\n<p>I used a big hammer and <a href=\"https:\/\/git.kernel.org\/linus\/c31dbb146dd44af44bc60780ce8fa7a9f5f746df\">pinned the <code>RLIMIT_STACK<\/code> values during exec<\/a>. There were multiple methods to change the limit (through at least <code>setrlimit()<\/code> and <code>prlimit()<\/code>), and there were multiple places the limit got used to make decisions, so it seemed best to just pin the values for the life of the exec so no games could get played with them. Too much assumed the value wasn&#8217;t changing, so better to make that assumption actually true. Hopefully this is the last of the <a href=\"https:\/\/git.kernel.org\/linus\/04e35f4495dd560db30c25efca4eecae8ec8c375\">fixes<\/a> for these bad interactions between stack limits and memory layouts during exec (which have all been defensive measures against flaws like <a href=\"https:\/\/blog.qualys.com\/securitylabs\/2017\/06\/19\/the-stack-clash\">Stack Clash<\/a>). <\/p>\n<p><strong>Variable Length Array removals start<\/strong><\/p>\n<p>Following some discussion over <a href=\"https:\/\/patchwork.kernel.org\/project\/kernel-hardening\/list\/?submitter=134511\">Alexander Popov&#8217;s ongoing port of the stackleak GCC plugin<\/a>, Linus declared that <a href=\"https:\/\/lkml.kernel.org\/r\/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com\">Variable Length Arrays (VLAs) should be eliminated from the kernel entirely<\/a>. This is great because it kills several stack exhaustion attacks, including weird stuff like stepping over guard pages with giant stack allocations. However, with several hundred uses in the kernel, this wasn&#8217;t going to be an easy job. Thankfully, a whole bunch of people stepped up to <a href=\"https:\/\/git.kernel.org\/pub\/scm\/linux\/kernel\/git\/torvalds\/linux.git\/log\/?h=v4.17&#038;qt=grep&#038;q=VLA+usage\">help out<\/a>: Gustavo A. R. Silva, Himanshu Jha, Joern Engel, Kyle Spiers, Laura Abbott, Lorenzo Bianconi, Nikolay Borisov, Salvatore Mesoraca, Stephen Kitt, Takashi Iwai, Tobin C. Harding, and Tycho Andersen. With Linus Torvalds and Martin Uecker, I also helped <a href=\"https:\/\/lwn.net\/Articles\/749064\/\">rewrite the <code>max()<\/code> macro<\/a> to <a href=\"https:\/\/git.kernel.org\/linus\/3c8ba0d61d04ced9f8d9ff93977995a9e4e96e91\">eliminate false positives<\/a> seen by the <code>-Wvla<\/code> compiler option. Overall, about 1\/3rd of the VLA instances were solved for v4.17, with many more coming for v4.18. I&#8217;m hoping we&#8217;ll have entirely eliminated VLAs by the time v4.19 ships.<\/p>\n<p><em>Edit: I missed this next feature when I first published this post:<\/em><\/p>\n<p><strong>syscall register clearing, x86<\/strong><\/p>\n<p>One of the ways attackers can influence potential speculative execution flaws in the kernel is to leak information into the kernel via &#8220;unused&#8221; register contents. Most syscalls take only a few arguments, so all the other <a href=\"http:\/\/man7.org\/linux\/man-pages\/man2\/syscall.2.html#NOTES\">calling-convention-defined registers<\/a> can be cleared instead of just left with whatever contents they had in userspace. As it turns out, clearing registers is very fast. Dominik Brodowski expanded a proof-of-concept from Linus Torvalds into a full <a href=\"https:\/\/git.kernel.org\/linus\/fa697140f9a20119a9ec8fd7460cc4314fbdaff3\">register-clearing syscall wrapper on x86<\/a>.<\/p>\n<p>That&#8217;s it for now! Please let me know if you think I missed anything. Stay tuned for <a href=\"\/blog\/archives\/2018\/08\/20\/security-things-in-linux-v4-18\/\">v4.18<\/a>; the merge window is open. :)<\/p>\n<p style='text-align:left'>&copy; 2018, <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.16. Linux kernel v4.17 was released last week, and here are some of the security things I think are interesting: Jailhouse hypervisor Jan Kiszka landed Jailhouse hypervisor support, which uses static partitioning (i.e. no resource over-committing), where the root &#8220;cell&#8221; spawns new jails by shrinking its own CPU\/memory\/etc resources and hands them over to [&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\/1171"}],"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=1171"}],"version-history":[{"count":12,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/1171\/revisions"}],"predecessor-version":[{"id":1198,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/1171\/revisions\/1198"}],"wp:attachment":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/media?parent=1171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/categories?post=1171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/tags?post=1171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}