S PECULARIZER : Detecting Speculative Execution Attacks via Performance Tracing

,


Introduction
Speculative execution attacks exploit micro-architectural design flaws and side channels in modern processors and enable unprivileged processes to exfiltrate sensitive information across security boundaries.These attacks have seriously undermined the fundamental security assumptions made in the design of the operating systems and have been in the spotlight since their very first public disclosure in early 2018.The most prominent examples of speculative execution attacks are Meltdown [27] and Spectre [23], and later variants, such as Foreshadow [41], Micro-architectural Data Sampling (MDS) [31,37,43], Load Value Injection (LVI) [42] are also well-known examples of such attacks.
In this paper, we apply the principle of defense-in-depth and propose SPECULAR-IZER 4 , a software framework for uncovering speculative execution attacks using hardware performance tracing features available in commodity processors, i.e., hardware performance counters (HPC) and processor trace (PT).SPECULARIZER complements existing defenses against speculative execution attacks, by offering a capability of logging both architectural and micro-architectural behaviors of the monitored software to enable forensic analysis and offline attack detection.
In contrast to prior work that detects cache side channels to identify speculative execution attacks [19], which can be easily circumvented by attacks using alternative side channels, SPECULARIZER is inspired by the following key observations: Although speculative execution attacks may leverage a variety of micro-architectural side channels (e.g., TLBs, caches) to leak secrets from speculatively executed instructions, the invariant of these attacks is the method with which the speculative execution can be triggered.In exception-based attacks, speculative execution is triggered by exceptions, which are either handled or suppressed; in misprediction-based attacks, speculative execution is triggered either by control-flow misprediction or by misprediction in the memory disambiguation.Therefore, SPECULARIZER utilizes the inevitable execution patterns of exceptions and mispredictions as signatures.
We identify PT packets and HPC events that can reveal crucial information necessary for attack detection, such as control-flow transfers for exception handling and TSX aborts, mispredicted branch instructions, machine clears due to memory order conflicts, etc.While each type of PT or HPC record alone is insufficient for reconstructing all attack activities, collectively they offer greater insight into the micro-architectural level behavior of the monitored applications.Therefore, we develop techniques to combine HPC and PT data to construct execution traces.With these traces, we build classification models using the Long Short Term Memory (LSTM) network to perform the classification of attack and benign programs.SPECULARIZER consists of two components: an online trace collection component that is integrated into the operating system of a production machine, on which the monitored application runs, and an offline attack detection component that performs HPC and PT records parsing, trace processing, and trace classification, which are timeconsuming and hard to finish in real-time.In fact, rarely do HPC or PT-based monitoring systems perform real-time analysis [14,51].As such, SPECULARIZER is best suited for VM or container-based cloud systems, where suspicious workloads from untrusted cloud tenants are monitored on cloud servers and forensic analyses are performed on separate servers to detect attack activities.While deferred attack detection does not prevent the attacks from happening, it can trigger further investigation of attacks to identify their sources and assess their consequences.
We have implemented a prototype of SPECULARIZER and evaluated its effectiveness and efficiency in a lab setting.Specifically, to evaluate SPECULARIZER, we develop parameterized models for each type of the speculative execution attacks we aim to detect, and then automatically generate thousands of attack variants by tuning the parameters of these models.With the data sets collected from both benign and attack samples, the evaluation of SPECULARIZER suggests it has promising detection accuracy while inducing reasonable performance overhead.The evaluation results also indicate that SPECULARIZER significantly raises the bar for performing speculative execution attacks even if the attackers understand the detection mechanism.
Contributions.The paper makes the following contributions: SPECULARIZER is the software tool that detects speculative execution attacks, by their triggers of speculative execution rather than specific covert channels.SPECULARIZER provides new insights of combined use of multiple performance tracing hardware features, e.g., PT and HPCs, in the context of offline attack detection.The paper presents parameterized models of speculative execution attacks and methods to automatically generate attack variants with varying attack success rates.The paper presents a prototype implementation of SPECULARIZER and empirically evaluates its selection of parameters, its effectiveness, and performance overhead.

