Secure coding

Uses your system voice; voice and rate in settings.

Snapshot

≈ 5 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

Secure coding is the practice of developing computer software in such 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. Some scholars have suggested that in order to effectively confront threats related to cybersecurity, proper security should be coded or "baked in" to the systems. With security being designed into the software, this ensures that there will be protection against insider attacks and reduces the threat to application security. Implementing secure coding practices is part of the secure by design approach to security engineering.

Secure coding is the practice of developing computer software in such 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. Some scholars have suggested that in order to effectively confront threats related to cybersecurity, proper security should be coded or "baked in" to the systems. With security being designed into the software, this ensures that there will be protection against insider attacks and reduces the threat to application security. Implementing secure coding practices is part of the secure by design approach to security engineering. include define SMALL 50 void vulnerable_function(char* large_user_input) { char dst[SMALL]; strcpy(dst, large_user_input); } 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. include define BUF_SIZE 100 void secure_function(char* user_input) { char dst[BUF_SIZE]; // copy a maximum of BUF_SIZE bytes strncpy(dst, user_input, BUF_SIZE); // set the last character in the buffer to NUL. dst[BUF_SIZE - 1] = '\0'; } Another secure alternative is to dynamically allocate memory on the heap using malloc. include include char* secure_copy(char* src) { size_t len = strlen(src); char* dst = (char*)malloc(len + 1); if (dst) { strncpy(dst, src, len); // append null terminator dst[len] = '\0'; } return dst; } 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. include void vulnerable_print(char* malicious_input) { printf(malicious_input); } 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. define MAX 5000 bool sum_is_valid_flawed(unsigned int x, unsigned int y) { unsigned int sum = x + y; return sum 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 maximum possible value of an unsigned int , the addition operation will overflow and perhaps result in a value less than or equal to MAX, even though the sum of x and y is greater than MAX. Below is a function which checks for overflow by confirming the sum is greater than or equal to both x and y. If the sum did overflow, the sum would be less than x or less than y. define MAX 5000 bool sum_is_valid_secure(unsigned int x, unsigned int y) { unsigned int sum = x + y; return sum >= x && sum >= y && sum https://www.example.net/cgi-bin/article.sh?name=dogfood.html If the script has no input checking, instead trusting that the filename is always valid, a malicious user could forge a URL to retrieve configuration files from the web server: https://www.example.net/cgi-bin/article.sh?name=../../../../../etc/passwd Depending on the script, this may expose the /etc/passwd file, which on Unix-like systems contains (among others) user IDs, their login names, home directory paths and shells. (See SQL injection for a similar attack.)

Text derived from https://en.wikipedia.org/w/index.php?oldid=1362514479 under CC BY-SA 4.0; modifications have been made.