Fuzzing a MACsec Key Agreement Protocol Application
Abstract
Along this thesis, a popular testing technique to find bugs and vulnerabilities called fuzzing is implemented in order to exploit an application developed by Technica Electronics Barcelona S.L. First, a study of how the application works is required in order to properly design the tests. This application implements the MACsec (a layer 2 secure communication protocol) Key Agreement Protocol. Then, a testing environment is generated to test the software of Technica Electronics using fuzzing. Finally, results are analyzed to find any security vulnerabilities the application may present. Preliminary results showed that the code is already robust as no bugs were found.
Full text
Fuzzing a MACsec Key Agreement Protocol Application Master Thesis submitted to the Faculty of the Escola T`ecnica d’Enginyeria de Telecomunicaci´o de Barcelona Universitat Polit`ecnica de Catalunya by Roger Sans Falip In partial fulfillment of the requirements for the master in Cybersecurity ENGINEERING Advisors: Ramon Llorca Queralt & Ren´e Serral Graci`a Barcelona, Date 1st of July of 2022 Developed in Technica Electronics Barcelona S.L.
Acknowledgements I would like to thank Ramon Llorca, my supervisor at Technica Electronics Barcelona S.L., for all the help and support given during this last year in the company. Thanks for introducing me to the world of work and for always asking me more and not letting me settle up with few. Also, thanks to my tutor, Ren´e Serral, for solving all my doubts and advising me on every academic aspect. This project could not have been conducted without the help of all Technica Electronics’ team, specially Andreu, Ferran, Johan, and Jordi. Thanks for the patience when I had doubts. Finally, thanks to my family for always being there and for giving me the required support during this project. 2
Contents List of Figures 5 List of Tables 7 Abstract 8 1 Introduction 9 1.1 Automotive Network Trend ............................ 9 1.2 Motivations ..................................... 10 2 State-Of-The-Art 11 2.1 MACsec ....................................... 11 2.1.1 MACsec Phases ............................... 11 2.1.2 MACsec Frame ............................... 14 2.1.3 Testing Tools to Test the MKA Software Module ............ 15 2.2 Fuzzing ....................................... 17 2.2.1 Types of Fuzzers .............................. 17 2.2.2 Vulnerabilities To Be Found ........................ 18 2.2.3 Fuzzer Selected ............................... 19 2.3 libFuzzer ....................................... 19 2.3.1 Mutation-Based Coverage-Guided Fuzzers ................ 19 2.3.2 LibFuzzer Overview ............................ 20 3 Implementation 23 3.1 Testing Environment Overview .......................... 23 3.2 MKA Block Diagram ................................ 24 3.2.1 Interaction Issues .............................. 25 3.3 Mock Functions ................................... 25 3.3.1 Mock Functions Criteria .......................... 26 3.4 Implementing the Fuzzers ............................. 28 4 Results 29 4.1 Libfuzzer’s Output ................................. 29 4.1.1 Coverage .................................. 29 4.2 Results ........................................ 31 4.2.1 Modules Results .............................. 31 4.2.2 MKA PAE Core Results .......................... 32 3
5 Conclusions and Future Work 33 5.1 Conclusions ..................................... 33 5.2 Discussion ...................................... 33 5.3 Improving our Fuzzers and Future Work ..................... 35 A LogOn Mock Functions Implementation 36 B Fuzzer Implementation Example 37 B.1 t fuzzing unit struct Example ........................... 37 B.2 LLVMFuzzerTestOneInput Example ....................... 37 C Edge 39 D Implemented Scripts 40 D.1 Coverage Cleaner Script .............................. 40 D.2 Results Extractor Script .............................. 40 4
List of Figures 1.1 Evolution of Car Communication Topologies [14]................ 9 1.2 MACsec in the OSI model [14].......................... 10 2.1 Port Authentication & Access Validation using EAP-TLS ........... 12 2.2 MACsec Key Structure ............................... 12 2.3 Port Authentication & Access Validation using PSK .............. 12 2.4 MKA Protocol Step ................................ 13 2.5 MKA Key Structure ................................ 13 2.6 MKA Message Exchange .............................. 14 2.7 MACsec MPDU [9]................................. 14 2.8 MAC Security TAG [9]............................... 15 2.9 Comparison of different vulnerability discovering techniques [10]....... 16 2.10 Fuzzing Process ................................... 19 2.11 libFuzzer Overview[21]............................... 20 3.1 Testing Environment Overview .......................... 23 3.2 MKA Block & Modules Diagram ......................... 24 3.3 PAE State Machines - Overview and Interfaces [8]............... 26 3.4 Logon Functions to Call Example ......................... 27 3.5 Mock Function Structure of Type Void ...................... 27 3.6 Mock Function Structure of Type Boolean .................... 27 3.7 Mock Function Structure of Type X ....................... 28 3.8 Fuzzer’s Code Structure .............................. 28 4.1 LibFuzzer Output Example ............................ 30 5.1 Double Free ..................................... 34 5.2 Fuzzer Output When Finding the Double Free ................. 34 A.1 Mock Functions Declaration (mocks.h) ...................... 36 A.2 Mock Functions Definition (mocks.cpp) ..................... 36 B.1 t fuzzing unit Example ............................... 37 B.2 LLVMFuzzerTestOneInput Example ....................... 38 C.1 Edge Example ................................... 39 D.1 Coverage After Coverage Cleaner Script ..................... 40 D.2 Printed Extracted Results from Log File ..................... 41 5
D.3 Marked Uncovered Program Counters of Source File .............. 42 6
List of Tables 4.1 Fuzzing Tests Results MKA PAE Core Modules ................. 31 4.2 Fuzzing Tests Results MKA PAE Core Block .................. 32 4.3 Modules Coverage when executing the whole Block ............... 32 7
Abstract Along this thesis, a popular testing technique to find bugs and vulnerabilities called fuzzing is implemented in order to exploit an application developed by Technica Electronics Barcelona S.L. First, a study of how the application works is required in order to properly design the tests. This application implements the MACsec (a layer 2 secure communication protocol) Key Agreement Protocol. Then, a testing environment is generated to test the software of Technica Electronics using fuzzing, where the application is tested using different fuzzer configurations. Finally, results are analyzed to find any security vulnerabilities the application may present. Preliminary results showed that the code is already robust as no bugs were found. 8
Chapter 1 Introduction In-vehicle network communication between car electronic components represents an important challenge regarding security and performance in the automotive industry. During the last couple of years, due to the limitations of IPsec in this industry, there has been a revolution as the implementation of a layer 2 secure network protocol, called Media Access Control Security (MACsec), is on the way to be present in the near future. In this work, we aim at trying to exploit the custom implementation of the MACsec’s key agreement protocol (MKA), defined in the IEEE802.1X-2020 Standard, developed by Technica Electronics Barcelona S.L.. In this introductory chapter, there are discussed this new trend in the automotive industry, and the motivations behind this Master’s Thesis. 1.1 Automotive Network Trend Car communication topology is evolving to a zonal architecture. The car infrastructure will consist of a central computer that has direct connection to different zonal gateways, which will control car electronic components (see Figure 1.1). This architecture will provide high flexibility and will facilitate maintenance duties. Figure 1.1: Evolution of Car Communication Topologies [14] 9
techniques, it was decided to use fuzzing. The reasons are the following [18]: •Fuzzing is an appropriate tool to test closed systems. A closed system is a system which remains independent from interactions of third parties (hardware, software, and updates from third parties are kept out). In our case, the MKA installed inside a car will remain as a closed system. •It tests the robustness of a program. •Simple, automated, and free of preconceptions of the system test that can easily escalate. •Fuzzing perfectly complements manual testing as it finds vulnerabilities that often can be missed by the human eyes. The MKA to be tested has been already manually tested. So, fuzzing will complement it. •According to Jun Li et al. [10], fuzzing is the most effective and efficient state-of-the-art vulnerability discovery technique (see Figure 2.9). •MKA application is programmed in embedded C. As it is a non-interpreted language, it is more susceptible to memory corruption vulnerabilities, which may lead to critical security issues. Among the most found vulnerabilities by fuzzing there are buffer overflows (see Section 2.2.2.1), which is an example of this kind of vulnerabilities. •MKA is a long-live user-level application. Therefore, it is very dangerous for the system if it collapses. Fuzzing is well-known for finding vulnerabilities such as memory leaks that may lead to a system collapse (see Section 2.2.2.2). Figure 2.9: Comparison of different vulnerability discovering techniques [10] However, fuzzing alone does not guarantee security in all aspects. The best way to reach that is by complementing. These are some disadvantages fuzzing may present [18]: •Low code coverage. •Low time efficiency. •Less effective with non-crashing related vulnerabilities like viruses, trojans... •No debugging possibilities. But, after considering all the advantages and disadvantages, fuzzing was seen as the technique that fits the most to test the MKA application. To get into more details about this technique, see Section 2.2. 16
2.2 Fuzzing Fuzzing is a testing technique that aims to find bugs and security vulnerabilities by feeding a program with randomly generated input data and monitoring the execution state. This section is going to cover the different types of fuzzers, and different vulnerabilities it can find. Then, it is going to be introduced the fuzzing library used in this Master’s Thesis. 2.2.1 Types of Fuzzers The primary classification of Fuzzers is by the traffic injected [10]: •Generation-based: Requires previous knowledge of the program input. It is more complex as it generates the inputs from scratch. •Mutation-based: Mutates fields of valid input values. Simpler and easier to start with. Nonetheless, this is not the only way to classify the types of fuzzers. In sections 2.2.1.1, 2.2.1.2, and 2.2.1.3 it is covered the other different classifications [10] [11]. 2.2.1.1 Dependence on Source Code Fuzzers can also be classified by at which level of the application they will act: directly with the source code, at the application level, or a mix of both. •White Box: The Fuzzer has full access to the source code. It ensures that the code is built correctly. However, it does not verify that the requirements of the software being tested are met. •Black Box: The fuzzer has no access to the source code. It is independent from the platform and it checks that the requirements specified are being met. •Gray Box: Has no access to the internals but gains information about it through analysis. It checks both, that the code is built correctly and that it works. 2.2.1.2 Strategies of Exploring the Program This classification aims to define fuzzers by how much code they cover. Fuzzers can be programmed to test a certain part of the code or can be guided to test as much code as possible. •Directed Fuzzing: Focuses on exploiting a specific part of code. •Coverage-guided Fuzzing: Tries to cover as much code as possible. It is slower than directed fuzzing but it may discover more bugs and vulnerabilities. 2.2.1.3 Feedback Based Finally, fuzzers can be split by the type of input they generate, depending of whether the fuzzer follows some input format or not. •Dumb Fuzzer: Generates completely random inputs without considering any input format. It focuses more on parsing bugs. •Smart Fuzzer: The program knows the input format and generates a valid input. Passes the parsing checker and gets to exploit other parts of the code that a dumb fuzzer may 17
not reach. 2.2.2 Vulnerabilities To Be Found Fuzzing is the most efficient and effective technique to discover vulnerabilities [10]. It is widely used by large and big companies. For example, Google reports all the issues found by fuzzing on the following page: [16]. In this section there are remarked some of those vulnerabilities. Note: These are not all the vulnerabilities fuzzing can find. There are more of them like for example double frees or use-after-frees. 2.2.2.1 Buffer Overflows Consists of, intentionally or unintentionally, overwriting the memory being used by some process. This vulnerability may lead to application crash, or to executing arbitrary code (access control). It is a common vulnerability for non-interpreted languages like C or C++. According to OWASP [17], buffer overflows mostly occur in code that: •Relies on external data to control its behavior. •Depends upon properties of the data that are enforced outside of the immediate scope of the code. •Is so complex that a programmer cannot accurately predict its behavior. 2.2.2.2 Memory Leak It happens when memory blocks are not freed when they are no longer used. It can lead to abnormal program behavior or to a denial of service. According to OWASP [19], the severity of this issue depends on the application itself: •Kernel processes - Critical: The memory dedicated to the kernel is lower than user-level applications. •User-level applications with short life - Little: Lost memory is recollected after program termination. •User-level applications with long life - Very Dangerous: Continuous wastage of memory until all RAM resources are consumed. 2.2.2.3 Out-of-bound Write/Read Given a buffer, when it is possible to read/write other memory locations that do not correspond to the buffer. Therefore, in the read case, sensitive information may be exposed to an attacker. Regarding the write case, it can result in a crash, corruption of data, or execution of other code. Despite other vulnerabilities may be discovered by fuzzing techniques, vulnerabilities like out-of-bound write/read, and information leakages are not a threat for the MKA Software Module. Keys are stored in a packet that is encrypted in the back-end of the OEM (Original Equipment Manufacturer). When they are installed in the ECU (Electronic Control 18
Unit), the HSM (Hardware Security Model) of the ECU has the correspondent certificates to decrypt the packet and store the keys. 2.2.3 Fuzzer Selected For this project, it has been used libFuzzer, a coverage-guided and mutation-based fuzzer (See Chapter 2.3). libFuzzer is integrated into the Clang C Compiler and it allows integrating the coverage-guided fuzzer logic into a C / C++ application. In this case, the C / C++ application is out MKA Software Module. 2.3 libFuzzer After introducing the general concepts and giving some context to this project in Chapter 1, we can now dive into understanding how the fuzzing tool works and what are its characteristics. First, it is going to be explained how a Mutation-Based Coverage-Guided fuzzer works to understand libFuzzer, covered in Section 2.3. 2.3.1 Mutation-Based Coverage-Guided Fuzzers First, it is required to specify the meaning of path for this chapter. A path is the sequence of executions of certain parts of the code. The process of this kind of fuzzers can be summarized in Figure 2.10. Figure 2.10: Fuzzing Process To get the fuzzer work, it is required to have an input seed. This input seed is the starting point of both, the paths and the mutations. Given the seed, the fuzzer starts mutating it. The fuzzer variates the valid input variables in order to discover a different behavior in our application. In Figure 2.10, Mutations 2 and 3 trigger the same behavior as the input seed, while Mutation 1 discovers a new path. The fuzzer saves that mutation and mutates it looking for uncovered paths. This process is repeated ad eternum until a bug is found or the fuzzer is stopped. To sum up, a mutation-based coverage-guided fuzzer needs a valid input as a reference. Then, it mutates it looking for unknown paths. When a new path is found, the input that triggered it is kept for future mutations aiming at the same objective. However, if one of this paths leads to a bug, the fuzzer reports it stopping its execution. 19
2.3.2 LibFuzzer Overview LibFuzzer can be described as in Figure 2.11. There are 4 main components: Corpus, Fuzz Engine, Sanitizer, and Fuzz Target (the application to be tested). Fuzz Engine, Sanitizer, and Corpus are explained in subsections 2.3.2.1,2.3.2.3,2.3.2.4 respectively. Figure 2.11: libFuzzer Overview[21] 2.3.2.1 Fuzz Engine The fuzz engine is the core of libFuzzer. It has three main functionalities. The first one is to mutate the given inputs. Once done, it will inject them into the fuzz target in order to test it. The second functionality of this component is to manage the coverage of the test. Through the usage of the SanitizerCoverage, libFuzzer is able to identify new triggered paths and unknown behaviors. Then, it takes the mutated input of that execution and passes it to the Corpus, which saves it to use it for future variations. The last main functionality it presents is to identify bugs, errors, and vulnerabilities from the sanitizers callbacks. However, LibFuzzer also presents other secondary capabilities, like the usage of dictionaries to improve the path discovery. 2.3.2.2 Dictionaries LibFuzzer implements the usage of dictionaries. The dictionaries are custom files that contain interesting byte sequences that the fuzz engine will try to clobber them in various ways. This feature speeds up the search of unknown behaviors and paths. 20
In addition, after the execution of a libFuzzer test, the fuzz engine will let the user know the most used byte sequences as recommended inputs for the dictionary file. 2.3.2.3 Sanitizers Sanitizers are instruments that permit tracking the execution of a code to report execution errors. These execution errors can be bugs, vulnerabilities, or simple timeouts when the code gets stuck. In addition, they can aim at different tasks. For example, as mentioned in Section 2.3.2.1, libFuzzer has a dedicated sanitizer in charge of the coverage, the SanitizerCoverage. Regarding the bug and vulnerability related sanitizers, stated below are the available options: •AddressSanitizer [1]: Fast memory error detector. Among other memory related bugs, it detects stack and heap overflows, use-after-free, and double free bugs. However, this sanitizer uses more real memory than a native run and it may cause memory overheads. •ThreadSanitizer [4]: This sanitizer is programmed to detect data races. A data race occur when multiple tasks or threads access a shared resource without sufficient protections, leading to undefined or unpredictable behavior. It has the same limitation as the AddressSanitizer. •MemorySanitizer [2]: It is a detector of uninitialized reads. This happens when the application tries to read a value that has not been initialized yet. It presents memory overheads limitations. •LeakSanitizer [3]: Memory leak detector. It is recommended to use it along with AddressSanitizer in order to cover both, memory error and leak detection. •UndefinedBehaviorSanitizer [5]: It detects undefined behaviors through modifying the program at compile-time. As specified in the CLANG documentation, it is not possible to combine AddressSanitizer (plus LeakSanitizer), ThreadSanitizer, and MemorySanitizer in the same execution. It can only be used one of them [6]. For our project, it was decided that the AddressSanitizer would fit better as it covers all memory error related bugs while the other two sanitizers are more specialized in finding specific bugs. So, the main sanitizers used in the fuzzing implementations are: •AddressSanitizer •LeakSanitizer •UndefinedBehaviorSanitizer However, for some tests there have been used the other sanitizers: ThreadSanitizer and MemorySanitizer. 2.3.2.4 Corpus The last component of libFuzzer is the Corpus. It consists of a sample of valid inputs that will continuously grow with mutations that triggered a new path or a bug. The former ones are kept for future variations [12]. The main advantages of using the Corpus is that it keeps track of the discovered paths 21
and the inputs that reached them. It is crucial not to waste time and rediscover already discovered paths in separate fuzzing execution. Furthermore, it acts as a regression check to confirm that previous inputs still work without any problem. 22
Chapter 3 Implementation This section covers the different parts of the implementation process. First, in Section 3.1 is described the environment where the fuzzer is executed. In Section 3.2, there are stated the different components of the MKA Software Module that can be tested, and which ones are the ones tested in this Master’s Thesis. The last part of this chapter (Section 3.3 and Section 3.4) contains the criteria followed when designing and programming the different tests. 3.1 Testing Environment Overview LibFuzzer is a fuzzing library that works with two compiler tools: CLANG and LLVM. The former is a subproject of the later. CLANG is a front end compiler, while LLVM is a back end compiler. So, to be able to compile and execute libFuzzer applications, both compilers are required. That’s why it was created a first docker container containing the different compilers, debuggers, adding also python3 support. Figure 3.1: Testing Environment Overview In Figure 3.1 is described the environment’s overview. The first component is a Docker 23
container that contains the previously mentioned compiler tools. It also adds python3. Then, this Docker unit is inherited by what will be the main Fuzzing Environment Container. This last unit adds libFuzzer library and its dependencies. Also, it contains the ut framework python script developed by Technica Electronics S.L. Its main purpose is to compile the fuzzer binaries through a file called wscript, which contains the description of our test (all the included files in our compilation, the sanitizers used, test flags...). The environment is run in a dedicated server from Technica Electronics. This server has an Intel core i9 12900k with 24 CPU cores and 256 GB of RAM. The number of CPU cores is important for the test executions due to libFuzzer enabling parallelism. There can be executed more than one fuzzing test at the same time where each test takes one core of the CPU. 3.2 MKA Block Diagram The MKA Software Module is divided into 5 main blocks described in Section 3.2. The blue components inside the MKA PAE Core block are its modules. Figure 3.2: MKA Block & Modules Diagram •Helpers & Libraries: Contains the libraries requires to execute the MKA Software Module. •RTOS Integration: Block that integrates the MKA into a Real-Time Operating System. •Linux Integration: Contains the implementation of the protocol into Linux systems. •PAE Management: In charge of loading the corresponding configuration to the whole MKA application. •MKA PAE Core: It is the most important block of the diagram. It is the responsible 24
to implement all the functionalities of the MKA Protocol. It has 4 different modules [8]: –Logon: Process that manages the use of authentication credentials, for initiating use of the PAE’s Supplicant and or Authenticator functionality, for deriving CAK, CKN tuples from PAE results, for maintaining PSKs, and for managing MKA instances. –MAC Security Key Agreement Entity (KaY): manages the operation of zero or more MKA instances, each identified by a CKN and using a specified CAK. –MAC Security Entity (SecY): The entity that operates the MAC Security protocol within a system. –PAE Control Port state machine(CP): State machine that controls the whole PAE Block. In this project it has been tested the whole MKA PAE block and modules. 3.2.1 Interaction Issues Figure 3.3, drawn in the IEEE802.1X-2020 standard [8], describes the interaction between the different MKA PAE Core components. Modules may rely on the interactions with other modules. Therefore, in order to test each module independently, it is required to emulate the behavior of the other modules in order not to condition the module being tested. The emulated functions are called mock functions, which are explained in Section 3.3. Note: The standard contemplated the modules “Supplicant PCAP”, “Authenticator PCAP”, and “CAK Cache” (see Figure 3.3. Technica decided not to implement them as it is used the PSK method and it is not required to identify a peer as the supplicant and the other as the authenticator. 3.3 Mock Functions To better understand the functioning of these functions, lets take the LogOn module as an example. If the interactions that the LogOn module depends on are isolated, the diagram is reduced in Figure 3.4. Note: The arrows that are pointing from the LogOn to the other components are the interactions Logon depends on from the other modules. So, when implementing the fuzzing tests for the Logon module, it is required to emulate the functions ConnectPending, ConnectUnauthenticated, ConnectAuthenticated, ConnectSecure from the CP module, and the functions CreateMKA, DeleteMKA, Participate from the KaY module. The criteria of what these functions should contain and return is discussed in Section 3.3.1. 25
4.2.2 MKA PAE Core Results This section contains the results of the fuzzing tests of the MKA PAE Core block. The block’s source file is mka main.c. The advantage of running the whole block is that we do not need to emulate the behavior of its modules. However, it still requires mock functions to emulate some functions of the other blocks of the diagram in Figure 3.2. As this component is more complex, there are tested the sanitizers that mutually exclude. In total, there are three main executions: one with the AddressSanitizer and LeakSanitizer, another one with the MemorySanitizer, and a last one using the ThreadSanitizer. Table 4.2 gathers the results provided by the fuzzer when testing the whole MKA PAE Core Block. Due to shortage of time, the tests using MemorySanitizer and ThreadSanitizer were shorter than the AddressSaniter ones and they were executed at the same time. Also, ThreadSanitizer was executed in another server of Technica Electronics which has 16 CPU cores and 126GB of RAM. Therefore, as each test takes a CPU Core, the amout of tests carried out with ThreadSanitizer are less than with MemorySanitizer and AddressSanitizer. Coverage mka main.c Sanitizer Used Tests Executed Units (Thousands of Millions) Average Execution Time per Test (hours) Total Execution Time (days) Detected Functions Entered Functions Edge Coverage Bugs Found AddressSanitizer + MemoryLeak- Sanitizer 24 87 84 84 100% 100% 81,55% 0 MemorySanitizer 24 51 54 54 100% 100% 81,55% 0 ThreadSanitizer 16 11 54 36 100% 100% 76,54% 0 TOTAL 64 149 N/A 174 N/A N/A N/A 0 Table 4.2: Fuzzing Tests Results MKA PAE Core Block Nontheless, as the block’s modules are included in the execution, libFuzzer also provides its coverage information. Table 4.3 contains the comparison among the different modules’ coverages using the different sanitizers. Coverage AddressSanitizer Coverage MemorySanitizer Coverage ThreadSanitizer Module Detected Functions Entered Functions Edge Coverage Detected Functions Entered Functions Edge Coverage Detected Functions Entered Functions Edge Coverage KaY 100% 93,75% 85,4% 100% 93,75% 85,44% 100% 93,75% 67,67% LogOn 100% 83,33% 73,68% 100% 83,33% 73,68% 100% 83,33% 63% CP 100% 97,37% 89,79% 100% 97,37% 89,79% 100% 97,37% 77,49% SecY 100% 90% 81,72% 100% 90% 82,5% 100% 90% 70,67% Table 4.3: Modules Coverage when executing the whole Block 32
Chapter 5 Conclusions and Future Work The purpose of this chapter is to discuss the results, and propose which should be the next steps on this Master’s Thesis. 5.1 Conclusions In this work, it has been presented an implementation of libfuzzer in order to try to exploit pieces of code. Given a target to exploit, there are injected a group of inputs that mutate to reach as much code as possible monitoring the executing state. If an error is triggered, it is reported by the fuzzer. The results shown in Section 4.2.2 presented a high coverage reached in every module when executing the whole MKA PAE Core block. On the other hand, when testing each component separately, the coverage reached in the KaY module is not as high as expected. The later case was expected to reach all its functions by executing its main functionalities. However, they were not reached. As far as the security purposes of the fuzzer are concerned, no bugs were found in any of the tests. This means that, either the tests is somehow malfunctioning (which is not the case as it is later shown), or the code is already very robust. In Section 5.2 it is discussed what could be the reasons on why no errors were triggered during the execution of the test. 5.2 Discussion The first thing that draws the attention about the results is that, after executing the different fuzzers for such a long time and getting that much coverage, it has not found any bug or vulnerability. What was thought after analyzing them was: “Are we sure that the fuzzer is able to find errors?”. So, a double free error was manually hidden (Figure 5.1) inside the tested code. Then, when executing the fuzzer, it managed to trigger the hidden errors. In Figure 5.2 there is the output of the fuzzer when it found the double free error. So, the answer to our question was yes, the fuzzer works correctly. 33
char *ptr; char *ptr1; ptr=malloc(5 * sizeof(*ptr)); ptr1=ptr; free(ptr); free(ptr1); Figure 5.1: Double Free Figure 5.2: Fuzzer Output When Finding the Double Free After validating that the fuzzer was working as expected, it was concluded that there could be three main reasons on why there were no bugs found. Design Flaws in the Fuzzing Test If the fuzzer is perfectly working, then the next thing that can be wrong is the design and configuration of our tests. An example of what would be considered as a design flaw would be the inputs chosen to mutate or the sanitizers selected. Not Reaching Full Coverage The main idea on this point is that, if we do not reach every line of the code, there can be some hidden errors that may not be discovered by the fuzzer. The Code is Already Robust Before implementing the fuzzers, there were conducted several unit tests by Technica Electronics Software Department to the MKA PAE Core block and its modules. Therefore, there were a lot of bugs that were already found and fixed. So, the code tested is already robust and it has been proven by the execution of the different fuzzers as they did not find any bug or vulnerability. In Section 5.3 there are propose solutions for the different possible problems and ideas to 34
improve our security tests as future work. 5.3 Improving our Fuzzers and Future Work The future work of this Master’s Thesis is divided in two parts. Firstly, verify if the possible reasons on why there were no bugs found are founded or not. Secondly, improve and complement our security tests of the MKA Software Module. Regarding the first part, several solutions are presented below: •Improve the coverage on the different tests. –Debug to find what can trigger the parts of the code that have not been reached. –Force those functions that have not been entered. For example, the uncovered functions when executing the fuzzer on the KaY module can be forced so the fuzzer executes them. •Add the inputs that were not considered in the tests to verify if they lead to some bug. •Execute the tests of the MKA PAE Core Block’s modules with the sanitizers that cannot work with AddressSanitizer: ThreadSanitizer and MemorySanitizer. Once the verifications have been conducted, the next steps on the Master’s Thesis would be: •Design and execute fuzzing tests in the other blocks defined in Figure 3.2 to cover the whole application. •Complement our tests by using other fuzzers such as AFL++ and HonggFuzzer. Arijit Pramanik and Ashwini Tayade showed in their study about fuzzers that those two fuzzers and libfuzzer have different final results on the same piece of code [22]. They have different coverages, approaches, and different bugs found. So, it would be interesting to conduct the same experiments using AFL++ and HonggFuzzer. 35
Appendix A LogOn Mock Functions Implementation The mock functions are declared in a file called mocks.h and defined in a file called mocks.cpp. In Figure A.1 there is the declaration of the mock functions for the LogOn tests. In Figure A.2 there are defined the different mock functions following the criteria specified in Section 3.3.1. Figure A.1: Mock Functions Declaration (mocks.h) Figure A.2: Mock Functions Definition (mocks.cpp) 36
Appendix B Fuzzer Implementation Example B.1 t fuzzing unit struct Example Figure B.1: t fuzzing unit Example B.2 LLVMFuzzerTestOneInput Example Note: Due to the randomization of the fuzzing variables defined in the fuzzing unit structure, they can be used as a random variable to build logic tables for functions that require different boolean parameters. 37
Figure B.2: LLVMFuzzerTestOneInput Example 38
Appendix C Edge As mentioned in Section 4.1.1, edges cover all the different paths the code may take during its execution. In Figure C.1a there is a function where there are marked the different lines the code can reach. This function checks whether the parameter passed is even or odd. When executing the function, it will always start from point A. Then, it will check if the variable num is even (point B). If so, it will print that it is an even number in point C. If not, the code will jump from point Bto point Dprinting that it is an odd number. Then, whether it is in Cor D, it will exit the function in point E. All this information can be summarized as a Control Flow Graph in Figure C.1b (a) Example Code (b) Control Flow Graph Figure C.1: Edge Example So, in this example there are a total of 5 edges: •A ->B •B ->C •B ->D •C ->E •D ->E 39
Appendix D Implemented Scripts D.1 Coverage Cleaner Script The script takes the steps stated below: 1. Iterates over all the libfuzzer’s log files in the specified folder. 2. In each function presented in the coverage section of the log file, erases the conflicting UNCOVERED PCs, counting how many UNCOVERED PCs have been erased. 3. Subtracts the number of erased UNCOVERED PCs to the total number of edges of that function. 4. Creates the modified file without the UNCOVERED PCs erased and the recalculation of edges in a folder called “fixed coverage”. Figure D.1 is the result after executing the script on the log file presented in Figure 4.1. All the UNCOVERED PCs in line 0 and the duplicated ones have been removed. Also, the total number of edges has been recalculated. Figure D.1: Coverage After Coverage Cleaner Script D.2 Results Extractor Script The script follows the next steps: 1. Iterates over all the libfuzzer’s log files in the specified folder. 2. For each function in the log file from the component being tested, checks if it has been covered or not, keeps the uncovered PC, and keeps the edges reached and the total number of edges of the function. 3. Calculates the different coverages and prints them. 40
4. Prints the final statistics of that test. 5. In case of a bug found, prints that an error has been found. 6. After iterating over all the files in the specified folder, it marks in the source file of the module being tested the lines that have not been covered by adding “-¿” at the beginning of the line. In Figure D.2 is shown how the results are shown to the user. It is shown the different coverages of the different log files and, in case of an error, it is also remarked. Then, in Figure D.3 is shown how the uncovered program counters are marked in the source file of the module being tested. In this case, lines 116 and 117 are the ones marked as uncovered. Figure D.2: Printed Extracted Results from Log File 41