« January 2006 | Main | March 2006 »

February 26, 2006

FOSDEM

FOSDEM did not deceive me at all - just the contrary.

There were many interesting things and the talk I liked the most was about valgrind. The very obvious idea after it was "why not develop a security scanner on the top of valgrind?". Valgrind is a framework to develop simulation-based tools, and MemCheck is just one of them. Valgrind handles the most tedious and demanding part of the job - simulating the processor instructions. It also handles the operating system calls, signals, and the rest, so the developer of a security scanner will be able to concentrate on the essential things.

Just an idea..

February 21, 2006

Nice dynamic graph

A nice dynamic graph: relation browser.

Something similar could be used in IDA Pro for inter-function navigation. The graph nodes would be functions and static data variables, the edges would represent function calls and data accesses...

February 20, 2006

Capricious programming

Textbooks on software engineering prescribe to check preconditions at the beginning of a function. This is a really good idea: the sooner we detect that the input data or environment does not match our expectations, the easier it is to trace and debug the application. A nice function with precondition checking refuses to "work" if the preconditions are not satisfied.

The next question is: how exactly should our function refuse to work it detects that an unsatisfied precondition? I see the following possible answers to this question (sorted from the least invasive to the most destructive):


  • silently repair and continue
  • return an error code
  • throw an exception
  • stop or abort the application

The least invasive approach, to repair and silently continue, is a bad idea. An application consisting of many 'intelligent' functions doing something despite of erroneous input would be extremely difficult to debug and use. Such an application would always return an answer but we would never know if this answer is correct at all.

The second approach, to return an error code, requires a lot of manual work. Not only we have to establish different error codes for different situations, we have also to generate them and the caller must not forget to check them. As common experience shows, we do forget to check error conditions...

Exceptions are much better since once an exception is thrown it will be propagated to the callers until someone catches it. So the programmer's burden of checking the error codes disappears. Or does it?... In fact it gets replaced by the burden of specifying exception handlers at the right places and by the burden of remembering that almost any line of the program can be interrupted by an exception. If we want to make our program not only 'exception generating' but also 'exception safe', then we have to consider many possible execution paths - with and without exception. This turns out to be quite a feat in itself. If you want more gory details, consider Exceptional C++ and its follow-ups. This book contains vital information about programming with exceptions.

The last choice is the easiest one. If the preconditions are not satisfied, simply abort the application. This is no brainer - no error codes, no exceptions, just pay the price of killing the application (if the application is a quick & dirty perl script, then the tradition is to tell it literally to die...)

Alas, this is acceptable only in a limited number of situations. If we encounter a fatal condition and the application can not meaningfully continue, then ok, there is nothing to lose, dump it. For example: a compiler which can not find the input file, or a mail client which can not find the account settings. The best thing they can do is to stop immediately.

But in all other cases, you should not use this kill the application. For example, you can not use this approach in IDA plugins. Imagine a plugin which works with MS Windows PE files. It is natural for such a plugin to check the input file type at the initialization time. This is the wrong way of doing it:

if ( inf.filetype != f_PE )
error("Sorry, only MS Windows PE are supported");

This is bad because as soon as we try to disassemble a file different from PE, our plugin will interfere and abort the whole application, i.e. IDA. This is quite embarrassing, especially for unsuspecting users of the plugin who never saw the source code of the plugin.

The right way of refusing to work is:

if ( inf.filetype != f_PE )
return PLUGIN_SKIP;

If the input file is not what we except, we return an error code. IDA will stop and unload the current plugin. The rest of the application will survive.

Do not let your software be capricious without a reason :)

February 13, 2006

FOSDEM

Each year in Brussels, Belgium there is a conference called FOSDEM. It is organized on the last weekend of February. There seem to be some interesting talks about security and program developement this year. The development track is almost fully dedicated to various CVSes. The choice of CVS is an object of faith like the choice of the editor so it doesn't hurt to go and see what others use...

I'll also attend the the talk about valgrind by Julian Seward. I loved the tool from the first time I used it.

See you there :)

February 07, 2006

FindCrypt2

I realized that it is quite easy to make FindCrypt work with big endian programs. For that we just need to know the size of each constant array element and swap them if required. So here is the second version of FindCrypt. It introduces the following improvements:

  1. it works with both little and big endian programs
  2. it knows to reuse old slots in the bookmarks if run repeatedly
  3. it is fully automatic and scans each new created database. manual scan is still available

Future possible improvement: a tool which would extract constant arrays from the source code of any project. This tool can be written on perl or python and will be quite simple (we only have to handle constant array definitions in C). More sophisticated tool could also take care of type definitions like "typedef long LONG"...


For your convenience, here are links to both versions: findcrypt.zip and findcrypt2.zip
Compare them to see the differences, there aren't many!

Latest news: Hex-Rays decompiler has been released!