Posts Tagged ‘nvram firmware hack’

PostHeaderIcon Minimal Firmware Injection: One Unused Vector, One Hook

Changing a behaviour usually means replacing an instruction. Adding a behaviour means putting new code somewhere and arranging for it to run, and that is a different problem with a different failure mode: the new code has to fit without moving anything the original depends on.

The technique that satisfies this is placement, and the smallest version of it changes two words of the original image.

Why placement is the constraint

An insertion moves code, and moving code breaks every relative branch that crosses the insertion point, every absolute address in a table, and every computed jump whose target was recorded. On a large image there may be thousands of such references, and fixing them is a project rather than a patch.

A redirection avoids moving code by replacing a call with a call to somewhere else. That is fine when there is a call to replace, and it costs one call site.

The third option is to find an entry point that already exists and is unused. A vector table entry that points at a handler which does nothing is exactly that, and occupying it costs one word.

Approach Words changed References to fix Risk
Insertion Many Many High
Call redirection One call site None Low
Unused vector One vector entry None Low
Unused handler body A few None Low

Where the new code goes

New code needs space that is not used and will not be used. Three regions qualify, and they differ in how confident the identification is.

The end of the used flash, after the last byte of the application and before the end of the array, is usually free. It is free because the linker did not need it, and it stays free unless the application writes configuration there.

An erased page in the middle of the image is free if the linker map shows a gap. These exist when a region is reserved for a future feature or when a bootloader and an application are laid out with padding between them.

Region Confidence it is free Risk
After the last used byte High A configuration record may live there
A linker gap High if documented Alignment padding may be assumed
An erased page inside the image Medium May be written at run time
External memory Depends Access path required

The second row requires the linker map, and without it the identification is a guess. A gap that looks like padding may be a region the application writes to at run time, and code placed there will be overwritten.

What the hook has to do

The injected code runs in a context that the developer did not design for, and three things have to be true before it does anything useful.

It has to save and restore the state it uses. An interrupt handler that clobbers a register the interrupted code was using produces a fault that appears at a random place, which is the hardest kind of fault to diagnose.

It has to return in the way the original vector’s handler would. A vector that expects a return from interrupt must get one; a vector that is a reset entry must not return at all.

It has to avoid the self-protection mechanisms that would notice it, which is why the placement analysis in self-protecting code precedes this work.

Requirement Consequence if wrong
Save and restore used registers Random faults after the hook runs
Correct return instruction Lock-up or a fault on return
No writes to protected regions Reset or erase reaction
Within the integrity-checked range? Either recompute or place outside

The fourth row is a design decision. If the injection is inside a region that a checksum covers, the checksum must be recomputed, which is the work described in patching IC code. If it is outside, which is possible when the injection lives in a page past the end of the checked region, no recomputation is needed and the injection is invisible to the check.

Choosing the vector

Not every unused vector is equally good. Three properties matter.

The vector has to be one the application does not enable. An interrupt that is never unmasked never fires, so occupying its vector is free — and it also means the hook never runs, which defeats the purpose unless the hook enables the interrupt itself.

The vector has to be one whose peripheral exists. A vector for a peripheral that is not present on the part may never fire regardless of the enable bit.

Vector property Good for injection Why
Unused and unmasked Occupying is free No interference
Unused, and the hook enables it Runs on demand A timer or a software interrupt
Unused but masked by the application Occupying is harmless No effect
Used by a library Occupying breaks it Conflict

The second row is the useful one. A software interrupt vector, or a timer that the application never starts, gives the hook a trigger that the attacker controls — a write to a register, a command on an interface, a specific sequence of inputs.

The two words that change

The minimal injection is two edits.

The first changes the vector table entry to point at the new code, with the correct bit set for the architecture. On a Cortex-M the Thumb bit must be set, and forgetting it produces a fault on the first instruction.

The second changes nothing in the original code and places the new handler at an address that is aligned to the architecture’s requirement — four bytes on most parts, eight on some.

Edit Location Size
Vector entry Offset in the vector table One word
Hook body Free space Tens to hundreds of bytes

Everything else is the hook’s own content, and its size is limited by the free space rather than by anything else.

Verification

The injection is verified in three steps, and the third is the one that catches the interesting errors.

The first is a static check: the new handler is disassembled in place and its return path is confirmed. The second is a check that the vector table’s other entries are unchanged, because an off-by-one in the table index redirects a different interrupt. The third is a run in which the hook is triggered and the rest of the device is exercised, because an injection that works but breaks a peripheral under load is an injection that fails in the field.

Step Catches
Disassemble the handler in place Wrong return, wrong alignment
Compare the whole vector table Off-by-one index
Trigger the hook under load Register clobbering, timing interference

Where this fits

Injection is the third of the three modification techniques, and it is the one to choose when the goal is to add rather than to change. The other two, substitution and redirection, are in patching IC code.

The space identification depends on the boundary analysis in finding the code, because “free space” is a conclusion from a region map rather than an observation. The verification that a device still meets its specification after an injection is the regression coverage described in attacking MCU firmware.

What we deliver is the modified image, a listing of every byte changed with its address, a disassembly of the injected code, and the results of the three verification steps. What we do not deliver is an injection without a documented trigger, because a hook that nobody can invoke is a byte change with no purpose.