Background
Speculative Execution Attacks.A speculative execution attack contains the following components [3]: Speculation primitive triggers speculative execution of instructions.Disclosure gadget transmits information through a side channel.Disclosure primitive reads the side-channel information that was transmitted by the disclosure gadget.As such, a speculative execution attack can be performed in the following steps: executes the speculation primitive to trigger speculative execution of instructions.utilizes the speculative instructions (including the speculation primitive itself) to access secrets across the security boundary; speculatively executes the disclosure gadget to encode the secret value into the cache states; uses the disclosure primitive to decode the secret data from cache states.
According to the speculation primitives, we classify speculative execution attacks into the following three categories [11].Misprediction-based attacks leverage branch, Store-To-Load (STL), and memory-order buffer mispredictions as the speculation primitive and performs attacks before the correct target is resolved.Exception-based attacks and assistance-based attacks use exceptions (e.g.Page fault, General Protection fault, etc.) and microcode assists (e.g.line-fill buffer, store buffer, and load port conflict [1], etc.) as speculation primitive, respectively, and speculatively execute instructions before they are handled by the processor.
Performance Tracing Hardware.Intel PT is a hardware feature available in Intel processors since Broadwell.It is designed to record the information regarding the controlflow transfers of software programs with very low performance overhead.The PT hardware generates PT packets to reconstruct the timestamped control flow for a program [17,45].HPCs are a set of model-specific registers that can be used to count user-selected processor architectural or micro-architectural events.Each HPC register can be configured to count a specific event supported by the processor.At runtime, when the specified event happens, the corresponding HPC counter will be incremented.
The HPCs have two different approaches for software to collect event samples.First, when the performance monitor interrupt (PMI) is enabled in a specific counter, a PMI will be triggered when the counter overflows, which provides the software with an opportunity to handle the HPCs data [7].However, the large volume of interrupts dramatically increases the performance overhead.Second, to address the performance issues, Intel introduces Precise Event-Based Sampling (PEBS), which can store the events in a buffer (dubbed Debug Store (DS) area).Only one interrupt is triggered when the buffer is almost full (determined by a threshold).
3 Threat Model and SPECULARIZER Overview Threat model.All misprediction-based and exception-based attacks are in-scope of this paper.Our method detects these two types of attacks by monitoring its execution of the speculation primitives, which are either a branch instruction that takes time to resolve its target address or a memory load that accesses data across the security boundary.We consider MDS and LVI attacks that are triggered by exceptions, which are the most common cases in current state-of-the-art attack examples, as exception-based attacks, and hence SPECULARIZER will detect those attacks.SPECULARIZER architecture.The overall architecture of SPECULARIZER is shown in Fig. 1, which consists of two components: Online Trace Collection and Offline Attack Detection.Online Trace Collection is an online component that runs on a production system, running as system programs, which produces execution traces collected using PT and HPC.Offline Attack Detection is a component that runs offline that includes two parts (i.e.Trace Processing and Attack Detection), possibly on a separate machine, and performs analysis of the collected traces to identify speculative execution attacks.Online trace collection.To detect exception-based speculative execution attacks, SPEC-ULARIZER monitors exceptions using PT.When the attacks use TSX to suppress exceptions, PT packets can record TSX aborts; when the attacks handle the exceptions directly, PT packets can record control-flow transfers that correspond to exception handling.
To detect misprediction-based attacks, SPECULARIZER needs to monitor the pattern of mispredictions, which includes misprediction in control-flow predictors (branch prediction units like BTB, PHT, and RSB) and data-flow predictors (the memory disambiguator in load/store buffers).However, PT is insufficient to monitor these microarchitectural events.HPCs are utilized instead.The limitation of using HPCs to monitor misprediction is that they are asynchronous with execution context, which is insufficient for detecting misprediction-based attacks.To address this problem, SPECULARIZER utilizes Intel PT to provide the execution contexts.
Offline attack detection.During the execution of benign programs, exceptions, TSX transaction aborts, and misprediction in control-flow and data loading is normal.There- fore, we cannot simply detect speculative execution attacks using exception handling/suppressing and branch/data misprediction as signatures.Instead, patterns of exceptions and mispredictions must be learned from both benign and attack programs and utilized to detect attacks in the program to be monitored.

