Definition
A vector table is a table in memory that holds the address of a handler, the routine that serves an event, for each exception a processor can take. An exception is an event that makes the processor suspend the running code: a device interrupt §6.11, a fault, a system call or a reset. On an exception, the processor hardware reads the matching entry and jumps to the address stored there. On Arm Cortex-M processors the first entry instead holds the initial top of the stack, the memory area for temporary data.
The idea
A processor needs to know where to go next at two moments when no software can tell it. The first is reset: memory holds a program, but nothing has run yet that could say where the program starts. The second is an exception: a timer expires or a radio finishes receiving a packet, and the processor must reach the code for that event within a few clock cycles, without first running code that asks which device needs service. The vector table answers both with one fixed lookup: exception number selects entry , and entry holds the address to jump to.
A switchboard with one speed-dial button per kind of emergency is a fair picture. When an alarm of a given kind sounds, the switchboard dials whatever number is stored under that button, without asking who stored it. The picture stops being accurate in one respect that matters for security: the code that the processor reaches through the table runs with full privilege. On Cortex-M, every handler runs in Handler mode, the processor state reserved for handlers, and Handler mode always has the higher of the processor’s two privilege levels §6.10. Privileged code may reconfigure the memory protection unit §6.8 (MPU), the hardware that limits which memory each piece of code may use, and every other protection setting. Whoever can write an entry of the vector table therefore chooses which code runs with full privilege the next time that exception occurs. The table is control data, like a return address on the stack, and needs the same protection as the code itself.
How it works
On processors that implement the Armv7-M architecture, such as the Cortex-M3 and the Cortex-M4 §6.3, each entry is one 32-bit word, 4 bytes long, and the entries are numbered by exception number :
entry 0: the initial value of the main stack pointer, the register that marks the top of the stack used by start-up code and by handlers;
entry 1: the reset handler, the first code to run after any reset;
entry 2: the NMI (non-maskable interrupt), an interrupt that software cannot disable;
entry 3: HardFault, the fault exception that takes every fault no other handler takes;
entries 4 to 6: MemManage, BusFault and UsageFault, three fault exceptions that software enables separately, described in the Exception and fault handler §6.15 entry;
entries 7 to 15: exceptions for system calls, for debugging, for switching between threads and for the processor’s built-in system timer, and some reserved entries;
entry : device interrupt number , for and so on up to the number of interrupts the chip has.
The entry for exception sits at byte offset from the start of the table.
The table drives the boot sequence, the chain of code that runs from reset to the application:
Reset. The processor reads entry 0 into the main stack pointer and entry 1 into the program counter §2.9, the register that holds the address of the next instruction, and starts executing. On the Cortex-M3 and the Cortex-M4 the table is read at address
0x00000000, which on many chips is the start of flash memory §6.6.Bootloader §6.30, if present. A bootloader is a small program that runs before the application, for example to check or install a firmware image. It owns the table at the reset location. When it has finished, it points the processor at the application’s own vector table, loads that table’s entry 0 into the stack pointer and jumps to the address in that table’s entry 1.
C runtime start-up. The application’s reset handler prepares memory for a C program: it copies the starting values of initialized global variables from flash memory into SRAM §6.7 (static random-access memory, the chip’s working memory), fills the uninitialized global variables with zeros, and often sets up the clocks.
Kernel initialization. With an RTOS (real-time operating system) such as the Zephyr RTOS §6.33, the start-up code then starts the kernel, which initializes its drivers in a fixed order of levels and starts its system timer.
Application. Only then does the application’s
main()function run.
Everything before the last step has already run, and taken time, when main() begins.
When exception is taken, the processor saves eight registers on the stack, reads the word at the table base plus , and starts the handler at that address in Handler mode. For a device interrupt, the NVIC §6.13 (Nested Vectored Interrupt Controller), the interrupt controller of Cortex-M processors, has already chosen which pending interrupt goes next; the vector table only supplies the address. No instruction of the interrupted program takes part in the lookup.
The table base is not fixed. The VTOR (Vector Table Offset Register), at address 0xE000ED08, holds it, and only privileged code can write the VTOR. The base must be aligned: it must be a multiple of the table’s size rounded up to a power of two, and at least a multiple of 128 bytes. Software can copy the table into SRAM and point the VTOR at the copy. That lets handlers be installed while the program runs, but it makes every entry an ordinary writable word.
Cortex-M processors execute only Thumb instructions, Arm’s compact instruction encoding §2.7, and bit 0 of every handler address in the table must be 1 to say so. A handler that starts at 0x00001234 is therefore stored as 0x00001235. If bit 0 is 0, the processor faults as soon as it tries to execute the handler.
Some operating systems add a second table in software. The Zephyr RTOS, for example, can point the hardware entries of all device interrupts at one common entry routine, which reads the number of the active interrupt, looks up the real handler and its argument in a software table, and calls it. Control flow then depends on the hardware table, the software table and the VTOR.
Worked example
Take a chip with 256 KiB of SRAM starting at address 0x20000000, flash memory starting at 0x00000000, and 48 device interrupts. At reset the processor reads the table at 0x00000000. 256 KiB is bytes, which is 0x40000 in hexadecimal, so SRAM ends just below 0x20040000. The stack starts at the top of SRAM and grows downwards, so entry 0 holds 0x20040000. Entry 1, at offset 4, holds 0x00000201: the reset handler starts at 0x00000200, and the final 1 is the Thumb bit.
Device interrupt number is exception number . Its entry sits at byte offset , which is 0x58 in hexadecimal, so at address 0x00000058. If the interrupt’s handler was placed at 0x00001234, that word holds 0x00001235. Whenever interrupt 6 is taken, the processor reads 0x00001235 and runs the code at 0x00001234.
Now the start-up code copies the table to the start of SRAM and sets the VTOR to 0x20000000. The table has entries, which is bytes. The smallest power of two that is at least 256 is 256 itself, and 256 is more than the minimum of 128, so the base must be a multiple of 256, which is 0x100 in hexadecimal; 0x20000000 is such a multiple. The entry for interrupt 6 now sits at 0x20000000 plus 0x58, which is 0x20000058.
A bug elsewhere in the program lets an attacker write one 4-byte value to an address of the attacker’s choice, and the attacker has placed a few bytes of machine code in a receive buffer at 0x20004000. The attacker writes 0x20004001 to 0x20000058. The next time interrupt 6 is taken, the processor reads 0x20004001, finds the Thumb bit set, and runs the attacker’s bytes at 0x20004000 in Handler mode, with full privilege. Nothing in the program called that code. No function pointer was used, so a check placed on the program’s own calls through function pointers never sees this transfer.
Two MPU settings change the outcome. If the MPU marks SRAM as execute-never, meaning the processor may not fetch instructions from it, the fetch from 0x20004000 raises a MemManage fault instead: the attack becomes a crash, a denial of service §1.28, not code execution. If the MPU also marks the 256 bytes from 0x20000000 as read-only for all code, privileged code included, the attacker’s write to 0x20000058 itself faults, and the entry never changes. A table left in flash memory resists such a write without any MPU setting, because changing flash memory takes more than one write: the program must first unlock the flash memory controller.
Stated precisely
Let be the table base held in the VTOR, let be an exception number, and let be the 32-bit word stored at address . Taking exception sets the program counter to with bit 0 cleared, and requires bit 0 of to be 1. Device interrupt has exception number . At reset, with the reset value of the VTOR, the main stack pointer is loaded with and the program counter with , bit 0 cleared. If the processor supports exception numbers 0 to , then must be a multiple of the smallest power of two that is at least .
The handler that runs for exception is the intended one exactly when four things hold at the moment the exception is taken:
the VTOR holds the intended base ;
the word holds the intended handler address;
the code at that address is the intended handler’s code;
if the entry leads to a common routine that consults a software table, that software table’s entry and argument hold the intended values.
Anyone who can write one of these four chooses privileged code. The guarantee therefore rests on two assumptions. First, only trusted privileged code can write the VTOR; the architecture enforces this part, because an unprivileged write to the VTOR faults. Second, the memory that holds the table, the handler code and any software table is protected against writes by everything else; flash memory write protection, the MPU or both must enforce this part.
Limits and common mistakes
Placing the table in flash memory is not a complete protection argument. Privileged code can unlock the flash memory controller and rewrite the page that holds the table, a debugger attached to the debug port can rewrite it, and a firmware update replaces it. Flash memory write protection, a locked debug port, and secure boot §6.31, which checks the signature of a firmware image, table included, before running it, cover these paths.
Protecting the program’s function pointers does not protect the vector table. Control-flow integrity checks that a compiler inserts test each indirect call in the program, a call through a function pointer, against a list of allowed targets; they never see the processor’s own read of a vector entry.
Checking only that an entry points somewhere into executable flash memory is too weak. Every function in flash memory is executable, and running the wrong one in Handler mode can still break the system.
A corrupted HardFault entry is especially harmful. When a fault occurs and the HardFault entry is invalid, the processor faults again while handling a fault, which it cannot escalate any further: it enters lockup and stops executing until a reset. A bus error while the processor reads a vector entry is itself reported as a HardFault, with the VECTTBL bit (vector table read error) set in the HFSR (HardFault Status Register).
On a Cortex-M chip whose table sits at address 0, reading through a null pointer §4.26 often does not fault. Address 0 then holds the initial stack pointer, which reads back as an ordinary number, unless the MPU forbids access to the lowest addresses.
A table copied to SRAM must be complete before the VTOR points at it. An interrupt taken between the two steps reads whatever the new location holds.
Details differ between architecture versions. The Cortex-M0 has no VTOR, and on the Cortex-M0+ it is optional. Processors with Arm’s TrustZone security extension, under the Armv8-M architecture, have two tables, one for the secure state and one for the non-secure state, each with its own VTOR.
Questions answered
What happens if an attacker can modify a handler entry?
The next time that exception occurs, the processor jumps to the address the attacker wrote and runs whatever code is there with full privilege. A handler entry is a word in the vector table, the table of addresses that the processor hardware reads to find the handler for each exception, such as a device interrupt or a fault. On Arm Cortex-M processors every handler runs in Handler mode, which is always privileged, and no software checks the address before the jump.
What follows depends on the target. If it is code the attacker supplied in executable memory, the attacker controls the device, because privileged code can reprogram the memory protection unit (MPU) and every other protection. If it is existing code, the attacker reuses that code out of context. If it is an invalid address, or memory marked execute-never, the processor faults, and the result is a crash, a denial of service. A changed entry 1 (the reset handler) or entry 0 (the initial stack pointer) in the table at the reset location takes effect at every boot, so that change persists across resets. No ordinary function call is involved, so protections placed on the program’s function pointers do not see the transfer.
The defense is to let only trusted privileged code write the entries, the register that holds the table base (the VTOR, Vector Table Offset Register) and the handler code: keep the table in write-protected flash memory, or make an SRAM copy read-only with the MPU; lock the debug port; and use secure boot so that a new firmware image with a modified table is rejected. A software table through which an operating system dispatches interrupts needs the same protection.
Related entries: Privilege level (§6.10), Read-only memory range (§4.47), Link register (LR) (§4.45), NVIC (§6.13), Exception and fault handler (§6.15), Memory protection unit (MPU) (§6.8), Bootloader (§6.30). Further reading: [81, 175].