r/programming •

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

[deleted] — view deleted post

0 Upvotes

661 comments sorted by

941

u/AReallyGoodName Apr 09 '14

Fucking hell. The things that had to come together to make this do what it does and stay hidden for so long blows my mind.

A custom allocator that is written in a way so that it won't crash or show any unusual behavior when allocation bounds are overrun even after many requests.

A custom allocator that favours re-using recently used areas of memory. Which as we've seen, tends to lead it to it expose recently decoded https requests.

Avoidance of third party memory testing measures that test against such flaws under the guise of speed on some platforms.

A Heartbeat feature that actually responds to users that haven't got any sort of authorization.

A Heartbeat feature that has no logging mechanism at all.

A Heartbeat feature that isn't part of the TLS standard and isn't implemented by any other project.

A Heartbeat feature that was submitted in a patch on 2011-12-31 which is before the RFC 6520 it's based on was created. By the same author as the RFC.

Code that is extremely obfuscated without reason.

PHK was right

94

u/iownacat Apr 09 '14

Code that is extremely obfuscated without reason

The reason is to hide these exploits :)

23

u/myztry Apr 09 '14

Good reasons and bad reasons are both reasons.

→ More replies (2)

329

u/pmrr Apr 09 '14

I bet the developer thought he was super-smart at the time.

This is a lesson to all of us: we're not as smart as we think.

514

u/zjm555 Apr 09 '14

Well said. This is why, after years of professional development, I have a healthy fear of anything even remotely complicated.

163

u/emergent_properties Apr 09 '14

But remember The Linux Backdoor Attempt of 2003

A malicious bug can hide in 1 line of code in plain sight.

Looking complex is not even necessary.

25

u/[deleted] Apr 09 '14

I propose we brand the phrase "given enough eyeballs all bugs are shallow" the Linus Fallacy.

→ More replies (19)
→ More replies (46)

351

u/none_shall_pass Apr 09 '14

Well said. This is why, after years of professional development, I have a healthy fear of anything even remotely complicated.

After spending the late 90's and early 2000's developing and supporting high profile (read: constantly attacked) websites, I developed my "3am rule".

If I couldn't be woken up out of a sound sleep at 3am by a panicked phone call and know what was wrong and how to fix it, the software was poorly designed or written.

A side-effect of this was that I stopped trying to be "smart" and just wrote solid, plain, easy to read code. It's served me well for a very long time.

This should go triple for crypto code. If anybody feels the need to rewrite a memory allocator, it's time to rethink priorities.

220

u/frymaster Apr 09 '14

A side-effect of this was that I stopped trying to be "smart" and just wrote solid, plain, easy to read code

There's a principle that states that debugging is harder than writing code, so if you write the "smart"est possible code, by definition you aren't smart enough to debug it :)

176

u/dreucifer Apr 09 '14

"Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?" -- Brian Kernighan The Elements of Programming Style

→ More replies (4)

36

u/none_shall_pass Apr 09 '14

That works.

I've always thought that complex code was the result of poor understanding of the problem or bad design.

→ More replies (21)
→ More replies (1)
→ More replies (17)

26

u/CheezyBob Apr 09 '14

I wish more of my co-workers thought like you. As a programmer, our job should be to manage and reduce complexity whenever possible. Software gets complex plenty fast without adding extra complexity just because it was fun, made you feel smart or was more "optimized".

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

58

u/emergent_properties Apr 09 '14

Nothing here implied intent.. but it also didn't discount it either.

Normally, I would say "Do not ascribe to malice to what could be incompetence." HOWEVER considering that this is probably one of THE most relied on packages.. and this is such a FAR REACHING BUG.. the author better have a damn good explanation.

It is speculation, but the converse is also true "Sufficiently advanced malice can be mistaken as incompetence."

What is the audit process? What is the proper discovery and reporting mechanisms of the people who developed OpenSSL?

→ More replies (55)
→ More replies (4)

64

u/dnew Apr 09 '14

submitted in a patch on 2011-12-31 which is before the RFC 6520 it's based on was created. By the same author as the RFC.

To be fair, that's not particularly suspicious. "Hey, I improved the implementation of this protocol I use. We ought to make that a standard so other implementations can add that to the protocol also."

I.e., if RFC-6520 was written by the same author, the patch wasn't based on the RFC. The RFC was based on the patch. Indeed, they're called "requests for comments" for that reason: "Look what I did. What do you think?"

I don't know of any RFC that was written before the first implementation was coded.

3

u/kolmogorovcomplex Apr 09 '14

Just had a look at the RFC. Why does it list "GWhiz Arts & Sciences" in the authors section? Sounds like a front to me.

Also, 66% of the authors have the first name "Michael". Not sure what to think about that.

37

