{"id":1275,"date":"2019-05-27T20:49:22","date_gmt":"2019-05-28T03:49:22","guid":{"rendered":"https:\/\/outflux.net\/blog\/?p=1275"},"modified":"2022-04-04T14:34:43","modified_gmt":"2022-04-04T21:34:43","slug":"security-things-in-linux-v5-1","status":"publish","type":"post","link":"https:\/\/outflux.net\/blog\/archives\/2019\/05\/27\/security-things-in-linux-v5-1\/","title":{"rendered":"security things in Linux v5.1"},"content":{"rendered":"<p>Previously: <a href=\"\/blog\/archives\/2019\/03\/12\/security-things-in-linux-v5-0\/\">v5.0<\/a>.<\/p>\n<p>Linux kernel <a href=\"https:\/\/lkml.kernel.org\/r\/CAHk-=whsbDxdapMJGvy9OkVSrKMsKRJBTrH-Qj-qTbkMUGnWUw@mail.gmail.com\">v5.1 has been released<\/a>! Here are some security-related things that stood out to me:<\/p>\n<p><strong>introduction of pidfd<\/strong><br \/>\nChristian Brauner landed the first portion of his work to remove pid races from the kernel: using a <a href=\"https:\/\/git.kernel.org\/linus\/3eb39f47934f9d5a3027fe00d906a45fe3a15fad\">file descriptor to reference a process<\/a> (&#8220;pidfd&#8221;). Now <code>\/proc\/$pid<\/code> can be opened and used as an argument for sending signals with the new <code>pidfd_send_signal()<\/code> syscall. This handle will only refer to the original process at the time the <code>open()<\/code> happened, and not to any later &#8220;reused&#8221; pid if the process dies and a new process is assigned the same pid. Using this method, it&#8217;s now possible to racelessly send signals to exactly the intended process without having to worry about pid reuse. (BTW, this commit wins the 2019 award for Most Well Documented Commit Log Justification.)<\/p>\n<p><strong>explicitly test for userspace mappings of heap memory<\/strong><br \/>\nDuring <a href=\"https:\/\/2019.linux.conf.au\/wiki\/Kernel_Hardening_BoF\">Linux Conf AU 2019 Kernel Hardening BoF<\/a>, Matthew Wilcox noted that there wasn&#8217;t anything in the kernel actually sanity-checking when userspace mappings were being applied to kernel heap memory (which would allow attackers to bypass the <code>copy_{to,from}_user()<\/code> infrastructure). Driver bugs or attackers able to confuse mappings wouldn&#8217;t get caught, so he added checks. To quote the commit logs: <a href=\"https:\/\/git.kernel.org\/linus\/2d432cb7091e99881af803cdd67a31969b863005\">&#8220;It&#8217;s never appropriate to map a page allocated by SLAB into userspace&#8221;<\/a> and <a href=\"https:\/\/git.kernel.org\/linus\/0ee930e6cafa048c1925893d0ca89918b2814f2c\">&#8220;Pages which use page_type must never be mapped to userspace as it would destroy their page type&#8221;<\/a>. The latter check almost <a href=\"https:\/\/lkml.kernel.org\/r\/20190312171421.GJ19508@bombadil.infradead.org\">immediately caught a bad case<\/a>, which was <a href=\"https:\/\/git.kernel.org\/linus\/0266def913771e718fd0c998eecb072e0685e2c9\">quickly fixed<\/a> to avoid page type corruption.<\/p>\n<p><strong>LSM stacking: shared security blobs<\/strong><br \/>\nCasey Schaufler has landed one of the major pieces of getting multiple Linux Security Modules (LSMs) running at the same time (called &#8220;stacking&#8221;). It is now possible for LSMs to <a href=\"https:\/\/git.kernel.org\/linus\/f4ad8f2c40769b3cc9497ba0883bbaf823f7752f\">share the security-specific storage &#8220;blobs&#8221;<\/a> associated with various core structures (e.g. inodes, tasks, etc) that LSMs can use for saving their state (e.g. storing which profile a given task confined under). The kernel originally gave only the single active &#8220;major&#8221; LSM (e.g. SELinux, Apprmor, etc) full control over the entire blob of storage. With &#8220;shared&#8221; security blobs, the LSM infrastructure does the allocation and management of the memory, and LSMs use an offset for reading\/writing their portion of it. This unblocks the way for &#8220;medium sized&#8221; LSMs (like <a href=\"https:\/\/sara.smeso.it\/en\/latest\/\">SARA<\/a> and <a href=\"https:\/\/landlock.io\">Landlock<\/a>) to get stacked with a &#8220;major&#8221; LSM as they need to store much more state than the &#8220;minor&#8221; LSMs (e.g. Yama, LoadPin) which could already stack because they didn&#8217;t need blob storage.<\/p>\n<p><strong id=\"v5.1-safesetid\">SafeSetID LSM<\/strong><br \/>\nMicah Morton <a href=\"https:\/\/git.kernel.org\/linus\/aeca4e2ca65c1aeacfbe520684e6421719d99417\">added<\/a> the new <a href=\"https:\/\/www.kernel.org\/doc\/html\/latest\/admin-guide\/LSM\/SafeSetID.html\">SafeSetID LSM<\/a>, which provides a way to narrow the power associated with the <code>CAP_SETUID<\/code> capability. Normally a process with <code>CAP_SETUID<\/code> can become any user on the system, including root, which makes it a meaningless capability to hand out to non-root users in order for them to &#8220;drop privileges&#8221; to some less powerful user. There are trees of processes under Chrome OS that need to operate under different user IDs and other methods of accomplishing these transitions safely weren&#8217;t sufficient. Instead, this provides a way to create a system-wide policy for user ID transitions via <code>setuid()<\/code> (and group transitions via <code>setgid()<\/code>) when a process has the <code>CAP_SETUID<\/code> capability, making it a much more useful capability to hand out to non-root processes that need to make uid or gid transitions.<\/p>\n<p><strong>ongoing: refcount_t conversions<\/strong><br \/>\nElena Reshetova <a href=\"https:\/\/git.kernel.org\/pub\/scm\/linux\/kernel\/git\/torvalds\/linux.git\/log\/?h=v5.1&#038;qt=grep&#038;q=onvert.*refcount_t\">continued landing<\/a> more <a href=\"\/blog\/archives\/2017\/09\/05\/security-things-in-linux-v4-13\/\"><code>refcount_t<\/code> conversions<\/a><\/a> in core kernel code (e.g. scheduler, futex, perf), with an additional conversion in btrfs from Anand Jain. The existing conversions, mainly when combined with syzkaller, continue to show their utility at <a href=\"https:\/\/git.kernel.org\/linus\/357798909164bf423eac6a78ff7da7e98d2d7f7f\">finding<\/a> <a href=\"https:\/\/git.kernel.org\/linus\/b987222654f84f7b4ca95b3a55eca784cb30235b\">bugs<\/a> <a href=\"https:\/\/git.kernel.org\/linus\/a622b40035d16196bf19b2b33b854862595245fc\">all<\/a> <a href=\"https:\/\/git.kernel.org\/linus\/b3098121c42caaf3aea239b8655cf52d45be116f\">over<\/a> the kernel.<\/p>\n<p><strong>ongoing: implicit fall-through removal<\/strong><br \/>\nGustavo A. R. Silva continued to make <a href=\"https:\/\/git.kernel.org\/pub\/scm\/linux\/kernel\/git\/torvalds\/linux.git\/log\/?h=v5.1&#038;qt=grep&#038;q=expect.*fall.*through\">progress on marking<\/a> more <a href=\"\/blog\/archives\/2019\/03\/12\/security-things-in-linux-v5-0\/\">implicit fall-through<\/a> cases. What&#8217;s so impressive to me about this work, like <code>refcount_t<\/code>, is how many bugs it has been finding (see all the &#8220;<a href=\"https:\/\/git.kernel.org\/pub\/scm\/linux\/kernel\/git\/torvalds\/linux.git\/log\/?h=v5.1&#038;qt=grep&#038;q=missing+break\">missing break<\/a>&#8221; patches). It really shows how quickly the kernel benefits from adding <code>-Wimplicit-fallthrough<\/code> to keep this class of bug from ever returning.<\/p>\n<p><strong>stack variable initialization includes scalars<\/strong><br \/>\nThe structleak gcc plugin (originally <a href=\"\/blog\/archives\/2017\/05\/02\/security-things-in-linux-v4-11\/\">ported from PaX<\/a>) had its <a href=\"https:\/\/outflux.net\/blog\/archives\/2017\/11\/14\/security-things-in-linux-v4-14\/\">&#8220;by reference&#8221; coverage<\/a> improved to <a href=\"https:\/\/git.kernel.org\/linus\/81a56f6dcd20325607d6008f4bb560c96f4c821a\">initialize scalar types<\/a> as well (making &#8220;structleak&#8221; a bit of a misnomer: it now stops leaks from more than structs). Barring compiler bugs, this means that all stack variables in the kernel can be initialized before use at function entry. For variables not passed to functions by reference, the <code>-Wuninitialized<\/code> compiler flag (enabled via <code>-Wall<\/code>) already makes sure the kernel isn&#8217;t building with local-only uninitialized stack variables. And now with <code>CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL<\/code> enabled, all variables passed by reference will be initialized as well. This should eliminate most, if not all, uninitialized stack flaws with very minimal performance cost (for most workloads it is lost in the noise), though it does not have the stack data lifetime reduction benefits of <a href=\"\/blog\/archives\/2018\/12\/24\/security-things-in-linux-v4-20\/\"><code>GCC_PLUGIN_STACKLEAK<\/code><\/a>, which wipes the stack at syscall exit. Clang has recently gained similar <a href=\"https:\/\/reviews.llvm.org\/D54604\">automatic stack initialization support<\/a>, and I&#8217;d love to this feature in native gcc. To evaluate the coverage of the various stack auto-initialization features, I also wrote regression tests in <a href=\"https:\/\/git.kernel.org\/linus\/50ceaa95ea09703722b30b4afa617c972071cd7f\">lib\/test_stackinit.c<\/a>.<\/p>\n<p>That&#8217;s it for now; please let me know if I missed anything. The <a href=\"\/blog\/archives\/2019\/07\/17\/security-things-in-linux-v5-2\/\">v5.2<\/a> kernel development cycle is off and running already. :)<\/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.0. Linux kernel v5.1 has been released! Here are some security-related things that stood out to me: introduction of pidfd Christian Brauner landed the first portion of his work to remove pid races from the kernel: using a file descriptor to reference a process (&#8220;pidfd&#8221;). Now \/proc\/$pid can be opened and used as an [&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\/1275"}],"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=1275"}],"version-history":[{"count":29,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/1275\/revisions"}],"predecessor-version":[{"id":1572,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/1275\/revisions\/1572"}],"wp:attachment":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/media?parent=1275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/categories?post=1275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/tags?post=1275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}