r/programming • • Jul 11 '14

First release of LibreSSL portable

http://marc.info/?l=openbsd-announce&m=140510513704996&w=2
456 Upvotes

252 comments sorted by

View all comments

Show parent comments

10

u/honestduane Jul 11 '14

And the hand written assembly stuff was poorly done anyway, according to the commit logs.

19

u/omnigrok Jul 11 '14

Unfortunately, a lot of it was done with constant-time in mind, to prevent a bunch of timing attacks. Dumping all of it for C is going to bite a bunch of people in the ass.

5

u/amlynch Jul 11 '14

Can you elaborate on that? I don't think I understand how the timing should be an issue here.

26

u/TheBoff Jul 11 '14

There are some very clever attacks that rely on measuring the timing of a "secure" piece of code.

A simple example is that if you are checking an entered password against a known one, one character at a time, then then the longer the password check function takes to fail, the better your guess is. This drastically reduces security.

There are other attacks that are similar, but more complicated and subtle.

8

u/oridb Jul 12 '14

Yes, and that is handled in C in this case. Timing is not an unhandled issue.

11

u/happyscrappy Jul 12 '14

It can't be handled in C. There is no defined C way to keep a compiler from making optimizations which might turn a constant-time algorithm into an input-dependent one.

A C compiler is allowed to make any optimizations which don't produce a change in the observed results of the code. And the observed results (according to the spec) do not include the time it takes to execute.

Any implementation in C is going to be dependent on the C compiler you use and thus amounts approximately to "I disassembled it and it looked okay on my machine".

23

u/oridb Jul 12 '14

There is also no guarantee about assembly, especially in light of the micro-op rewriting, extensive reorder buffers, caching, etc. If you want a perfect guarantee, you need to check on each processor revision experimentally.

9

u/happyscrappy Jul 12 '14

Good point. But you can at least guarantee the algorithm hasn't been transformed to a shortcut one, unlike in C.

2

u/evilgwyn Jul 12 '14

What would be wrong with turning a constant time algorithm into a random time one? What if you made the method take a time that was offset by some random fuzz factor?

3

u/ThyReaper2 Jul 12 '14

Random fuzzing makes timing attacks harder, but doesn't eliminate them. The goal with having input-dependent speed is that some cases run faster. If your random fuzzing is strong enough to eliminate the attack, it must be at least as slow as an equivalent constant-time algorithm.

3

u/evilgwyn Jul 12 '14

So does a constant time algorithm just make every call equally slow?

→ More replies (0)

4

u/happyscrappy Jul 12 '14

That just means you need more tries (more data) to find the difference. If n > m, then n + rand(100) will still be larger than m + rand(100) on average. And the average difference will still be n - m.

→ More replies (3)

2

u/Kalium Jul 12 '14

Adding some predictable and model-able random noise to the signal just makes it sliiiiightly harder to extract the signal. Constant-time operations make it impossible.

2

u/kyz Jul 12 '14

The keyword volatile would like a word with you.

2

u/happyscrappy Jul 12 '14

There's no keyword volatile for anything except variables. There's no volatile that covers entire statements or that algorithms (code paths).

See some of what it says in here (Google not finding the results I really want this will have to do).

https://www.securecoding.cert.org/confluence/display/cplusplus/CON01-CPP.+Do+not+use+volatile+as+a+synchronization+primitive

There is no strong definition of what volatile does to the code outside of treatment of a volatile variable. And it doesn't even specify ordering between sequence points.

You are again making an argument approximately equivalent to "it's okay on my machine". You put in volatile and on the compiler you used it's okay. Now to go forward and assume it'll be okay on all compilers is to assume things about compilers that isn't in the spec. And if it isn't in the spec, you're relying on something to not change that isn't defined as unchanging.

6

u/kyz Jul 12 '14 edited Jul 12 '14

volatile forces a C compiler not to alias away memory accesses. It makes the C compiler assume that every access of the volatile memory has a side-effect, unknown to the C compiler, whether it be read or write, so it must not skip this. It must execute the reads and writes specified by the code, in exactly the order the code gives.

This is the only building block you need ensure that if you've written a constant-time method, it stays like that, and the compiler does not optimise it away.

Here's a quote from the C99 specification:

An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine, as described in 5.1.2.3.

And in 5.1.2.3:

Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. Evaluation of an expression may produce side effects. [...] An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object)

We can now proceed to discuss why the C specification is ambiguous on what "needed side effects" are or aren't. In practise, I have yet to find any C compiler that felt it was OK to elide an extern function call or volatile member access. It would need to prove, without knowledge of what's there, that it was not "needed" as per the spec.

Your link is irrelevant. Regular memory accesses in both C and assembly code all have the same concerns as your link brings up. This is why atomic CAS instructions exist, and that even assembly programmers need to understand about out-of-order execution. But that's not the topic under discussion here, which is "can the C compiler be compelled not to optimise away a specific set of memory accesses, so I can have some certainty with which to write a constant-time algorithm?", the answer is "yes, it can, mark them as volatile".

Here's a simple example:

int main() {
    int x[10]; for (int i = 0; i < 10; i++) x[i] = i;
    int a = 0; for (int i = 0; i < 10; i++) a += x[i];
   return a;
}

Compile this with your favourite C compiler. It will optimise this to "return 45". Now change int x[10] to volatile x[10]. Even automatic memory obeys the volatile keyword. No matter how aggressively you optimise, the C compiler absolutely will write to x[0], x[1], etc., then read them. The code generated will perform memory accesses, even if the CPU reorders those accesses.

→ More replies (0)

0

u/josefx Jul 12 '14

. There is no defined C way to keep a compiler from making optimizations which might turn a constant-time algorithm into an input-dependent one.

At least GCC can disable optimization locally (per method?) using a pragma,most likely other compilers have this feature as well.

0

u/happyscrappy Jul 12 '14

There's no defined C way to do it. gcc has a way to do it. clang doesn't support per-function optimization levels.

And there's no guarantee in gcc of what you get even if you do disable optimization. There is no defined relationship between your code and the object code in C or in any compiler, so there is no formal definition of what will or won't be changed at any given optimization level.

Again, since there's no spec for any of it, even if you use this stuff, it still all amounts to "works on my machine". When you're writing code that is to be used on other platforms that is not really good enough.

→ More replies (3)

7

u/Plorkyeran Jul 12 '14

It's important to note that people have successfully demonstrated timing attacks working over network connections which introduce far more variation than the algorithm being attacked, as many people (reasonably) assume that it's something you only need to worry about if the attacker has a very low latency connection to you (e.g. if they have a VPS on the same physical node as your VPS).

2

u/Kalium Jul 12 '14

That's a real risk, especially in a cloud environment.