Posts Tagged ‘recover chip archive’

PostHeaderIcon Recover Chip PIC12C509A Firmware: A Stack Two Levels Deep Is a Test, Not a Limitation

Every architecture has one number that constrains how code is written and, read backwards, constrains how a recovered image can be checked. On the PIC12C509A that number is two. The hardware stack holds two return addresses, a call from inside a called routine is legal but a call from inside that one is not, and the compiler or the assembly author who wrote the firmware knew it every time they wrote a CALL.

That makes the stack a free verification instrument. Once the image is out, the call graph can be built from the instruction stream, and any path that needs three levels is evidence of a bad read rather than evidence of a firmware bug. Very few parts offer so clean a test.

The rest of the device is unremarkable in the best sense: 1024 twelve-bit words of one-time-programmable program memory arranged as two pages of 512, 41 bytes of RAM, six I/O pins in an eight-pin package, thirty-three instructions, a watchdog, and a factory-trimmed internal oscillator whose calibration word sits at the top of the map.

The part in numbers

item PIC12C509A
program memory 1024 by 12 bits, OTP
pages 2, selected by PA0 in the status register
return stack 2 levels, not readable by software
data RAM 41 bytes
I/O 6 pins, one input-only
interrupts none
entry points exactly one, address zero
calibration word top program address

The stack row deserves emphasis because it is not observable from inside the part. There is no register that reports the stack pointer, no overflow flag, and no instruction that pushes or pops for data purposes. A program that overflows it does not trap; it wraps, and the return goes to whatever address was there two calls ago.

What a depth-three path means in a dump

When a recovered image shows a chain of calls three deep, exactly three explanations exist, and they can be separated.

The first is that the read is wrong at one instruction. A single bit error can turn a MOVF into a CALL, which manufactures a nesting level that the firmware never had. The check is positional: if the offending word sits at an address whose neighbours are inconsistent with it, the word is corrupted.

The second is that a computed jump was disassembled as a call. The twelve-bit core jumps with GOTO into the middle of a routine for skip tables, and a tool that mislabels the target as a subroutine entry will build a chain that does not exist in the machine.

The third is that the code is executing data. This is the interesting one and it is common on parts this small, because a 41-byte RAM budget pushes designers to store lookup tables in program memory and read them with CALL and RETLW. Those tables are legitimate and they are reached by call, but they are one level deep by construction. A table reached by call from inside a routine that was itself called is at level two, which is fine; a table reached from a table is impossible.

Data that decodes as a call

The RETLW table is the idiom that makes a 12-bit part feel larger than it is. A block of words, each one RETLW with a different literal, is indexed by adding an offset to the program counter and calling into the middle of the block. The return value is the literal.

For a disassembler this block is a trap in both directions. Read as code it is a subroutine that always returns a constant and never falls through, which looks plausible. Read as data it is a table of byte values, which is what the firmware intended. Telling the two apart needs the surrounding code: the CALL that reaches the block usually has its address computed a few instructions earlier from an index register.

The practical consequence for a delivery is that a raw disassembly of this part should not be presented as source. It should be presented as an image plus a listing, with the table blocks marked as tables. Customers who receive a flat listing where the tables are rendered as forty consecutive subroutines tend to conclude the recovery was scrambled.

Two pages and one status bit

1024 words is two pages of 512, and the page is chosen by a single bit in the status register. The bit is not restored automatically after a call returns, which is the detail that catches hand-written code and catches disassemblers even more often: a routine that switches page, calls within the new page and returns leaves the caller in the wrong page unless the caller put it back.

The stack and the page interact in exactly one way worth knowing. A CALL at the last address of page zero stores the return address correctly and pushes the programme counter into page one or into the call target, depending on how the bit was set. There is no hardware protection against a call whose target page does not match the bit. If the recovered image shows a call into the upper half and the page bit was clear at that point, either the bit was set by an instruction the disassembly skipped, or the word is a misread.

Using the constraint as a check

A verification pass on this part needs three steps and no simulator.

Build the call graph from the reset vector, following GOTO as a jump and CALL as an edge, and stopping at RETLW and RETURN. Record the maximum depth reached on any path. If it exceeds two, the image has a false edge, and the address of that edge is where to re-read first.

Then check that every CALL target lands inside 1024 words and, on the assumption that the design is ordinary code, that the target is a plausible entry rather than a word in the middle of a table. Entry points in hand-written assembly tend to be preceded by a RETLW or a GOTO, which is a weak signal but a useful one.

