Defensive programming

This is an old revision of this page, as edited by 203.202.185.178 (talk) at 02:35, 29 April 2004 (removed undesired carriage return). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Defensive programming is a form of defensive design intended to ensure the continuing function of a piece of software in spite of unforeseeable usage of that piece of software. The idea can be viewed as reducing or eliminating the prospect of Murphy's Law having effect. Defensive programming techniques come into their own when a piece of software could be misused mischievously or inadvertantly to catastrophic effect.

Here are some hints on defensive programming techniques to avoid creating security problems. Many of these techniques also improve general quality of code, because almost any major bug can be potentially used by a cracker for a Denial of Service or other attack.

Note that the techniques below are not sufficient to ensure security: see the articles computer insecurity and secure computing for more information.


Please expand this article. These random notes should be changed to a more coherent article.

  • One of the most common problems is unchecked use of constant-size structures and functions for dynamic-size data (the buffer overflow problem). This is especially common for string data in C.

You should never use functions like gets and scanf.

  • Never make your code more complex than necessary. Complexity breeds bugs, including security problems.
  • Either leave your code available to everyone on the Net (see Free software or open source definition) or hire someone who will do security audit for you.
  • If possible, reuse code instead of writing from scratch.
  • Encrypt all important data that flows the Net.
  • All data is important until proved otherwise.
  • All code is unsecure until proven otherwise.
  • Never make your program setuid unless you're really sure it's secure.
  • If you check data for correctness, check if it's correct, not if it is incorrect.

Crackers are likely to invent new kinds of incorrect data. For example, if you checked if a requested file is not "/etc/passwd", a cracker might pass another name of this file, like "/etc/../etc/passwd".


Preconditions, Postconditions and Invariants validation are also part of Defensive Programming. This may involve checking arguements to a function or method for validity before execution of the body of the function. After the body of a function, doing a check of object state (in OO languages) or other held data and the return value before exits (break/return/throw/error code) is also wise.

Within functions, you may want to double check that you aren't referencing something that is not valid (ie: null) and that array lengths are valid before referencing elements with indexes on all temporary/local instantiations. A good heuristic is to not trust the libraries you didn't write either. So any time you call them, check what you get back from them. It often helps to create a small library of "asserting" and "checking" functions to do this along with a logger so you can trace your path and reduce the need for extensive debugging cycles in the first place. With the advent of logging libraries and aspect oriented programming, many of the tedious aspects (yes, a pun) of defensive programming are mitigated.

Generally speaking then, it is preferrable to throw intelligible exception messages that enforce part of your API contract and guide the client programmer instead of returning values that a client programmer is likely to be unprepared for and hence minimize their complaints and increase robustness and security of your software.


Books:

  • William R. Cheswick and Steven M. Bellovin, Firewalls and Internet Security: Repelling the Wily Hacker ISBN 0201633574 http://www.wilyhacker.com/

External references: