emin_int11

cortexm4

Mar 22nd, 2016
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. 2 gecədir durmaksızın ARM Cortex üzərində reversing ilə məşğulam əlimdə olan STM32 MCU (ARM Cortex M4 processor depended) - ((greetZ Sənan:P )) üzərində RTOS proyekti hazırlayıram hal hazırda debugging prosesində maraqlı nüansla qarşılaşdım. İntel x86-64 arch üzərində aşkarlanmış EoP ARM üzərində tətbiq etməyi nəzərdə tutdum.
  2.  
  3. Theory^^^^
  4.  
  5. ARM Cortex M4 - 2 processor modundan və 2 privilige level-dan ibarətdir.
  6.  
  7. Processor mode and privilege levels for software execution
  8. The processor modes are:
  9. Thread mode Used to execute application software. The processor enters Thread mode
  10. when it comes out of reset.
  11. Handler mode Used to handle exceptions. The processor returns to Thread mode when it
  12. has finished all exception processing.
  13.  
  14. The privilege levels for software execution are:
  15.  
  16. Unprivileged The software:
  17. • has limited access to the MSR and MRS instructions, and cannot use the
  18.  
  19. CPS instruction
  20. • cannot access the system timer, NVIC, or system control block
  21. • might have restricted access to memory or peripherals.
  22. Unprivileged software executes at the unprivileged level.
  23.  
  24. Privileged
  25. The software can use all the instructions and has access to all resources.
  26. Privileged software executes at the privileged level.
  27.  
  28.  
  29. ARM MCU-de CONTROL register-i (Special register) proqram təminatının privilege yaxud unprivileged modda icrasına nəzarət edir. Unprivileged proqram təminatı SVC instruction istifade etdikdə (supervisor call (syscall)) cari axış privileged moda yönləndirilə bilər.
  30.  
  31.  
  32. CMSIS core :
  33. __set_CONTROL(THREAD_MODE_UNPRIVILEGED | SP_PROCESS);
  34.  
  35. disas mode:
  36. 0x0800027c <+68>: movs r3, #3
  37. 0x0800027e <+70>: str r3, [r7, #0]
  38. 0x08000280 <+72>: ldr r3, [r7, #0]
  39. 0x08000282 <+74>: msr CONTROL, r3
  40.  
  41.  
  42.  
  43. inline asm:
  44. __ASM volatile ("MSR control, %0" : : "r" (control) : "memory");
  45.  
  46.  
  47. hardware state switching ===== >
  48.  
  49. _______ ________
  50. | | | |
  51. | PRIV | ===========-> | UNPRIV |
  52. |_______| |________|
  53.  
  54.  
  55. CONTROL REGISTER
  56. ______________________________________________________3_2_1_0_
  57. | | | | |
  58. |_______________________________________________________|_|_|_|
  59. | |
  60. SPSEL ___| |
  61. |
  62. nPRIV _____|
  63.  
  64. Table 2.10. CONTROL register bit assignments
  65. Bits Name Function
  66. [31:2] - Reserved.
  67. [1] SPSEL = Defines the currently active stack pointer: In Handler mode this bit reads as zero and ignores writes. The Cortex-M3 updates this bit automatically on exception return.
  68. 0 = MSP is the current stack pointer
  69. 1 = PSP is the current stack pointer.
  70. [0] nPRIV Defines the Thread mode privilege level:
  71. 0 = Privileged
  72. 1 = Unprivileged.
  73.  
  74.  
  75. Privileged moda keçid üçün isə SOFTWARE based interrupt istifadə edilməlidir. Daha sonra isə uyğun interrupt handler icra edilir. Context switching əvvəlində current state (registers) stack-e əlavə edilir. Və Unprivileged moda təkrar qayıtdıqda trap frame istifadə edilərək register state (old registers) bərpa edilir. GDB üzərində interrupt routin-i icra edildiyində NUCLEO üzərindəki RESET button vasitəsi ilə RESET interrupt vektoru handle etdikdə unpriv moda dönməmişdən əvvəl CONTROL REGİSTER STATE = nPRİV bit(1) bu state-də interrupt handle edilir. Bunun nəticəsində isə ortaya çıxır ki user depended registerləri istəyimizə uyğun dəyişib arbitrary code exec yola açmaq mümkündür.
  76.  
  77. SVC FRAME ------------>
  78. ________ ________
  79. | | | |
  80. | UNPRIV | ===========-> | PRIV |
  81. |________| |________|
  82.  
  83. will throw a HF handler
  84. `void HardFault_Handler(void)`
  85. |
  86. |
  87. |
  88. ----------------------------
  89. | hardware fault exception |
  90. ----------------------------
  91. |
  92. |
  93. hijack control flow
Advertisement
Add Comment
Please, Sign In to add comment