Closed SethGen closed 1 month ago
Thanks for taking your time to reply @maxeisele! I genuinely appreciate it.
I do have another question for you. I have navigated my way through most of the issues I was having before and can now fuzz .elf files on my STM32 Nucleo Board. I am now trying to fuzz STM32 MCUs which are not part of a Nucleo Board, and therefore don't have the integrated ST-Link Debug Serial + Programming capabilities.
Is there a way to do this? Suppose I have a 6-Pin JTAG Pogo setup for repeatedly flashing and receiving debug info. I also break out the UART Lines from the STDC14 Adapter(Pins 13 & 14) and connect that to the DUT's UART Lines for serial communication. I can now repeatedly flash the device and have serial communication with it while running a gdb server. Is there anything I am missing? With the STLINK-V3SET Programmer among others, this is possible.
Is this something you have tried before? Any tips would be greatly appreciated.
Thank you again for your help.
Hi @SethGen Great that fuzzing on your Dev-Board works now. Regarding your other STM32 Boards. Any Debug Probe should do the job. It might even be possible to reuse the integrated debugger from your STM32 Nucleo Board and connect it to an external MCU. However, I do not have experience with that setup.
The STLink should be fine, as well.
Hello Max and The Bosch Research team
I am currently trying to Fuzz an Embedded Application on the STM32G474RE Nucleo Board - NUCLEO G474RE. I am going to quickly walk you through the steps I am currently taking while trying to Fuzz, along with my output on execution. I have used your "GDBFuzz on STM32 B-L4S5I-IOT01A board" detailed instructions as my initial tutorial. Any support/ walking through the steps taken for you to test your STM32 B-L4S5I-IOT01A firmware would be greatly appreciated.
1. In Ubuntu 22.04.3, head to gdfuzz/example_firmware and create a new folder
In my case, I copied the "stm32_disco_arduinojson" folder, pasted it and renamed it to "stm32g4_sample". From here, I made the appropriate changes to the platform.ini file, allowing PlatformIO to configure itself for the NUCLEO-G474RE.
I want to believe that this part is okay due to my ability to work with PlatformIO. I will show this in a moment.
2. Get a binary(.elf) file from STM32Cube after Building the Project
After building in STM32CubeIDE, the .elf file is conveniently placed within the 'Binaries' folder. From here, I simply copy and paste that file into "gdbfuzz/example_firmware/stm32g4_sample", where the binary file is renamed to "firmware.elf". In this compiled project I have written a fuzz wrapper and test function. The symbols(FuzzMe & LLVMFuzzerTestOneInput) are both found by gdbfuzz and can be found within the .elf file.
This leads me into my first question.
How are you implementing your fuzz wrapper in STM32's framework?
In the configuration file, you state that I can also specify a symbol name when configuring the entry point. The thing is, how do I implement that entry point in C code? In traditional software fuzzers(LLVM for example), it is implemented as shown above and then compiled with clang(-fsanitize=fuzzer) to produce a fuzz executable. I guess I am just struggling to understand how the entrypoint function must be implemented in order to function properly.
In the output files shown later, I had used LLVMFuzzerTestOneInput as my entrypoint, set up exactly as shown in the code snippet above.
3. Ensure STLINK can be communicated with and has appropriate privileges
To do this, I modified my udev rules to contain the necessary information such that my STM32G474RE's STLINK-V3 communicates as intended. The following code is used to reload and start a udev instance
I can now check the STLINKs permissions with
Thus, I have shown that I have proper read-write permissions for the STLINK
4. Use PlatformIO to Flash Firmware to Device
As you can see, I am able to program to the STM32G474RE with PlatformIO and see that a program has been flashed to my MCU. However, the binary file "gdbfuzz/example_firmware/stm32g4_sample/.pio/build/nucleo_g474re/firmware.elf" is not the same as "gdbfuzz/example_firmware/stm32g4_sample/firmware.elf". One thing that I have noticed about this process is that if I now replace "gdbfuzz/example_firmware/stm32g4_sample/.pio/build/nucleo_g474re/firmware.elf" with "gdbfuzz/example_firmware/stm32g4_sample/firmware.elf", the program that I wrote in STM32Cube will be flashed.
Essentially, when I initially upload, the binary file in .pio/build/nucleo_g474re is not the same as the firmware.elf file that I want to fuzz. This means that my initial flashing of the device does not flash the device with the firmware to be fuzzed. It isn't until I replace that "gdbfuzz/example_firmware/stm32g4_sample/.pio/build/nucleo_g474re/firmware.elf" file with the file to be fuzzed that I am able to flash the file to be fuzzed onto my MCU. This leads me to my second question.
Is the binary file that populates .pio/build/nucleo_g474re after a successful flash supposed to be the same as the binary file that I took from STM32Cube which contains my program to be fuzzed? Am I supposed to change this file to the file to be fuzzed?
I have tried both not changing the .pio/build binary file after initial flash and changing that file to the binary file to be fuzzed. Neither have solved my issue.
So, I have shown that I am able to successfully communicate with my STM32G474RE via PlatformIO, however I am unsure if everything is set up appropriately.
5. Configuring The Fuzzer
Now this is where I am having the most troubles. I am using the integrated STLINK on my Nucleo Board, so I likely ignorantly assume that I should be using a fuzz_serial.cfg or fuzz_serial_json.cfg configuration file. I simply copied the fuzz_serial_json.cfg file from your stm32_disco_arduinojson example and made the necessary changes.
What configuration do you suggest for simple testing of firmware via STLINK? I am pretty sure it is serial, but I am not understanding why you used JSON for some examples but the non-JSON version for others. Additionally, how can I tell whether or not my entrypoint is being entered as intended?
I have enabled DEBUG logging for the fuzzer. I am not able to discern any helpful information that it relays to me, as I am unsure if I am even entering my program and fuzzing the entrypoint. As you are about to see from the Fuzzer's output, the Fuzzer is able to connect to GDB, ghidra_bridge and the SUT, however there is something still going wrong.
6.Running The Fuzzer
I should note that I have ran the following initialization code prior to this process
As stated in your demonstration, I must first start a GDB Server using st-util
We can see that the STLINK is found and a GDB Server has started listening to Port 4242
To run the GDBFuzz, I input the following:
Here is the GDB Server Output after fuzzing for a minute or two:
We can see that the crashing -> reprogramming/rerun is functioning. The program is crashing(?) and we can see it starts up on its own and runs it again. This is the only output I see on the GDB Server side. What you see above repeats throughout the entire fuzzing process. I feel like the "cannot recv: -2" is a hint at what might be going wrong, but my searches on the internet have been fruitless.
Now this is where I could really use your expertise. Here is the Fuzzer output on the Fuzzer side (loglevel = DEBUG). out.log
It appears that the same input is being sent repeatedly, and not much else is happening. In the "gdbfuzz/output/stm32g4_sample/trial-0/crashes" folder, there is one crash file titled: "0x080057200xfffffff90x081a3f320x080045ba" with the inner content:
I hope there is something in my process which is flawed such that this is an easy fix.
As I said before, any help and guidance would be much appreciated. Another resource that I would love to take a look at is Max's Black Hat Europe Presentation: https://www.blackhat.com/eu-23/arsenal/schedule/index.html#gdbfuzz-embedded-fuzzing-with-hardware-breakpoints-35796 I am sure there is some good information in there related to what I am asking, however have not been able to find a recording of his presentation and am wondering if you have and are willing to share this with me.
Best,
Seth G