Trace Collection
The overall workflow of trace collection (shown in Fig. 2) is as follows: 1 SPECULAR-IZER enables PT and HPC to monitor the execution of the target program and specify the memory buffer to record the execution traces. 2 When the memory buffer is full, an interrupt is triggered. 3After replacing the full buffer with an empty one for the hardware to continue recording data, the interrupt handler sends a signal to the userspace data collector. 4Upon receiving the signal, the data collector reads data from the full buffer. 5Finally, the data collector saves the collected data into files.
Collecting Traces from HPC.To gain visibility into micro-architectural events, SPEC-ULARIZER activates HPCs to monitor branch mispredictions (e.g.direct, indirect branches) and machine clear events caused by memory order conflicts by activating the events BR MISP RETIRED.ALL BRANCHES and MACHINE CLEARS.MEMORY ORDERING.SPECULARIZER uses the PEBS to monitor the branch misprediction event and PMI to monitor the memory order conflict event, as the memory order conflict event is not available in PEBS mode.These two events are monitored simultaneously on different HPCs.When monitoring in the PMI mode, the overhead mainly comes from PMI handling.When monitoring in the PEBS mode, although the interrupts are significantly reduced, there are still two sources of overhead: First, writing each PEBS record into the DS area takes about 200 nanoseconds [8].Second, DS-area-overflow interrupts need to be generated when the DS area is full (maximum size is 4 MB).Thousands of interrupts need to be generated during trace collection for one application.
Two performance optimization were implemented: First, SPECULARIZER implements a ring buffer [51] to cache the data in the DS area.Specifically, SPECULARIZER allocates two buffers for the DS area to reduce the overhead introduced by dumping data inside the interrupt handler.When the data in one of the buffers reaches the threshold, SPECULARIZER switches the buffer used by the DS area upon receiving the interrupt.A signal is sent to the user-space component of SPECULARIZER to dump data from the full buffer.Second, to reduce the number of generated PEBS records, SPECULARIZER tunes the PEBS sampling rate (ρ), which indicates the fraction of events (1/ρ) sampled by PEBS to create PEBS records.ρ > 1 means PEBS are sampled less frequently with a higher performance overhead and hence some branch misprediciton information is missing.We will evaluate the impact of ρ on detection accuracy in Sec. 8.
Collecting Traces from PT.To collect control-flow transfer and timestamp information, SPECULARIZER activates Intel PT by setting the following control bits of the MSR IA32 RTIT CTL: TraceEn (to enable PT), BranchEn (to generate controlflow related packets, e.g., TNT, TIP & FUP), OS & User (to monitor both user-mode processes kernel threads), TSCEn, MTCEn & CYCEn (to generate timestamp related packets, e.g.TSC, MTC & CYC).
The overhead incurred in generating PT packets is negligible.The main overhead comes from handling the memory buffer that stores the PT packets when it is full.Unlike PEBS's DS area, which has a fixed size (i.e., 4MB), the memory buffer used by PT can vary.Specifically, PT uses a Table of Physical Addresses (ToPA) to store all generated packets, which is a linked list that links multiple output regions.Therefore, the total size of the ToPA is flexible, and the number of generated interrupts can be controlled to decrease the runtime performance overhead.

Trace Processing
SPECULARIZER processes HPC events and PT packets offline, possibly on a machine that is different from the host that implements the SPECULARIZER monitors.The exceptionbased output sequences are generated using PT traces only, and the misprediction-based output sequences are extracted with information from both PT traces and HPC records.

Processing Exceptions
Among the three approaches to tackling exceptions in exception-based speculative execution attacks, namely handling exceptions, suppressing exceptions with TSX, and suppressing exceptions with branch misprediction, the first two cases trigger an indirect control-flow transfer.Therefore, SPECULARIZER extracts exception-triggered control flow transfers in collected PT records.The third case is categorized as mispredictionbased and discuss later.
Extracting addresses of exceptions.When the exception is handled by exception handlers, the control flow will transfer from user space to kernel space.With the PT packets, we can extract all kernel traces-a sequence of instructions in the kernel space.Afterwards, by comparing those traces with the kernel symbol table, the kernel traces can be used to identify different types of exceptions.
When the exception is suppressed by TSX transactions, the exception type is not revealed through kernel traces.Nevertheless, the exception is recorded by the MODE packet which has a field called TXAbort, with its value as 1.The addresses of the instructions that trigger TXAbort are recorded by the FUP packet that follows.
Extracting timestamps of exceptions.PT can be used to recover the timestamp of exceptions, as PT records the following time-related packets: Timestamp Counter (TSC) packets provide the wall-clock time (wc); Mini Time Counter (MTC) packets are generated periodically based on the core-crystal clock (ccc); a TMA packet is generated immediately after each TSC packet, with a common timestamp copy (ctc) value in its payload; a Cycle Accurate (CYC) packet is generated immediately preceding TIP packets and provides the accurate ctc value since the last CYC packet.To extract exception timestamp, SPECULARIZER calculates ccc for each TIP packet based on the relationship between these time-related packets [7], as PT generates a TIP packet when an exception is raised.
Output.SPECULARIZER analyzes each PT trace offline, identifies and records all exceptions, the virtual address of the instruction that triggers it, as well as the timestamps of the identified exceptions.Two parameters, δ and µ, were involved in the data output: PT traces are segmented into windows of δ CPU cycles, and the attack detection algorithm runs over the traces in each window.
The output of this step is a set of sequences of two tuples, which is denoted as , where e k is the virtual address of the instruction that triggers the exception, c i indicates whether exists an exception of the i th occurrence of the virtual address e k , t i is the timestamp of its occurrence, and µ is the length of each sequence, which is the input of attack detection model in Sec. 6.For each δ-cycle window, one or multiple sequences are gathered: if the total number of exceptions is greater than µ, a new sequence is created; a sequence less than µ is padded to µ with (0, 0).We will evaluate the impact of different values of δ and µ on the effectiveness of the detection algorithm in Sec. 8.