u/Advacar Apr 09 '14

Also, 66% of the authors have the first name "Michael". Not sure what to think about that.

That Michael is a really common name? Are you desperate to find a conspiracy or something?

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

14

u/xiongchiamiov Apr 09 '14

I generally find that PHK is right about things. He seems to have a really well-tuned engineering spidey-sense.

10

u/infamous_blah Apr 09 '14

Is there a transcript for that video?

10

u/dezmd Apr 09 '14

In the PHK video, 44:49 cuts out an answer on his advice on how to explain the issue to politicians. Thats pretty fucking surreal.

→ More replies (1)

15

u/input Apr 09 '14

Thanks for the video really entertaining.

→ More replies (3)

3

u/[deleted] Apr 09 '14

How about the fact that the heartbeat feature was hardly used from what I've read?

3

u/lwdoran Apr 10 '14

I just finished watching that video. That was a very worthwhile 30 minutes. Thanks for link!

3

u/Lord_NShYH Apr 10 '14

PHK was right

"We have all the rooted CAs."

I have been paranoid to suspect this since early on in my career. The CA cartels are so damn powerful.

15

u/gigadude Apr 09 '14

The things that had to come together to make this do what it does and stay hidden for so long blows my mind.

First time is happenstance, second time is coincidence, third time is enemy action.

→ More replies (10)
→ More replies (23)

150

u/tenpn Apr 09 '14

Can someone explain that in english?

408

u/Aethec Apr 09 '14

Theo de Raadt says the memory allocation and release methods on modern systems would've prevented the "Heartbleed" flaw, but OpenSSL explicitly chose to override these methods because some time ago on some operating systems performance wasn't very good. Also, they didn't test the code without this override, so they couldn't remove it once it wasn't needed any more.
Now, a significant portion of Internet servers have to revoke their private keys and regenerate new ones, as well as assume that all user passwords may have been compromised... because the OpenSSL guys "optimized" the code years ago.

49

u/obsa Apr 09 '14

You don't get to put quotes around optimized. It was a legitmate optimization at the time. Whether or not it should have been done, or if it could have been done better, is a different debate entirely.

→ More replies (16)

22

u/adrianmonk Apr 09 '14 edited Apr 09 '14

How would these have prevented the heartbleed flaw? In not seeing it. The flaw is caused by trusting an external source to tell you how big of a size argument to pass to memcpy() (and malloc()).

EDIT: OK, they're talking about guard pages. Guard pages would use the MMU to detect when something is reading or writing in a place where it shouldn't be.

40

u/Aethec Apr 09 '14

Because the memory accessed by that flaw is often memory that was freed before, so there's an opportunity to prevent the program from accessing it since it shouldn't do so.

→ More replies (14)
→ More replies (2)
→ More replies (28)

170

u/turol Apr 09 '14

OpenBSD has protections in place to mitigate this kind of bug. Instead of leaking info it should crash the program. The side effect is slightly slower malloc/free.

OpenSSL developers decided that on some platforms malloc/free is too slow and implemented their own allocators on top of it. This neatly sidesteps the exploit mitigation features.

21

u/emergent_properties Apr 09 '14

Choices to override default security behavior should be a BIG red flag.

We didn't notice because either no auditing was done, shitty auditing was done, or the auditing didn't matter.

Because bounds checks are one of the oldest exploitation techniques..

31

u/WhoTookPlasticJesus Apr 09 '14

To be fair, there's no indication that they rolled their own mem management explicitly to avoid security protection nor that the OpenSSL team was even aware of the security benefits of built-in malloc and free. If you've ever spent any time in the OpenSSL codebase I think you'll instead come to the same conclusion as I: it was a hazardous combination of incompetence and hubris.

→ More replies (3)
→ More replies (10)

69

u/willvarfar Apr 09 '14
  • OpenSSL has been run on a very wide range of platforms and architectures.
  • It's performance is critical.
  • At one time, they found that some platforms had very very slow malloc()
  • So they wrote their own.

Its enabled by default, and they've long stopped testing it disabled.

54

u/criolla Apr 09 '14 edited Apr 09 '14

At one time, they found that some platforms had very very slow malloc()

Are the specifics of this documented somewhere? In a commit message?

What platforms? How slow? That little comment is very sloppy and written with a "this is the way it is, for all time" arrogance.

edit: here's the full comment and the relevant commit. Any further details are not documented there as far as I can see.

20

u/xiongchiamiov Apr 09 '14

Memory saving patch.

I mean, what more could you want from a commit message? /s

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

7

u/[deleted] Apr 09 '14

its performance is critical

I can definitely see that for Yahoo!, Google et al. But I wonder how critical the performance would be for the bottom 95% of sites? The bottom 50%?

