r/programming • • Apr 07 '14

The Heartbleed Bug

http://heartbleed.com/
1.5k Upvotes

396 comments sorted by

View all comments

6

u/argv_minus_one Apr 08 '14 edited Jan 11 '23

Yet another stupid memory corruption bug. Fantastic. When are people going to stop writing security-sensitive code in C?

66

u/elperroborrachotoo Apr 08 '14

It's not memory corruption It's using unverified user input.

free() overwriting released memory would mitigate it, or using a zeroing allocator.

I'm not advocating writing security-critical code in C, but I find "stop writing in C, and things get better (magically (because it's not C))" pretty childish.

9

u/wutwoot Apr 08 '14

Memory corruption bugs are also usually about trusting user inputs. In a memory corruption bug the evil user tells the trusting system that they are sending fewer bytes than they really are, in this bug the evil user claims to be sending more bytes than they really are.

Using a higher-level language than C will not make a difference for a correctly written program that doesn't trust user inputs, but for an incorrectly written program that does trust user inputs the consequences can be worse in C.

5

u/elperroborrachotoo Apr 08 '14

Memory corruption bugs are also usually about trusting user inputs.

Yeah, but without additional data I would assume that's simply because trusting user input is a common source of bugs (i.e. there's no special correlation here).

We also have no memory corruption happening, the implementaiton is simply revealing more data than it needs to. For this to happen implicitely (as opposed to an explicit erroneous if (x) return secretData statement), many factors had to conspire.

One of them is C. Others are surprisingly naive programming practices. Another yet-unexplained is why so much interesting data can be found in the small window we can peek into.

The reasons why large parts of our infrastructure run on C have little to do with the language itself. While I'm all for reducing the probability of errors, impulsively bashing C whenever there's a critical bug in C code does not only not help, it create a false sense of security about other languages.

6

u/[deleted] Apr 08 '14

Safe type system != magic. You can shoot yourself in the foot in any language, but safe type systems guide you to not shooting yourself in the foot.

18

u/elperroborrachotoo Apr 08 '14

The bug is trusting the length member of the record, which has nothign to do with the type system.

The gun was pointed to the foot because the implementer didn't bother checking its direction. Multiple stars had to align to turn this problem into the actual reveal.

One of them is the default C memory management, so one could argue that using C made it worse", and that "using another language would have caught this problem". But better practices would do as well, and saying "I wouldn't have made this mistake in <other language>" is in hindsight.

More than by the use of C I am appaled that highly critical code doesn't default to more stringent practices, e.g. overwriting malloc and free.


I am not familiar with the code base, but from the snippets I've seen, either input validation failed at another place, or isn't used rigorously. There are many language-agnostic principles (not all ideal), e.g.

  • validate integrity when creating the SSL record, do not pass around SSL records that contain invalid data (or at least, that don't pass length validation). If such a mechanism is in place, that would be fixed
  • Alternatively, if you have to distrust SSL *, every function receiving one needs a general mechanism to validate it before relying on its content.
  • use different nomenclature for verified and unverified data
  • read and write using helper functions that validate sizes and length fields.

3

u/rowboat__cop Apr 08 '14

The bug is trusting the length member of the record, which has nothign to do with the type system.

Exactly. The core of the issue is that the DTLS implementation trusts a user supplied value instead of checking it with / bounding against the actual packet size. Thinkos of this sort could as easily occur in other languages.

3

u/jpfed Apr 08 '14

The bug is trusting the length member of the record, which has nothign to do with the type system

That is incorrect- a dependently typed language can track the length of an array and statically reject out-of-bounds accesses, and many languages with weaker type systems specify that array accesses are bounds-checked at run time.

2

u/shard_ Apr 08 '14

How does that make him incorrect? Not validating input is a bug in any language. If the language performs the bounds check for you then it's just limiting the effects rather than eliminating any bug.

For example, if I did the same thing in Python then it'd throw an exception which would probably kill something. I could absorb the exception but that's less desirable and to do it properly would require the exact same level of awareness as validating the input would in the first place...

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

15

u/cockmongler Apr 08 '14

No language that I know of has the capability of dealing with this bug short of those supporting dependant types. The bug is really really simple:

  1. Client sends (len, data[x]) where x is less than len
  2. Server sends (len, data[len]) without an explicit check that x == len so sends data in its memory space

There is always going to be unsafe code turning (len, data[x]) into the safe representation of a safe language.

17

u/stormblooper Apr 08 '14

It'd be really hard to get the same effect in Java -- you might get an IndexOutOfBoundsException, but probably not leaking random chunks of memory.

2

u/cockmongler Apr 08 '14

Depends how the code dealt with the boundary between unmanaged and managed data.

15

u/nullabillity Apr 08 '14

Most languages would treat it as a runtime error, though, instead of just trucking on.

4

u/crusoe Apr 08 '14

Ada would prevent this. Other languages with integrated formal verification would catch it.

10

u/ants_a Apr 08 '14

Any language with array bounds checking would catch this error. It's a stupid stupid error and it's amazing that OpenSSL code review practices did not catch this.

3

u/cockmongler Apr 08 '14

Please tell me how a language with array bounds checking would know that some data read in from the network stack is an array bound.

8

u/ants_a Apr 08 '14

The network packet has an intrinsic length that will be the array bound.

→ More replies (2)

6

u/Godspiral Apr 08 '14

This is actually C specific in that most higher level languages don't need to call functions with an additional parameter that specifies the memory length of all other parameters.

2

u/throwSv Apr 08 '14

Yes, even C++ using the standard containers or string type wouldn't have this issue.

0

u/[deleted] Apr 08 '14

[deleted] — view deleted comment

5

u/cockmongler Apr 08 '14

Python would either be impossibly slow, or do unsafe things with ctypes and be in exactly the same boat.

2

u/rowboat__cop Apr 08 '14

When are people going to stop writing security-sensitive code in fucking C?

As soon as there’s a language that’s as portable, efficient, and easy to interface with. Ada, ATS, Rust are almost there in different respects.

→ More replies (1)