Commit 9af7c9c
committed
aarch64: Add AArch64 Kernel Control Flow Integrity implementation
Implement AArch64-specific KCFI backend providing runtime validation of
indirect function calls with ARM exception handling infrastructure.
Core AArch64 KCFI Features:
* Function preamble generation using .word directives for type ID storage
at -4 byte offset from function entry point (no prefix NOPs needed due
to 4-byte instruction alignment)
* Enhanced debugging through ESR (Exception Syndrome Register) encoding
in BRK instruction immediate values for precise failure analysis
* Scratch register allocation using w16/w17 (x16/x17) following
AArch64 procedure call standard for intra-procedure-call registers
* Support for both regular calls (BLR) and sibling calls (BR) with
appropriate register usage and jump instructions
Assembly Code Generation:
* Atomic bundled KCFI check + call/branch sequences using UNSPECV_KCFI_CHECK
to prevent optimizer separation and maintain security properties
* Constant loading for type IDs using MOV/MOVK instruction pairs
for values requiring 32-bit representation
* Direct comparison approach using CMP instruction for type validation
without arithmetic operations (contrast with x86_64's additive approach)
Assembly Code Pattern for AArch64:
ldur w16, [target, #-4] ; Load actual type ID from preamble
mov w17, #type_id_low ; Load expected type (lower 16 bits)
movk w17, #type_id_high, lsl gcc-mirror#16 ; Load upper 16 bits if needed
cmp w16, w17 ; Compare type IDs directly
b.eq .Lpass ; Branch if types match
.Ltrap: brk #esr_value ; Enhanced trap with register info
.Lpass: blr/br target ; Execute validated indirect transfer
ESR (Exception Syndrome Register) Integration:
* BRK instruction immediate encoding format:
0x8000 | ((TypeIndex & 31) << 5) | (AddrIndex & 31)
* TypeIndex indicates which W register contains expected type (W17 = 17)
* AddrIndex indicates which X register contains target address (0-30)
* Example: brk #33313 (0x8221) = expected type in W17, target address in X1
* Enables kernel exception handlers to precisely identify KCFI violation context
* Supports advanced debugging and forensic analysis of control flow attacks
AArch64-Specific Optimizations:
* No prefix NOP calculation needed due to natural 4-byte instruction alignment
* Type ID storage using single .word directive in function preambles
* Register allocator integration via explicit w16/w17 clobber annotations
* Function label emission coordination through ASM_OUTPUT_FUNCTION_LABEL macro
redirection to aarch64_declare_function_name() for preamble integration
* Support for large immediate values with MOV/MOVK instruction generation
Target Hook Implementation:
* aarch64_kcfi_calculate_prefix_nops(): Returns 0 (no alignment needed)
* aarch64_kcfi_gen_checked_call(): Bundled check+call RTL generation
* aarch64_kcfi_emit_type_id_instruction(): .word directive emission
* aarch64_kcfi_add_clobbers(): w16/w17 register constraint management
* Integration in aarch64_override_options() for initialization
Machine Description Integration:
* UNSPECV_KCFI_CHECK unspec for atomic check+call bundling
* Support for both regular calls and sibling calls with distinct patterns
* Runtime ESR value calculation for accurate register encoding
* Clobber specifications for w16 (loaded type) and w17 (expected type)
Security Properties:
* Direct comparison-based type validation with immediate trap on mismatch
* Enhanced exception context through ESR encoding for precise failure analysis
* Tamper-resistant type ID storage with ARM exception infrastructure integration
* Support for cross-compilation and accurate register allocation across targets
Build and run tested with Linux kernel ARCH=arm64.
Signed-off-by: Kees Cook <[email protected]>1 parent f7c1640 commit 9af7c9c
File tree
4 files changed
+260
-1
lines changed- gcc/config/aarch64
4 files changed
+260
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1285 | 1285 | | |
1286 | 1286 | | |
1287 | 1287 | | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
1288 | 1292 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| 86 | + | |
86 | 87 | | |
87 | 88 | | |
88 | 89 | | |
| |||
19521 | 19522 | | |
19522 | 19523 | | |
19523 | 19524 | | |
| 19525 | + | |
| 19526 | + | |
| 19527 | + | |
19524 | 19528 | | |
19525 | 19529 | | |
19526 | 19530 | | |
| |||
25578 | 25582 | | |
25579 | 25583 | | |
25580 | 25584 | | |
| 25585 | + | |
| 25586 | + | |
| 25587 | + | |
25581 | 25588 | | |
25582 | 25589 | | |
25583 | | - | |
| 25590 | + | |
| 25591 | + | |
25584 | 25592 | | |
25585 | 25593 | | |
25586 | 25594 | | |
| |||
32811 | 32819 | | |
32812 | 32820 | | |
32813 | 32821 | | |
| 32822 | + | |
| 32823 | + | |
| 32824 | + | |
| 32825 | + | |
| 32826 | + | |
| 32827 | + | |
| 32828 | + | |
| 32829 | + | |
| 32830 | + | |
| 32831 | + | |
| 32832 | + | |
| 32833 | + | |
| 32834 | + | |
| 32835 | + | |
| 32836 | + | |
| 32837 | + | |
| 32838 | + | |
| 32839 | + | |
| 32840 | + | |
| 32841 | + | |
| 32842 | + | |
| 32843 | + | |
| 32844 | + | |
| 32845 | + | |
| 32846 | + | |
| 32847 | + | |
| 32848 | + | |
| 32849 | + | |
| 32850 | + | |
| 32851 | + | |
| 32852 | + | |
| 32853 | + | |
| 32854 | + | |
| 32855 | + | |
| 32856 | + | |
| 32857 | + | |
| 32858 | + | |
| 32859 | + | |
| 32860 | + | |
| 32861 | + | |
| 32862 | + | |
| 32863 | + | |
| 32864 | + | |
| 32865 | + | |
| 32866 | + | |
| 32867 | + | |
| 32868 | + | |
| 32869 | + | |
| 32870 | + | |
| 32871 | + | |
| 32872 | + | |
| 32873 | + | |
| 32874 | + | |
| 32875 | + | |
| 32876 | + | |
| 32877 | + | |
| 32878 | + | |
| 32879 | + | |
| 32880 | + | |
| 32881 | + | |
| 32882 | + | |
| 32883 | + | |
| 32884 | + | |
| 32885 | + | |
| 32886 | + | |
| 32887 | + | |
| 32888 | + | |
| 32889 | + | |
| 32890 | + | |
| 32891 | + | |
| 32892 | + | |
| 32893 | + | |
| 32894 | + | |
| 32895 | + | |
| 32896 | + | |
| 32897 | + | |
| 32898 | + | |
| 32899 | + | |
| 32900 | + | |
| 32901 | + | |
| 32902 | + | |
| 32903 | + | |
| 32904 | + | |
| 32905 | + | |
| 32906 | + | |
| 32907 | + | |
| 32908 | + | |
| 32909 | + | |
| 32910 | + | |
| 32911 | + | |
| 32912 | + | |
| 32913 | + | |
| 32914 | + | |
| 32915 | + | |
| 32916 | + | |
| 32917 | + | |
| 32918 | + | |
| 32919 | + | |
| 32920 | + | |
| 32921 | + | |
| 32922 | + | |
| 32923 | + | |
| 32924 | + | |
| 32925 | + | |
| 32926 | + | |
32814 | 32927 | | |
32815 | 32928 | | |
32816 | 32929 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1665 | 1665 | | |
1666 | 1666 | | |
1667 | 1667 | | |
| 1668 | + | |
| 1669 | + | |
| 1670 | + | |
| 1671 | + | |
| 1672 | + | |
1668 | 1673 | | |
1669 | 1674 | | |
1670 | 1675 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
417 | 417 | | |
418 | 418 | | |
419 | 419 | | |
| 420 | + | |
420 | 421 | | |
421 | 422 | | |
422 | 423 | | |
| |||
1316 | 1317 | | |
1317 | 1318 | | |
1318 | 1319 | | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
| 1324 | + | |
| 1325 | + | |
| 1326 | + | |
| 1327 | + | |
| 1328 | + | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
| 1334 | + | |
| 1335 | + | |
| 1336 | + | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
| 1346 | + | |
| 1347 | + | |
| 1348 | + | |
| 1349 | + | |
| 1350 | + | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
| 1392 | + | |
| 1393 | + | |
| 1394 | + | |
| 1395 | + | |
| 1396 | + | |
| 1397 | + | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
| 1440 | + | |
| 1441 | + | |
| 1442 | + | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
| 1452 | + | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
1319 | 1456 | | |
1320 | 1457 | | |
1321 | 1458 | | |
| |||
0 commit comments