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 Topics 1 and 2 of the Michael Hicks’s amazing software security course.

Watch each of the videos for these two weeks (note that Topic 1 is black hat while Topic 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)?

Other languages that do many things C does are Zig and Odin.

C Secure Programming Resources

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

We’ll say a few words about the latter in the next section. But first, a case study of sorts. The curl team writes really safe C. How do they do it? Watch:

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 vs. Recommendations

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!

The Safer-C Landscape

Let’s gather up guidelines, recommendations, and alternatives. Here’s a table mostly generated by a friendly AI chat bot (the one from OpenAI):

NameWhat it isApproachEnforcementPopularity / AdoptionNotes
Safe-CDialect / Subset of C plus toolsForbids dangerous constructs, requires annotations, static checksTooling plus compile-time enforcementNiche, mainly for high-integrity embedded systemsMarketed as a language
MISRA CGuidelines / Coding StandardSafe subset of C, focused on safety-critical systemsStatic analysis tools, manual checksVery high (automotive, aerospace)Widely used in ISO 26262 / DO-178C environments
CERT CGuidelines / Coding StandardSecurity-focused safe C rules and recommendationsStatic analysis toolsMedium-high (security auditing, government, industry)Emphasizes avoiding undefined behavior, concurrency risks
CycloneActual programming language (C fork)Adds safe pointers, checked types, region-based memory managementCompiler + static checksAcademic, legacyDiscontinued but influential, inspired Rust's early days
Checked CExtension to C originally by Microsoft ResearchChecked pointers, bounds checking, optional static analysisCompiler extensions, static checksActively researched, growing awarenessExperimental, aims to be a superset of C
ZigSystems programming languageBuilt from scratch: safe by design, no hidden control flow, manual mem mgmt but checkedCompiler, compile-time checksGrowing fast, espeically in embedded and low-level areasSimpler than Rust, “a better C”
OdinSystems programming languageSimplicity and performance, data-oriented design, better ergonomics than CCompiler, explicit controlNiche but activeAimed at game dev, high-performance apps, elegant syntax
RustModern systems programming languageOwnership model, borrow checker, memory safety without GCCompiler, very strict compile-time checksVery high, mainstream (Linux kernel, browsers, cloud infra)Strong community, excellent tooling, growing ecosystem

Recall Practice

Here are some questions useful for your spaced repetition learning. Many of the answers are not found on this page. Some will have popped up in lecture. Others will require you to do your own research.

  1. What is perhaps the most unsafe aspect of C?
    It lacks memory safety.
  2. Who is the author of the software security course is recommended in these notes?
    Michael Hicks
  3. What are some back hat topics in low level security?
    Buffer overflow, code injection, format string attacks.
  4. What are some white hat topics in low level security?
    Memory safety, type safety, return oriented programming (ROP), control flow integrity (CFI), secure coding practices.
  5. What is the difference between spatial and temporal memory safety?
    Spatial memory safety means that a program cannot read or write outside the bounds of allocated memory. Temporal memory safety means that a program cannot use memory after it has been freed.
  6. What is an example of a lack of type safety in low level code?
    A program that uses a pointer to an integer as a pointer to a character.
  7. What are techniques supplied by compilers and operating systems to compensate for poorly written software with memory vulnerabilities?
    Canaries, W+X, ASLR, CFI.
  8. What is a canary?
    A special value written somewhere in a stack frame, so that buffer overflow attacks will almost assuredly overwrite it and hence be detected.
  9. What is W+X?
    Hardware support for regions of memory that are either writable XOR executable.
  10. Why does W+X defeat the traditional naïve buffer overflow attack?
    Because the attacker cannot write executable code to the stack.
  11. What is ASLR?
    Address Space Layout Randomization: Randomizing the location of memory blocks so an attacker cannot craft an exploit assuming to find certain data at a known address.
  12. What is CFI?
    Control Flow Integrity: An assurance that a program only goes through predetermined points of execution.
  13. What are some low level languages that are alternatives to C?
    Rust, Zig, Odin.
  14. What is the name of the free book by David A. Wheeler?
    Secure Programming HOWTO - Creating Secure Software
  15. What is the name of the book by Matt Bishop?
    Robust Programming
  16. Who published CERT-C?
    The Software Engineering Institute at Carnegie Mellon.
  17. What is the difference between rules and recommendations in the CERT-C Coding Standard?
    Rules are requirements that if violated will almost surely result in an exploitable vulnerability. Recommendations basically improve software quality, but violations are not necessarily defects.
  18. About how many rules and recommendations are present in CERT-C today?
    120 rules and 186 recommendations.
  19. What are the major areas addressed in CERT-C? (Name just a few.)
    Preprocessor, Declarations and Initialization, Expressions, Integers, Floating-Point, Arrays, Characters and Strings, Memory Management, Input/Output, Environment, Signals, Error Handling, Application Programming Interfaces, Concurrency.

Summary

We’ve covered:

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