Identifying Branch and Data Misprediction
SPECULARIZER identifies branch and data misprediction from the recorded HPC events.Particularly, SPECULARIZER first extracts the timestamp of each misprediction event from the HPC records, then extracts the timestamp of each branch instruction from the PT traces.Finally, by aligning the timestamp information from the HPC records and PT traces, SPECULARIZER outputs traces of correctly predicted and mispredicted branches for attack detection.HPC records parsing.SPECULARIZER parses the HPC records and identifies the records that are related to either branch misprediction or data misprediction, and then outputs a sequence of two tuples: , where c i is the event (i.e., the branch misprediction or data misprediction) of the ith occurrence of the misprediction and t i is its timestamp.The accuracy of misprediction information could depend on the PEBS overflow threshold ρ discussed in Sec. 4.
PT trace reconstruction.SPECULARIZER first reconstructs the program execution trace and the timestamp value of each branch with packets generated by the PT hardware.Meanwhile, PT timestamp packets are used to reconstruct the timestamp of each branch instruction using the method described in Sec.5.1.By combining program execution trace with the timestamp information, SPECULARIZER outputs a sequence of two tuples: , where b i is the virtual address of the ith occurrence of the branch and t i is the timestamp when the branch is executed.
HPC and PT alignment.SPECULARIZER aligns HPC records with the control-flow transfer information collected from PT to attribute HPC records to a specific branch of the program.The alignment can be performed by matching the timestamp value t i in the two sequences.Particularly, for each element (c k , t k ) in the HPC sequence, we search the PT sequence to find an element with index i that satisfies t i ≤ t k < t i+1 .Then we associate (c k , t k ) with b i .
Output.For each δ-cycle window, each branch instruction b k , SPECULARIZER outputs a set of sequences of two tuples, which is denoted as where t i is the timestamp when the i th execution of the branch b k , c i indicates whether there is a misprediction and the misprediction type (i.e.branch or data) in the i th execution of this branch, and µ is the length of the sequences.

Attack Detection
Given the traces produced in the previous section, SPECULARIZER uses the LSTM to extract the temporal information of the traces for attack detection.SPECULARIZER uses four detection models to detect four different attack types, which are exception-based attacks, misprediction-based attacks exploiting BTB/PHT, RSB, and memory disambiguator, respectively.These four detection models share the same layout: one LSTM layer and one Dense layer.Particularly, the detection model inputs the traces to the LSTM layer and outputs the likelihood for the trace to be an attack (between 0 and 1) from the Dense layer.
An end-to-end construction of SPECULARIZER, therefore, works as follows: (1) for every program monitored, both HPC and PT traces are collected and processed; (2) all processed traces for the program are classified by all four models.If one of the models classifies any of the traces as "attack" with a likelihood higher than a threshold α, the program is labeled by SPECULARIZER as performing speculative execution attacks.

Attack Variants Generation
To systematically evaluate how accurate SPECULARIZER can detect speculative execution attacks, we produce a data set of attack variants.To do so, we first propose parameterized models for speculative execution attacks and then systematically tuning the parameters of these models to generate a set of attack variants.