Finally check address zero. With one entry point and no interrupts, the reset vector must decode to a GOTO that lands in the lower page, or to a MOVLW that begins an oscillator-trim sequence. An image that fails this test is off by an offset, and no amount of downstream analysis repairs it.

For the sibling with half the array and no page bit, PIC12C508A firmware reversal shows where the simplification helps. For the same core in a twenty-pin package where paging runs to four pages, PIC16C57C firmware carries the argument further. And for a mid-range part where the stack is eight levels and the constraint disappears entirely, PIC16F72 secured data is the contrast.

FAQ

Does the part trap on stack overflow? No. The two-level stack is a ring of registers with no detection logic. An overflow returns to the wrong address and the program continues from there.

Can the stack contents be read? Not through any instruction or programming command. The return addresses are only observable indirectly, by watching where a program goes.

Why does a recovered listing show more subroutines than the firmware has? Because lookup tables written with RETLW are reached by CALL and look exactly like subroutines. They are data with a calling convention.

The short version

Two levels of stack, one entry point, one page bit, and a calibration word at the top. The stack is the free test: build the call graph, and any path that reaches three levels is a bad read rather than a bad design. Mark the RETLW tables as tables, verify address zero, and this part verifies more cleanly than most parts ten times its size.

PostHeaderIcon Attack Secured GD32F305VET6 Memory: Fault Past the FMC

The GD32F305VET6 is the 512 KB flash member of the line in the LQFP100 body, and like every protected part in the family its read lock is enforced by the FMC, not by the application. To attack the secured GD32F305VET6 memory you do not break a cipher; you attack the one moment the hardware decides whether to open the flash, and you make that decision fall the wrong way. The method is fault injection, aimed squarely at the memory controller.

The decision worth attacking

After reset the FMC samples the SPC level and gates the AHB window into flash. That gate is re-evaluated on the cycle where the security check runs, and only then. A probe that simply attaches and reads meets a closed window for the entire session; the attack has to land on that single evaluation so the window is open for the few milliseconds the stream needs. Everything in the technique is about hitting that narrow point on the 512 KB array rather than on the code.

Three ways to inject the fault

The fault can be delivered three ways, and the right one depends on the unit. A clock glitch shortens or stretches the clock on the critical cycle; a voltage glitch dips the core supply at the same moment; an electromagnetic pulse couples into the die from a probe held over it. The clock and voltage routes are repeatable and cheap, and are the ones described in our notes on clock glitching and power glitching. The EM route is the fallback when the package or layout makes the other two hard to land on the VET6.

Why LQFP100 gives more room

The larger body helps the attacker as much as the owner. With 100 leads the part is easy to fixture and the power and clock nets are reachable, and the 512 KB array means a longer but still bounded capture once the window holds. The glitch margin — the width of the exploitable window — is wider here than on the smallest parts, which is why the VET6 is a friendlier target than the tight 128 KB RBT6. Margin is the difference between a one-in-ten shot and a repeatable procedure, and a wider margin also lets the rig trade raw precision for repetition, running the same fault many times and keeping only the captures that landed rather than having to nail the timing on the first attempt.

What makes an attack fail

Most failed attempts are timing, not technique: the fault arrives a cycle early or late and the check holds. The rest are parts at level 2, where the SPC is latched and the check no longer runs at all — there is nothing to attack, only a decapsulation to fall back on. Telling the two apart quickly is what keeps an attack from running forever on a part it can never open. On the F305 the USB OTG block shares the same reset and clock domain as the FMC, so a fault aimed at the memory controller has to be scoped to that domain and not spill into the USB state machine, which is a second reason the VET6 benefits from the room its larger package gives the rig. The smaller RCT6 flash read uses the same opening and the same discipline on a shorter array.

Verifying the window held

A successful attack is only proven by the data. The 512 KB is read in blocks and each block is checksummed against a second pass; a window that closed mid-read shows up as a mismatch on exactly the blocks captured during the closure, and those are re-read. Only a clean, complete image is accepted, because a silently truncated capture is worse than no capture. The binary decode that follows assumes the image it receives is whole.

Fault type How it lands When it is used
Clock glitch stretches the critical cycle default, repeatable
Voltage glitch dips core supply when clock is hard to reach
EM pulse couples into the die package blocks the others

How the memory is returned

The result of the attack is the full 512 KB array, read while the window was open, delivered as a binary with the option bytes. The part leaves the bench electrically as it arrived; only its contents have moved. Where the goal is a copy rather than an analysis, the same image feeds the program duplication of the 1 MB VGT6, which is the same attack scaled to a larger array.