News for hackers. Stuff that matters.
Re:The hijacking flaw that lurked in Intel chips is worse th... by geofft on Sunday May 07, 2017 @01:46AM (#14284059)
At what point does it become reasonable to conclude that strncmp, and every other string API that treats the pointer to the string and its length as separable variables, are too dangerous to be used in security-sensitive software? I feel like I've seen another vulnerability this week from someone sending the wrong length to a standard C string function.

If you want to fix this, there's no strict need to move away from C (not that I'd particularly encourage you to stay, but if you have some reason to prefer C, you can solve this in C). There are a number of C libraries that give you a struct string {char *ptr; int len;} of some sort, from bstring to GLib. And there's always C++, too.

What need does the Intel AMT firmware have for compatibility with POSIX string APIs? It's not running a POSIX OS, is it?

Re:The hijacking flaw that lurked in Intel chips is worse th... by jjnoakes on Sunday May 07, 2017 @12:32PM (#14286198)
> At what point does it become reasonable to conclude that strncmp, and every other string API that treats the pointer to the string and its length as separable variables, are too dangerous to be used in security-sensitive software?

In general I agree, we have a long way to go to writing more secure software by default.

But this specific issue is different. The issue was not the string length was incorrect (the length passed to strncmp was the correct length for the response string); the problem here was a logic problem, which is harder to get right just by switching string implementations.

The buggy code in question was basically (in an imaginary language with fully managed strings):

    if response is_a_prefix_of expected_hash:
      allow_login()
when it should have been

    if response is_equal_to expected_hash:
      allow_login()
Re:The hijacking flaw that lurked in Intel chips is worse th... by geofft on Sunday May 07, 2017 @12:44PM (#14286257)
Yeah, but if you are writing that pseudocode, it's obvious that is_a_prefix_of is the wrong operation.

The problem is that strncmp is in fact a prefix-comparison function when used with n calculated one way, but also it's the "safer" version of strcmp when used with n calculated another way, and it's easy to confuse the two uses.

In any other string implementation, you'd just use the most obviously-named compare function, there would be no need for a "safer" version of that function (there'd just be one reasonable comparison function), and it would do the right thing.

Re:The hijacking flaw that lurked in Intel chips is worse th... by jjnoakes on Sunday May 07, 2017 @01:31PM (#14286464)
> also it's the "safer" version of strcmp when used with n calculated another way

This isn't true; first off it isn't "safer" than strcmp() at all (this isn't strcpy vs strncpy after all), and second, no length argument to strncmp() will make it act like strcmp(). In order to get strcmp(), you have to also check the lengths are the same first.

    strlen(a) == strlen(b) && strncmp(a, b, strlen(either))
This is less safe than using strcmp() when you want equality (as this Intel issue shows) because it is easy to forget the length check. strcmp() does it implicitly for you.

I agree the names are not ideal (in fact I write my own streq() and strpfx() routines because I think it reads easier and it is also easy to get the return value of strcmp() wrong), but again, that has nothing to do with separating the string's pointer and its length.

Re:The hijacking flaw that lurked in Intel chips is worse th... by geofft on Sunday May 07, 2017 @01:45PM (#14286526)
I agree with you, but please argue with the other person who objected to my comment saying that strncmp was obviously the safer version of strcmp. :-)

An API where experienced users don't even agree what the function is supposed to be is a bad API.

Re:The hijacking flaw that lurked in Intel chips is worse th... by jjnoakes on Sunday May 07, 2017 @09:14PM (#14288618)
How do you know all parties involved in this discussion are experienced?
Re:The hijacking flaw that lurked in Intel chips is worse th... by code_sloth on Sunday May 07, 2017 @02:05AM (#14284115)
Could you elaborate on why "treating the pointer to a string and the string length as separate vars" is dangerous to someone who hasn't done a lot of C?
Re:The hijacking flaw that lurked in Intel chips is worse th... by geofft on Sunday May 07, 2017 @12:41PM (#14286241)
Basically, it encourages you to store only the string pointer and re-calculate the length (or let a library function do so) or use the wrong length value, which means if there's some specific length or capacity worth paying attention to, it's easy to get it wrong.

Really you want both a length, of the data actually in the string, and a capacity, marking how much memory is valid.

If I'm understanding the vulnerability right: strncmp takes three arguments, the beginning of the first string, the beginning of the second string, and the maximum number of bytes to compare. That maximum isn't quite a length or a capacity. It's a bound on the length, if one of the strings isn't null-terminated. But if you provide too small a maximum, it'll only compare the first few characters, and return a value based on that.

In particular they compared the target string to the user-provided string, with a user-provided length, so if you provide an empty string, it compares 0 bytes and returns success.

An API of the form strcmp(struct actual_string a, struct actual_string b) wouldn't have this problem - the actual string structures (instead of a char pointer) would provide a length for both strings, so the API wouldn't let you make this sort of error.

Re:The hijacking flaw that lurked in Intel chips is worse th... by qb45 on Sunday May 07, 2017 @05:09AM (#14284554)
Because occasionally you use wrong length with wrong pointer and read or overwrite something you didn't want to.

It's more convenient and safer to have these two variables packed together as a "string object" and have string functions operate on that - then they always know where the string starts and ends in memory.

Re:The hijacking flaw that lurked in Intel chips is worse th... by pabl0rg on Sunday May 07, 2017 @02:13AM (#14284142)
If you access a memory location beyond the end of the string, you can get all sorts of unexpected behavior. The string's length is something you'll pretty much always need to take into account.
Re:The hijacking flaw that lurked in Intel chips is worse th... by DaiPlusPlus on Sunday May 07, 2017 @02:37AM (#14284205)
Many libraries and systems (e.g. Win32) just go with null-terminated strings and their functions don't accept a length argument at all - so I wonder if some people never store the length simply because they don't believe they'll ever need it.
Re:The hijacking flaw that lurked in Intel chips is worse th... by Kubuxu on Sunday May 07, 2017 @03:50AM (#14284366)
strncmp and other API that treats the pointer to the string and its length as separable variables are not dangerous. They are much more secure than the non 'n' options.

With them you don't have to worry about lack of null termination as the n determines the length of the string.

Problem here was usage of them at all. Hashes are not really strings, memcmp should have been used or even better secure (that doesn't leek timings) memcmp should be used.

Problem here was just mistake in the program not in the C lib.

Re:The hijacking flaw that lurked in Intel chips is worse th... by geofft on Sunday May 07, 2017 @11:55AM (#14286035)
> strncmp and other API that treats the pointer to the string and its length as separable variables are not dangerous. They are much more secure than the non 'n' options.

"More secure" doesn't imply "not dangerous". It's definitely safer to drive a car without seatbelts than to ride a bike down the highway, but that doesn't mean that not wearing your seatbelt isn't dangerous.

> Problem here was usage of them at all. Hashes are not really strings, memcmp should have been used or even better secure (that doesn't leek timings) memcmp should be used.

This is incorrect for two reasons.

The first is that hashes in ASCII format are strings: it's useful to want, for example, case-insensitive hash comparisons.

The second is that (if I'm understanding the vulnerability correctly) the exact same bug would be present with memcmp:

    if (memcmp(target, user_string, user_string_length) == 0)
> Problem here was just mistake in the program not in the C lib.

Yes. But a library that people make mistakes with all the time is dangerous. If you are a programmer who ever makes mistakes (and clearly the AMT folks are, but I'm pretty sure so is everyone), you should avoid dangerous libraries.