Exception-based Attack Variants
Modeling attacks.The attack model of exception-based speculative execution attacks is described in Fig. 3 (a) , which depicts the timestamps of exceptions that happened at a specific virtual address of the monitored program; each dot on the timeline represents the occurrence of an exception.N a is the number of exceptions in a cluster that any two consecutive exceptions are no more than L a cpu cycles apart.
To understand the practical implication of N a and L a , we performed an empirical evaluation of these two parameters using the Proof-of-Concept (PoC) code provided by Canella et al. [11].We executed each of the PoC 10, 000 times when the system is idle and report the relationship between minimum N a and the success rate (p) in Table 1.When the system is busy, the N a increases for the same p.Therefore, we only present  the data when the system is idle in Table 1.As we see from the result, when utilizing different speculation primitives, to have p ≥ 95%, N a ranges from 1 to 1,300,000.We also measured the relationship between L a and p.When F LU SH +RELOAD is selected as the disclosure primitive, it takes at least 150, 000 CPU cycles to finish reloading 255 elements (the minimum for encoding one byte).Therefore, with N a = 100, we select L a from 150K, 250K, 350K, 450K, 550K, 650K, 750K cycles.The experiment results suggest that the variation of L a does not have an observable effect on p.
Therefore, in total 9 × 7 × 23 = 1449 attack variants were generated.Then we created 3 separate data sets from these samples.Specifically, we first selected three thresholds (i.e., 85%, 90%, and 95%) for the attack success rate p, as attack variants with low p are meaningless, which will be discussed in Sec. 8. Second, for each p and each type of speculation primitives, we determine the minimum N a such that attack variants with equal or greater N a yield attack success rates larger than the corresponding p (from Table 1).As such, the three data sets have 476, 448, and 399 attack variants, respectively.

BTB/PHT Misprediction Variants
Modeling attacks.To perform a successful misprediction-based speculative execution attack against BTB (e.g., Spectre-BTB) and PHT (e.g., Spectre-PHT), one needs to train (poison) the prediction unit in a loop multiple times before performing the attack to retrieve one byte of data [23].This training can be performed either from the same address space or cross different address spaces [11]; moreover, the training can be performed either in-place or out-of-place [11].Our detection target is the process that performs the training, regardless of whether it aims to perform same-address-space/cross-addressspace or in-place/out-of-place attacks.
Therefore, the attack model of different types of misprediction-based speculative execution attacks is described in Fig. 3(b), which depicts the timestamps of branch/data prediction happened at a specific virtual address of the monitored program; each dot on the timeline represents the occurrence of one prediction.There are four parameters: N a is the total number of attack attempts, L a is the time interval between an attack attempt and the next training phase (in cpu cycles), N t is the number of training attempts in each training phase, and L t is the time interval between two consecutive training attempts.
To measure the parameters of the model, we used the PoC from Kocher et al. [23] and Canella et al. [11].First, we tested the relationship between the occurrence of branch misprediction when the "attack" is performed and the success rate of the attack by leveraging the HPC event.The result shows that whenever the branch misprediction occurs, the attack can always have a 100% attack success rate.This is because the speculative window caused by BTB/PHT misprediction is large enough to load secret into the microarchitecture [52].Therefore, an occurrence of a branch misprediction is equivalent to a successful attack.
Next, we evaluate how N t and L t affect the success rate of triggering branch misprediction.In the experiments described below, N a = 1 and L a = 150K cycles, and the result is shown in the BTB/PHT portion of Table 2.Each L t is the CPU cycles (starting from the minimum value 350) and N t enumerates each integer between 1 and 7 (inclusive).Each number in the table is the attack success rate in 10, 000 trials.As we see from the table, when L t ≥ 450 and N t ≥ 2, p is greater than 95%.
Finally, we evaluated how N a and L a affect the attack success rate (p).In these experiments, we set N t = 1 and L t = 350 cycles, because this pair of N t and L t has the worst p, which is the best scenario for analyzing the effects of N a and L a .The result in Table 2 shows that larger N a has greater p. L a has very little impact on p: For L a between 250k and 550k CPU cycles, p is greater than 95% when N a > 30.When L a is large enough (e.g.L a > 450k CPU cycles), L a has no observable effect on p.

