Secure C

C is notorious for being an unsafe language. But this is by design! Programming in C requires discipline and a lot of detailed knowledge of memory management and other low-level concepts. There is much to learn!

Background

These notes will assume you’re a fairly capable C programmer. In particular, you should already know:

The Biggest Security Problems in C

Remember that C is by design NOT a memory-secure programming language. It’s not a good choice for high-level applications, but it might be the best you have for system and embedded software.

Memory safety is the biggest problem. The language does not prevent buffer overruns (underflow and overflow) and format string attacks. These things still happen today.

For an excellent overview of security concerns in C, see weeks 1 and 2 of the Coursera course Software Security with Michael Hicks.

Watch each of the videos for these two weeks (note that week 1 is black hat, week 2 is white hat):

As you go through these videos, make notes of:

If you don’t have to use C, maybe you can just use Rust.

Exercise: Learn about the programming language Rust. Does it try to replace C? Or C++? Is it as efficient? What is its approach to memory safety (in three sentences or less)?

C Secure Programming Resources

Where else can we learn how to be a security-minded developer? Here are some resources:

Let’s say a few words about the latter in the next section.

CERT-C

There’s a fantastic amount of information at The SEI CERT C Coding Standard from the Software Engineering Institute at Carnegie Mellon.

The Standard is organized into a number of Guidelines, divided into Rules and Recommendations, grouped into sections numbered and titled as follows:

Rules are basically requirements that if violated will almost surely result in an exploitable vulnerability. They are generally in principle checkable by static analysis tools (or by a competent human code reviewer). Recommendations basically improve software quality, but violations are not necessarily defects.

Examples of Rules:

Examples of Recommendations:

The online standard has a page listing all of the rules and a page listing all of the recommendations. You can begin your study simply by reading the title of each of the 120 rules and 186 recommendations. Then dive into the ones of interest.

Reading the whole standard can be worthwhile too! There’s sime nice introductory material, explanations of the standard’s organization and how to get the most from it, some history, and discussion of its relationship with other standards and publications. The Back Matter section has some good stuff summarizing useful bits from the C Language Specification and a listing of some tools commonly used in industrial C programming.

CLASSWORK
We’re going to browse a few of these guidelines!

Summary

We’ve covered:

  • How C is insecure by design
  • Known problems
  • Resources for secure C programming
  • CERT C