News for hackers. Stuff that matters.
Re:Algorithmic complexity attacks and libc qsort() by simias on Wednesday June 11, 2014 @11:01AM (#7878330)
I'm surprised the article does not point out more directly that it's usually pretty simple to mitigate this attack vector by switching to mergesort. The worst case is O(n log n) so there's no real "killer input".

I think it's a good rule of thumb to say "if you need to sort large quantities of untrusted data you should probably use mergesort".

Re:Algorithmic complexity attacks and libc qsort() by j2kun on Wednesday June 11, 2014 @11:04AM (#7878346)
I've always liked mergesort for this reason. It's easier to understand and there's no wondering about complexity.
Re:Algorithmic complexity attacks and libc qsort() by astrada on Wednesday June 11, 2014 @11:20AM (#7878447)
Well, sometimes you have space constraints. The O(n) extra-space required by mergesort isn't always available. But if you don't have space constraints, mergesort is a very good choice because you can easily write a highly scalable parallel version of the algorithm.
Re:Algorithmic complexity attacks and libc qsort() by boomzilla on Wednesday June 11, 2014 @02:02PM (#7879419)
You can implement merge sort in place too:

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.22....

Looks like the algo is more complicated, but surely implementable. In most modern architectures, the key for performance is how one exploits the caching behavior. Quicksort is very cache friendly, as well as the common implementation of merge sort. Not so for heapsort.

Re:Algorithmic complexity attacks and libc qsort() by phkahler on Wednesday June 11, 2014 @12:54PM (#7879007)
"The O(n) extra-space required by mergesort isn't always available."

Heapsort

Re:Algorithmic complexity attacks and libc qsort() by lsb on Wednesday June 11, 2014 @12:34PM (#7878899)
No, but mergesort can easily fall back to dumping its sorted contents to disk, for later merging; this disk-based merge sort was invented by van Neumann in the 1940s, and is how the unix utility sort works (dozen-way merge of sorted shards of a file).
Re:Algorithmic complexity attacks and libc qsort() by nly on Wednesday June 11, 2014 @05:40PM (#7880597)
Mergesort gets pretty damn efficient, even with moderate buffer space.
Re:Algorithmic complexity attacks and libc qsort() by matslina on Wednesday June 11, 2014 @11:56AM (#7878657)
Mergesort would be a poor choice for qsort() due to the linear space complexity. With N bytes of RAM, you'd only be able to sort (a bit less than) N/2 bytes of data. An in-place algorithm is preferable.

Glibc is the only libc I'm aware of that implements mergesort. It still falls back to quicksort for large inputs though.

Re:Algorithmic complexity attacks and libc qsort() by mzs on Wednesday June 11, 2014 @12:18PM (#7878797)
I just heard about smoothsort: http://code.google.com/p/combsortcs2p-and-other-sorting-algo...

Also, nice little write-up, thanks. I also dig the extracted sort implementations you dug-out: https://github.com/matslina/qsort

Re:Algorithmic complexity attacks and libc qsort() by tedunangst on Wednesday June 11, 2014 @12:51PM (#7878987)
All BSD derived libcs contains mergesort(), and heapsort() as well.
Re:Algorithmic complexity attacks and libc qsort() by mzs on Wednesday June 11, 2014 @01:40PM (#7879283)
True, but parent meant used in the qsort and qsort_r implementations.
Re:Algorithmic complexity attacks and libc qsort() by tedunangst on Wednesday June 11, 2014 @03:31PM (#7879967)
Oh, of course. reading fail.
Re:Algorithmic complexity attacks and libc qsort() by valarauca1 on Wednesday June 11, 2014 @11:14AM (#7878407)
If you are ever handling input data from basically anything outside of the program. Its better to use a constant speed algorithm like merge sort. Just because of attacks like this, I'm really surprised algorithm exploits attacks like this haven't come to the forefront of attacks recently.

Application level DDoS's leave the kernel/network/sockets layers exposed for exploitation. Since their tasks will take priority over user's applications.

Re:Algorithmic complexity attacks and libc qsort() by Gracana on Wednesday June 11, 2014 @11:33AM (#7878523)
> Application level DDoS's leave the kernel/network/sockets layers exposed for exploitation. Since their tasks will take priority over user's applications.

So what's the benefit of that, just that you can maximize the chaos you cause by attacking in two different ways?

Re:Algorithmic complexity attacks and libc qsort() by valarauca1 on Wednesday June 11, 2014 @11:53AM (#7878638)
One attack and obfuscate the other. Like if you server is at 90% cpu use, and the vast majority of it is server application. You won't consider anything strange happening. You can keep attacking the system with strange algorithm exploits or stop. Nobody will expect an attack in the system since well last time I check the box was at 90% CPU, and it was the server. I'd check again, but maybe we just have a lot of traffic today or something similar.

Basically its the old, can't see the forest past the trees routine.

Its more gaming the operators then the system.

Re:Algorithmic complexity attacks and libc qsort() by Gracana on Thursday June 12, 2014 @12:37PM (#7884393)
Ah okay, I wondered if it was that simple.
Re:Algorithmic complexity attacks and libc qsort() by userbinator on Wednesday June 11, 2014 @09:36PM (#7881390)
I think heapsort is a better replacement - in-place, guaranteed O(n log n) time (so also quite resistant to information leakage via timing disclosure, which could be important in higher security applications.)