Developing Secure Code Robert Seacord
Developing Secure Code Robert Seacord
Developing Secure Code Robert Seacord
Agenda
Software Security CERT Secure Coding Initiative CERT Secure Coding Standards Top 10 Secure Coding Practices
& continuity
Critical infrastructures & government
Increasing Vulnerabilities
Reacting to vulnerabilities in existing systems is not working
generic countermeasures are no longer adequate Applications are the primary gateway to sensitive data
87% of respondents: poor
Tehang / Wattal, Carnegie Mellon Univerisity, 2004 "Impact of Software Vulnerability Announcements on the Market Value of Software Vendors an Empirical Investigation"
operates
Engineering software so that it continues to function under attack The ability of software to recognize, resist, tolerate, and recover from events that threaten it Goal: Better, defect-free software that can function more robustly in its operational production environment
7
Application Security
Absent or minimal consideration of security during all life cycle phases Not thinking like an attacker
10
cross-site-scripting, injection flaws Heffley/Meunier (2004): Can Source Code Auditing Software Identify Common Vulnerabilities and Be Used to Evaluate Software Security?
Cross-site scripting, SQL injection at top of the statistics (CVE, Bugtraq) in 2006 We wouldn't need so much network security if we didn't have such bad software security --Bruce Schneier
11
Coding Securely May Be Harder Than You Think! char x, y; x = -128; y = -x; if if if if if (x == y) puts("1"); ((x - y) == 0) puts("2"); ((x + y) == 2 * x) puts("3"); (((char)(-x) + x) != 0) puts("4"); (x != -y) puts("5");
12
W32.Blaster.Worm
Infected unpatched system connected to the Internet without user involvement. At least eight million Windows systems have been infected by this worm [Lemos 04]. Exploits a vulnerability in the DCOM RPC interface of Windows. Propagates via TCP/IP. Economic damage estimated to be at least $525 million.
13
August 2: Indications of attacks sent through Internet Relay Chat (IRC) networks discovered
August 11: Blaster discovered spreading through the Internet August 14: Over 1 million computers infected
14
15
17
A vulnerability exists in bash versions 1.14.6 and earlier where bash can be tricked into executing arbitrary commands.
18
19
20
(where \377 represents the single character with value 255 decimal) executes two commands, ls and who.
21
Integer Security
Integers represent a growing and underestimated source of vulnerabilities in programs. Integer range checking has not been systematically applied in the development of most software.
security flaws involving integers exist a portion of these are likely to be vulnerabilities
22
Agenda
Software Security CERT Secure Coding Initiative CERT Secure Coding Standards Top 10 Secure Coding Practices
23
24
We dont make the software you use, we make the software you use run slower.
25
Overall Thrusts
Advance the state of the practice in secure coding Identify common programming errors that lead to software vulnerabilities Establish standard secure coding practices Educate software developers
26
Current Capabilities
Secure Coding in C and C++
Addison-Wesley book Training
Secure coding web pages www.cert.org/secure-coding/ Secure string and integer library development Involvement in international standards activities:
ISO/IEC JTC1/SC22/WG14 C programming language
27
Training courses
Direct offerings Partnered with industry
knowledge course.
Students are interested in taking a class that goes
beyond policy
29
errors
describes how errors can lead to vulnerable code evaluates available mitigation strategies
Useful to anyone involved in developing secure C and C++ programs regardless of the application
30
Agenda
Software Security CERT Secure Coding Initiative CERT Secure Coding Standards Top 10 Secure Coding Practices
31
32
Scope
The secure coding standards proposed by CERT are based on documented standard language versions as defined by official or de facto standards organizations. Secure coding standards are under development for:
C programming language (ISO/IEC 9899:1999) C++ programming language (ISO/IEC 14882-2003 )
Applicable technical corrigenda and documented language extensions such as the ISO/IEC TR 24731 extensions to the C library are also included.
33
34
Rules
Coding practices are defined as rules when
Violation of the coding practice will result in a security
no such conditions) where violating the coding practice is necessary to ensure the correct behavior for the program.
Conformance to the coding practice can be verified.
35
36
37
Recommendations
Coding practices are defined as recommendations when
Application of the coding practice is likely to improve
system security.
One or more of the requirements necessary for a coding
38
MEM00-A. Allocate and free memory in the same module, at the same level of abstraction
Allocating and freeing memory in different modules and levels of abstraction burdens the programmer with tracking the lifetime of that block of memory. This may cause confusion regarding when and if a block of memory has been allocated or freed, leading to programming defects such as double-free vulnerabilities, accessing freed memory, or writing to unallocated memory. To avoid these situations, it is recommended that memory be allocated and freed at the same level of abstraction, and ideally in the same code module. Freeing memory in different modules resulted in a vulnerability in MIT Kerberos 5 MITKRB5-SA-2004-002 .
39
Compliant Solution
void func2(int *list, size_t list_size) { if (size < MIN_SIZE_ALLOWED) { /* Handle Error Condition */ return; } Eliminated free() /* Process list */ in support function. } void func1 (size_t number) { int *list = malloc (number * sizeof(int)); if (list == NULL) { /* Handle Allocation Error */ } func2(list,number); /* Continue Processing list */ free(list); }
call
41
10
15
20
25
30
35
Preprocessor (PRE) Declarations and Expressions (EXP) Integers (INT) Floating Point (FLP) Arrays (ARR) Strings (STR) Memory Input Output (FIO) Temporary Files Environment (ENV) Signals (SIG) Miscellaneous (MSC) POSIX
C Rule/Recommendation Distribution
77 Rules
99 Recommendations
42
Threaded discussions used for public vetting Candidate coding practices are moved into a secure coding standard when consensus is reached
43
L2 P6-P9
L3 P1-P4
44
45
Examples of vulnerabilities resulting from the violation of this recommendation can be found on the CERT website .
46
Train software professionals Certify programmers in secure coding Establish requirements for software analysis tools Certify software systems
47
Compass / ROSE
Coverity Prevent
48
Agenda
Software Security CERT Secure Coding Initiative CERT Secure Coding Standards Top 10 Secure Coding Practices
49
2.
3.
Validate input. Validate input from all untrusted data sources. Proper input validation can eliminate the vast majority of software vulnerabilities. Be suspicious of most external data sources, including command line arguments, network interfaces, environmental variables, and user controlled files [Seacord 05]. Heed compiler warnings. Compile code using the highest warning level available for your compiler and eliminate warnings by modifying the code. Architect and design for security policies. Create a software architecture and design your software to implement and enforce security policies. For example, if your system requires different privileges at different times, consider dividing the system into distinct intercommunicating subsystems, each with an appropriate privilege set.
50
5.
Keep it simple. Keep the design as simple and small as possible [Saltzer 74, Saltzer 75]. Complex designs increase the likelihood that errors will be made in their implementation, configuration, and use. Additionally, the effort required to achieve an appropriate level of assurance increases dramatically as security mechanisms become more complex. Default deny. Base access decisions on permission rather than exclusion. This means that, by default, access is denied and the protection scheme identifies conditions under which access is permitted [Saltzer 74, Saltzer 75].
51
7.
Adhere to the principle of least privilege. Every process should execute with the the least set of privileges necessary to complete the job. Any elevated permission should be held for a minimum time. This approach reduces the opportunities an attacker has to execute arbitrary code with elevated privileges [Saltzer 74, Saltzer 75]. Sanitize data sent to other systems. Sanitize all data passed to complex subsystems such as command shells, relational databases, and commercial off-the-shelf (COTS) components. Attackers may be able to invoke unused functionality in these components through the use of SQL, command, or other injection attacks. This is not necessarily an input validation problem because the complex subsystem being invoked does not understand the context in which the call is made. Because the calling process understands the context, it is responsible for sanitizing the data before invoking the subsystem.
52
9.
10.
Practice defense in depth. Manage risk with multiple defensive strategies, so that if one layer of defense turns out to be inadequate, another layer of defense can prevent a security flaw from becoming an exploitable vulnerability and/or limit the consequences of a successful exploit. For example, combining secure programming techniques with secure runtime environments should reduce the likelihood that vulnerabilities remaining in the code at deployment time can be exploited in the operational environment [Seacord 05]. Use effective quality assurance techniques. Good quality assurance techniques can be effective in identifying and eliminating vulnerabilities. Penetration testing, fuzz testing, and source code audits should all be incorporated as part of an effective quality assurance program. Independent security reviews can lead to more secure systems. External reviewers bring an independent perspective; for example, in identifying and correcting invalid assumptions [Seacord 05]. Adopt a secure coding standard. Develop and/or apply a secure coding standard for your target development language and platform.
53
2.
Define security requirements. Identify and document security requirements early in the development life cycle and make sure that subsequent development artifacts are evaluated for compliance with those requirements. When security requirements are not defined, the security of the resulting system cannot be effectively evaluated. Model threats. Use threat modeling to anticipate the threats to which the software will be subjected. Threat modeling involves identifying key assets, decomposing the application, identifying and categorizing the threats to each asset or component, rating the threats based on a risk ranking, and then developing threat mitigation strategies that are implemented in designs, code, and test cases [Swiderski 04].
54
Future Directions
Continue to develop and enhance existing secure coding standards and associated checkers Develop secure coding standards for other languages and programming environments
Java Web Development Language independent Ada, SPARK
55
Key Points
Everyday software defects cause the majority of software vulnerabilities. Software developers are not always properly trained and equipped to program securely. The result is numerous delivered defects, some of which can lead to vulnerabilities. Understanding the sources of vulnerabilities and learning to program securely is imperative to protecting the Internet and ourselves from attack.
56
References
[Saltzer 74] Saltzer, J. H. "Protection and the Control of Information Sharing in Multics." Communications of the ACM 17, 7 (July 1974): 388-402. [Saltzer 75] Saltzer, J. H. & Schroeder, M. D. "The Protection of Information in Computer Systems." Proceedings of the IEEE 63, 9 (September 1975), 1278-1308. [Seacord 05] Seacord, R. Secure Coding in C and C++. Upper Saddle River, NJ: Addison-Wesley, 2006 (ISBN 0321335724). [Swiderski 04] Swiderski, F. & Snyder, W. Threat Modeling. Redmond, WA: Microsoft Press, 2004.
57
Questions
58
Contact Presenter
Robert C. Seacord [email protected]
Hotline: +1 412 268 7090 CERT/CC personnel answer 24x7, 365 days per year Fax: +1 412 268 6989
Mailto: [email protected]
59