"A lemonade stand inventory system lets you check stock levels by
index. Simple enough - what could go wrong with a number?" Source
included (vuln.c), 500 points, no decoys anywhere - the entire
challenge is one missing bound.
Hint
The bounds check only compares against one side of the array. Which side did it forget?
1static int chk(long v) { return v < N; }
Insight
chk only tests the upper bound. Nothing rejects a negative index -
and the index is a long while the backing array holds int, so
nothing about the types silently clamps it either. g.b[k] with
k = -1 is a perfectly legal expression as far as both the compiler and
this check are concerned.
The global layout makes that fatal:
1struct blob_t { int a; int b[N]; };
2struct blob_t g;
g.a sits at offset 0, g.b starts at offset 4 - so g.b[-1] and
g.a are the exact same four bytes. The menu's "Set stock level"
option writes g.b[k] = (int)v with no further validation. Setting
index -1 to value 1 therefore sets the admin flag directly:
2 / -1 / 1 # g.b[-1] = 1 -> g.a = 1
3 # admin panel -> reveal() -> flag
Option 3 takes the if (g.a) branch, prints Welcome, admin!, and
calls reveal(), which opens flag.txt and prints it straight back.
Note
Verified two ways before touching the live box: locally first - compile
vuln.c, drop a known test string in flag.txt, feed the same
2/-1/1/3 sequence, confirm the local build prints it back - which
proves the alias exists at all without risking anything on the real
instance. Then, on the live instance, reading stock index -1 back
after the write (option 1) confirms it reads 1, the exact value just
written through g.a - the aliasing really is real, not a lucky guess
at which offset to hit.
L3m0n{4cf7e3663e5de329d9f3c01b2f50b4fc}