Apple's SSL/TLS bug and programming language design
By anders pearson 22 Feb 2014
A big story in the tech world recently has been Apple’s bug deep in the SSL/TLS stack. There’s a good writeup of the code on ImperialViolet that tracks down the exact point in the code. It boils down to these couple lines of C code:
The crux of the problem is that the indentation of the second goto fail;
makes it appear to be part of the same clause as the first one. The double gotos look weird but harmless at a glance. But a closer look reveals that there are no braces after the if
statement. The indentation of the second goto is a lie, so it actually parses like so:
The second goto fail;
isn’t in a conditional at all and so a goto will always be executed. Clearly not what the original programmer(s) intended.
Apple has the resources to hire top-notch programmers and focus intently on quality in their work. This being security related code, was probably subject to more scrutiny and code review than most other code. Still, this simple but brutal mistake slipped through and a lot of systems are vulnerable as a result. The point here isn’t that the programmers responsible for this code were bad; it’s that even the very best of us are human and will still miss things.
These kinds of mistakes fascinate me though and I like to think about how they can be prevented.
Designers of some more “modern” languages have solved this particular problem. Python, which catches its share of flak for having significant whitespace eliminates this type of problem. In fact, preventing this kind of disconnect between the intent of the code and its appearance on screen is a large part of why Python has significant whitespace. Imagine if that block of code had been written in Python (of course Python doesn’t have goto
s, etc, etc, but you catch my drift):
“A” and “B” there are the two possible ways it could be written. Both are unambiguous; they each work exactly like they look like they should work. It’s much harder for Python code to develop those subtle traps. This isn’t to say Python doesn’t have its own share of blind spots. Arguably, Python’s dynamic typing opens up a whole world of potential problems, but at the very least, it avoids this one.
Go, Another language I spend a lot of time with, which is heavily C-influenced, also manages to sidestep this particular trap. In Go, it’s simply required to use braces after an if
statement (or for the body of a for
loop). So a Go translation of the code would have to look like one of the following:
Again, there’s really no way to write the code in a deceptive manner. You could indent it however you want, but the required closing brace would stick out and make a problem obvious long before it went out to half the computers and mobile devices on the planet.
Go even goes a step further by including go fmt, a code formatting tool that normalizes indentation (and does many other things) and the Go community has embraced it whole-heartedly.
A code formatting tool run over the original C code would’ve made the problem immediately obvious, but there doesn’t seem to be strong push in that community for promoting that kind of tool.
I try to always remember that programming complex systems and maintaining them across different environments and through changing requirements is one of the most difficult intellectual tasks that we can take on. Doing it without making mistakes is nearly impossible for mere humans. We should lean on the tools available to us for avoiding traps and automating quality wherever and whenever we can. Language features, compiler warnings, code formatters, linters, static analysis, refactoring tools, unit tests, and continuous integration are all there to help us. There may be no silver bullet, but we’ve got entire armories full of good old fashioned lead bullets that we might as well put to use.
Tags: programming python golang security software engineering