taviso

linux, programming and security

Quick Security Auditing Puzzle

, , ,

Here's an interesting problem, how can I make this simple program crash? (assume IA32)

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    int a, b;

    if (argc != 3)
        return 1;

    a = atoi(argv[1]);
    b = atoi(argv[2]);

    return b ? a / b : 0;
}


scroll down for the solution.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


The code correctly checks for divide by zero, so attempting this wont work:

$ ./a.out 42 0; echo $?
0
$


But not every invalid operation has been checked, while every programmer knows to avoid dividing by zero, very few are aware that it's also illegal to divide INT_MIN by -1. The reason is obvious, on a twos complement system |INT_MIN| is one greater than INT_MAX, so the result of the operation simply cannot fit in an integer.

This little known fact is almost guaranteed to crash any system that uses integer division on user controlled values as so few developers check for this unusual case.

$ ./a.out -2147483648 -1; echo $?
Floating point exception (core dumped)
136


McAfee Unhappy with Gentoo SecurityPartitioning schemes and security

Comments

Anonymous Thursday, December 21, 2006 8:52:33 PM

Anonymous writes: It's important to note that many interpreted languages do not seem to be vulnerable to this, and they support growing past those bounds with a seemingly transparent big integer implementation. $ php 2147483648

Anonymous Saturday, December 23, 2006 8:53:56 AM

Anonymous writes: OMFG. Kudos to you, didn't know that one.

Anonymous Saturday, December 23, 2006 11:40:04 AM

Anonymous writes: most dynamic typed languages aren't vulnerable like this

Anonymous Wednesday, January 10, 2007 12:22:26 AM

Schmurtz writes: PowerPC code is not vulnerable : there's a convention where 0/0=0 and -2147483648/-1=0 (for the divw opcode at least)

Anonymous Thursday, February 22, 2007 7:35:22 PM

Anonymous writes: wouldn't this be the same case for INT_MIN*-1?

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies