Kalpana Kalpana

Secure coding

Updated on
Share on FacebookTweet on TwitterShare on LinkedIn

Securing coding is the practice of developing computer software in a way that guards against the accidental introduction of security vulnerabilities. Defects, bugs and logic flaws are consistently the primary cause of commonly exploited software vulnerabilities. Through the analysis of thousands of reported vulnerabilities, security professionals have discovered that most vulnerabilities stem from a relatively small number of common software programming errors. By identifying the insecure coding practices that lead to these errors and educating developers on secure alternatives, organizations can take proactive steps to help significantly reduce or eliminate vulnerabilities in software before deployment.


Buffer Overflow Prevention

Buffer overflows, a common software security vulnerability, happen when a process tries to store data beyond a fixed-length buffer. The overflowed data may overwrite other data which can result in a security vulnerability (stack smashing) or program termination (segmentation fault).

An example of a C program prone to a buffer overflow is

If the user input is larger than the destination buffer, a buffer overflow will occur. To fix this unsafe program, use strncpy to prevent a possible buffer overflow.

Another secure alternative is to dynamically allocate memory on the heap using malloc.

In the above code snippet, the program attempts to copy the contents of src into dst, while also checking the return value of malloc to ensure that enough memory was able to be allocated for the destination buffer.

Format String Attack Prevention

A Format String Attack is when a malicious user supplies specific inputs that will eventually be entered as an argument to a function that performs formatting, such as printf(). The attack involves the adversary reading from or writing to the stack.

The C printf function writes output to stdout. If the parameter of the printf function is not properly formatted, several security bugs can be introduced. Below is a program that is vulnerable to a format string attack.

A malicious argument passed to the program could be “%s%s%s%s%s%s%s”, which can crash the program from improper memory reads.

Integer Overflow Prevention

Integer overflow occurs when an arithmetic operation results in an integer too large to be represented within the available space. A program which does not properly check for integer overflow introduces potential software bugs and exploits.

Below is a program which checks for overflow by confirming the sum is greater than or equal to x and y. If the sum did overflow, the sum would be less than x or less than y.

If the sum of x and y are less than the defined MAX, the program will return true, otherwise isValid will return false. The problem with the code is it does not check for integer overflow on the addition operation. If the sum of x and y is greater than the available space to store the integer, the integer will overflow and “roll over” to a value less than MAX.

Below is a program which checks for overflow by confirming the sum is greater than or equal to x and y. If the sum did overflow, the sum would be less than x or less than y.


Secure coding Wikipedia

Similar Topics
No sos vos, soy yo
José Daniel Barquero Cabrero
David Millar