Where is the threshold where security trumps performance? Certainly I would rather my bank run a more expensive/powerful server than be vulnerable to Heartbeat for two years.

Surely there'd be a market for an extra-fortified, not-as-fast version of SSL?

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

123

u/sigzero Apr 09 '14

"OpenSSL is not developed by a responsible team."

Wow!

111

u/Catsler Apr 09 '14

Some coding style and functions on display.

https://www.peereboom.us/assl/assl/html/openssl.html

38

u/semperverus Apr 09 '14

Why is chrome telling me that sites certificate can't be trusted?

80

u/obfuscation_ Apr 09 '14

Because it's a self-signed certificate

24

u/nosneros Apr 09 '14

Self signed certificate.

21

u/FudgeCakeOmNomNom Apr 09 '14

It is still encrypted, but as others pointed out, your browser doesn't recognize the issuer of their certificate because it was self-signed and not by one of the built-in root certificate authorities.

→ More replies (2)
→ More replies (13)

35

u/dzamir Apr 09 '14

This was painful to read

31

u/[deleted] Apr 09 '14

When I come to power, indentation practices like that will result in a severe beating. There's no reason for that to have been checked in.

→ More replies (5)
→ More replies (1)
→ More replies (15)

18

u/frezik Apr 09 '14

Theo didn't even write the quote in the title. The "responsible team" quote would have been a better choice.

→ More replies (1)

2

u/clavicle Apr 10 '14

You're not familiar with Theo, right?

→ More replies (1)

56

u/[deleted] Apr 09 '14

That's insane. If I were writing a SSL library, security takes precedence over performance so long as it's still usable.

35

u/happyscrappy Apr 09 '14

So long as it's still usable depends on the client. If you have a server which handles a hundred requests a second, is openSSL still usable? What if you want to service a thousand?

Problem is it's a library, people use it in different ways.

→ More replies (11)

14

u/[deleted] Apr 09 '14

The problem is, an SSL library will get called a lot. All your traffic might pass through it. It may in fact not be usable unless it is extremely fast.

8

u/chadmill3r Apr 09 '14

Writing a library? There are hundreds of those already. The market of ideas ignores them because they don't perform as well.

8

u/ithika Apr 09 '14

That it is performing is not under issue. What it is performing is the question we should ask of tools like this.

→ More replies (2)

42

u/_4p3 Apr 09 '14

"Only two remote holes in the default install, in a heck of a long time!"

So, when will they update this?

99

u/[deleted] Apr 09 '14
2002    2007    2014
(x      {x)      x}
 |       |       |
 |       |       |
 '---v---^---v---'
     |       |       
     |       |
   heck     heck
   of a     of a
   long     long
   time     time
→ More replies (1)

20

u/sandsmark Apr 09 '14

AFAIK a default install doesn't listen on anything, and therefore this doesn't impact that.

18

u/protestor Apr 09 '14

That's the default C program:

int main()
{
  return 0;
}

No vulnerabilities yet (as of 2014), if ran on the default operating system.

→ More replies (2)

67

u/[deleted] Apr 09 '14

The joke is that they've had quite a lot of more bugs than that, but since most of the features are turned off in default install, they haven't had many bugs in default install

31

u/sigzero Apr 09 '14

Since they're explicit about "default install" I don't think it is a joke.

→ More replies (2)
→ More replies (6)
→ More replies (5)

56

u/jgotts Apr 09 '14

Mr. de Raadt is correct. OpenSSL is a nasty piece of software. Just try doing anything with it using the command-line programs and that fact should become immediately obvious: Poor documentation, too many different intermediate file formats, and non-standard and obsolete command syntaxes.

OpenSSL has always struck me as a package written by mathematicians whose second job is programming. I know there are many mathematicians who are excellent programmers, but when programming is an afterthought to the underlying mathematics chances are you will produce poor-quality software

I hate to be so negative about such a useful piece of software. I think that OpenSSL could be refactored/reorganized to produce a really great piece of software. Mainly, they need to revise the command-line options to be modern (use GNU standards), write proper documenation, and as other people have said, improve the code base.

2

u/darkslide3000 Apr 10 '14

too many different intermediate file formats

You make it sound like this was OpenSSL's fault. Remember, they didn't invent SSL, they just took the crap that was specified by some stillborn monkeys on meth and tried to implement it as best as they could. ASN.1 is a crappy mess and there's just no way to make it look pretty. At least OpenSSL allows you to do all that crazy stuff that you sometimes need to do all from the comfort of their impressively powerful command line... show me another tool that can just encrypt/decrypt/hash random stuff with every single cryptographic algorithm (combination) known to man, create/sign/verify all kinds of certificates, and provide a really useful SSL-telnet for debugging (even with STARTTLS support!).

