Back

 

From Wikipedia, the free encyclopedia

Secure Coding is a form of defensive design intended to ensure the continuing function of a piece of software in spite of unforeseeable usage of said software. The idea can be viewed as reducing or eliminating the prospect of Murphy's Law having effect. Secure Coding techniques are used especially when a piece of software could be misused mischievously or inadvertently to catastrophic effect.Secure Coding is an approach to improve software and source code, in terms of:

Contents

1 Secure Programming

2 Some Secure Coding techniques

    2.1 Reduce source code complexity

    2.2 Source code reviews

    2.3 Software testing

    2.4 Intelligent source code reuse

    2.4.1 The legacy problems

    2.5 Secure Input / Output Handling

    2.6 Canonicalization

    2.7 Principle of least privilege

    2.8 Low tolerance against "potential" bugs

    2.9 Other techniques

Secure Programming

Secure Coding is sometimes referred to as defensive programming by computer scientists who state this approach minimizes bugs. Software bugs can be potentially used by a cracker for a code injection, denial-of-service attack or other attack.

A difference between Secure Coding and normal practices is that few assumptions are made by the programmer, who attempts to handle all possible error states. In short, the programmer never assumes a particular function call or library will work as advertised, and so handles it in the code. An example follows:

int low_quality_programming (char *input)
{
  char str[256];
  strcpy ( str, input ); // copy input
  ...
  ...
}

The function will crash on very long input. Many mainstream programmers may not feel that this is a problem because "Surely no one will enter that long of an input!". A programmer practicing Secure Coding would not allow the bug, because if the application contains a known bug, Murphy's Law dictates that the bug will occur in use. This particular bug demonstrates a vulnerability which enables buffer overflow exploits.

Some Secure Coding techniques

Here are some Secure Coding techniques suggested by some leading computer scientists to avoid creating security problems and software bugs. These computer scientists state that while this process can improve general quality of code, it not sufficient to ensure security. See the articles computer insecurity and secure computing for more information.

Defensive software programming principles described by leading proponents include:

Reduce source code complexity

Never make code more complex than necessary. Complexity breeds bugs, including security problems. This goal can conflict with the goal of writing programs that can recover from any error and handle any user input. Handling all unexpected occurrences in a program requires the programmer to add extra code, which may also contain bugs.

Source code reviews

A source code review is where someone other than the original author performs a code audit. A do-it-yourself security audit is insufficient: the review must be made by a non-author, just as when writing a book, it must be proofread by someone other than the author.

Simply making the code available for others to read is insufficient: there is no guarantee that the code will ever be looked at, let alone that it will be rigorously reviewed.

Software testing

Software testing should include both whether the software works as intended, and what is supposed to happen when deliberately bad input is supplied.

Testing tools can capture keystrokes associated with normal operations, then the captured keystroke strings can be copied and edited to try out all permutations of combinations, then extended for later tests after any modifications. Proponents of key logging state that programmers who use this method should make sure that the people whose keystrokes are being captured are aware of this, and for what purpose, to avoid accusations of privacy violation.

Intelligent source code reuse

If possible, reuse code instead of writing from scratch. The idea is to capture the benefits of well written and well tested source code, instead of creating unnecessary bugs.

However, re-using code is not always the best way to go forward, particularly when business logic is involved. Reuse in this case may cause serious business process bugs.

The legacy problems

Before reusing old source code, libraries, APIs, configurations and so forth, it must be considered if the old work is valid for reuse, or if it is likely to be prone to legacy problems.

Legacy problems are problems inherent when old designs are expected to work with today's requirements, especially when the old designs were not developed or tested with those requirements in mind.

Many software products have experienced problems with old legacy source code, for example:

Notable examples of the legacy problem:

Secure Input / Output Handling

Managing input is a difficult problem, which is detailed in Secure input and output handling.

Canonicalization

Crackers are likely to invent new kinds of representations of incorrect data.

For example, if you checked if a requested file is not "/etc/passwd", a cracker might pass another variant of this file name, like "/etc/./passwd".

To avoid bugs due to non-canonical input, employ Canonicalization API's.

Principle of least privilege

Employ Principle of least privilege. Avoid having software running in a privileged mode if possible;

Low tolerance against "potential" bugs

Assume that code constructs that appear to be problem prone (similar to known vulnerabilities, etc.) are bugs and potential security flaws. The basic rule of thumb is: "I'm not aware of all types of security exploits. I must protect against those I do know of and then I must be proactive!".

Other techniques

  1. 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. C library functions like "gets" should never be used since the maximum size of the input buffer is not passed as an argument. C library functions like "scanf" can be used safely, but require the programmer to take care with the selection of safe format strings, by sanitizing it before using it.
  2. Encrypt/authenticate all important data transmitted over networks. Do not attempt to implement your own encryption scheme, but use a proven one instead.
  3. All data is important until proven otherwise.
  4. All data is tainted until proved otherwise.
  5. All code is insecure until proven otherwise.
    1. You cannot prove the security of any code in userland, or, more canonically: "never trust the client".
  6. If data are checked for correctness, verify that they are correct, not that they are incorrect.
  7. Design by Contract
    1. Design by contract uses preconditions, postconditions and invariants to ensure that provided data (and the state of the program as a whole) is sanitized. This allows code to document its assumptions and make them safely. This may involve checking arguments to a function or method for validity before executing the body of the function. After the body of a function, doing a check of object state (in Object-oriented programming languages) or other held data and the return value before exits (break/return/throw/error code) is also wise.
  8. Assertions
    1. Within functions, you may want to check that you are not referencing something that is not valid (i.e., null) and that array lengths are valid before referencing elements, especially on all temporary/local instantiations. A good heuristic is to not trust the libraries you did not 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 of Secure Coding are mitigated.
  9. Prefer exceptions to return codes
    1. Generally speaking, it is preferable 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.

Most text is sampled from Wikipedia and available under the terms of the GNU Free Documentation License. (See Copyrights for details.) Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a US-registered 501(c)(3) tax-deductible nonprofit charity.