This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a security exercises repository focused on teaching secure C programming practices. The exercises are designed to help developers understand and prevent common vulnerabilities, with detailed references to CERT C Coding Standard, "Effective C", and "The Art of Software Security Assessment".
exercises/
├── integer_overflow/ # Integer overflow detection exercises
│ └── safe_size_t_addition_exercise.md
└── buffer_overflow/ # Buffer overflow prevention exercises
└── secure_string_concatenation_exercise.md
Located in integer_overflow/, these exercises focus on:
- Unsigned integer wraparound detection (CERT C INT30-C)
- Size_t arithmetic overflow prevention
- Precondition and postcondition overflow testing
- Real-world CVE scenarios (CVE-2009-1385, CVE-2014-4377)
Located in buffer_overflow/, these exercises focus on:
- Secure string operations (CERT C STR31-C, STR07-C)
- Proper use of
strncat()and bounds-checking functions - Off-by-one error prevention
- Integer underflow in size calculations
- Real-world CVE scenarios (CVE-2009-0587, CVE-2009-1252)
Standard compilation with security flags:
gcc -std=c11 -Wall -Wextra -Wpedantic -Werror \
-fsanitize=address,undefined \
-D_FORTIFY_SOURCE=2 -fstack-protector-strong \
-o <output> <source>.cRun with AddressSanitizer:
./<executable>Check with Valgrind:
valgrind --leak-check=full ./<executable>For overflow detection tests that should exit:
./<executable>
echo "Exit code: $?"Each exercise follows a consistent format:
- Exercise Overview: Difficulty level and objective
- Inspiration: References to CERT C, security books, and standards
- Background: Real-world context and CVE examples
- Requirements: Function signatures and compliance requirements
- Noncompliant Examples: Common vulnerable patterns to avoid
- Test Cases: Comprehensive test suite including CVE simulations
- Hints: Implementation guidance
- Bonus Challenges: Advanced variations
All exercises are based on:
- CERT C Coding Standard 2016: Specific rule citations (e.g., INT30-C, STR31-C)
- Effective C, 2nd Edition: Chapter and page references
- The Art of Software Security Assessment: Chapter and page references
- Unsigned wraparound is well-defined but dangerous
- Never test
sum > SIZE_MAXafter the operation (always false) - Use precondition:
b > SIZE_MAX - a - Or postcondition:
sum < aindicates wraparound - Exit code 136 used for overflow errors in exercises
- Never use
strcat()directly (no bounds checking) strncat(dest, src, n)where n is remaining space, not total size- Account for null terminator:
remaining = dest_size - strlen(dest) - 1 - Check for integer underflow before subtraction:
strlen(dest) + 1 < dest_size - Validate destination is null-terminated before operations
The buffer overflow exercise (secure_string_concatenation_exercise.md) recommends completing the integer overflow exercise (safe_size_t_addition_exercise.md) first, as size calculations in buffer operations require overflow detection.
- Buffer Overflow: Off-by-one errors, incorrect size calculations
- Integer Overflow: Wraparound in size_t operations
- Integer Underflow: Unsigned subtraction leading to large values
- Improper Null Termination: Missing or incorrect null byte handling
Exercise solutions should:
- Include all required headers:
<stddef.h>,<stdint.h>,<stdio.h>,<stdlib.h>,<string.h> - Validate all input parameters (NULL checks, size checks)
- Use
SIZE_MAXfor overflow detection - Write errors to
stderrusingfprintf() - Follow exact error message formats specified in exercises
- Return specific error codes or exit codes as specified