Also, even if we were to redo the whole SSL/x509/ASN.1 stack from the ground up, I don't think crypto will ever be "easy". It's just complicated stuff and you need to handle things like serialized certificates and arbitrary-length numbers in your code to make it work. This will never look all that pretty in C.

→ More replies (5)

130

u/karlthepagan Apr 09 '14

Voodoo optimization: this is slow in one case 10 years ago. So, we will break the library for many years to come!

0

u/newmewuser Apr 09 '14

Bullshit, this has nothing to do with optimization. This is all about a missing check.

6

u/karlthepagan Apr 09 '14

Wrong.

RTFA:

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. Some other debugging toolkits get them too. To a large extent these come with almost no performance cost. 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.

The code has a comment explaining the custom allocator:

/* On some platforms, malloc() performance is bad ...

Meaning that the exploit mitigation which would have lessened the impact of Heartbleed (no passwords, private keys, OAuth tokens in the bleed... instead a server crash) is not in place.

5

u/JoseJimeniz Apr 09 '14

That's what i'm missing. People are bitching about a custom memory allocator. That may be a defense-in-depth precaution, by using the standard allocator. But it's certainly not a holy thing to use the standard allocator.

The real problem is the actual problem:

  • reading a value from the client and assuming it is valid

The other problem, reading past the end of a buffer, is a situation endemic to the entire C language (and any language that allows pointers).

→ More replies (5)

154

u/muyuu Apr 09 '14

Yep looking at that part of the code was a bit of a WTF moment. Also, there's a variable called "payload" where the payload length is stored... what kind of monster chose that name, I don't know.

23

u/alektro Apr 09 '14

So if you were to look at the code before this whole thing started you would have recognized the problem? The code is open source after all.

23

u/muyuu Apr 09 '14

Maybe. But this is code that entered OpenSSL 2 years ago.

And in any case one doesn't simply go reading the whole code running in systems. Literally by the time you finish there's a dumpload of new code to check. You'd never finish.

But I'd have expected that important stuff like this was more scrutinized by security people. It was found... 2 years later.

11

u/[deleted] Apr 09 '14

But I'd have expected that important stuff like this was more scrutinized by security people. It was found... 2 years later.

And this right here is critical to understand whenever anyone tries to make the argument that open source is safer just because it's open source. The source has to actually be audited by someone with sufficient qualifications.

→ More replies (4)

8

u/xiongchiamiov Apr 09 '14

Well, that's why pre-merge code review is so important.

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

72

u/WHY_U_SCURRED Apr 09 '14 edited Apr 09 '14

It raises the questions; who wrote it, who do they work for, and what were their motives?

Edit: English

136

u/dontera Apr 09 '14

This guy http://www.robin-seggelmann.de/ wrote it. His motivations were likely because he wrote his PhD thesis on streaming encryption and he thought he was clever. Also, he wrote the TLS Heartbeat RFC.

Here is the commit that brought us this, https://github.com/openssl/openssl/commit/4817504d069b4c5082161b02a22116ad75f822b1

33

u/Grimoire Apr 09 '14

16

u/thebigslide Apr 09 '14

Haven't we learned a thing or two recently about what can happen if you don't add braces to one line if blocks!? Especially with returns after them... I know it was hurried, but there's really no excuse for that.

→ More replies (2)
→ More replies (2)
→ More replies (2)

90

u/gvtgscsrclaj Apr 09 '14
  1. Some programmer.
  2. Some corporation.
  3. Laziness and tight deadlines.

I mean, I know the NSA crap that's been floating around makes that a legit possibility, but cases like this really feel like your normal level of sloppiness that's bound to happen in the real world. Nothing and no one is absolutely perfect.

12

u/zjm555 Apr 09 '14

Agreed. This is an Ockham's Razor scenario -- for every NSA backdoor, there are probably thousands of vulnerabilities that simply slip in by pure accident. Probability of such accidents goes up with the complicatedness of the solution, and with the number of lines of code.

41

u/paffle Apr 09 '14 edited Aug 17 '26

Almond cobblestone coherent society encouraging stocking spoon

This post was anonymized with Redact

→ More replies (16)

24

u/emergent_properties Apr 09 '14 edited Apr 09 '14

And there is the International Obfuscated C Code Contest The Underhanded C Contest .. of which the goal is to make an app that has a sly code payload hidden in it that can be passed off as a mistake.

Plausible deniability is a thing, ESPECIALLY in this realm.

I am not saying that it was intentional or malicious, but you bet your ass with a security hole this big we shouldn't assume automatically innocence first..

EDIT: Corrected contest URL.

22

u/spook327 Apr 09 '14

I think you've confused two completely different things; the IOCCC is for making unreadable code. The one about programs that have a secret critical vulnerability is The Underhanded C Contest

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

6

u/technocratofzigurrat Apr 09 '14

The ASN

7

u/PT2JSQGHVaHWd24aCdCF Apr 09 '14

The ASN.1? I knew they were evil the day I looked at the specs!

→ More replies (3)
→ More replies (5)

2

u/2Xprogrammer Apr 09 '14 edited Apr 09 '14

I'm pretty sure "payload" is standard networking jargon to refer to the actual data part of a packet (as opposed to the headers etc.), in perfectly legitimate contexts. It's a little odd, in sort of the same way that "hash maps" and "binary search trees" are odd if you think about what those words normally mean, but I don't think that choice of variable name is especially suspicious.

24

u/[deleted] Apr 09 '14

The point was that that variable does not hold the actual data part of a packet. It holds its length.

12

u/muyuu Apr 09 '14 edited Apr 09 '14

This is C code. We have an unsigned int and a pointer, if you don't say it's the length, it can be very easily mistaken for the location/offset of the payload.

In fact the main part of the fix consists of checking that the actual record is sufficiently long. This would be substantially more evident (that this wasn't being checked) if the variable looked like a length.

There are a number of bad practices making this possible, though. The main one being... bypassing the system malloc (!!) just because it may be slow in some systems.

→ More replies (3)

84

u/ACTAadACTA Apr 09 '14

There should be an alternative to OpenSSL that is easy to use, formally verified and as small as possible.

I know, I'm a dreamer.

97

u/KitsuneKnight Apr 09 '14

There's several alternatives, including NSS (used by Firefox & Chrome), cryptlib, polarSSL, and even GnuTLS (I wouldn't suggest migrating to that last one :P). Likely none of them are particularly easy to use (which is a major issue that people tend to overlook...), and probably none that are even slightly widely used are formally verified.

Fedora is actually working to migrate things over to using NSS, and has been for a while. At least as things stand right now, NSS seems like a far better option than OpenSSL (plus, there's less issues with the license).

37

u/[deleted] Apr 09 '14

Not like OpenSSL is particularly easy to use.

This has been linked a bunch but it agrees with my experience and random looks at the source have been, if anything, worse than what's in there.

→ More replies (1)

31

u/chiniwini Apr 09 '14

I once took a look at the NSS code and after a few hours I wanted to shower myself in napalm. I don't know how bad OpenSSL code is, but I would bet my right hand NSS isn't much better.

5

u/TMaster Apr 09 '14

Chrome is said to be switching to OpenSSL.

I know, I have goosebumps too. The bad kind.

3

u/RealDeuce Apr 09 '14

I actually find cryptlib to be insanely simple to use.

→ More replies (1)

7

u/Hashiota Apr 09 '14

http://en.wikipedia.org/wiki/Comparison_of_TLS_implementations
The implementations targeting embedded systems are usually small and written in readable ANSI C, without many dependencies (some corners of OpenSSL need even Perl to run).

5

u/FUZxxl Apr 09 '14

IMHO it would be a great idea to write a library that puts the encryption keys into a different address space (i.e. a different process) as to make it impossible for any flaw to read it out.

15

u/exscape Apr 09 '14

So this may sound naive, but... If a flaw can't read them from a separate process, how can the program itself? Or can you really write it such that the separate process handles all the encryption/decryption in safe(r) manner?

12

u/FUZxxl Apr 09 '14

The main program cannot read out the key but it can perform cryptographic operations with it, for instance it can tell the key-process to encrypt or decrypt a piece of data for him. Even though an attacker could still do nasty things in such a model, he could not get the key.

→ More replies (6)
→ More replies (1)
→ More replies (4)

3

u/until0 Apr 09 '14

I sit here reading all these articles and all I can think is that I wish I had a better understanding of the inner workings of cryptography so I could make an attempt.

Unfortunately, I don't care much for math. Comp Sci maybe wasn't the smartest idea...

→ More replies (5)

27

u/zalifer Apr 09 '14

The title is incorrect, so far as it suggests that Theo de Raadt said that.

On Tue, Apr 08, 2014 at 15:09, Mike Small wrote:

nobody <openbsd.as.a.desktop <at> gmail.com> writes:

"read overrun, so ASLR won't save you"

What if malloc's "G" option were turned on? You know, assuming the subset of the worlds' programs you use is good enough to run with that.

No. OpenSSL has exploit mitigation countermeasures to make sure it's exploitable.

As the formatting in reddit shows, Mike Small wrote the sentence quoted in the title.

4

u/amertune Apr 09 '14

To me, it looks like Mike Small wrote

What if malloc's "G" option were turned on? You know, assuming the subset of the worlds' programs you use is good enough to run with that.

12

u/XplodingForce Apr 09 '14 edited Apr 09 '14

No, zalifer is right that the quote is not by Theo de Raadt. The formatting in that usenet log is terrible (somewhat ironically).

Look at it from the top level, the title tells you that the main text was written by Theo de Raadt. He quotes Mike Small, who quotes nobody, who starts his post with a quote of somebody else. The confusing thing is that this last quote does not have any header indicating who it was quoted from.

Luckily, Theo de Raadt seems to ben in agreement with Mike Small Ted Unangst, so the misattribution does not really change much.

Edit: It's even weirder, and even more ironic. Theo de Raadt was quoting Ted Unangst, without having the proper header above the quote. Still, the quote in the title was not from Theo de Raadt.

→ More replies (1)

2

u/[deleted] Apr 09 '14

Actually, it does not show that. The attribution line precedes the quoted lines. Mike Smalls said "What if malloc's "G" option were turned on?".

75

u/[deleted] Apr 09 '14

There are two types of people who write open source code:

Those who have heard the music of the spheres and write in crystalline prose no mortal can ever understand, and those who think a quadruple indented 15 clause if/else tree is a valid way to deal with complexity.

53

u/void_fraction Apr 09 '14

Of course, type B tend to think they're members of type A.

24

u/Neebat Apr 09 '14

Also vice versa. Good programmers tend to distrust themselves.

29

u/shub Apr 09 '14

If mortals can't understand it, it's bad code, although sometimes it's unavoidable. If I've misunderstood and you meant that all open source code is bad then we're in agreement.

31

u/keteb Apr 09 '14

I don't know man... once you reach certain levels of complexity sometimes you need to have absurd amounts of the program & goals in your mind to understand the full scope of what's happening.

I'll have times where I slip back out of the zone, look at my own code, and am like "Damn that works well, but I have zero confidence I could write that again".

20

u/strcrssd Apr 09 '14

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." – Brian W. Kernighan

10

u/0007000 Apr 09 '14

Implying the definition is correct.

→ More replies (4)
→ More replies (2)

2

u/myalternatelife Apr 10 '14

Music of the spheres

Hyperion?

→ More replies (1)

3

u/bordumb Apr 10 '14

Probably built that way so that the NSA could have a nice backdoor to everything :)

2

u/6nf Apr 10 '14

It's funny because it's true

16

u/kevwil Apr 09 '14

Theo de Raadt & Linus Torvalds - opinionated, blunt, maybe even rude, but absofuckinglutely right about almost every thing they've ever said or written. This is what separates them from the rest of us.

6

u/reini_urban Apr 09 '14

Note that Theo de Raadt didn't say that cited headline. He just defends Ted, who said that, and explains why.

3

u/smellyegg Apr 10 '14

'Maybe' even rude? Linus is a total prick.

4

u/niknej Apr 09 '14

I think it is Dijkstra to whom the aphorism "All significant systems still contain bugs" is most commonly attributed, but I'll bet he was not the first to think of it. Nobody can realistically claim that openssl, much less the internet, is trivial.

So let's bite on the bullet. There are (still) security bugs in the net. We should be alert for them, and swat them when they come to light, but there is a limit to how much breast-beating they are worth. This bug is dead (or at least moribund, until people update their openssl). Next week, another bug!

2

u/skillcode Apr 09 '14

This is pretty worrying, especially if we consider the fact that the exploit has been out in the wild for a long, long time. I've patched up all my stuff, and wishing everyone else to do the same quickly.

2

u/stmfreak Apr 10 '14

Smart developers always seem to think they know better than whoever wrote the old code.

2

u/zvrba Apr 10 '14

Hah, I had a good hunch when I wondered how the damn thing didn't crash on so large (64kB, spanning a few pages) buffer overruns.

2

u/i-node Apr 09 '14

He is right. Anyone who has looked at openssl knows he is right. Even their make system doesn't properly track dependencies to build in parallel. (at least it didn't last time I checked) Many of the algorithms they implement are complicated enough most people assume they are correct (it is hard to audit a section of code for correctness when the algorithm is hard to follow) . This feature he shows however only works for the right allocator. Some people would still be affected but it is more likely the openssl team would find this bug had they not worked around the allocator.

2

u/partyboy690 Apr 09 '14

What surprises me by this bug is that a program like OpenSSL isn't that heavily unit tested, I mean the most basic unit test would catch this. It is a programming error and a classic C error too, I don't exactly know how OpenSSL works so forgive me if some of this comes as ignorant but surely this process would be working in its own virtual set of memory, if it did try and access cached memory outside of the virtual set the operating system would complain regardless if it used a bespoke memory allocation routine?

4

u/hastor Apr 10 '14

Exactly, and a unit test would have caught the goto fail from Apple.

The problem with these crypto libraries is that their implementations are made to be untestable. The Chrome guys said that exactly the same code path as the goto fail from Apple was not testable in Chrome either.

We need to "dumb down" our crypto until we can at least write unit tests. Complex algorithms or implementations without tests cannot be used.

1

u/looneysquash Apr 09 '14

I'm not defending OpenSSL in general. I don't know enough about it's internals to comment on it.

But caching memory allocated by malloc() is a pretty common technique. It's not an unreasonable thing to do, and it does not automatically make someone an unreasonable developer.

Doing something like that unconditionally is usually better than doing it conditionally. Maybe some platform has a really fast malloc and it's not necessary, but if you have two code paths, then you have two code paths to debug. (In this case apparently they did support both ways, but the one that wasn't used unsurprisingly didn't work.)

11

u/hegbork Apr 09 '14

But caching memory allocated by malloc() is a pretty common technique. It's not an unreasonable thing to do, and it does not automatically make someone an unreasonable developer.

If someone does this without credentials proving that they've successfully worked in memory management on a real operating system the code gets immediately rejected in review. Clever memory caching is what every malloc does internally, except that the people who wrote malloc hopefully know what they are doing and the average developer doesn't.

There was an excuse to do this on VAX where function call overhead was big, mallocs inefficient, memory small so fragmentation wasn't a problem and heap overflow exploits weren't invented yet.

Malloc is much slower than your freelist? There are options:

  1. You are smarter than whoever wrote it, the glow of your genius outshines the sun, people should bow at your mad skillz.
  2. malloc does those slow things for many reasons that the developers have figured out over decades of debugging and experience. You're disabling important security, debugging, performance and anti-fragmentation features that will bite you when you least expect it leading to undebuggable problems at the worst possible time.
  3. You're in the 0.01% of use cases where a certain use pattern of memory in critically performance sensitive code that just has to use dynamic allocation in the hot path improves things so vastly it is the difference between something working and not working.
  4. You're using an operating system that is shit. Use an external malloc library. You are not the first one to have this problem. One of the dozen popular malloc implementations has surely been ported to your system.
  5. You learned programming in the 80s and are of the opinion that nothing worth knowing has happened since then.

2

u/looneysquash Apr 09 '14

Here's an example: glib Memory Slices.

I've used it before. It sped up my code.

If malloc is slower than my freelist, it's probably because malloc is a very generalized tool and my freelist, or the one I'm using, is much more specialized.

→ More replies (1)

2

u/RoadieRich Apr 09 '14

It looks like a quick and easy fix - even if it's just a temporary one - is to add a line or two to set the memory contents to zero immediately before it is released back to the cache. You're not disabling the cache just yet, but you're removing (at least, as I understand it) the cause of this exploit. Or am I misunderstanding the vulnerability?

→ More replies (1)

1

u/hpzr24w Apr 09 '14

It's the sinister option.

1

u/GeorgeForemanGrillz Apr 10 '14

I agree with Theo here. They have the balls to ask for donations too. Fuck them.

1

u/matholio Apr 10 '14

Most of this is whoosh for me, but enjoyed reading the comments of mature coders talking able the perils if feeling smart.

1

u/SlobberGoat Apr 10 '14

a wooden stake being driven through OpenSSL's heart = HeartBleed

1

u/Andrey_Karpov_N Apr 16 '14

Some time ago, a vulnerability was revealed in OpenSSL, and I guess there's no programmer who hasn't been talking about it since then. I knew that PVS-Studio could not catch the bug leading to this particular vulnerability, so I saw no reason for writing about OpenSSL. Besides, quite a lot of articles have been published on the subject recently. However, I received a pile of e-mails, people wanting to know if PVS-Studio could detect that bug. So I had to give in and write this article: http://www.viva64.com/en/b/0250/

1

u/syncsynchalt Apr 09 '14

This email was a lot more confusing until I stopped parsing "openssl" as "openssh" (due to proximity to TdR).

2

u/[deleted] Apr 09 '14

A lot of people do that, it used to be OpenBSD and OpenSSH, so everyone assumed OpenSSL was their work too.

It got even worse with OpenSNMP, OpenNTPD, OpenIKED, OpenBGPD/OpenOSPFD, mandoc, pf, and OpenOffice... Can you name which ones are OpenBSD related?

→ More replies (1)

-2

u/drguildo Apr 09 '14

Remember when that Debian developer "fixed" that OpenSSL Valgrind warning. Yeah. Some people shouldn't be allowed near a C compiler. Actually, can we just stop using C now, please?

10

u/partyboy690 Apr 09 '14

C is an extremely versatile and powerful language, the reason they used it over say Java was for performance reasons, as much as people would like C to go away it is here to stay. Also if you read the content in the link you'll realise that this wasn't caused by a design feature in the C language because if the OpenSSL team hadn't rolled their own custom memory allocation function then the operating system would have taken care of this and the bug would have been found ages ago. The problem is that when the OpenSSL's version of free is called it doesn't zero the memory AND it allows you do to a memcpy over your buffer. The regular C library would mitigate both those issues.

→ More replies (3)

7

u/[deleted] Apr 09 '14

We can stop using C once there's an alternative that fits in all the tight spaces it can.

That single criterion instantly rules out 99% or more of "nice" languages.

6

u/Zarutian Apr 09 '14

FORTH LOVE IF HONK THEN

;-)

1

u/[deleted] Apr 09 '14

I don't really like to write C, but objectively C will always be the king.

-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.

→ More replies (17)
→ More replies (10)

-26

u/makeswordclouds Apr 09 '14

Here is a word cloud of all of the comments in this thread: http://i.imgur.com/I0EA0Si.png


source ^code | contact ^developer

9

u/rs-485 Apr 09 '14

Well, if you read the biggest horizontal words, you get:

Slow OpenSSL code, one time!

That sounds like an appropriate beginning to a story explaining what happened here.

-9

u/[deleted] Apr 09 '14

this bug existed for a long time and every person who was worth a damn knew about it I don't understand why its so hyped up now?You people act like its the end of the world and it hasn't been fixed yet.

6

u/[deleted] Apr 09 '14

Are you claiming that you knew about the heartbleed bug before it was publicly made known a few days ago? Or are you talking about the memory management part?

-4

u/[deleted] Apr 09 '14

Yes. This has happened before in 2011 but was known about well before then. http://www.theregister.co.uk/2011/09/19/beast_exploits_paypal_ssl

9

u/[deleted] Apr 09 '14

Umm, no. The current bug has absolutely nothing to do with the BEAST attack. The current one is orders of magnitude worse and has only been known for several days: http://heartbleed.com/

0

u/[deleted] Apr 09 '14

[deleted] — view deleted comment

3

u/mort96 Apr 09 '14

Heck, it could even be an awesome band name.

1

u/hyperforce Apr 10 '14

I think "Exploit Mitigation" would make a good debut album, and then "Countermeasures" would be the sophomore effort.

0

u/[deleted] Apr 09 '14

[deleted] — view deleted comment

35

u/xiongchiamiov Apr 09 '14

Uh, how much of ssl did you implement? All of those ciphers?

13

u/frezik Apr 09 '14

OpenSSL implements nearly every digest and cipher out there, plus means of encoding messages for transport and ways to convert between encodings. All while hopefully being speedy, as well (which was the problem that created the Heartbleed bug).

I don't mean that as an apology for OpenSSL; it's a good reason for why it needs to be more organized than it is. Rather, it should be clear that you're not going to do all that in a 500-line project.

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

3

u/[deleted] Apr 09 '14

[removed] — view removed comment

11

u/wllmsaccnt Apr 09 '14

They wrote a custom memory management layer which circumvents some exploit mitigation code...mitigation code that would have caused an immediate crash instead of allowing an exploit like this one.

2

u/[deleted] Apr 09 '14

[removed] — view removed comment

5

u/[deleted] Apr 10 '14

The defense I've seen is that at the time, performance was so poor on some platforms that the software wouldn't have been usable, thus no encryption there unless they made this faster. And then for simplicity they just made this wrapper the default for everything. And then their tests wouldn't work without this wrapper enabled, so there was resistance to ever switching back to native malloc as the default. It reads like typical industrial disasters, where safety systems were disabled, precautions not taken, lax procedures, and then a huge disaster.

5

u/adrianmonk Apr 09 '14

A lot of memory allocation library have security features built-in these days. For example, they can give you a larger piece of memory than you requested, and use the extra space adjacent to what you requested to store random values, which they then check at the time you free the memory. If the random values are no longer the same, it's a good indicator you had a buffer overflow. There are others, too, like guard pages, which is the one they are talking about here.

He's saying openssl could have used these protection measures but chose not to.

2

u/mcguire Apr 09 '14

He's saying openssl could have used these protection measures but chose not to.

OpenSSL could have used those measures if it did not have its own wrapper around malloc to cache allocated memory (IIUC). By itself, as loonysquash points out, that's not an unreasonable thing to do---there are often circumstances where a general purpose malloc can be outperformed by relatively simple techniques. Further, there's not really any specification of malloc's behavior that says it must or should help catch memory or security errors, so it's not immediately obvious that avoiding calling it has a downside.

2

u/[deleted] Apr 10 '14

In all the coding I've done over the years, pretty quickly I saw that any performance-optimized allocators must turn into direct calls to malloc/free when debugging, so that all the checking features of them could be used to catch bugs (double-free, bounds checking, leak checking).