cybersecurity · intermediate · ~20 min
Robust binary-header parsing with bounds + sentinel checks.
Given the first 64 bytes of an ELF64 binary, extract three fields:
class — buf[4]: 1 = 32-bit, 2 = 64-bitdata — buf[5]: 1 = little-endian (LSB), 2 = big-endian (MSB)machine — buf[18..19] little-endian (assume LSB; we'll only test LSB binaries)Implement
int parse_elf64_ident(const unsigned char *buf, int len, int *class_, int *data, int *machine).
Return 1 on success (and the three outputs filled). Return 0 if:
buf is NULL or len < 20The ELF header is the universal Linux binary fingerprint. Knowing how to extract its fields makes you fluent in any reverse-engineering tool.
64+ byte buffer + length.
0/1 + filled outputs.
Bound-check every read.
#include <stddef.h>
int parse_elf64_ident(const unsigned char *buf, int len, int *class_, int *data, int *machine) { /* TODO */ (void)buf; (void)len; (void)class_; (void)data; (void)machine; return 0; }
Indexing buf[18] without confirming len >= 20.
Magic mismatch; short buffer; NULL outputs.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.