r/programming • • Apr 09 '14

Theo de Raadt: "OpenSSL has exploit mitigation countermeasures to make sure it's exploitable"

[deleted] — view deleted post

2.0k Upvotes

661 comments sorted by

View all comments

-36

u/[deleted] Apr 09 '14

Why are we still trusting security software written in C? It's clear that anything written in C or C++ CANNOT EVER BE TRUSTED. Auditing might have caught it, but clearly there isn't the resources to do so and it's no guarantee.

21

u/[deleted] Apr 09 '14 edited Apr 09 '14

This has been stated 100000000 times, and even in this thread:

They choose to circumvent tooling and standard library calls that have been around for quite sometime that wold have prevented this bug. This was a conscious choice on the part of the OpenSSL dev team and contributor (if he is not actually apart of the team), simply for performance reasons.

C or C++ is not at fault here. If you and everyone else could stop being mindless idiots about a language that'd be great.

I'll prepare for my second round of down votes that were so graciously handed to me yesterday from the illustrious /r/programming subreddit. Something something Rust and Ada.

6

u/adrianmonk Apr 09 '14

They choose to circumvent tooling and standard library calls that have been around for quite sometime that wold have prevented this bug.

The trouble is, while all this tooling (ASLR, guard pages, canaries, etc.) is really, really clever and does some amazing things, it's not very efficient. It's all really, really clever precisely because it has to work around weaknesses in C.

Take guard pages for example. In lieu of just knowing the size of an array and doing 1 machine instruction (or maybe 2) to validate that a pointer or offset is within bounds, you have to use the MMU hardware to map THREE pages in memory, one to make sure nobody reads/writes before the desired region, one to make sure they don't write after it, and one dedicated to hold the contents of that allocation and that allocation only. Binding address space to pages with the MMU isn't cheap, either. It takes page table entries and doing extra bindings makes the TLB less efficient so everything runs slower.

And if I want 17 bytes from malloc(), that's not a multiple of the size of a page, so you've got to waste memory. You can't put more than one allocation in a single page if you want guard pages to work. At least not if you want them to protect against this exploit, which only relies on doing out an out-of-bounds read.

Of course, even OpenBSD doesn't actually take guard pages as far as they would need to to protect against this. Look at this presentation, where it says that in OpenBSD:

Each bigger than one page is followed by a guard page --> segmentation fault if overflow.
Smaller allocations are randomly placed inside one page.

So for smaller allocations (which this likely is... an exploit heartbeat message is small), OpenBSD punts and doesn't provide the full protection of guard pages, because the cost is just too high. Instead, it randomizes layout, which could actually help an attacker use this exploit because they can just keep retrying and get a variety of different data.

So all these amazing tools exist, and I think it's a much better decision to use them than to not use them if you're using C.

But C doesn't get off the hook here, because all these things are a great big giant band-aid on C. With C, you get two choices: unsafe code or code that is kind of safe because it is covered in costly band-aids.

C is to blame because it only gives you bad choices. As an industry, we can do better. We have the technology. We've had it for 30 or 40 years. We just need to apply it.

3

u/[deleted] Apr 09 '14

You are right to a large extent. I think though, this bug would have been found a lot sooner, regardless of how certain systems choose to implement various safe guards, if they had decided to use sensible standard practices.

I think at the heart of this issue is I really feel like C is just a scapegoat, because I feel like it was really just bad engineering. C certainly had a part to play in this bug, I'm not denying that. But technology can only limit a developer's idiocy to a certain extent. People foaming out the mouth going "this is why we can't use C because of this!" are really missing what actually happened here, in my opinion.

