Lab 6: GL-E00 Capstone: Cross-ABI ELF32, PT_INTERP, Vermagic & Boot-Blob Preflight Gate
Audit 32-bit ARMv7-A hard-float ELF binaries (EM_ARM, EF_ARM_ABI_FLOAT_HARD, PT_INTERP, DT_NEEDED), diagnose Linux 6.12 .modinfo vermagic/CRC module rejections, and unify host pre-deployment verification in the GL-E00 Workbench Preflight Capstone.
Prerequisites in this module
What ./check.sh verifies
- Builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror.
- Fix compiler warnings or errors in files/core.c.
- Archives core.o into libcore.a and links independent TAP verifier.
- Preserve public declarations in files/core.h so tests.c links.
step 1/15 · inspect starter files and specification
Deploying unverified cross-build outputs into /srv/tftp and /srv/nfs/rootfs turns single-line build mistakes — an uncompiled .dts, a soft-float 0x200 tool, a missing /lib/ld-linux-armhf.so.3 symlink, or a -dirty .ko — into bewildering target boot failures.
In this GL-E00 Capstone Project, you build a complete C11 binary preflight gate that validates every boot, user-space, and kernel-module artifact in microseconds before target deployment.
learner@ksa:~/gl-e00/lab-m06ls -lastarter/Makefilestarter/cli.cstarter/core.cstarter/core.hlearner@ksa:~/gl-e00/lab-m06starter: starter/Makefile
learner@ksa:~/gl-e00/lab-m06$ bat starter/Makefile File: starter/Makefile CC_BIN = gcc AR = ar CFLAGS_ALL = -std=c11 -Wall -Wextra -Werror -O2 LDLIBS = all: libcore.a lab_cli libcore.a: core.o $(AR) rcs $@ $^ lab_cli: cli.o libcore.a $(CC_BIN) $(CFLAGS_ALL) cli.o libcore.a $(LDLIBS) -o $@ %.o: %.c core.h $(CC_BIN) $(CFLAGS_ALL) -c $< -o $@ .PHONY: all clean clean: rm -f core.o cli.o libcore.a lab_clistarter: starter/cli.c
learner@ksa:~/gl-e00/lab-m06$ bat starter/cli.c File: starter/cli.c #define _POSIX_C_SOURCE 200809L #include "core.h" #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static size_t synth_elf32(uint8_t *b, uint8_t cls, uint16_t mach, uint32_t eflags, const char *interp) { size_t ilen = strlen(interp) + 1U; memset(b, 0, 128U); memcpy(b, "\x7f" "ELF", 4U); b[4] = cls; b[5] = 1U; b[0x10] = 2U; b[0x12] = (uint8_t)(mach & 0xFFU); b[0x13] = (uint8_t)(mach >> 8U); b[0x1C] = 52U; b[0x24] = (uint8_t)(eflags & 0xFFU); b[0x25] = (uint8_t)((eflags >> 8U) & 0xFFU); b[0x26] = (uint8_t)((eflags >> 16U) & 0xFFU); b[0x27] = (uint8_t)((eflags >> 24U) & 0xFFU); b[0x28] = 52U; b[0x2A] = 32U; b[0x2C] = 1U; b[52] = 3U; /* p_type = PT_INTERP */ b[52 + 4] = 84U; /* p_offset = 84 */ b[52 + 16] = (uint8_t)ilen; b[52 + 20] = (uint8_t)ilen; memcpy(b + 84U, interp, ilen); return 84U + ilen; } static int cmp_u64(const void *a, const void *b) { uint64_t va = *(const uint64_t *)a; uint64_t vb = *(const uint64_t *)b; return (va > vb) - (va < vb); } static int run_bench(void) { uint8_t zimg[64] = {0}, dtb[64] = {0}, elf_ok[128]; size_t elen = synth_elf32(elf_ok, 1U, 40U, 0x05000400U, "/lib/ld-linux-armhf.so.3"); static const char ko_ok[] = "license=GPL\0vermagic=6.12.0-ksa SMP preempt mod_unload ARMv7 p2v8 \0"; uint64_t samples[40]; volatile uint32_t sink = 0U; zimg[0x24] = 0x18U; zimg[0x25] = 0x28U; zimg[0x26] = 0x6FU; zimg[0x27] = 0x01U; zimg[0x2E] = 0x40U; dtb[0] = 0xD0U; dtb[1] = 0x0DU; dtb[2] = 0xFEU; dtb[3] = 0xEDU; dtb[7] = 64U; for (int w = 0; w < 5; w++) { sink ^= (uint32_t)preflight_boot_blobs(zimg, sizeof(zimg), dtb, sizeof(dtb)); sink ^= (uint32_t)preflight_elf32_armhf(elf_ok, elen, "/lib/ld-linux-armhf.so.3"); sink ^= (uint32_t)preflight_ko_modinfo(ko_ok, sizeof(ko_ok) - 1U, "6.12.0-ksa"); } for (int s = 0; s < 40; s++) { struct timespec t0, t1; clock_gettime(CLOCK_MONOTONIC, &t0); for (int iter = 0; iter < 500; iter++) { sink ^= (uint32_t)preflight_boot_blobs(zimg, sizeof(zimg), dtb, sizeof(dtb)); sink ^= (uint32_t)preflight_elf32_armhf(elf_ok, elen, "/lib/ld-linux-armhf.so.3"); sink ^= (uint32_t)preflight_ko_modinfo(ko_ok, sizeof(ko_ok) - 1U, "6.12.0-ksa"); } clock_gettime(CLOCK_MONOTONIC, &t1); samples[s] = (uint64_t)(t1.tv_sec - t0.tv_sec) * 1000000000ULL + (uint64_t)(t1.tv_nsec - t0.tv_nsec); } qsort(samples, 40U, sizeof(samples[0]), cmp_u64); printf("BENCH lab_m06_workbench_preflight_capstone clock=CLOCK_MONOTONIC batches=40x500 " "p50_ns=%llu p95_ns=%llu p99_ns=%llu sink=%u\n", (unsigned long long)samples[20], (unsigned long long)samples[38], (unsigned long long)samples[39], sink); return 0; } int main(int argc, char **argv) { if (argc > 1 && strcmp(argv[1], "--bench") == 0) return run_bench(); uint8_t zimg[64] = {0}, dtb[64] = {0}, elf_hf[128], elf_soft[128]; size_t n_hf = synth_elf32(elf_hf, 1U, 40U, 0x05000400U, "/lib/ld-linux-armhf.so.3"); size_t n_sf = synth_elf32(elf_soft, 1U, 40U, 0x05000200U, "/lib/ld-linux.so.3"); static const char ko_ok[] = "license=GPL\0vermagic=6.12.0-ksa SMP preempt mod_unload ARMv7 p2v8 \0"; static const char ko_dirty[] = "license=GPL\0vermagic=6.12.0-ksa-dirty SMP preempt mod_unload ARMv7 p2v8 \0"; zimg[0x24] = 0x18U; zimg[0x25] = 0x28U; zimg[0x26] = 0x6FU; zimg[0x27] = 0x01U; zimg[0x2E] = 0x40U; dtb[0] = 0xD0U; dtb[1] = 0x0DU; dtb[2] = 0xFEU; dtb[3] = 0xEDU; dtb[7] = 64U; printf("CAPSTONE_BLOBS status=%d\n", (int)preflight_boot_blobs(zimg, sizeof(zimg), dtb, sizeof(dtb))); printf("CAPSTONE_ELF armhf_ok=%d softfloat_err=%d missing_interp_err=%d\n", (int)preflight_elf32_armhf(elf_hf, n_hf, "/lib/ld-linux-armhf.so.3"), (int)preflight_elf32_armhf(elf_soft, n_sf, "/lib/ld-linux-armhf.so.3"), (int)preflight_elf32_armhf(elf_hf, n_hf, NULL)); printf("CAPSTONE_KO clean_ok=%d dirty_err=%d\n", (int)preflight_ko_modinfo(ko_ok, sizeof(ko_ok) - 1U, "6.12.0-ksa"), (int)preflight_ko_modinfo(ko_dirty, sizeof(ko_dirty) - 1U, "6.12.0-ksa")); return 0; }starter: starter/core.c
learner@ksa:~/gl-e00/lab-m06$ bat starter/core.c File: starter/core.c #include "core.h" #include <string.h> static uint32_t rd_le16(const uint8_t *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8U); } static uint32_t rd_le32(const uint8_t *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8U) | ((uint32_t)p[2] << 16U) | ((uint32_t)p[3] << 24U); } static uint32_t rd_be32(const uint8_t *p) { return ((uint32_t)p[0] << 24U) | ((uint32_t)p[1] << 16U) | ((uint32_t)p[2] << 8U) | (uint32_t)p[3]; } enum preflight_status preflight_boot_blobs(const uint8_t *zimage, size_t zlen, const uint8_t *dtb, size_t dlen) { uint32_t z_start, z_end, fdt_size; if (!zimage || !dtb || zlen < 0x30U || dlen < 40U) return PREFLIGHT_ERR_TRUNCATED; if (rd_le32(zimage + 0x24U) != 0x016F2818U) return PREFLIGHT_ERR_BAD_MAGIC; z_start = rd_le32(zimage + 0x28U); z_end = rd_le32(zimage + 0x2CU); if (z_end <= z_start) return PREFLIGHT_ERR_TRUNCATED; if (rd_be32(dtb) != 0xD00DFEEDU) return PREFLIGHT_ERR_BAD_MAGIC; fdt_size = rd_be32(dtb + 4U); if (fdt_size < 40U || fdt_size > dlen) return PREFLIGHT_ERR_TRUNCATED; return PREFLIGHT_OK; } enum preflight_status preflight_elf32_armhf(const uint8_t *elf, size_t len, const char *rootfs_available_interp) { uint32_t phoff, eflags; uint16_t phentsize, phnum; bool found_interp = false; if (!elf || len < 52U) return PREFLIGHT_ERR_TRUNCATED; if (memcmp(elf, "\x7f" "ELF", 4U) != 0) return PREFLIGHT_ERR_BAD_MAGIC; if (elf[4] != 1U || elf[5] != 1U || rd_le16(elf + 0x12U) != 40U) return PREFLIGHT_ERR_WRONG_ARCH; eflags = rd_le32(elf + 0x24U); if ((eflags & 0x00000600U) == 0U) /* BUG: accepts soft-float 0x200 as valid armhf! */ return PREFLIGHT_ERR_FLOAT_ABI; phoff = rd_le32(elf + 0x1CU); phentsize = (uint16_t)rd_le16(elf + 0x2AU); phnum = (uint16_t)rd_le16(elf + 0x2CU); if (phnum > 0U && phentsize < 32U) return PREFLIGHT_ERR_TRUNCATED; for (uint16_t i = 0U; i < phnum; i++) { size_t off = (size_t)phoff + (size_t)i * (size_t)phentsize; if (off + 32U > len) return PREFLIGHT_ERR_TRUNCATED; if (rd_le32(elf + off) == 3U) { /* PT_INTERP */ uint32_t ioff = rd_le32(elf + off + 4U); uint32_t isz = rd_le32(elf + off + 16U); if (isz == 0U || (size_t)ioff + (size_t)isz > len) return PREFLIGHT_ERR_TRUNCATED; if (memchr(elf + ioff, '\0', isz) == NULL) return PREFLIGHT_ERR_TRUNCATED; found_interp = true; (void)rootfs_available_interp; /* BUG: skips PT_INTERP presence check in staged rootfs! */ } } return found_interp ? PREFLIGHT_OK : PREFLIGHT_ERR_MISSING_INTERP; } static const char *modinfo_lookup(const char *blob, size_t len, const char *key) { size_t klen = strlen(key); size_t pos = 0U; while (pos < len) { const char *ent = blob + pos; const char *nul = memchr(ent, '\0', len - pos); if (!nul) break; if ((size_t)(nul - ent) > klen && strncmp(ent, key, klen) == 0 && ent[klen] == '=') return ent + klen + 1U; pos = (size_t)(nul - blob) + 1U; } return NULL; } enum preflight_status preflight_ko_modinfo(const char *modinfo, size_t len, const char *expected_release) { const char *lic, *vm; size_t rlen; if (!modinfo || !expected_release || len == 0U) return PREFLIGHT_ERR_TRUNCATED; lic = modinfo_lookup(modinfo, len, "license"); if (!lic || strncmp(lic, "GPL", 3U) != 0) return PREFLIGHT_ERR_LICENSE; vm = modinfo_lookup(modinfo, len, "vermagic"); rlen = strlen(expected_release); if (!vm || strncmp(vm, expected_release, rlen) != 0) /* BUG: allows 6.12.0-ksa-dirty prefix! */ return PREFLIGHT_ERR_VERMAGIC_RELEASE; if (strstr(vm, "ARMv7") == NULL || strstr(vm, "p2v8") == NULL) return PREFLIGHT_ERR_VERMAGIC_ARCH; return PREFLIGHT_OK; }starter: starter/core.h
learner@ksa:~/gl-e00/lab-m06$ bat starter/core.h File: starter/core.h #ifndef KSA_LAB_M06_WORKBENCH_PREFLIGHT_CAPSTONE_H #define KSA_LAB_M06_WORKBENCH_PREFLIGHT_CAPSTONE_H #include <stdbool.h> #include <stddef.h> #include <stdint.h> enum preflight_status { PREFLIGHT_OK = 0, PREFLIGHT_ERR_TRUNCATED, PREFLIGHT_ERR_BAD_MAGIC, PREFLIGHT_ERR_WRONG_ARCH, PREFLIGHT_ERR_FLOAT_ABI, PREFLIGHT_ERR_MISSING_INTERP, PREFLIGHT_ERR_VERMAGIC_RELEASE, PREFLIGHT_ERR_VERMAGIC_ARCH, PREFLIGHT_ERR_LICENSE, }; enum preflight_status preflight_boot_blobs(const uint8_t *zimage, size_t zlen, const uint8_t *dtb, size_t dlen); enum preflight_status preflight_elf32_armhf(const uint8_t *elf, size_t len, const char *rootfs_available_interp); enum preflight_status preflight_ko_modinfo(const char *modinfo, size_t len, const char *expected_release); #endif /* KSA_LAB_M06_WORKBENCH_PREFLIGHT_CAPSTONE_H */step 2/15 · 2. learning objectives
- Verify
ARMzImage(0x016F2818LE) and FlattenedDevice Tree.dtb(0xD00DFEEDBE +totalsize) headers safely inpreflight_boot_blobs(). - Audit
52-byteElf32_Ehdr+Elf32_Phdrbounds,EM_ARM (40),EF_ARM_ABI_FLOAT_HARD (0x400), andPT_INTERProotfs resolution inpreflight_elf32_armhf(). - Enforce
GPLmodule licensing,ARMv7 p2v8architecture flags, and exact token-boundedUTS_RELEASEmatching inpreflight_ko_modinfo().
step 3/15 · 3. environment requirements
All automated verification steps run in unprivileged user space on a POSIX Linux host using gcc -std=c11 -Wall -Wextra -Werror and clock_gettime(CLOCK_MONOTONIC). Recorded transcripts in captures.json were generated on x86_64 running kernel 7.0.0-42-generic and prove protocol, state-machine, and memory invariants only. Physical bus timing, DMA cache coherency, and board peripheral behavior require target validation on Linux kernel 6.12 LTS (ARMv7-A Cortex-A7).
step 4/15 · 4. required software and hardware
- Host workstation:
gcc(-std=c11 -Wall -Wextra -Werror), GNUmake,ar(binutils), and POSIX/bin/sh. - Target environment (optional hardware step, Section 13): Complete
GL-E00Linux host/srv/tftp+/srv/nfs/rootfsbench servingi.MX6ULLARMv7-AU-Boot2025.x andLinux kernel6.12.
step 5/15 · 5. complete executable commands
learner@ksa:~/gl-e00/lab-m06$ bat script.sh File: script.sh make -B -C files ./files/lab_cli ./files/lab_cli --bench sh check.shstep 6/15 · 6. annotated source code
files/core.c enforces the production invariants that starter/core.c breaks:
learner@ksa:~/gl-e00/lab-m06$ bat example.c File: example.c eflags = rd_le32(elf + 0x24U); if ((eflags & 0x00000400U) == 0U || (eflags & 0x00000200U) != 0U) return PREFLIGHT_ERR_FLOAT_ABI; /* ... */ if (!vm || strncmp(vm, expected_release, rlen) != 0 || vm[rlen] != ' ') return PREFLIGHT_ERR_VERMAGIC_RELEASE;starter/core.c masks eflags with 0x600 (accepting soft-float 0x200 binaries!), ignores rootfs_available_interp when checking PT_INTERP, and omits vm[rlen] != ' ' (allowing 6.12.0-ksa-dirty modules to slip past 6.12.0-ksa!).
step 7/15 · 7. expected output
Passing files/ run (sh check.sh, exit code 0):
learner@ksa:~/gl-e00/lab-m06$ bat listing.txt File: listing.txt TAP version 13 1..5 ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror ok 2 - archives core.o into libcore.a and links independent TAP verifier ok 3 - validates little-endian ARM zImage (0x016F2818 @ 0x24) and big-endian FDT .dtb (0xD00DFEED @ 0x00) ok 4 - rejects x86_64 ELFs, soft-float 0x200 binaries, and missing rootfs PT_INTERP dynamic linkers ok 5 - enforces GPL license, ARMv7 p2v8 tokens, and exact token-bounded vermagic release (rejecting -dirty) # pass 5/5CLI verification output (./files/lab_cli):
learner@ksa:~/gl-e00/lab-m06$ bat listing.txt File: listing.txt CAPSTONE_BLOBS status=0 CAPSTONE_ELF armhf_ok=0 softfloat_err=4 missing_interp_err=5 CAPSTONE_KO clean_ok=0 dirty_err=6Defective starter/ run (exit code 1): emits FAIL: diagnostics on the broken contract assertions.
step 8/15 · 8. explanation of observed behavior
When all three checks are tightened, preflight_elf32_armhf() guarantees VFP hard-float register ABI compatibility and /lib/ld-linux-armhf.so.3 presence, preflight_ko_modinfo() blocks -dirty and non-ARMv7/p2v8 modules prior to finit_module(), and preflight_boot_blobs() guards bootz against malformed kernel/DTB files.
step 9/15 · 9. experiment variations
- Near transfer: Extend
preflight_elf32_armhf()to accept static binaries (ET_EXECwith zeroPT_INTERPheaders when-staticis explicitly allowed via flag). - Far transfer: Walk
/srv/nfs/rootfsrecursively with POSIXnftw()to audit every executable and.kofile in a single host CLI command.
step 10/15 · 10. performance measurements
Recorded host benchmark (./files/lab_cli --bench, CLOCK_MONOTONIC, warmup=5, N=40 batches):
learner@ksa:~/gl-e00/lab-m06$ bat listing.txt File: listing.txt BENCH lab_m06_workbench_preflight_capstone clock=CLOCK_MONOTONIC batches=40x500 p50_ns=37127 p95_ns=38508 p99_ns=41995 sink=0./files/lab_cli --bench records CLOCK_MONOTONIC host execution time across 40 x 500 complete 3-stage preflight sweeps (zImage/.dtb + ELF32 + .modinfo), demonstrating sub-millisecond CI/Makefile overhead on the host workstation.
step 11/15 · 11. troubleshooting
| Symptom | Root cause | Fix |
|---|---|---|
Check 4 fails on e_sf (soft-float not rejected) | 0x05000200 & 0x600 is non-zero, letting EF_ARM_ABI_FLOAT_SOFT pass. | Require (eflags & 0x400U) != 0U && (eflags & 0x200U) == 0U. |
Check 4 fails when rootfs_available_interp is NULL | PT_INTERP string was never compared against rootfs_available_interp. | Return PREFLIGHT_ERR_MISSING_INTERP if !rootfs_available_interp or strcmp != 0. |
Check 5 fails on ko_dirty | strncmp(vm, "6.12.0-ksa", 10) returns 0 on "6.12.0-ksa-dirty ...". | Require vm[rlen] == ' ' immediately after the expected release string. |
step 12/15 · 12. guided questions
- Why does
preflight_elf32_armhf()verifymemchr(elf + ioff, '\0', isz) != NULLBEFORE callingstrcmp((const char *)(elf + ioff), ...)?
- *Hint 1:* What if a corrupted or fuzz-crafted
ELFbinary hasPT_INTERPp_filesz = 24filled with non-zero bytes up to the end ofelf? - *Hint 2:*
strcmpscans until it hits'\0';memchr(..., '\0', isz)proves a NUL terminator exists inside the bounds-checked[ioff, ioff + isz)slice first!
- Why do
rd_le32()andrd_be32()assembleuint32_tvalues fromuint8_tbyte shifts instead of casting*(const uint32_t *)(buf + offset)?
- *Hint 1:* What does ISO C11 say when
buf + offsetis not4-byte aligned or aliases acharbuffer? - *Hint 2:* Direct
uint32_t *casts risk alignment faults/Undefined Behavior and silently depend on host CPU endianness, whereas byte shifts are 100% portable C11.
step 13/15 · 13. independent challenges
Extend preflight_ko_modinfo() to parse depends=<mod1>,<mod2> from .modinfo and verify against a caller-supplied array of staged .ko module names that every prerequisite module is present under /srv/nfs/rootfs/lib/modules/<release>/ before depmod -a runs.
step 14/15 · 14. solutions and explanations
The reference files/core.c combines endian-explicit zImage/FDT header checks, bounds-safe ELF32 armhf (0x400) + PT_INTERP verification, and token-bounded .modinfo vermagic/GPL validation to pass 5/5 TAP checks cleanly.
step 15/15 · run verification and inspect output
Run the verification command and confirm the output matches the expected contract.
learner@ksa:~/gl-e00/lab-m06./files/lab_clilearner@ksa:~/gl-e00/lab-m06CAPSTONE_BLOBS status=0CAPSTONE_ELF armhf_ok=0 softfloat_err=4 missing_interp_err=5CAPSTONE_KO clean_ok=0 dirty_err=6Progressive hints & solution
hint 1
Compare starter/Makefile against the module lessons. Focus first on: Builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror.
hint 2
Compile with -std=c11 -Wall -Wextra -Werror, check every system-call return value before touching output buffers, and verify cleanup on error paths.
solution
warning: Reveal the reference solution files? Running ./check.sh against your own attempt first builds stronger retention.
solution: files/Makefile
learner@ksa:~/gl-e00/lab-m06$ bat files/Makefile File: files/Makefile CC_BIN = gcc AR = ar CFLAGS_ALL = -std=c11 -Wall -Wextra -Werror -O2 LDLIBS = all: libcore.a lab_cli libcore.a: core.o $(AR) rcs $@ $^ lab_cli: cli.o libcore.a $(CC_BIN) $(CFLAGS_ALL) cli.o libcore.a $(LDLIBS) -o $@ %.o: %.c core.h $(CC_BIN) $(CFLAGS_ALL) -c $< -o $@ .PHONY: all clean clean: rm -f core.o cli.o libcore.a lab_clisolution: files/cli.c
learner@ksa:~/gl-e00/lab-m06$ bat files/cli.c File: files/cli.c #define _POSIX_C_SOURCE 200809L #include "core.h" #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static size_t synth_elf32(uint8_t *b, uint8_t cls, uint16_t mach, uint32_t eflags, const char *interp) { size_t ilen = strlen(interp) + 1U; memset(b, 0, 128U); memcpy(b, "\x7f" "ELF", 4U); b[4] = cls; b[5] = 1U; b[0x10] = 2U; b[0x12] = (uint8_t)(mach & 0xFFU); b[0x13] = (uint8_t)(mach >> 8U); b[0x1C] = 52U; b[0x24] = (uint8_t)(eflags & 0xFFU); b[0x25] = (uint8_t)((eflags >> 8U) & 0xFFU); b[0x26] = (uint8_t)((eflags >> 16U) & 0xFFU); b[0x27] = (uint8_t)((eflags >> 24U) & 0xFFU); b[0x28] = 52U; b[0x2A] = 32U; b[0x2C] = 1U; b[52] = 3U; /* p_type = PT_INTERP */ b[52 + 4] = 84U; /* p_offset = 84 */ b[52 + 16] = (uint8_t)ilen; b[52 + 20] = (uint8_t)ilen; memcpy(b + 84U, interp, ilen); return 84U + ilen; } static int cmp_u64(const void *a, const void *b) { uint64_t va = *(const uint64_t *)a; uint64_t vb = *(const uint64_t *)b; return (va > vb) - (va < vb); } static int run_bench(void) { uint8_t zimg[64] = {0}, dtb[64] = {0}, elf_ok[128]; size_t elen = synth_elf32(elf_ok, 1U, 40U, 0x05000400U, "/lib/ld-linux-armhf.so.3"); static const char ko_ok[] = "license=GPL\0vermagic=6.12.0-ksa SMP preempt mod_unload ARMv7 p2v8 \0"; uint64_t samples[40]; volatile uint32_t sink = 0U; zimg[0x24] = 0x18U; zimg[0x25] = 0x28U; zimg[0x26] = 0x6FU; zimg[0x27] = 0x01U; zimg[0x2E] = 0x40U; dtb[0] = 0xD0U; dtb[1] = 0x0DU; dtb[2] = 0xFEU; dtb[3] = 0xEDU; dtb[7] = 64U; for (int w = 0; w < 5; w++) { sink ^= (uint32_t)preflight_boot_blobs(zimg, sizeof(zimg), dtb, sizeof(dtb)); sink ^= (uint32_t)preflight_elf32_armhf(elf_ok, elen, "/lib/ld-linux-armhf.so.3"); sink ^= (uint32_t)preflight_ko_modinfo(ko_ok, sizeof(ko_ok) - 1U, "6.12.0-ksa"); } for (int s = 0; s < 40; s++) { struct timespec t0, t1; clock_gettime(CLOCK_MONOTONIC, &t0); for (int iter = 0; iter < 500; iter++) { sink ^= (uint32_t)preflight_boot_blobs(zimg, sizeof(zimg), dtb, sizeof(dtb)); sink ^= (uint32_t)preflight_elf32_armhf(elf_ok, elen, "/lib/ld-linux-armhf.so.3"); sink ^= (uint32_t)preflight_ko_modinfo(ko_ok, sizeof(ko_ok) - 1U, "6.12.0-ksa"); } clock_gettime(CLOCK_MONOTONIC, &t1); samples[s] = (uint64_t)(t1.tv_sec - t0.tv_sec) * 1000000000ULL + (uint64_t)(t1.tv_nsec - t0.tv_nsec); } qsort(samples, 40U, sizeof(samples[0]), cmp_u64); printf("BENCH lab_m06_workbench_preflight_capstone clock=CLOCK_MONOTONIC batches=40x500 " "p50_ns=%llu p95_ns=%llu p99_ns=%llu sink=%u\n", (unsigned long long)samples[20], (unsigned long long)samples[38], (unsigned long long)samples[39], sink); return 0; } int main(int argc, char **argv) { if (argc > 1 && strcmp(argv[1], "--bench") == 0) return run_bench(); uint8_t zimg[64] = {0}, dtb[64] = {0}, elf_hf[128], elf_soft[128]; size_t n_hf = synth_elf32(elf_hf, 1U, 40U, 0x05000400U, "/lib/ld-linux-armhf.so.3"); size_t n_sf = synth_elf32(elf_soft, 1U, 40U, 0x05000200U, "/lib/ld-linux.so.3"); static const char ko_ok[] = "license=GPL\0vermagic=6.12.0-ksa SMP preempt mod_unload ARMv7 p2v8 \0"; static const char ko_dirty[] = "license=GPL\0vermagic=6.12.0-ksa-dirty SMP preempt mod_unload ARMv7 p2v8 \0"; zimg[0x24] = 0x18U; zimg[0x25] = 0x28U; zimg[0x26] = 0x6FU; zimg[0x27] = 0x01U; zimg[0x2E] = 0x40U; dtb[0] = 0xD0U; dtb[1] = 0x0DU; dtb[2] = 0xFEU; dtb[3] = 0xEDU; dtb[7] = 64U; printf("CAPSTONE_BLOBS status=%d\n", (int)preflight_boot_blobs(zimg, sizeof(zimg), dtb, sizeof(dtb))); printf("CAPSTONE_ELF armhf_ok=%d softfloat_err=%d missing_interp_err=%d\n", (int)preflight_elf32_armhf(elf_hf, n_hf, "/lib/ld-linux-armhf.so.3"), (int)preflight_elf32_armhf(elf_soft, n_sf, "/lib/ld-linux-armhf.so.3"), (int)preflight_elf32_armhf(elf_hf, n_hf, NULL)); printf("CAPSTONE_KO clean_ok=%d dirty_err=%d\n", (int)preflight_ko_modinfo(ko_ok, sizeof(ko_ok) - 1U, "6.12.0-ksa"), (int)preflight_ko_modinfo(ko_dirty, sizeof(ko_dirty) - 1U, "6.12.0-ksa")); return 0; }solution: files/core.c
learner@ksa:~/gl-e00/lab-m06$ bat files/core.c File: files/core.c #include "core.h" #include <string.h> static uint32_t rd_le16(const uint8_t *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8U); } static uint32_t rd_le32(const uint8_t *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8U) | ((uint32_t)p[2] << 16U) | ((uint32_t)p[3] << 24U); } static uint32_t rd_be32(const uint8_t *p) { return ((uint32_t)p[0] << 24U) | ((uint32_t)p[1] << 16U) | ((uint32_t)p[2] << 8U) | (uint32_t)p[3]; } enum preflight_status preflight_boot_blobs(const uint8_t *zimage, size_t zlen, const uint8_t *dtb, size_t dlen) { uint32_t z_start, z_end, fdt_size; if (!zimage || !dtb || zlen < 0x30U || dlen < 40U) return PREFLIGHT_ERR_TRUNCATED; if (rd_le32(zimage + 0x24U) != 0x016F2818U) return PREFLIGHT_ERR_BAD_MAGIC; z_start = rd_le32(zimage + 0x28U); z_end = rd_le32(zimage + 0x2CU); if (z_end <= z_start) return PREFLIGHT_ERR_TRUNCATED; if (rd_be32(dtb) != 0xD00DFEEDU) return PREFLIGHT_ERR_BAD_MAGIC; fdt_size = rd_be32(dtb + 4U); if (fdt_size < 40U || fdt_size > dlen) return PREFLIGHT_ERR_TRUNCATED; return PREFLIGHT_OK; } enum preflight_status preflight_elf32_armhf(const uint8_t *elf, size_t len, const char *rootfs_available_interp) { uint32_t phoff, eflags; uint16_t phentsize, phnum; bool found_interp = false; if (!elf || len < 52U) return PREFLIGHT_ERR_TRUNCATED; if (memcmp(elf, "\x7f" "ELF", 4U) != 0) return PREFLIGHT_ERR_BAD_MAGIC; if (elf[4] != 1U || elf[5] != 1U || rd_le16(elf + 0x12U) != 40U) return PREFLIGHT_ERR_WRONG_ARCH; eflags = rd_le32(elf + 0x24U); if ((eflags & 0x00000400U) == 0U || (eflags & 0x00000200U) != 0U) return PREFLIGHT_ERR_FLOAT_ABI; phoff = rd_le32(elf + 0x1CU); phentsize = (uint16_t)rd_le16(elf + 0x2AU); phnum = (uint16_t)rd_le16(elf + 0x2CU); if (phnum > 0U && phentsize < 32U) return PREFLIGHT_ERR_TRUNCATED; for (uint16_t i = 0U; i < phnum; i++) { size_t off = (size_t)phoff + (size_t)i * (size_t)phentsize; if (off + 32U > len) return PREFLIGHT_ERR_TRUNCATED; if (rd_le32(elf + off) == 3U) { /* PT_INTERP */ uint32_t ioff = rd_le32(elf + off + 4U); uint32_t isz = rd_le32(elf + off + 16U); if (isz == 0U || (size_t)ioff + (size_t)isz > len) return PREFLIGHT_ERR_TRUNCATED; if (memchr(elf + ioff, '\0', isz) == NULL) return PREFLIGHT_ERR_TRUNCATED; found_interp = true; if (!rootfs_available_interp || strcmp((const char *)(elf + ioff), rootfs_available_interp) != 0) return PREFLIGHT_ERR_MISSING_INTERP; } } return found_interp ? PREFLIGHT_OK : PREFLIGHT_ERR_MISSING_INTERP; } static const char *modinfo_lookup(const char *blob, size_t len, const char *key) { size_t klen = strlen(key); size_t pos = 0U; while (pos < len) { const char *ent = blob + pos; const char *nul = memchr(ent, '\0', len - pos); if (!nul) break; if ((size_t)(nul - ent) > klen && strncmp(ent, key, klen) == 0 && ent[klen] == '=') return ent + klen + 1U; pos = (size_t)(nul - blob) + 1U; } return NULL; } enum preflight_status preflight_ko_modinfo(const char *modinfo, size_t len, const char *expected_release) { const char *lic, *vm; size_t rlen; if (!modinfo || !expected_release || len == 0U) return PREFLIGHT_ERR_TRUNCATED; lic = modinfo_lookup(modinfo, len, "license"); if (!lic || strncmp(lic, "GPL", 3U) != 0) return PREFLIGHT_ERR_LICENSE; vm = modinfo_lookup(modinfo, len, "vermagic"); rlen = strlen(expected_release); if (!vm || strncmp(vm, expected_release, rlen) != 0 || vm[rlen] != ' ') return PREFLIGHT_ERR_VERMAGIC_RELEASE; if (strstr(vm, "ARMv7") == NULL || strstr(vm, "p2v8") == NULL) return PREFLIGHT_ERR_VERMAGIC_ARCH; return PREFLIGHT_OK; }solution: files/core.h
learner@ksa:~/gl-e00/lab-m06$ bat files/core.h File: files/core.h #ifndef KSA_LAB_M06_WORKBENCH_PREFLIGHT_CAPSTONE_H #define KSA_LAB_M06_WORKBENCH_PREFLIGHT_CAPSTONE_H #include <stdbool.h> #include <stddef.h> #include <stdint.h> enum preflight_status { PREFLIGHT_OK = 0, PREFLIGHT_ERR_TRUNCATED, PREFLIGHT_ERR_BAD_MAGIC, PREFLIGHT_ERR_WRONG_ARCH, PREFLIGHT_ERR_FLOAT_ABI, PREFLIGHT_ERR_MISSING_INTERP, PREFLIGHT_ERR_VERMAGIC_RELEASE, PREFLIGHT_ERR_VERMAGIC_ARCH, PREFLIGHT_ERR_LICENSE, }; enum preflight_status preflight_boot_blobs(const uint8_t *zimage, size_t zlen, const uint8_t *dtb, size_t dlen); enum preflight_status preflight_elf32_armhf(const uint8_t *elf, size_t len, const char *rootfs_available_interp); enum preflight_status preflight_ko_modinfo(const char *modinfo, size_t len, const char *expected_release); #endif /* KSA_LAB_M06_WORKBENCH_PREFLIGHT_CAPSTONE_H */Run the check
From labs/lab_m06_workbench_preflight_capstone/, run ./check.sh. A complete solution emits TAP version 13 with every test point marked ok and exits 0:
learner@ksa:~/gl-e00/lab-m06./check.shTAP version 131..5ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werrorok 2 - archives core.o into libcore.a and links independent TAP verifierok 3 - validates little-endian ARM zImage (0x016F2818 @ 0x24) and big-endian FDT .dtb (0xD00DFEED @ 0x00)ok 4 - rejects x86_64 ELFs, soft-float 0x200 binaries, and missing rootfs PT_INTERP dynamic linkersok 5 - enforces GPL license, ARMv7 p2v8 tokens, and exact token-bounded vermagic release (rejecting -dirty)# pass 5/5On an incomplete starter tree, ./check.sh exits non-zero and marks the unmet assertion not ok with an indented YAML diagnostic block:
learner@ksa:~/gl-e00/lab-m06./check.shTAP version 131..5ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werrorok 2 - archives core.o into libcore.a and links independent TAP verifierok 3 - validates little-endian ARM zImage (0x016F2818 @ 0x24) and big-endian FDT .dtb (0xD00DFEED @ 0x00)not ok 4 - rejects x86_64 ELFs, soft-float 0x200 binaries, and missing rootfs PT_INTERP dynamic linkersFAIL: require EF_ARM_ABI_FLOAT_HARD (0x400) without 0x200 and verify PT_INTERP == rootfs_available_interp --- message: 'got ELF32 armhf / PT_INTERP preflight mismatch' hint: 'require EF_ARM_ABI_FLOAT_HARD (0x400) without 0x200 and verify PT_INTERP == rootfs_available_interp' ...not ok 5 - enforces GPL license, ARMv7 p2v8 tokens, and exact token-bounded vermagic release (rejecting -dirty)FAIL: check vm[rlen] == space after strncmp(vm, expected_release, rlen) in preflight_ko_modinfo --- message: 'got .ko modinfo vermagic/license preflight mismatch' hint: 'check vm[rlen] == space after strncmp(vm, expected_release, rlen) in preflight_ko_modinfo' ...# pass 3/5note: How to read TAP diagnostics: 1..N states the total test plan; each ok k confirms a passed check; not ok k pinpoints the failing check alongside message: (what went wrong) and hint: (where to look next) between --- and ....
inspect harness: check.sh
learner@ksa:~/gl-e00/lab-m06$ bat check.sh File: check.sh #!/bin/sh set -u cd "$(dirname "$0")" || exit 1 PASS=0 WORK=$(mktemp -d "${TMPDIR:-/tmp}/ksa_lab.XXXXXX") || exit 1 trap 'rm -rf "$WORK"' EXIT HUP INT TERM cp -R files "$WORK/files" printf 'TAP version 13\n1..5\n' if make -B -C "$WORK/files" >"$WORK/build.log" 2>&1; then printf 'ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror\n' PASS=$((PASS + 1)) else printf 'not ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror\n' printf 'FAIL: fix compiler warnings or errors in files/core.c\n' fi if [ -f "$WORK/files/libcore.a" ] && gcc -std=c11 -Wall -Wextra -Werror -O2 -I"$WORK/files" tests.c "$WORK/files/libcore.a" -o "$WORK/verify" >>"$WORK/build.log" 2>&1; then printf 'ok 2 - archives core.o into libcore.a and links independent TAP verifier\n' PASS=$((PASS + 1)) else printf 'not ok 2 - archives core.o into libcore.a and links independent TAP verifier\n' printf 'FAIL: preserve public declarations in files/core.h so tests.c links\n' fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 1 2>&1); then printf 'ok 3 - validates little-endian ARM zImage (0x016F2818 @ 0x24) and big-endian FDT .dtb (0xD00DFEED @ 0x00)\n' PASS=$((PASS + 1)) else printf 'not ok 3 - validates little-endian ARM zImage (0x016F2818 @ 0x24) and big-endian FDT .dtb (0xD00DFEED @ 0x00)\n' printf 'FAIL: verify LE 0x016F2818 at zimage+0x24 and BE 0xD00DFEED plus totalsize <= dlen in preflight_boot_blobs\n' printf " ---\n message: 'got %s'\n hint: 'verify LE 0x016F2818 at zimage+0x24 and BE 0xD00DFEED plus totalsize <= dlen in preflight_boot_blobs'\n ...\n" "${OUT:-build_failed}" fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 2 2>&1); then printf 'ok 4 - rejects x86_64 ELFs, soft-float 0x200 binaries, and missing rootfs PT_INTERP dynamic linkers\n' PASS=$((PASS + 1)) else printf 'not ok 4 - rejects x86_64 ELFs, soft-float 0x200 binaries, and missing rootfs PT_INTERP dynamic linkers\n' printf 'FAIL: require EF_ARM_ABI_FLOAT_HARD (0x400) without 0x200 and verify PT_INTERP == rootfs_available_interp\n' printf " ---\n message: 'got %s'\n hint: 'require EF_ARM_ABI_FLOAT_HARD (0x400) without 0x200 and verify PT_INTERP == rootfs_available_interp'\n ...\n" "${OUT:-build_failed}" fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 3 2>&1); then printf 'ok 5 - enforces GPL license, ARMv7 p2v8 tokens, and exact token-bounded vermagic release (rejecting -dirty)\n' PASS=$((PASS + 1)) else printf 'not ok 5 - enforces GPL license, ARMv7 p2v8 tokens, and exact token-bounded vermagic release (rejecting -dirty)\n' printf 'FAIL: check vm[rlen] == space after strncmp(vm, expected_release, rlen) in preflight_ko_modinfo\n' printf " ---\n message: 'got %s'\n hint: 'check vm[rlen] == space after strncmp(vm, expected_release, rlen) in preflight_ko_modinfo'\n ...\n" "${OUT:-build_failed}" fi printf '# pass %d/5\n' "$PASS" [ "$PASS" -eq 5 ]Definition of done
Verification checklist
status: 0/3 checks verified
Troubleshooting
Compiler fails under -Werror on sign conversion or unused args
Use exact POSIX return types (ssize_t for read()/write(), size_t for byte counts) rather than relaxing flags.
./check.sh reports missing files in files/
Invoke ./check.sh from inside labs/lab_m06_workbench_preflight_capstone/ so relative paths to files/ resolve directly.