Remotely triggerable ICMP buffer underwrite in the FreeBSD kernel reddit / BSD ---------------------------------------------------------------------- TheGrandSchlonging · 2018-11-08 04:29 [Feel free to send this to the FreeBSD lists or whatever to get it fixed. I honestly can't be bothered making a throwaway email and going through the subscription process, nor do I want "TheGrandSchlonging" to appear in FreeBSD commit logs.] This buffer underwrite affects 64-bit versions of the FreeBSD kernel by default from July 2018 (including the 12 betas). Earlier 64-bit versions of the FreeBSD kernel are affected only when a non-default net.inet.icmp.quotelen sysctl has been set that's high enough. HardenedBSD has pulled in the buggy code as well. One workaround, if you're at all concerned about this, is to set net.inet.icmp.quotelen back to 8 and to use `/etc/sysctl.conf` to make the change permanent. The bug is in `icmp_error()` in `sys/netinet/ip_icmp.c`. Recently, this function was targeted for remote memory corruption in the XNU kernel, but none of the main BSDs (OpenBSD, FreeBSD, NetBSD) are or have ever been vulnerable to the XNU bug. (The XNU bug appears to have been misunderstood in the technical write-up and the press reports. It actually results from the erroneous use of an older `MH_ALIGN()` on an mbuf with an attached cluster. See my analysis in [this post](https:// old.reddit.com/r/netsec/comments/9tcv2x/semmle_discovers_six_critical_ vulnerabilities/e8vqtm6/) if interested. I've also written [another po st](https://old.reddit.com/r/BSD/comments/9v6xwg/remotely_triggerable_ icmp_buffer_underwrite_in/e9ehmk0/) that clarifies the impact of three separate ICMP bugs on XNU, OpenBSD, FreeBSD, and NetBSD.) Back in July of this year, a FreeBSD developer bumped up the net.inet.icmp.quotelen sysctl (variable icmp_quotelen) from 8 to 548 to conform with RFC 1812. This activated a dormant flaw in the way size checks and mbuf data alignment are performed. Many different types of IP datagrams will trigger the flaw, but I'll take a simple case: a 137- to 139-byte IP datagram without any IP options and without TCP or SCTP as the L4 protocol (this is simply so I can skip over some TCP and SCTP special casing for this example). oiphlen = oip->ip_hl << 2; // oiphlen = 20 nlen = m_length(n, NULL); // nlen = [137, 139] icmpelen = max(8, min(V_icmp_quotelen, ntohs(oip->ip_len) - oiphlen)); // icmpelen = max(8, min(548, [137, 139] - 20)) // icmpelen = max(8, min(548, [117, 119])) // icmpelen = max(8, [117, 119]) // icmpelen = [117, 119] icmplen = min(oiphlen + icmpelen, nlen); // icmplen = min(20 + [117, 119], [137, 139]) // icmplen = min([137, 139], [137, 139]) // icmplen = [137, 139] if (MHLEN > sizeof(struct ip) + ICMP_MINLEN + icmplen) m = m_gethdr(M_NOWAIT, MT_DATA); // MHLEN = 168 on 64-bit // if (168 > 20 + 8 + [137, 139]) // if (168 > [165, 167]) // yes icmplen = min(icmplen, M_TRAILINGSPACE(m) - sizeof(struct ip) - ICMP_MINLEN); // icmplen = min([137, 139], MHLEN - 20 - 8) // icmplen = min([137, 139], 168 - 20 - 8) // icmplen = min([137, 139], 140) // icmplen = [137, 139] m_align(m, ICMP_MINLEN + icmplen); // m_align(m, 8 + [137, 139]) // m_align(m, [145, 147]) // // from the m_align() definition: // adjust = M_SIZE(m) - len; // m->m_data += adjust &~ (sizeof(long)-1); // // adjust = MHLEN - [145, 147] // adjust = 168 - [145, 147] // adjust = [21, 23] // [21, 23] &~ (sizeof(long) - 1) // [21, 23] &~ 7 (LP64) // 16 // m->m_data += 16 m->m_data -= sizeof(struct ip); ... nip = mtod(m, struct ip *); bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip)); // 4-byte underwrite Note that an old Net/3 check, still harmlessly in use by OpenBSD (because it doesn't have XNU's insanely huge mbuf pkthdrs driving down MHLEN or FreeBSD's large/adjustable ICMP quote length), was removed from FreeBSD but would have stopped the underwrite cold (albeit with an undesirable kernel panic a la Darwin Nuke): if (m->m_data - sizeof(struct ip) < m->m_pktdat) panic("icmp len"); This check was removed in 2005, when the ICMP quote length was adjustable as it is now (though safe by default, unlike now) and when the underwrite would've led to a dangerous corruption of pkthdr data. Nowadays, the underwrite hits a harmless area of the mbuf pkthdr. Even the 4 bytes written are practically fixed (0x45, 0x00, 0x??, 0x??, with next to no flexibility for the ??). It's not entirely self- healing, however: m_len is fine, but m_data remains pathologically pointing to 4 bytes outside its designated buffer space. Look what would happen with macros such as `M_LEADINGSPACE()`, called directly and from other macros such as `M_PREPEND()`: #define M_LEADINGSPACE(m) \ (M_WRITABLE(m) ? ((m)->m_data - M_START(m)) : 0) The main concern at this point is with L2 code after the `ip_output()` hand-off. \*hug\* P.S. I recommend setting net.inet.ip.maxfragsperpacket to 0 to prepare for what's coming next. ---------------------------------------------------------------------- MrThugger · 2018-11-08 06:18 it's funny that NetBSD is the only one that is not affected, FreeBSD and OpenBSD and XNU fixed the bug recently but NetBSD did that a long time ago ​ ---------------------------------------------------------------------- WeaklyConsistent · 2018-11-08 16:57 >P.S. I recommend setting net.inet.ip.maxfragsperpacket to 0 to prepare for what's coming next. Aww jeez ---------------------------------------------------------------------- X-Istence · 2018-11-08 22:16 https://reviews.freebsd.org/rS340260 The default on FreeBSD 11 and below is still 8 for the sysctl, so is not affected so long as the user has not modified this. Review of the fragmentation code is also underway with the hint to set it to 0. ---------------------------------------------------------------------- TotesMessenger · 2018-11-09 14:15 I'm a bot, *bleep*, *bloop*. Someone has linked to this thread from another place on reddit: - [/r/bprogramming] [Remotely triggerable ICMP buffer underwrite in the FreeBSD kernel](https://www.reddit.com/r/bprogramming/comments/9vg doc/remotely_triggerable_icmp_buffer_underwrite_in/) - [/r/pfsense] [Ping of Death vulnerability in latest FreeBSD kernel ( CVE-2018- 17156)](https://www.reddit.com/r/PFSENSE/comments/9vkqkr/ping_of_death _vulnerability_in_latest_freebsd/)  *^(If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads.) ^\([Info](/r/TotesMessenger) ^/ ^[Contact](/message/compose?to=/r/TotesMessenger))* ---------------------------------------------------------------------- notaplumber · 2018-11-09 14:17 What part of this commit leads you to believe that OpenBSD was affected? Because it was not.. the code in question was harmless thanks to prior work on OpenBSD, it was simply fixed for correctness. https://github.com/openbsd/src/commit/e0f050718218cef3e4fe9ce49d325749 a7fdfa01 ---------------------------------------------------------------------- MrThugger · 2018-11-09 17:38 On the commit you noted, no prior work was done in OpenBSD to protect against this. Stop acting like OpenBSD applied some sanity because they applied none. They fixed the bug because their code was buggy, period. And by the way you are looking at the wrong commit! I see that you were confused by the cvs log which you didn't understand. OpenBSD actually fixed a whole bunch of bugs in their ICMP, they again had done no "prior work" against that. ---------------------------------------------------------------------- Claitu · 2018-11-09 18:12 \> I honestly can't be bothered making a throwaway email and going through the subscription process No, you're just too lazy. On [https://www.freebsd.org/security/](https://www.freebsd.org/security/) you can read: \> FreeBSD security issues specific to the base system should be reported via email to the [FreeBSD Security Team](mailto:secteam@FreeBSD.org) No account whatsoever is required. ​ ---------------------------------------------------------------------- notaplumber · 2018-11-09 20:44 *Yawn.* The only other recent commit, is not related and is addressing the ICMP input path. OpenBSD is not effected, and it does have other checks in this path, like this one from 18 years ago, from NetBSD. https://github.com/openbsd/src/blob/master/sys/netinet/ip_icmp.c#L194 But it's clear you have some kind of agenda. ---------------------------------------------------------------------- MrThugger · 2018-11-09 21:45 You didn't look at the log! Three bugs fixed in OpenBSD on Nov 5, 2018: [https://github.com/openbsd/src/commits/master/sys/netinet/ip\_icmp.c] (https://github.com/openbsd/src/commits/master/sys/netinet/ip_icmp.c) All of those were already fixed in NetBSD and some were also already fixed in FreeBSD and XNU. Do you get it or do you prefer to double down in cluelessness? Yes the bugs were already fixed in NetBSD, that's it. It is "funny" but nothing more and you don't need to start b\*tching around like this. Move on now. ---------------------------------------------------------------------- notaplumber · 2018-11-09 23:03 I did look. But you apparently can't read yourself. You're the one claiming that OpenBSD is affected by this, so prove it. > "Not a security issue since the cluster code is not reachable, there is enough space in an mbuf." > "This code was never reached as ICMP length was truncated before, but fix the wrong calculation anyway." That sounds like an awful like "not affected" to me, and just taking the opportunity to be a bit more proactive. > Move on now. ಠ_ಠ Why, afraid you might learn something? ---------------------------------------------------------------------- LocalRefuse · 2018-11-09 23:33 I think the frustration here is about not giving proper credit. ---------------------------------------------------------------------- notaplumber · 2018-11-10 00:07 ¯\\_(ツ)_/¯ I couldn't imagine why. ---------------------------------------------------------------------- LimbRetrieval-Bot · 2018-11-10 00:07 You dropped this \ *** ^^ To prevent anymore lost limbs throughout 2;Reddit, correctly escape the arms and should ers by typing the shrug as `¯\\\_(ツ)_/¯` o r `¯\\\_(ツ)\_/¯` [^^Click here to see why this is necessar y](https://np.reddit.com/r/OutOfTheLoop/comments/3fbrg3/is_there_a_rea son_why_the_arm_is_always_missing/ctn5gbf/) ---------------------------------------------------------------------- notaplumber · 2018-11-10 00:09 Too slow, bot. ---------------------------------------------------------------------- MrThugger · 2018-11-10 00:39 First bug: fixed in NetBSD in July 2010 Second bug: fixed in NetBSD in January 2018 Third bug: fixed in NetBSD in February 2018, fixed in FreeBSD in April 2016 NetBSD fixed the bugs before OpenBSD did. That's it, nothing you can do about it. You are trying to say that those aren't bugs thanks to OpenBSD's "prior work". This is wrong, the BSDs had the same bugs and the same mistakes. Then there are BSDs that did fix in advance and BSDs that didn't. ---------------------------------------------------------------------- MrThugger · 2018-11-10 00:49 As a big user of FreeBSD and NetBSD I do recognize the values of each, and I am getting tired of the clueless OpenBSD fanbois that try to claim credit but don't have an idea of what they're talking about. Like in this thread I just have to make one simple general remark for a clueless OpenBSD fanboi to come and say that OpenBSD did some "prior work" not to be affected. This is just wrong, they did none. If we want to give credit to anyone here it should be for NetBSD which fixed the bugs in advance contrary to the others. It is all I am saying I don't understand why there needs to be a tense atmosphere. You can't make any remark on reddit anymore. ---------------------------------------------------------------------- [deleted] · 2018-11-10 01:47 [deleted] ---------------------------------------------------------------------- TheGrandSchlonging · 2018-11-10 03:52 You're both right about something: NetBSD's `icmp_error()` is the cleanest and didn't need any changes recently. OpenBSD's `icmp_error()` was sloppy but the sloppiness was unreachable. NetBSD right now has the better written network stack in general, probably largely owing to the work of Maxime Villard. For desktop use, it's just a shame that Chromium still appears to be a WIP with NetBSD, whereas it's been `pledge()`'d up the wazoo in OpenBSD. There are actually three separate bugs: **1)** The XNU one. The cluster path was reachable because of XNU's huge pkthdrs driving down MHLEN, and then XNU's `MH_ALIGN()` was erroneous for the mbuf with the attached cluster. OpenBSD checks that M_EXT is clear before calling a similar implementation of `MH_ALIGN()`. NetBSD also checks that M_EXT is clear but in addition has kernel assertions internal to its `MH_ALIGN()` for added robustness. FreeBSD uses `m_align()`, which is type-generic and will therefore work with the cluster code (it's also made `MH_ALIGN()` and `M_ALIGN()` simple wrappers for `m_align()`). I still fail to see why Apple has suggested an attack for this bug isn't Internet-routable. The existence of the Internet-routable Darwin Nuke some years back shows that you can route IP options that XNU will consider bad -- which is necessary for triggering `icmp_error()` from `ip_dooptions()` -- but that intermediary routers will happily pass. **2)** The FreeBSD one described in this thread. NetBSD isn't affected, because it calls `MH_ALIGN()` accounting for struct ip and doesn't bother with the subtraction nonsense. (The 20-byte struct ip leads to an optimization trade-off on 64-bit. You can long-align the IP header or the ICMP header, but not both. NetBSD has chosen to long- align the IP header.). XNU will catch the problematic subtraction and leave cleanly. OpenBSD will catch the problematic subtraction and panic, but this is unreachable as a consequence of a fixed ICMP quote length of 8 and enough room to accommodate a 60-byte inner IP header. **3)** The OpenBSD one. This was in the cluster code as a 20-byte underwrite of the cluster, but the cluster code isn't reachable. NetBSD isn't affected, because as noted, it scraps all the subtraction nonsense. FreeBSD's cluster code is reachable, but the type-generic `m_align()` does the right thing for clusters, and the subtraction is safe on account of the large size of a cluster. I haven't examined Apple's fix for the XNU bug yet, but note that you can turn its problematic `MH_ALIGN(m, ICMP_MINLEN + icmplen)` into a no-op by ensuring that `ICMP_MINLEN + icmplen == MHLEN`. Then the cluster can be underwritten by 20 bytes as opposed to sending m_data off into the wild with the large size_t resulting from `ICMP_MINLEN + icmplen > MHLEN`. ---------------------------------------------------------------------- notaplumber · 2018-11-10 04:29 Thank you for taking the time to expand on this. ---------------------------------------------------------------------- notaplumber · 2018-11-10 04:39 I agree that Maxime Villard has done some excellent work for NetBSD, and even keeps OpenBSD on its toes. But he can be very disagreeable, and a bit egotistical, especially the little Twitter infographics he puts out there. MrThugger's is also pretty transparent, considering his post history is exclusively knocking OpenBSD. ---------------------------------------------------------------------- MrThugger · 2018-11-10 05:57 >MrThugger's is also pretty transparent, considering his post history is exclusively knocking OpenBSD. I am subscribed to upvote the BSD content but it is sad that I am always fact-checking OpenBSD fanbois. Bye! ---------------------------------------------------------------------- daemonpenguin · 2018-11-10 18:47 I don't think you understood what the OP wrote. What you replied with is no way invalidates the OP's claim that they don't want to create a separate e-mail account to use to send to the security mailing list. They're not saying they want to avoid creating a FreeBSD account. They're saying they don't want to have to create a separate e-mail to post to the list because that is both A) public and B) likely to result in spam. You even quoted their explanation which you skipped over in your reply. ---------------------------------------------------------------------- gaussHaus · 2018-11-10 19:06 But as shown in the commit (that fixed this issue) posted earlier in this thread, the credit simply reads "a reddit user". Furthermore, secteam@ is a private mailing list, with no public archives. ---------------------------------------------------------------------- TheGrandSchlonging · 2018-11-11 00:52 I deleted a very ungracious reply to that poster with some obscenities, but I'll just point out two things: 1. Emailing secteam still requires a throwaway email, which is the bulk of the hassle, not so much the subscription process for freebsd- current. I remember trying to make a throwaway email a few months back so I could email an escort service, and I went through a ton of providers, all of which fell into at least one of these categories: no sending allowed, mobile phone verification, broken site with 503s, test email to receive-only box not arriving at all. Then throw in the filters for spammy providers. No thanks. 2. Anyone who's reported issues to FreeBSD knows that the quickest way to get something fixed is to mail the freebsd-current list. It's not at all unheard of for issues reported to secteam to sit in limbo for three or four months (some secteam members themselves have raised the point). No production releases are affected by this by default, and it's not even clear that this merits a panic button. ---------------------------------------------------------------------- WeaklyConsistent · 2018-11-11 22:49 >No production releases are affected by this by default, and it's not even clear that this merits a panic button. What about the fragment reassembly bug you alluded to? ​ ​ ---------------------------------------------------------------------- BumpitySnook · 2018-11-14 05:48 FreeBSD developers sometimes notice (or are directed to) posts on /r/BSD . The redacted credit was due to the request in the OP: "nor do I want "TheGrandSchlonging" to appear in FreeBSD commit logs." ---------------------------------------------------------------------- BumpitySnook · 2018-11-14 05:50 I love Maxime Villard's twitter