{"id":1497,"date":"2021-04-05T16:24:07","date_gmt":"2021-04-05T23:24:07","guid":{"rendered":"https:\/\/outflux.net\/blog\/?p=1497"},"modified":"2022-04-04T17:01:45","modified_gmt":"2022-04-05T00:01:45","slug":"security-things-in-linux-v5-9","status":"publish","type":"post","link":"https:\/\/outflux.net\/blog\/archives\/2021\/04\/05\/security-things-in-linux-v5-9\/","title":{"rendered":"security things in Linux v5.9"},"content":{"rendered":"<p>Previously: <a href=\"\/blog\/archives\/2021\/02\/08\/security-things-in-linux-v5-8\/\">v5.8<\/a><\/p>\n<p>Linux <a href=\"https:\/\/lore.kernel.org\/lkml\/CAHk-=wi-u86++np80GQvgDuARdt9xpBNho6SjHLmYgm8jibGag@mail.gmail.com\">v5.9 was released in October, 2020<\/a>. Here\u2019s my summary of various security things that I found interesting:<\/p>\n<p><strong id=\"v5.9-notif_addfd\">seccomp user_notif file descriptor injection<\/strong><br \/>\nSargun Dhillon added the ability for <code>SECCOMP_RET_USER_NOTIF<\/code> filters to <a href=\"https:\/\/git.kernel.org\/linus\/7cf97b12545503992020796c74bd84078eb39299\">inject file descriptors into the target process<\/a> using <code>SECCOMP_IOCTL_NOTIF_ADDFD<\/code>. This lets container managers fully emulate syscalls like <code>open()<\/code> and <code>connect()<\/code>, where an actual file descriptor is expected to be available after a successful syscall. In the process I <a href=\"https:\/\/git.kernel.org\/linus\/d9539752d23283db4692384a634034f451261e29\">fixed<\/a> a couple <a href=\"https:\/\/git.kernel.org\/linus\/4969f8a073977123504609d7310b42a588297aa4\">bugs<\/a> and <a href=\"https:\/\/git.kernel.org\/linus\/c0029de50982c1fb215330a5f9d433cec0cfd8cc\">refactored<\/a> the file descriptor receiving code.<\/p>\n<p><strong id=\"v5.9-var-init\">zero-initialize stack variables with Clang<\/strong><br \/>\nWhen Alexander Potapenko landed support for Clang&#8217;s <a href=\"\/blog\/archives\/2019\/07\/17\/security-things-in-linux-v5-2\/#v5.2-var-init\">automatic variable initialization<\/a>, it did so with a byte pattern designed to really stand out in kernel crashes. Now he&#8217;s added support for doing <a href=\"https:\/\/git.kernel.org\/linus\/f0fe00d4972a8cd4b98cc2c29758615e4d51cdfe\">zero initialization<\/a> via <code>CONFIG_INIT_STACK_ALL_ZERO<\/code>, which besides actually being faster, has a few behavior benefits as well. &#8220;Unlike pattern initialization, which has a higher chance of triggering existing bugs, zero initialization provides safe defaults for strings, pointers, indexes, and sizes.&#8221; Like the pattern initialization, this feature stops entire classes of uninitialized stack variable flaws.<\/p>\n<p><strong id=\"v5.9-common-entry\">common syscall entry\/exit routines<\/strong><br \/>\nThomas Gleixner created <a href=\"https:\/\/git.kernel.org\/linus\/fc4177be963dccad73b98d7db3a8a38911f952b7\">architecture-independent code to do syscall entry\/exit<\/a>, since much of the kernel&#8217;s work during a syscall entry and exit is the same. There was no need to repeat this in each architecture, and having it implemented separately meant bugs (or features) might only get fixed (or implemented) in a handful of architectures. It means that features like seccomp become much easier to build since it wouldn&#8217;t need per-architecture implementations any more. Presently only <a href=\"https:\/\/git.kernel.org\/linus\/27d6b4d14f5c3ab21c4aef87dd04055a2d7adf14\">x86 has switched over to the common routines<\/a>.<\/p>\n<p><strong id=\"v5.9-slab\">SLAB <code>kfree()<\/code> hardening<\/strong><br \/>\nTo reach <code>CONFIG_SLAB_FREELIST_HARDENED<\/code> feature-parity with the SLUB heap allocator, I added <a href=\"https:\/\/git.kernel.org\/linus\/dabc3e291d56e3125113101bc6d53d1a1738294d\">naive double-free detection<\/a> and the ability to <a href=\"https:\/\/git.kernel.org\/linus\/3404be67bf73515babd74acd8525d09dafe4234d\">detect cross-cache freeing<\/a> in the SLAB allocator. This should keep a class of type-confusion bugs from biting kernels using SLAB. (Most distro kernels use SLUB, but some smaller devices prefer the slightly more compact SLAB, so this hardening is mostly aimed at those systems.)<\/p>\n<p><strong id=\"v5.9-capabilities\">new <code>CAP_CHECKPOINT_RESTORE<\/code> capability<\/strong><br \/>\nAdrian Reber added the <a href=\"https:\/\/git.kernel.org\/linus\/124ea650d3072b005457faed69909221c2905a1f\">new <code>CAP_CHECKPOINT_RESTORE<\/code> capability<\/a>, splitting this functionality off of <code>CAP_SYS_ADMIN<\/code>. The needs for the kernel to correctly checkpoint and restore a process (e.g. used to move processes between containers) continues to grow, and it became clear that the security implications were lower than those of <code>CAP_SYS_ADMIN<\/code> yet distinct from other capabilities. Using this capability is now the preferred method for doing things like <a href=\"https:\/\/git.kernel.org\/linus\/ebd6de6812387a2db9a52842cfbe004da1dd3be8\">changing <code>\/proc\/self\/exe<\/code><\/a>.<\/p>\n<p><strong id=\"v5.9-debugfs\"><code>debugfs<\/code> boot-time visibility restriction<\/strong><br \/>\nPeter Enderborg added the <a href=\"https:\/\/git.kernel.org\/linus\/a24c6f7bc923d5e2f3139855eb09b0d480d6b410\"><code>debugfs<\/code> boot parameter<\/a> to control the visibility of the kernel&#8217;s debug filesystem. The contents of debugfs continue to be a common area of sensitive information being exposed to attackers. While this was effectively possible by unsetting <code>CONFIG_DEBUG_FS<\/code>, that wasn&#8217;t a great approach for system builders needing a single set of kernel configs (e.g. a distro kernel), so now it can be disabled at boot time.<\/p>\n<p><strong id=\"v5.9-seccomp\">more seccomp architecture support<\/strong><br \/>\nMichael Karcher implemented the <a href=\"https:\/\/git.kernel.org\/linus\/0bb605c2c7f2b4b314b91510810b226de7f34fa1\">SuperH seccomp hooks<\/a>, Guo Ren implemented the <a href=\"https:\/\/git.kernel.org\/linus\/e95a4f8cb985e759648b32ed0b721a472deb86a5\">C-SKY seccomp hooks<\/a>, and Max Filippov implemented the <a href=\"https:\/\/git.kernel.org\/linus\/da94a40f72859ce24dc72de9292981513a33e427\">xtensa seccomp hooks<\/a>. Each of these included the ever-important updates to the seccomp regression testing suite in the kernel selftests.<\/p>\n<p><strong id=\"v5.9-ssp-riscv\">stack protector support for RISC-V<\/strong><br \/>\nGuo Ren implemented <a href=\"https:\/\/git.kernel.org\/linus\/f2c9699f65557a31fed4ddb9e5b4d9489b1bf32f\"><code>-fstack-protector<\/code> (and <code>-fstack-protector-strong<\/code>) support for RISC-V<\/a>. This is the initial global-canary support while the patches to GCC to support per-task canaries is getting finished (similar to the per-task canaries done for arm64). This will mean nearly all stack frame write overflows are no longer useful to attackers on this architecture. It&#8217;s nice to see this finally land for RISC-V, which is quickly approaching architecture feature parity with the other major architectures in the kernel.<\/p>\n<p><strong id=\"v5.9-tasklet\">new <code>tasklet<\/code> API<\/strong><br \/>\nRomain Perier and Allen Pais <a href=\"https:\/\/git.kernel.org\/linus\/12cc923f1ccc1df467e046b02a72c2b3b321b6a2\">introduced a new <code>tasklet<\/code> API<\/a> to make their use safer. Much like the <a href=\"\/blog\/archives\/2018\/02\/05\/security-things-in-linux-v4-15\/#v4.15-timer_list\"><code>timer_list<\/code> refactoring<\/a> work done earlier, the <code>tasklet<\/code> API is also a potential source of simple function-pointer-and-first-argument controlled exploits via linear heap overwrites. It&#8217;s a smaller attack surface since it&#8217;s used much less in the kernel, but it is the same weak design, making it a sensible thing to replace. While the use of the <code>tasklet<\/code> API is <a href=\"https:\/\/git.kernel.org\/pub\/scm\/linux\/kernel\/git\/torvalds\/linux.git\/tree\/include\/linux\/interrupt.h?h=v5.9#n588\">considered deprecated<\/a> (replaced by <a href=\"https:\/\/lore.kernel.org\/lkml\/20200716081538.2sivhkj4hcyrusem@linutronix.de\">threaded IRQs<\/a>), it&#8217;s not always a simple mechanical refactoring, so the old API still needs refactoring (since that CAN be done mechanically is most cases).<\/p>\n<p><strong id=\"v5.9-fsgsbase\">x86 <code>FSGSBASE<\/code> implementation<\/strong><br \/>\nSasha Levin, Andy Lutomirski, Chang S. Bae, Andi Kleen, Tony Luck, Thomas Gleixner, and others landed the long-awaited <a href=\"https:\/\/git.kernel.org\/linus\/4da9f3302615f4191814f826054846bf843e24fa\"><code>FSGSBASE<\/code> series<\/a>. This provides task switching performance improvements while keeping the kernel safe from modules accidentally (or maliciously) trying to use the features directly (which exposed an <a href=\"https:\/\/lwn.net\/Articles\/821723\/\">unprivileged direct kernel access hole<\/a>).<\/p>\n<p><strong id=\"v5.9-msr-filter\">filter x86 MSR writes<\/strong><br \/>\nWhile it&#8217;s been long understood that writing to CPU Model-Specific Registers (MSRs) from userspace was a <a href=\"https:\/\/lore.kernel.org\/lkml\/20130208191213.GA25081@www.outflux.net\/\">bad idea<\/a>, it has been left enabled for things like <a href=\"http:\/\/manpages.ubuntu.com\/manpages\/xenial\/man8\/x86_energy_perf_policy.8.html\"><code>MSR_IA32_ENERGY_PERF_BIAS<\/code><\/a>. Boris Petkov has decided enough is enough and has now enabled logging and kernel tainting (<code>TAINT_CPU_OUT_OF_SPEC<\/code>) by default and a way to <a href=\"https:\/\/git.kernel.org\/linus\/a7e1f67ed29f0c339e2aa7483d13b085127566ab\">disable MSR writes<\/a> at runtime. (However, since this is controlled by a normal module parameter and the root user can just turn writes back on, I continue to recommend that people build with <code>CONFIG_X86_MSR=n<\/code>.) The expectation is that userspace MSR writes will be entirely removed in future kernels.<\/p>\n<p><strong id=\"v5.9-uninitialized\"><code>uninitialized_var()<\/code> macro removed<\/strong><br \/>\nI made <a href=\"https:\/\/git.kernel.org\/linus\/3f649ab728cda8038259d8f14492fe400fbab911\">treewide changes<\/a> to <a href=\"https:\/\/git.kernel.org\/linus\/63a0895d960aa3d3653ef0ecad5bd8579388f14b\">remove the <code>uninitialized_var()<\/code> macro<\/a>, which had been used to silence compiler warnings. The rationale for this macro was weak to begin with (&#8220;the compiler is reporting an uninitialized variable that is clearly initialized&#8221;) since it was mainly papering over compiler bugs. However, it creates a much more fragile situation in the kernel since now such uses can actually <a href=\"https:\/\/lore.kernel.org\/lkml\/20200603174714.192027-1-glider@google.com\/\">disable automatic stack variable initialization<\/a>, as well as mask legitimate &#8220;unused variable&#8221; warnings. The proper solution is to just initialize variables the compiler warns about.<\/p>\n<p><strong id=\"v5.9-Wcast-function-type\">function pointer cast removals<\/strong><br \/>\nOscar Carter has started <a href=\"https:\/\/git.kernel.org\/linus\/5cdfbdce5de6b5b56e104676409762fc1289a9c2\">removing<\/a> function pointer <a href=\"https:\/\/git.kernel.org\/linus\/875102ea4b7700330a33c0db71555d91dafa9c82\">casts<\/a> from the kernel, in an effort to allow the kernel to build with <code>-Wcast-function-type<\/code>. The future use of Control Flow Integrity checking (which does validation of function prototypes matching between the caller and the target) tends not to work well with function casts, so it&#8217;d be <a href=\"https:\/\/github.com\/KSPP\/linux\/issues\/20\">nice to get rid of these<\/a> before CFI lands.<\/p>\n<p><strong id=\"v5.9-flex-array\">flexible array conversions<\/strong><br \/>\nAs part of Gustavo A. R. Silva&#8217;s <a href=\"\/blog\/archives\/2021\/02\/08\/security-things-in-linux-v5-8\/#v5.8-flex-array\">on-going work<\/a> to replace <a href=\"https:\/\/git.kernel.org\/pub\/scm\/linux\/kernel\/git\/torvalds\/linux.git\/log\/?h=v5.9&#038;qt=grep&#038;q=zero-length+array\">zero-length<\/a> and <a href=\"https:\/\/git.kernel.org\/pub\/scm\/linux\/kernel\/git\/torvalds\/linux.git\/log\/?h=v5.9&#038;qt=grep&#038;q=one-element+array\">one-element<\/a> arrays with flexible arrays, he has <a href=\"https:\/\/git.kernel.org\/linus\/68e4cd17e218971a2fd60c30fe14078dc0d8a68e\">documented the details of the flexible array conversions<\/a>, and the <a href=\"https:\/\/git.kernel.org\/pub\/scm\/linux\/kernel\/git\/torvalds\/linux.git\/log\/?h=v5.9&#038;qt=grep&#038;q=flex_array_size\">various helpers<\/a> to be used in kernel code. Every commit gets the kernel closer to building with <code>-Warray-bounds<\/code>, which catches a lot of potential buffer overflows at compile time.<\/p>\n<p>That&#8217;s it for now! Please let me know if you think anything else needs some attention. Next up is Linux <a href=\"\/blog\/archives\/2022\/04\/04\/security-things-in-linux-v5-10\/\">v5.10<\/a>.<\/p>\n<p style='text-align:left'>&copy; 2021 &#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.8 Linux v5.9 was released in October, 2020. Here\u2019s my summary of various security things that I found interesting: seccomp user_notif file descriptor injection Sargun Dhillon added the ability for SECCOMP_RET_USER_NOTIF filters to inject file descriptors into the target process using SECCOMP_IOCTL_NOTIF_ADDFD. This lets container managers fully emulate syscalls like open() and connect(), where [&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\/1497"}],"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=1497"}],"version-history":[{"count":22,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/1497\/revisions"}],"predecessor-version":[{"id":1587,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/1497\/revisions\/1587"}],"wp:attachment":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/media?parent=1497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/categories?post=1497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/tags?post=1497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}