General Guidelines for Secure Code Review
Overview
Secure Code Review is a special activity compared to a normal code review. While the typical code review is focused on software quality, including usability, reusability, and maintainability, secure code reviews are focused on software security aspects, including but not limited to confidentiality, integrity, and availability (C.I.A.).
In 2006, Howard from Microsoft, published “A Process for Performing Security Code Reviews.”. It provides some general guidelines for performing a security code review. The guidelines are still valid today:
- Make sure you know what you are doing
- Prioritize
- Review the code.
Make sure you know what you are doing
Before you review code, please make sure you understand the following:
- Threat Model and Security Architecture of the feature, including assets, security objectives, adversaries, and the mitigations.
- The general secure design and coding principles for EDK II.
Prioritize
According to “A Process for Performing Security Code Reviews.”, the priority of common software code is below:
- Old code
- Code that runs by default
- Code that runs in an elevated context
- Anonymously accessible code
- Code listening on a globally accessible network interface
- Code is written in C/C++/assembly language
- Code with a history of vulnerabilities
- Code that handles sensitive data
- Complex code
- Code that changes frequently
Except for items #4 and #5, all other rules apply to EDK II firmware.
Review the code
Reviewing the code involves three steps:
- Rerun all available code-analysis tools.
- Look for common vulnerability patterns, such as
- Integer arithmetic vulnerabilities
- Buffer-overrun vulnerabilities
- Cryptographic vulnerabilities
- Structured Query Language (SQL) Injection vulnerabilities
- Cross-site scripting vulnerabilities
- Dig deep into risky code, such as
- Are there logic or off-by-one errors (for example, ‘>’ vs. ‘>=’ or ‘||’ vs. ‘&&’)?
- Is the data correctly validated?
- Are buffer lengths constrained correctly?
- Are integer values range-checked correctly?
- Are pointers validated?
- Can the code become inefficient (for example, O(N2)) due to some malformed data (for example, a hash table look-up becomes a list look-up)?
- Are errors handled correctly?
Other than #2.iv and #2.v above, all other rules apply to EDK II firmware.
Besides “A Process for Performing Security Code Reviews.”, Ransome provided some good suggestions in the book “Core Software Security: Security at the Source” on how to perform the SDL activity including security code review.
EDK II Secure Coding Guidelines
We also provided the guideline for EDK II Secure Coding. People need to fully understand the EDK II secure coding best practices before doing the security code review.