RSB and STL Misprediction Variants
Modeling attacks Spectre-RSB [11] and spectre-STL [11] exploits RSB and the memory disambiguator to trigger misprediction.In these two attacks, because RSBs can be poisoned by push and pop instructions, which is difficult to monitor using HPC and PT, and the memory disambiguator can be triggered simply by load instructions, which does not need training phase.Therefore, we use the model described in Fig. 3(a) to model these attacks.
To measure the impact of the parameters of the model on the success rate of the attacks, we used the PoC released with the published paper [11].Using HPC events, we tested the relationship between p with N a and L a , respectively.Tested L a and N a start from the minimum ones, 150K and 1, respectively.The results are shown in Table 2.For Spectre-RSB, the value of N a must be greater than 10, 000 for p to be larger than 90%.For Spectre-STL, the value of N a must be greater than 20, 000 to achieve a similar success rate.For both attacks, L a does not seem to play a significant role.

Evaluation
In this section, we evaluate the detection accuracy and performance of SPECULARIZER.The data sets used in the evaluation are collected in the following approaches: The benign programs are selected from GNU Binutils 5 and SPEC benchmark 2006.The attack samples are drawn from the attack variants discussed in Sec. 7. The experiments were conducted on desktops with Intel Core i7-7700 Processors and 32GB RAMs.64-bit Ubuntu 16.04.6LTS operating systems with the kernel version 5.4.0 were installed on the desktops.

