cybersecurity · beginner · ~15 min · safe pentest lab

Is the canary intact?

Model the canary integrity check.

Challenge

Model the integrity check a function epilogue performs on its stack canary: the stored value must still equal the current one, or an overflow corrupted it.

Task

Implement int canary_intact(unsigned long stored, unsigned long current) that returns 1 if the two values are equal, 0 otherwise.

Input

  • stored: the canary value placed in the prologue.
  • current: the canary value read back in the epilogue.

Output

Returns 1 if stored == current (canary intact), 0 otherwise (smashed).

Example

canary_intact(0xdeadbeef, 0xdeadbeef)   ->   1
canary_intact(0xdeadbeef, 0x41414141)   ->   0   (overflow corrupted it)

Edge cases

  • Any difference between the two values means the canary was smashed.

Input format

The prologue's stored canary and the epilogue's current canary (unsigned long).

Output format

1 if stored == current; otherwise 0.

Constraints

Equality check; a mismatch signals a stack overflow.

Starter code

int canary_intact(unsigned long stored, unsigned long current) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.