cybersecurity · intermediate · ~15 min · safe pentest lab
Apply bounded-copy + early failure on overflow.
You're given a vulnerable function that copies a string into a fixed-size buffer with no bounds check:
void render(const char *user_input, char *banner) {
strcpy(banner, "Welcome, ");
strcat(banner, user_input);
}
Rewrite as int render_safe(const char *user_input, char *banner, size_t banner_sz) that produces "Welcome, <input>", returning 0 on success or -1 on overflow (caller's buffer untouched on overflow except for guaranteed NUL termination).
Patching a deliberately vulnerable function teaches the diagnostic mindset: spot the unbounded write, audit the destination size, replace with a bounded equivalent. This is the daily work of security auditors.
#include <stdio.h>
#include <string.h>
int render_safe(const char *user_input, char *banner, size_t banner_sz) {
/* TODO */
return -1;
}
Replacing only the symptom (the obvious strcpy) without checking the rest of the function — overflows often come in pairs. Using strncpy (gotcha — doesn't NUL-terminate when source fits exactly).
Maximum-length input. Empty input. Input containing NUL bytes (rare in text, common in binary).
O(input length).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.