Evaluation of SPECULARIZER's Parameters
There are a few parameters that can be tuned for SPECULARIZER: In the collection phase, the PEBS sampling rate (ρ) specifies the accuracy of branch misprediction records.We collected traces with 4 different ρ values: 1, 3, 5, 10.In the trace processing phase, the window size δ and trace length µ determine how the collected HPC and PT data are segmented for the LSTM algorithm to work on.We particularly picked two window sizes δ, 10 million CPU cycles and 100 million CPU cycles, and two trace lengths, 500 and 1000 data points.The parameter we use to select training data set is the success rate p of the attack variants, which can be chosen from 85%, 90%, and 95%.Table 3. Data sets for parameter evaluation.
In this section, we analyze how these parameters affect the detection results.We created 8 data sets, whose parameter configuration is shown in Table 3.Each data set contains four groups of traces; each group is used to evaluate one LSTM model, as specified in Sec. 6.In each group, around 30, 000 benign traces and 30, 000 attack traces were collected.Then the traces in each group are randomly split into the training set (80%) and a testing set (20%).
By running the LSTM classification, the algorithm outputs a class label ("benign" or "attack") for each trace together with the likelihood between 0 and 1.We selected a threshold α of the likelihood, such that SPECULARIZER alerts the detection of an "attack" trace when the LSTM classifier outputs "attack" with a likelihood greater than α.Two values were selected for α, 0.5 and 0.75.The accuracy is evaluated using the F1 scores when α = 0.5 and when α = 0.75.A high F1 score suggests a balanced precision and recall.Here recall is defined as the percentage of detected attack traces in all attack traces and precision is defined as the percentage of correctly detected attack traces in all detected attack traces.
PEBS sampling rate ρ.We only evaluated ρ for misprediction-based attacks, because the detection of exception-based attacks does not need HPC.The data sets we used in this test are (2), ( 4), (5), and (6) (as shown in Table 3), where the window size (δ) is selected as 100 million cycles, trace size (µ) is selected as 1000, and p > 95% for attack variants selected in the training/testing set.The result is shown in Table 4.We see from the table that ρ only affects the detection of BTB/PHT-based attacks.Specifically, only when ρ ≤ 3, F1 scores yield good detection accuracy (F1 score greater than 90%).In contrast, regardless of the ρ value, the detection accuracy for RSB and STL-based attacks is high.This is because losing branch misprediction information due to larger ρ values is more critical to detecting attacks that require training.
Window size δ.To evaluate the effect of δ, we used data set (1) and (2), where for both data sets ρ = 1, p > 95%, and µ = 1000.For each window size (δ), we evaluate the F1 score when the threshold is 0.5 and 0.75 and the result is shown in Table 5, which suggests δ does not have a strong impact on the detection accuracy.Trace length µ.The evaluation utilized data set (2) and ( 3), with ρ = 1, p ≥ 95% and δ = 100M cycles.The result is presented in Table 5, which means µ = 500 or µ = 1000 does not affect the detection accuracy dramatically.
Success rate threshold p.The data sets used in this evaluation are (2), ( , with ρ = 1, µ = 1000 and δ = 100M cycles.The result shown in Table 5 suggests that p does not have much impact on the detection accuracy.
Classification likelihood threshold α.The result shown in Table 4 and Table 5 suggests that α = 0.5 or α = 0.75 does not affect F1 score.Thus, we chose α = 0.5 for the following evaluation.
Summary: In parameters ρ, δ, µ, p, and α; only ρ has a significant impact on detection accuracy of attacks that exploit BTB/PHT.

Evaluation of Detection Accuracy
We evaluated the detection accuracy of the LSTM models trained using data set (4) in Table 3.Using these parameters, in the following experiments, we evaluate the models' capability of detecting various attack variants.Because the traces that are classified as benign all have a precision that is close to 100%, the F1 score does not provide more information than recall, or true positive rate (TPR).Therefore, we use TPR as the metric for evaluating detection accuracy, which is defined as the percentage of correctly classified traces among all traces that are classified as attacks.The results are represented in Fig. 4, while the blue line is the TPR and the red line is the attack success rate p.In the cases where TPR>p means the probabilistic to detect the attack is higher than the secret been leaked.
Exception-based attacks.We collected 11, 700 traces from all types of exceptionbased variants we generated and split them into separate groups according to their N a value.Then we perform classification on each of the groups and show the results in Fig. 4(a) .In this figure, the X-axis is the value of N a , the red line is the attack success rate p and the blue line is the TPR.When N a = 4, TPR= 99.1%; when N a > 10, TPR≥ 99.9%; but when N a ≤ 3, TPR drops to 0, which means we were not able to detect exception-based attacks with fewer than 4 attempts within a time window of 100 million CPU cycles.
Misprediction-based attacks on BTB/PHT.We collected 980, 000 traces from the BT-B/PHT attack variants.To evaluate SPECULARIZER with varying N t , N a , L t , and L a values, we split the traces accordingly.The results are shown in Fig. 4  Misprediction-based attacks on RSB and memory disambiguator.We collected 7, 500 traces from attack variants exploiting RSB and memory disabmiguators.To evaluate SPECULARIZER with varying L a and N a values, we split the traces accordingly.
The results are presented in Fig. 4 (g), Fig. 4 (i), Fig. 4 (f), and Fig. 4 (h), respectively.As we see from these figures, SPECULARIZER can detect attack variants with 150K≤L a ≤ 550K cpu cycles with TPR> 80%.TPR increases almost monotonically when N a increases.SPECULARIZER can detect attack variants when N a > 3000 with TPR> 80%.
It is worth noting that when TPR < 80% for both attacks, the success rate of these attacks goes below 30%, which suggests that the adversary needs to balance the attack efficiency with the risk of detection.
Summary: With the selected parameters, SPECULARIZER can detect most of the attack traces we collected from the generated attack variants with high recalls.However, In cases where the detection is less accurate, p of these attack variants is also low.

End-to-End Evaluation
In practice, SPECULARIZER monitors the execution of a program and raises alarms if any of the traces collected from the program is detected as "attacks".To perform endto-end evaluation, we use the same model as trained using data set (4).
The data set we used has 26 benign programs collected from GNU Binutils and SPEC benchmark 2006, and randomly selected 160 attack variants we generated (i.e.40 variants for each attack type).Each of the 186 programs was examined using all the four LSTM models.Among the 41 benign programs, only one (gobmk) is falsely classified as BTB/PHT misprediction attacks and four benign programs (i.e., ld, perlbench, sophlex, and gobmk) were misclassified as exception-based attacks.However, in all these misclassified cases, less than 3 traces (out of over 1000 traces) extracted from each program were indeed misclassified, which means these false detections can be prevented if SPECULARIZER only raises alarms when multiple traces (e.g., > 3) were detected as attacks, which can be another parameter the user of SPECULARIZER could tune.Nevertheless, all attack variants are successfully detected by their corresponding LSTM classifier.The BTB/PHT classifier also detects 117 out of 120 other attack variants, because these attack variants also exhibit this type of branch misprediction.

Overhead of Online Trace Collection
In our experiments, SPECULARIZER enabled trace collection of both HPC and PT, with the HPC events and MSR configurations specified in Sec. 4. The ρ was set to 3. The experiments on LMbench [29] show the runtime overhead on I/O is negligible.The results of the SPEC benchmark are shown in Fig. 5.The runtime overhead was introduced from 0.038% to 231.42%, with a geometric mean of 14.36%.Some of the benchmark programs (e.g.mcf, gobmk, and sjeng) had high performance overhead; as their execution triggers a lot of branch misprediction.We note that the performance can be reduced with Intel's new feature that redirects PEBS's sampling output to PT packets [7], as PT packet generation introduces much less overhead [16].We leave this evaluation to future work.Running Time of Offline Attack Detection Fig. 6 shows the running time of offline attack detection.The number above each bar is the average running time (of 1000 trials) of the offline analysis for each SPEC benchmark (in seconds), which ranges from 3s to 1709s with PT trace files between 13M bytes and 13G bytes.More specifically, the offline analysis includes three phases: trace loading, trace processing, and attack detection.On average, they account for 70.01%, 29.85%, and 0.14% of the entire running time.One reason for the long-running time for trace loading/processing (99.86%) is that PT generates a large number of packets, which takes a long time to parse and analyze.The attack detection phase typically takes less than 1s.Finally, it is worth noting our offline analyses were performed within a single thread with limited memory, which can be further optimized using multi-threading and larger memory.And for applications such as forensics, the overhead of offline analysis is not critical.

Discussion
Detecting assistance-based attacks.Microcode assist was exploited in some recent works [31,37,42,43].However, there is no systematic study of these microcode assists yet.It is not clear how many methods can trigger microcode assists and how many of them can be exploited in speculative execution attacks by unprivileged programs.Without such systematic exploration, an ad-hoc detection technique is likely to be bypassed.We leave the detection of assistance-based attacks to future work.
Completeness of the attack data sets.We could hardly claim that our generated attack data sets cover all possible attack variants.However, as the models used for attack variants generation only specify the patterns of misprediction and exception, they are general enough for modeling attacks that use different types of disclosure gadgets and disclosure primitives.Moreover, the parameters in the attack models can be tuned to alter specific properties of an attack variant, which in combination can be used to approximate most attack methods one could think of.
Using simpler classification models for attack detection.One might think deep learning algorithms like LSTM are too heavyweight for our scenarios.In fact, we have also tested multiple alternatives, such as decision trees, K-means, random forest, etc..However, we found those models very fragile for any practical use.In contrast, LSTM offers an automated selection of parameters and thresholds, greatly reducing the subjectivity in the selection of classification models.
Adversarial machine learning (AML).SPECULARIZER is vulnerable to AML-based techniques that generate carefully crafted attack variants to evade detection.As shown in Fig. 4, in general, attack code that evades detection is likely to have a lower success rate.In that sense, SPECULARIZER makes speculative execution attacks harder to perform, but may not eliminate the threats.However, we note this arms race is common in all machine-learning-based defense systems [14,32].
Real-time attack detection.Ideally, attack detection should be performed in real-time and for all programs.However, as parsing PT packets and processing the traces are time-consuming (as shown in Fig. 6), it is very challenging to do so in practice.Moreover, enabling whole system monitoring with PT will drastically increase the overhead of trace parsing and analysis.These are common issues for PT/HPC-based monitoring systems [39,51].
10 Related Work Detecting speculative execution attacks.Prior works on detecting speculative execution attacks mainly focus on the detection of disclosure primitives, such as the Flush+Reload cache side channels [19].In contrast, SPECULARIZER detects the speculative execution attacks by monitoring its root cause-the speculation primitives.Close to our work is due to [25,44] who also leverages HPC to detect speculative execution attacks.However, as their approach only uses HPC, it omits the context of program execution in the detection of attacks.Therefore, their approach is less accurate and only applicable to simple proof-of-concept attacks.

Conclusion
In this paper, we present SPECULARIZER, a software tool for uncovering speculative execution attacks using performance tracing hardware features (PT and HPCs).SPEC-ULARIZER monitors the execution of the inspected applications in an online mode, introducing modest runtime performance overhead, and then performs attack detection in an offline analysis using LSTM networks.Empirical evaluation of SPECULARIZER suggests that the proposed approach leads to high detection accuracy with reasonable overhead, particularly suitable for offline forensic analysis.

Fig. 5 .
Fig. 5.The performance overhead of online trace collection.

g r o m a c s s p e c r a n d s je n g c a lc u li x a s t a r z e u s m p p o v r a y m il c o m n e t p p m c f g c c p e r lb e n c h x a la n c b m k d e a lI I n a m d g a m e s s li b q u a n t uFig. 6 .
Fig. 6.Running time of offline attack detection.

Table 1 .
Relationship between Na and p in exception-based attacks.

Table 2 .
The success rate of misprediction-based attacks.
Index Window Size δ Trace Length µ Sample ρ Success Rate p

Table 5 .
Impact of window size δ, trace length µ and threshold of attack success rate p.