I don't think C is an angel. I think there are certainly languages out there even now that will come to replace it in due time, simply based on merit. But we'll run into other problems, certainly. Recognizing bad engineering decisions is key to moving forward, not scapegoating an entire tool set (somewhat coincidentally C's memory management can certainly fall under the former part of my statement).

1

u/[deleted] Apr 09 '14

You C apologists can keep overflowing buffers, overwriting pointers and indexing arrays out of bounds to your heart's content. But this will not excuse the constant stream of security bugs which would not exist if a sane programming language was used. The OpenBSD feature is just a hacky workaround for a low-level memory model that should have been a long-forgotton artefact of the 1970s.

6

u/[deleted] Apr 09 '14

But this will not excuse the constant stream of security bugs which would not exist if a sane programming language was used.

Problem is, there isn't a "sane programming language" that is sufficiently mature and can provide the interoperability a library like OpenSSL needs.

1

u/newmewuser Apr 09 '14

LOL, You must really suck at C, like thousands of newfags that think they can code in C, but they are just bug breeders.

-4

u/[deleted] Apr 09 '14

Did you even read the link? Do you even write C or C++ code in any form?

3

u/[deleted] Apr 09 '14

Yes I have been writing C and C++ code for at least 10 years (5 years professionally).

-7

u/[deleted] Apr 09 '14

And yet you don't understand how this isn't simply a C/C++ issue then? I'm going to give you the benefit of the doubt here, and say you haven't really read much, especially considering you didn't even answer my question.

6

u/[deleted] Apr 09 '14 edited Apr 09 '14

I have yet to actually find a good explanation of what the heartbleed bug was caused by (Google only returns fluff pieces by the media). Do you have a link to the commit diff that fixes the bug?

Edit: According to this article, it was caused by a missing bounds check. Classic vulnerability that very few languages except for C would suffer from (since sane languages would throw an exception in this case).

I still re-iterate that I cannot trust software written in a language that permits egregious programming errors to allow exploits quietly by instead of failing in a predictable manner.

0

u/[deleted] Apr 09 '14

The synopsis is in the very article you are commenting on. You still have not read it apparently. So why are you commenting?

So years ago we added exploit mitigations counter measures to libc malloc and mmap, so that a variety of bugs can be exposed. Such memory accesses will cause an immediate crash, or even a core dump, then the bug can be analyed, and fixed forever.

But around that time OpenSSL adds a wrapper around malloc & free so that the library will cache memory on it's own, and not free it to the protective malloc.

These methodologies have been around for quite some time. OpenSSL decided to circumvent it for optimization purposes. Even though on 99.99% of platforms it doesn't matter.

12

u/[deleted] Apr 09 '14

I have read it, maybe it is you who are not understanding. The "countermeasures" are a workaround for the fact that C does not check array bounds accesses! They are not 100% guaranteed to work in all cases since system memory is returned in pages which might include several malloc()ed blocks of memory within them. Of course the caching was dumb and made the countermeasure less effective, and maybe it would have been caught sooner. But it does not negate the fact that C allows this kind of errant runtime behaviour to fall through without complaint!

→ More replies (3)

1

u/artee Apr 09 '14

He does, and is right that this is very much a flat memory model issue (of which C and C++ are by far the most important examples). I challenge you to reproduce any of these problems in Java, Smalltalk, Scheme, .NET (managed) or any functional language (Haskell, whatever you want).

And sure, you get exploitable VM bugs in return, that's true. But those can at least be fixed by just updating the VM and not all programs written against it.

→ More replies (6)

1

u/[deleted] Apr 09 '14

I'm honestly not sure how you can suggest that "C or C++ is not at fault here" with a straight face.

Note that I'm not saying that the OpenSSL team was following best practices, but we could point to any bug and simply say "well, the programmer shouldn't have done that!". Obviously that's trivially true, but it's not a terribly useful statement.

C absolutely was at fault here - any language which does not permit unsafe access to memory would have rendered this bug impossible. If this were one of a few C memory access bugs that had ever occurred throughout history, fine, the programmers are just idiots. But when it happens over and over and over again, at what point do we actually admit that it is almost impossible to write safe software using C?

Based on this and countless other bugs, even the best programmers obviously can't write safe non-trivial software in C. So why do we keep blaming the programmers?

-2

u/[deleted] Apr 09 '14

Another person who has not read the thing they are commenting on. And continue to blame it on C, despite standard tooling that prevents exactly what happened.

If you're not going to read, and you don't actually understand the problem, then don't comment.

4

u/[deleted] Apr 09 '14

I did read it. I fully understand that this problem would have been caught if they had used the standard malloc / free. Just as every other bug would have been caught or not happened in the first place if the programmers had done things the right way. Pointing out that the programmers made a mistake in conjunction with a bug is a tautology; of course the programmers did something wrong, otherwise there wouldn't have been a bug.

Buffer overflows, reading from uninitialized memory, and other such problems have been a constant and unavoidable plague on C software. C is simply not safe for human use.

3

u/[deleted] Apr 09 '14

I did read it. I fully understand that this problem would have been caught if they had used the standard malloc / free.

It would have been prevented on OpenBSD, because they value security a lot more than performance. No other widely used allocator implementation has these protections. It's possible to opt-in to them in jemalloc (zero and junk fill options), but it's marked as a debugging feature and is not used by the three biggest users (Facebook, Firefox + other Mozilla applications, FreeBSD libc).

1

u/[deleted] Apr 10 '14

Well, sure, but catching it anywhere would have exposed the problem and presumably resulted in a fix for all platforms.

1

u/[deleted] Apr 10 '14

I don't think it would have been caught this way without using a fuzzer, unless this was actually being exploited in the wild.

-1

u/[deleted] Apr 09 '14

Pointing out that the programmers made a mistake in conjunction with a bug is a tautology

I think it's a little bit more nuanced than that.

Buffer overflows, reading from uninitialized memory, and other such problems have been a constant and unavoidable plague on C software. C is simply not safe for human use.

It works pretty damn well, considering it runs the majority of software you use every. single. day.

3

u/[deleted] Apr 09 '14

It works pretty damn well, considering it runs the majority of software you use every. single. day.

I didn't say it didn't work. I said it's not safe. Those are very, very different things.

Things can work fine for typical day-to-day use while being full of security holes which only show up under deliberate attack. C is an incredibly unsafe programming language, and is responsible for the vast majority of security problems found in the wild. And yes, many of those security holes have occurred in software I use "every. single. day.".

→ More replies (1)
→ More replies (1)