Live data from Hacker News

It's All About Time – Remote timing attacks in PHP

blog.ircmaxell.com

41–50 of 52 posts

Re: It's All About Time – Remote timing attacks in PHP

#41
post #37

Earlier quoted context omitted.

Doesn't have to exhibit "the temporal properties of purely linear character-by-character matches" and noone said that strawman. It just has to exhibit the "correct matching strings return faster" property, whatever more complicated search it does.

Doesn't it have to exhibit "correctly matching strings return faster ... on a per-character or other tiny chunk basis " in order to work the match towards correctness?

Yes, and it will indeed try by all means to exhibit that behavior -- unless if it's some contrived regex designed to counter this.

The key word here is "short cirtuit" the match, and any regex engine worth its salt will try to do that as much as possible, tranforming the regex to the faster FSM it can.

Re: It's All About Time – Remote timing attacks in PHP

#42

Earlier quoted context omitted.

It's misleading because it implies this is a unique problem scoped in PHP. That is factually incorrect and hence misleading any reader to assume this article is about PHP's poor security against timing attacks. It'd simply be nice if the scope of the discussion was about how all languages have more or less the same flaws. Anyone writing code with security in mind should understand how to avoid those flaws. There are…

The second you say "install this PECL extension" people hop on the fast train to nopesville. That is the problem I ran into getting people to adopt libsodium :\

I honestly don't see why. It's easy enough to install pecl extensions.

Re: It's All About Time – Remote timing attacks in PHP

#43

Earlier quoted context omitted.

I don't really find it misleading. It's about all sorts of remote timing attacks (including cache-timing attacks), not just passwords. I'm the author of the pull request that prompted the discussion, though, so I'm probably biased. :)

It's misleading because it implies this is a unique problem scoped in PHP. That is factually incorrect and hence misleading any reader to assume this article is about PHP's poor security against timing attacks. It'd simply be nice if the scope of the discussion was about how all languages have more or less the same flaws. Anyone writing code with security in mind should understand how to avoid those flaws. There are…

The audience is PHP users. It's only misleading to outsiders.

Re: It's All About Time – Remote timing attacks in PHP

#44
post #29

Earlier quoted context omitted.

OP here. Seeing as this question is getting asked a lot , I'll edit something into the post, but I wanted to answer you here as well. So, there are a few problems with this technique. 1. It ignores the local timing leak An attacker who can get code running on the server (shared hosts for example), can carefully monitor the CPU usage to see when the process is actually doing work, vs when it sleeps. So really, its not…

Thank you for answering, it is true that if someone has access to the server you might be in trouble, but if he has access to a cpu monitor, he might also have access to RAM and could just get the data from there. For the precision of sleep, http://php.net/manual/en/function.time-nanosleep.php might be more appropriate Also, you would only need to slightly clamp very important functions, so DoS attacks aren't that li…

Well, access to the CPU happens with every user (since you can see the current mode of every core as an unprivileged user - idle, wait or running).

Accessing RAM requires system level access (privileged users, super user really) or running as the same user as the other process.

So unless the server is horribly misconfigured, or you exploit another vulnerability, reading from RAM isn't as likely as monitoring the CPU.

Re: It's All About Time – Remote timing attacks in PHP

#45
post #36

Earlier quoted context omitted.

OP here. Seeing as this question is getting asked a lot , I'll edit something into the post, but I wanted to answer you here as well. So, there are a few problems with this technique. 1. It ignores the local timing leak An attacker who can get code running on the server (shared hosts for example), can carefully monitor the CPU usage to see when the process is actually doing work, vs when it sleeps. So really, its not…

How can you actually make sensitive operations take constant time? This sounds impossibly hard. For example, your operating system could be context switching thousands of times per second. Your password comparison function could cause a page fault because the trailing end of the password spans onto another page of virtual memory. These are all factors that would throw any calculation for constant time out of the wind…

> How can you actually make sensitive operations take constant time? This sounds impossibly hard. For example, your operating system could be context switching thousands of times per second.

Sorry, it appears that I didn't actually define constant time anywhere. What I really mean is that:

    Runtime does not depend in any way on the *value* of secret data.
So while actual runtime may vary, it's not varying because of the value of something we want to protect.

So it's not about keeping "absolute" time constant, but only the impact of the secret on runtime.

Re: It's All About Time – Remote timing attacks in PHP

#46
post #31

Earlier quoted context omitted.

OP here. Seeing as this question is getting asked a lot , I'll edit something into the post, but I wanted to answer you here as well. So, there are a few problems with this technique. 1. It ignores the local timing leak An attacker who can get code running on the server (shared hosts for example), can carefully monitor the CPU usage to see when the process is actually doing work, vs when it sleeps. So really, its not…

I'm not an expert on timing attacks, but without clamping it seems quite tricky to guarantee that sensitive operations actually take constant time. There can be numerous subtle ways that timing information leaks while the code appears to be constant time. And programmers who touch sensitive code can easily forget the requirement for constant-time behaviour. Yes, having constant time operation without clamping is the…

The far better approach is to just make the operations not depend on the secret.

You only really need to worry about timing attacks for values that the attacker doesn't know, and you don't want them to know.

So it's only things like encryption keys, passwords, session identifiers, reset tokens, etc that you need to worry about.

> And programmers who touch sensitive code can easily forget the requirement for constant-time behaviour.

