CodeBlanks Logo Icon CodeBlanks

Summary - Security

Combine buffer overflow, canary and ASLR to write a safe input function.

Summary - Security

This group covered five common C security mistakes and their protections: buffer overflow, stack canary, ASLR, format string and safe functions.

Buffer overflow is a stack smash; the canary detects it. ASLR randomizes memory regions; the format string flaw comes from printf(input). Safe alternatives like fgets and strncpy prevent overflow. You'll now write a mini task that combines the five concepts and shows each one's name and fix on screen.

Practice Print security concepts

Define void print_security(const char *topic, const char *fix) with body printf("%s -> %s\n", topic, fix);. In main call it for five topics: ("overflow", "bounds"), ("canary", "ssp"), ("aslr", "randomize"), ("format_string", "%s"), ("safe", "fgets/strncpy").

Starter code

main.c
#include <stdio.h>

// Define print_security function
// Print five topics: overflow, canary, aslr, format_string, safe

int main(void) {
    return 0;
}

Sample solution

main.c
#include <stdio.h>

void print_security(const char *topic, const char *fix) {
    printf("%s -> %s\n", topic, fix);
}

int main(void) {
    print_security("overflow", "bounds");
    print_security("canary", "ssp");
    print_security("aslr", "randomize");
    print_security("format_string", "%s");
    print_security("safe", "fgets/strncpy");
    return 0;
}

Expected output

overflow -> bounds
canary -> ssp
aslr -> randomize
format_string -> %s
safe -> fgets/strncpy

Security basics done

Congratulations! You've finished the C security basics. You now know buffer overflow, canary, ASLR, format string flaws and safe functions. You should consider these five together whenever you write C.

Defense is layered: safe functions avoid bugs, the canary alarms if a bug is caught, ASLR hides target addresses from attackers. No single protection is enough; they work together. This was the final expert group in the CodeBlanks C course. You can apply what you learned to real projects.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which function should a program use to prevent overflow?

  • strcpy
  • printf
  • gets
  • strncpy Correct answer

Explanation: strncpy takes a size parameter and prevents overflow.

2 Match five security concepts

Term Meaning
overflow Overflow
canary Stack alarm
aslr Address randomization
format_string Format string flaw
safe Bounded functions

All lessons in Expert