182 points · by _vvdf · 9y ago · 94 comments
$ echo "install dccp /bin/true" >> /etc/modprobe.d/dccp.conf
$ sudo rmmod dccp # in case it's already loaded
This is a good idea for any modules you don't expect to ever need. In my case: $ cat /etc/modprobe.d/disabled_modules.conf
install appletalk /bin/true
install bluetooth /bin/true
install cramfs /bin/true
install dccp /bin/true
install firewire-core /bin/true
*snipped*
install tipc /bin/true
install udf /bin/true
install usb-storage /bin/true
install vfat /bin/true
The original -- to me -- recommendations for this were found in some "hardening guide" for RHEL (CIS, NSA, etc.), although I don't remember which.See also the "modprobe.blacklist=" kernel parameter, which you'll have to use for "modules" that are compiled into the kernel itself (i.e., they are not actually loadable kernel modules).
15 years ago, when building your own kernels was a normal everyday thing, I simply built my kernels with everything compiled in and modules disabled. This (would have) prevented attacks such as kernel-level rootkits.
In addition, "one neat trick" was that you could halt (not poweroff) the machine (!) -- such as in the case of a Linux box simply acting as a router/firewall -- and the kernel would still be running. Good luck compromising that! :-)
If you Google for amodload, you can find (uuencoded, in a Bugtraq message --- it's 2017 and I just uudecoded something!) Josh Daymont's old SunOS 4.1.3 modloader hack, which overwrote a kernel socket callback and used socket buffers to hold module text.
I really hate to question you but... are you sure?
The particular case that I'm thinking of was probably ~15 years ago. There was some 0-day or worm or something (details are a little vague now, obviously) that would install a kernel module and then loaded it to hide itself.
I can recall a discussion at the time and this was a recommendation. It wasn't just disabling individual modules -- it was actually disabling support for loading kernel modules. That is, whatever the kernel itself needed in order to support dynamic loading of modules at run-time was needed; the kernel was not able to load a module.
Like I said, it's been probably 15 years ago and I'm sure I'm not remembering everything 100% correctly so you may very well be correct... but I'm almost certain you could entirely disable loading of kernel modules (the kernel itself didn't support loading them).
Googling "amodload" now, thanks for the pointer...
EDIT: Here's an example of this very thing. https://www.blackhat.com/presentations/bh-europe-09/Lineberr...
modprobe dccp #with /bin/false fails with
modprobe: ERROR: Error running install command for dccp
modprobe: ERROR: could not insert 'dccp': Operation not permittedDue to the way modprobe may be hooked by various things, its best to use /bin/true to ensure less breakage.
I think the original documentation described them something like this: /bin/true does nothing, successfully. /bin/false does nothing, unsuccessfully.
On my Debian kernel, CONFIG_IP_DCCP is set to "m" (in /boot/config-`uname -r`), which means that DCCP support is built as a module. The code isn't loaded until the first program tries to call socket(...IPPROTO_DCCP). At that point, the kernel will look at /proc/sys/kernel/modprobe and run that program, /sbin/modprobe by default, to load dccp.ko.
Automatic module loading is great when e.g. udev runs and detects what hardware you have, but it's probably not something you'd ever need once a system has completed boot. A very simple hardening measure for machines running untrusted unprivileged code is to echo /bin/false > /proc/sys/kernel/modprobe, late in the boot process (e.g., in /etc/rc.local).
The downside is that system administrator won't be able to run tools that require loading modules, of which iptables is probably the most notable one. A better option than /bin/false is a shell script that logs its arguments to syslog, e.g., `logger -p authpriv.info -- "Refused modprobe $*"`. The sysadmin can manually run modprobe on whatever module name got syslogged (or temporarily set /proc/sys/kernel/modprobe back to /sbin/modprobe). And you can alert on that syslog line to see if there's an attack in progress.
(Does anyone know if it's possible to disable module auto-loading for a tree/namespace of processes, e.g. a Docker container, but keep it working for the base system?)
If you only need a specific set of modules, you might as well just build them in and just forget LKMs altogether.
It's not very convenient when you run into some obscure hardware/fs/protocol, but depending on your use case, that might not be an issue (and even in the rare case where it does come up, it's not the end of the world – a minimal kernel build only takes a couple of minutes on a reasonably fast machine).
The only case where LKMs really seem necessary (besides in generic distro kernels) is for module development/debugging, but you're probably not going to do that with a production kernel anyway.
As soon as you've administered 10+ servers at the same time, your patience for variation from the norm and manual patch building goes way down.
Once you've administered 100+ servers at the same time, any thoughts of doing that go right out the window. along with the person that brought it up.
At that point, any changes are diffs applied to the original package (or a brand new package from scratch) that you put in your local repo. Kernels actually get updates fairly often, which means you either have a lot of work, or you ignore the non-essential updates. Neither are ideal.
It's certainly possible to build infrastructure to automate compiling, testing, and pushing out a new kernel, but very few organizations are going to justify that much development effort just for security reasons. If you're already building your own kernels because you have other technical reasons for it, and therefore have already put this effort in, then yes, just turn off CONFIG_IP_DCCP and call it done.
The best back-of-the-napkin calculation for "How many bugs does my software have" can be calculated as: a linear function of your LOC. You have 2x as many LOC as you did yesterday? Constant factors aside, it's reasonable to assume you have 2x as many bugs as you did yesterday.
Now you are dealing with a system (Linux) that has almost no self-protection features to stop attack methods. You are also dealing with a language in which a single error like a double free is not only very easily accomplishable, but can now lead to full system compromise. The whole system is tens-of-millions of LOC. It is not hard to see that this approach of playing "recompile every week" is going to scale very poorly overall, and it cannot be done by everyone (due to logistic and cognitive overhead).
Furthermore, stopping actual attack vectors has massive bonuses compared to the whack-a-mole game. For one, it protects against current threats only discovered in the future. This vulnerability is a decade old. There are likely on the order of _millions_ of machines with affected kernels. Many of these machines likely will never get upgraded again (vendor kernels, EOL, whatever).
You won't be able to set CONFIG_MODULES=n on the 8 year old Linux-based router/firewall some SMB has running (after a shitty Outlook server employee password gets hacked, and someone downloads a still-active VPN certificate in their corporate email and logs in to begin pivoting -- and it isn't long till they can persist on something like that if they see it).
Second, along the same lines -- whack-a-mole does nothing to prevent against targeted attacks. A theoretical targeted attacker is going to be vastly more capable than someone running a pre-canned exploit -- you cannot assume they are somehow both A) competent enough to pull off a deeply targeted attack, yet B) too stupid to fall to basic measures like `CONFIG_MODULES=n`
A recompiled kernel is not going to stop a dedicated attacker from finding a stable exploit in the other N-million lines of code inside Linux. You're playing an unwinnable game in this scenario, where the adversary only needs time. So both of these methods fail: in the small scale, it will fail for the vast majority of already-affected systems. On the large scale for targeted attacks, it will fail to truly competent attackers.
Sitting around and investing in massive kernel building infrastructure can be completely obviated by doing one thing: running grsecurity. Or improving Linux's real self-protection/security features. Only one of those is viable at the moment.
Then, none of these attacks will work. While there is the aspect that exploits may not be tuned to attack grsecurity -- it also fundamentally makes many attack vectors impossible. This should be the goal -- to make exploits almost pointlessly difficult, even with a bug at hand. Even with 5 bugs it should be maddeningly difficult. For example, it completely stops refcount based overflows. PAX_MEMORY_SANITIZE would stop the most major attack vector of this particular bug -- the ability to write or invoke a function pointer through a UAF on the affected, allocated block. UDEREF and KERNEXEC stop almost every major userland/kernel-land cross execution attack, especially the most trivial ones which you see relatively often -- and it works on every platform you can think of (where SMAP/SMEP-like features are limited to people who have new computers, Intel only).
We're clearly trying the whack-a-mole game now. CONFIG_MODULES=n is just another version of it. It isn't working.
Block CAP_SYS_MODULE or filter init_module syscalls. Container software should have this enabled as sensible default.
Does Docker actually block that? amluto's comment about the Timgad LSM implies that such functionality doesn't exist yet. (And the replies on LKML show the same confusion between explicit module loading, which requires privilege, and auto-loading, which does not. https://lkml.org/lkml/2017/2/6/279)
The easy way to try this is to lsmod and look for dccp and dccp_ipv4, `modprobe -r` them if they're already there, run
python -c 'from socket import *; socket(AF_INET, 6, 33)'
in your container, and lsmod again and see if they're present. make menuconfig && make dep && make clean && make bzImage
And build a package from your new shiny kernel...echo 1 > /proc/sys/kernel/modules_disabled
If you're root in a container, but not root on the outside system, you shouldn't be able to write to /proc/sys/kernel/modprobe, no?
Disabling module loading is not reversible, you need to reboot.
- goto discard;
+ consume_skb(skb);
+ return 0;
One of the rare cases in the wild where a goto really was considered harmful! ;-) if (foo() != 0)
goto fail;
goto fail;
That extra line of code could just as likely have been a return statement or anything else. The problem was not with the goto statement itself.Edited to add: 'goto fail' is a valid construct and can be used to handle finalization in C functions. Consider:
int
foo(char *bar, int baz) {
sometype_t *obj = NULL;
int fd = -1;
if ((obj = sometype_new(bar, baz)) == NULL) {
goto fail;
}
if ((fd = open(sometype_path(obj), O_RDONLY)) < 0) {
goto fail;
}
/* ... */
return 0;
fail:
if (fd < 0) {
close(fd);
}
if (obj != NULL) {
sometype_free(obj);
}
return -1;
}
There's nothing inherently wrong about that.For Ubuntu versions:
Ubuntu 12.04.3 LTS
Ubuntu 14.04.5 LTS
The following should get you patched up, you will need a reboot though: sudo apt-get update
sudo apt-get install linux-headers-3.13.0-110 linux-headers-3.13.0-110-generic linux-image-3.13.0-110-generic
After reboot: uname -a
Linux hostname 3.13.0-110-generic #157-Ubuntu
Any Digital Ocean users may need to power down their droplet and switch the kernel to the following version: DigitalOcean GrubLoader v0.2 (20160714) Ubuntu
Without this the new kernel may not be used.Edit: I'm looking for something for CentOS but I can't even find a tracker where they put CVE's?
You could theoretically locate teh SRPM for the kernel and build from that, but I haven't done that in years and the last I heard RedHat was going to make that harder because they weren't happy Oracle was copying their work wholesale and then selling it as a competitor, so that may be harder now?
Redhat is finished, CentOS is probably just lagging a little bit.
linux (4.4.0-64.85) xenial; urgency=low
* CVE-2017-6074 (LP: #1665935)
- dccp: fix freeing skb too early for IPV6_RECVPKTINFO
-- Stefan Bader <stefan.bader@canonical.com> Mon, 20 Feb 2017 10:06:47In the CVE it almost hints that this specifically is UDP related.
Am I right in thinking this?
void *x = malloc(16);
free(x);
...
free(x);
When malloc makes the allocation, it actually allocates a bit more than 16 bytes so it knows how much to free. Usually what it'll do is store a structure right before the pointer it returns; for instance, it might allocate 24 bytes, use the first 8 bytes to store the number 24, and then return the rest of that allocation. Then, when free is called, it subtracts 8 from what it was given, and frees that many bytes. (More complicated malloc implementations will have more complicated structures, possibly including pointers to other, shared structures.)If you call free a second time on the same pointer, a lot of things could happen. In particular, if there was another call to malloc() in between, it might reuse that same location, and you might free that. Or if some other sequence of allocations happened, there might be a completely nonsensical number stored at x-8.
If the second call "frees" data that's still in use, two parts of the program will be writing to the same memory. Maybe one thing is storing the return value of a call, and the other thing is storing the current UID. So when the first thing writes 0, now you're UID 0.
Alternatively, if the second call frees something nonsensical, you might "free" memory that was never the allocator's in the first place - and cause the next call to malloc() to return a pointer to something important elsewhere in memory, perhaps even the program's stack frame. If the program then reads attacker-controlled data into that newly-allocated memory, the attacker takes over the program's control flow.
DCCP is a protocol that is kind of like UDP, in that it's message-oriented, but kind of like TCP, in that it's reliable and implements congestion control. It's not very common, and uncommon networking protocols have been a historic good source of vulnerabilities in the Linux kernel.
DCCP implements congestion control but is not reliable.
OpenBSD's malloc implementation for example actually will let you enable an option that instead marks unused pages in the freelist maintained by malloc as PROT_NONE, i.e. reuse of a page will cause a segfault.
However, there are just various tradeoffs here. For example, OpenBSD's malloc does not guarantee that a `free` will immediately cause a freelist entry to become unused (AFAICS reading the man page -- option `F` vs `U`). So it will not immediately be marked as PROT_NONE, but only once a number of unused entries get 'flushed'. This is (presumably) an optimization so malloc can keep unused chunks around for quick-reuse, without having to do remappings.
In the case of the Linux kernel, obviously it is using a different allocator, but the same basic ideas apply.
For a protection against this particular exploit, you can look to grsecurity. In particular, I believe PAX_MEMORY_SANITIZE nullifies the major attack vector of this bug, immediately -- as it frees/erases all unused slab/memory pages immediately. If a page or object that is freed contains a pointer (i.e. the traditional mode of attack requires some pointer dereference, instead pointing to attacker-controlled code) -- this will trip PAX_MEMORY_SANITIZE as use of a freed pointer, and kill the kernel task.
Other techniques for exploiting this are probably handled by similar defenses.
(So I think this is like, kernel exploit #93,234,893 stopped immediately by a grsecurity feature?)
EDIT: mikeash also points out another good bit I forgot, which is that the basic defenses don't immediately catch a kind of 'ABA-style' double free. e.g. you free, something else gets the allocation instantly after, then you cause another free again. You'll only crash later.
But nullifying the attack vector is what's really important here, which is: you need to stop this from extending into an (almost arbitrary) read/write vector.
x_1 = malloc(n)
free(x_1)
...
x_2 = malloc(n) // malloc returns the same address as x_1, ie x_1 == x_2
...
free(x_1)
...
example_function(x_2)
How would the runtime know the difference between a double free of x_1 and freeing x_1 then later x_2? And now we think x_2 points to valid data when it doesn't, which can lead to a use after free error.Quick solution: "echo "install dccp /bin/true" >> /etc/modprobe.d/modprobe.conf"
The DISA IASE [2] publishes hundreds of "Security Technical Implementation Guides" (STIGs) for various operating systems (including several Linux distributions [3]), software applications, networking devices, and so on.
The OpenSCAP [4] Security Guides [5] are also a good reference. They are primarily aimed at compliance w/ security requirements (C2S, PCI-DSS, USGCB) -- for example, the "U.S. Government Commercial Cloud Services (C2S)" [6].
I don't 100% fully implement any of these, but I have a lot of RHEL and CentOS boxes publicly accessible on the Internet and they don't become accessible until they've had the majority of these recommendations implemented.
P.S. Look around. There are tons of shell scripts, Ansible roles, Puppet modules, etc., that will take care of 90% of this for you. There's no excuse for having public-facing machines that aren't locked down.
[0]: https://benchmarks.cisecurity.org/downloads/benchmarks/
[1]: https://benchmarks.cisecurity.org/tools2/linux/cis_ubuntu_li...
[2]: http://iase.disa.mil/Pages/index.aspx
[3]: http://iase.disa.mil/stigs/os/unix-linux/Pages/index.aspx
[4]: https://github.com/OpenSCAP/scap-security-guide/
[5]: https://www.open-scap.org/security-policies/choosing-policy/
[6]: http://static.open-scap.org/ssg-guides/ssg-rhel7-guide-C2S.h...
Makes one wonder why these things aren't default on a linux distro...
As an embedded software developer it's a bit inconvenient that it's all focused towards the big distributions (ubuntu, RHEL, ...), but still interesting input for securing an embedded linux device :-)
It's just too variable.