And that's why I support the discussion we were having on PHP's internals list where we talked about making functions which are commonly used with secrets timing safe by default. As long as there isn't a non-trivial performance penalty to it at least.

As far as worrying about it, I'd rather people understand SQLi and XSS better. They are both FAR bigger surface areas than a timing attack ever will be. And likely going to be the bigger threat to 99.99% of applications.

Re: It's All About Time – Remote timing attacks in PHP

#47
post #5

> NOTE In general, it's not possible to prevent length leaks. So it's OK to leak the length. I would imagine that you can prevent length leaks by looping through the characters of the known value and then returning that comparison with an additional check of length. function timingSafeEquals($safe, $user) { $safeLen = strlen($safe); $userLen = strlen($user); $result = 0; for ($i = 0; $i

So, I tried this in the past.

The problem you'll run into with PHP specifically is that reading an undefined string offset (past the end) will result in a notice: http://3v4l.org/nIkf5

Which means that errors are triggered. So you can increase the length of the user string and note a linear increase in runtime until you increase it past the length of the string, at which point it becomes MUCH slower on a per-character basis (even if you don't do anything with the notice, the error mechanism is still triggered internally, which isn't cheap).

Actually, my original code was more robust as it never read past the end of the string, preventing the notice:

    /**
     * A timing safe equals comparison
     *
     * To prevent leaking length information, it is important
     * that user input is always used as the second parameter.
     *
     * @param string $safe The internal (safe) value to be checked
     * @param string $user The user submitted (unsafe) value
     *
     * @return boolean True if the two strings are identical.
     */
    function timingSafeEquals($safe, $user) {
        // Prevent issues if string length is 0
        $safe .= chr(0);
        $user .= chr(0);

        $safeLen = strlen($safe);
        $userLen = strlen($user);

        // Set the result to the difference between the lengths
        $result = $safeLen - $userLen;

        // Note that we ALWAYS iterate over the user-supplied length
        // This is to prevent leaking length information
        for ($i = 0; $i 
There are a few problems here though that are non-trivial as are explained in the post: http://security.stackexchange.com/questions/49849/timing-saf...

Basically, while it may keep the length %64 safe (since cache lines are 64 bites wide), it doesn't keep the length safe in general. Some length information will be leaked on larger strings. And considering it's impossible to protect the length in the general case, making a function which says it protects length is a lie. Therefore I don't even try and hence save the complexity.

But let me ask this: what cases would you have where are you trying to protect the length? Anything with variable length input (like a password) should likely be one-way hashed anyway. So you'd be comparing fixed-length hashes. So where's the possible leak?

Re: It's All About Time – Remote timing attacks in PHP

#48

Earlier quoted context omitted.

It's misleading because it implies this is a unique problem scoped in PHP. That is factually incorrect and hence misleading any reader to assume this article is about PHP's poor security against timing attacks. It'd simply be nice if the scope of the discussion was about how all languages have more or less the same flaws. Anyone writing code with security in mind should understand how to avoid those flaws. There are…

The audience is PHP users. It's only misleading to outsiders.

The title of the article is not the same as HN's title (which is a HN guideline). I was commenting on the editorializing on HN.

Re: It's All About Time – Remote timing attacks in PHP

#49
post #29

Earlier quoted context omitted.

Thank you for answering, it is true that if someone has access to the server you might be in trouble, but if he has access to a cpu monitor, he might also have access to RAM and could just get the data from there. For the precision of sleep, http://php.net/manual/en/function.time-nanosleep.php might be more appropriate Also, you would only need to slightly clamp very important functions, so DoS attacks aren't that li…

Well, access to the CPU happens with every user (since you can see the current mode of every core as an unprivileged user - idle, wait or running). Accessing RAM requires system level access (privileged users, super user really) or running as the same user as the other process. So unless the server is horribly misconfigured, or you exploit another vulnerability, reading from RAM isn't as likely as monitoring the CPU.

Most operating systems will not idle on a sleep() call, as far as I remember. Since the server is executing multiple applications, it is very likely that the processor will be assigned to another running application. The only way to really know this would be to know the state of the specific process php is using for the request, which seems unfeasible in a production environment (except if you have admin of course).

Re: It's All About Time – Remote timing attacks in PHP

#50
post #49

Earlier quoted context omitted.

Well, access to the CPU happens with every user (since you can see the current mode of every core as an unprivileged user - idle, wait or running). Accessing RAM requires system level access (privileged users, super user really) or running as the same user as the other process. So unless the server is horribly misconfigured, or you exploit another vulnerability, reading from RAM isn't as likely as monitoring the CPU.

Most operating systems will not idle on a sleep() call, as far as I remember. Since the server is executing multiple applications, it is very likely that the processor will be assigned to another running application. The only way to really know this would be to know the state of the specific process php is using for the request, which seems unfeasible in a production environment (except if you have admin of course).

Well, it won't idle if there is another process ready to execute (load is greater than 1). If there is no process wanting to execute, it will idle.

Again, I'm not saying this is practical. I'm saying it might be possible (even if improbable).

And don't get me wrong, I'm not saying "OMG YOU ARE BAD IF YOU DON"T PROTECT THIS RIGHT". I'm more leaning on the side of "if there's a chance, I assume someone could possibly figure out a way".

Post reply on HN