cybersecurity · intermediate · ~15 min · safe pentest lab
Aligned byte-stride scanning with a composite predicate.
Implement:
#include <stdint.h>
#include <stddef.h>
int has_canary_pattern(const uint8_t *frame, size_t n);
A glibc stack canary on x86_64 has its low byte = 0x00 and the upper 7 bytes random. We treat the frame as 8-byte aligned slots and look for one whose:
Return:
1 if any slot matches.0 if none does.-1 if frame == NULL or n % 8 != 0.for (i = 0; i < n; i += 8).byte 0 == 0 AND (byte 1 | byte 2 | … | byte 7) != 0.Recognising the canary's shape is foundational defensive knowledge — it explains why naive overflows fail and why mitigations matter.
A const byte buffer + its length (multiple of 8).
0, 1, or -1.
Stride 8. Predicate: low byte zero, upper 7 bytes have at least one non-zero.
#include <stdint.h>
#include <stddef.h>
int has_canary_pattern(const uint8_t *frame, size_t n) {
/* TODO */
(void)frame; (void)n;
return -1;
}
Treating all-zero slot as canary. Ignoring the n % 8 != 0 invariant. Walking unaligned.
Empty frame. Single slot. Canary at the very last slot.
O(n).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.