Patent Description:
To manage the invocation and execution of functions, most compilers compile programs to use some form of call stack when executing. Each time a function is called (e.g., a() called b()), a corresponding new stack frame is pushed to the call stack (in this case, a frame for b() is pushed on top of a frame for a()). The new frame contains a return address, i.e., the point in the program where the corresponding function was invoked (in the example, a location in a() where b() was called). Each time a function exits, the function's frame is popped from the call stack and the control flow passes to whichever return address is in the frame. A frame can also be used to store parameters passed to/from its function and local data declared in its function.

At any given time, the order of frames on the call stack reflects the current order of chained function calls, where each frame represents a function that has not yet exited. It is often desirable to know the current chain of function invocations when a program is executing. This information, sometimes called a stack trace or backtrace, can be useful for, among other things, debugging and performance profiling. As discussed next, there are different ways that a call stack can be used to construct a backtrace, but all prior approaches for backtracing incur computing overhead to the extent that in some scenarios backtracing is not practical. For instance, in many production systems and user devices, even a small increase in incurred latency can make backtracing prohibitive.

One prior approach for backtracing involves saving a caller's frame address in the callee's stack frame, and using a dedicated register (e.g., the x86 EBP register) to point to the current callee's frame. The dedicated register is used to get the current stack frame, from which the caller's stack frame's address is obtained, from which the caller's stack frame address is obtained. In each frame, the return address of the corresponding function is stored at a fixed offset. Therefore, to get the stack backtrace, the return address of each frame is found and dumped.

Another approach involves not saving the caller's stack frame address in the callee's stack frame. Instead, the stack frame size is computed by reverse executing function prologues. A stack backtrace starts by computing the stack size, which is then subtracted from the stack pointer to get the return address. Using the return address, the caller's stack frame size can be computed, which gets the caller's return address, and so forth. Compared to the first approach, this approach is more efficient when not backtracing (lower support overhead), but is less efficient when carrying out a stack backtrace.

Yet another approach is to use special hardware support such as Intel's Last Branch Record (LBR). If the LBR is configured to call-stack-mode, it will record active calls. Such active call information can be dumped as a stack backtrace.

While these prior approaches may yield the desired output, the overhead they incur is not negligible. The first approach requires extra instructions to save the caller's stack frame address in the callee's stack frame, and the caller's frame address must be computed when "walking the stack". The second approach requires intensive memory lookups to find the frame size information. The third approach puts stress on thread context switches because many extra registers need to be saved and restored for each context switch.

Discussed below are techniques for using a shadow stack to facilitate backtracing with negligible overhead. <CIT> relates to technologies for control flow exploit mitigation using processor trace. Certain ROP (return-oriented programming) exploits may be prevented by maintaining a "shadow stack" in parallel with the ordinary system stack. The shadow stack maintains a copy of the legacy stack in memory inaccessible to ordinary software, and may be used to determine if the legacy stack has been tampered with by malware. A computing device for control flow exploit detection and mitigation includes a processor having hardware real-time instruction tracing (RTIT) support. In use, the computing device executes software with RTIT support enabled and the processor automatically outputs trace data indicative of the control flow of the software. The computing device initializes real-time instruction tracing using the RTIT support of the processor. The computing device injects additional data into the trace data stream to allow shadow stack monitoring. The computing device constructs and/or maintains a shadow stack in the shadow stack area based on the trace data and identifies suspected ROP exploits by comparing the active system stack to the shadow stack. There is no need to output the return address to the trace data as part of the method, because the RTIT support of the processor will automatically output a TIP (target instruction pointer) packet with the return address for mispredicted return instructions, and the return address may be determined from the shadow stack for correctly predicted return instructions.

<CIT> relates to method of protecting a computer stack. Information stored on a stack is protected in an easily accessible but un-modifiable location, and the unmatched calls/returns are protected from being overwritten. Two computer stacks are created, a first computer stack operates as a traditional stack would, and a second computer stack, or shadow stack, stores the return address upon a call to a subroutine, the address of the return address on the first computer stack, and a user-definable state variable. After jumping to, and executing a subroutine, the return address from both the first computer stack and the second computer stack are compared. When a mismatch occurs, the second stack is searched for a matching return address. First, below the stack top is searched, and then above the stack top is searched. If a matching return address is found, it is checked to make sure that the corresponding address of the stack pointer stored in the second stack matches the current location of the first stack's pointer, and that the user-definable variable indicates that the data was stored on the second stack as a return address datatype.

<NPL>, relates to SoK: Shining Light on Shadow Stacks. Control-Flow Hijacking attacks are the dominant attack vector against C/C++ programs. Control-Flow Integrity (CFI) solutions mitigate these attacks on the forward edge, i.e., indirect calls through function pointers and virtual calls. Shadow Stacks are a fully precise mechanism for protecting backwards edges, and should be deployed with CFI mitigations. Calls for a shadow stack design that leverages a dedicated register are renewed resulting in low performance overhead, and minimal memory overhead, but sacrifices compatibility. Two new Intel x86 extensions for memory protection (MPX), and page table control (MPK) are repurposed. Building on the isolation efforts with MPX and MPK, the design requirements for a dedicated hardware mechanism to support intra-process memory isolation are presented.

<NPL>), relates to Transparent Runtime Shadow Stack: Protection against malicious return address modifications. Exploitation of buffer overflow vulnerabilities constitutes a significant portion of security attacks in computer systems. One of the most common types of buffer overflow attacks is the hijacking of the program counter by overwriting function return addresses in the process' stack so as to redirect the program's control flow to some malicious code injected into the process' memory. The Transparent Runtime Shadow Stack (TRUSS) is used to protect against function return address modification. Our proposed scheme is built on top of DynamoRIO, a dynamic binary rewriting framework. TRUSS is able to operate with an average performance overhead of about <NUM>% to <NUM>%. A runtime shadow stack of return addresses is maintained. On the execution of a procedure return, the return addresses on both the program stack and the shadow stack are compared. If there is any discrepancy, an error is raised. TRUSS is an implementation in a runtime binary rewriting framework.

It is the object of the present invention to improve software development with minimum computational overhead.

The following summary is included only to introduce some concepts discussed in the Detailed Description below. This summary is not comprehensive and is not intended to delineate the scope of the claimed subject matter, which is set forth by the claims presented at the end.

A program is executed using a call stack and shadow stack. The call stack includes frames having respective return addresses. The frames may also store variables and/or parameters. The shadow stack stores duplicates of the return addresses in the call stack. The call stack and the shadow stack are maintained by, (i) each time a function is called, adding a corresponding stack frame to the call stack and adding a corresponding return address to the shadow stack, and (ii) each time a function is exited, removing a corresponding frame from the call stack and removing a corresponding return address from the shadow stack. A backtrace of the program's current call chain is generated by accessing the return addresses in the shadow stack. The outputted backtrace includes the return addresses from the shadow stack and/or information about the traced functions that is derived from the shadow stack's return addresses.

Many of the attendant features will be explained below with reference to the following detailed description considered in connection with the accompanying drawings.

The present description will be better understood from the following detailed description read in light of the accompanying drawings, wherein like reference numerals are used to designate like parts in the accompanying description.

Embodiments discussed below relate to using a shadow stack to enable efficient stack backtracing. Discussion will begin with an overview of call stacks and shadow stacks. Various ways of implementing a shadow stack are then described, followed by details of methods for using a shadow stack to facilitate stack backtraces.

<FIG> shows an executing program <NUM> and a corresponding call stack <NUM> and shadow stack <NUM>. The program <NUM> may be compiled machine instructions, bytecode, source code being executed by an interpreter, and so forth. The program <NUM> includes various functions and calls to the functions. The call stack <NUM> may be implemented in any of many known ways. As mentioned in the Background, frames are pushed and popped in correspondence with function invocations and exits/returns, respectively.

The shadow stack <NUM> may also be implemented in known ways, using software, hardware, or both. As noted in the Background, in nearly all call stack implementations, when a function exits, its stack frame is popped off the call stack and execution control passes to whichever code location the frame's return address points to, usually, a location in the function that called the exiting function. The call stack's role in flow control - of providing return addresses - is a well-known security weakness. Using various techniques, the call stack can be altered or corrupted to change a return address, thereby directing execution to the location of malicious code.

Shadow stacks are one solution to the return address vulnerability. Briefly, a shadow stack is a stack that is separate from the call stack and is synchronized with the call stack so that each time a frame is added to the call stack the frame's return address is added to the shadow stack. Each time a frame is popped from the top of the call stack the return address at the top of the shadow stack is correspondingly popped off the shadow stack. If the popped return address does not match the return address in the popped frame, then an error has occurred and a remedial action can be taken.

<FIG> shows an example of source code <NUM> compiled by a compiler <NUM> to produce an executable program <NUM>. The source code <NUM> contains various functions and function calls. The compiler <NUM> translates the source code into the executable program <NUM>, which might be in the form of machine code, bytecode, intermediate code, object code, etc. The compiled executable program <NUM> has units of code/instructions that respectively correspond to the functions in the source code. Except perhaps in some managed runtime environments, the compiler also adds to the executable program <NUM> instructions for implementing the call stack. As discussed in detail below, in embodiments that use a software-based shadow stack, the compiler <NUM> also adds instrumentation code for implementing the shadow stack.

<FIG> shows details of the call stack <NUM> and the shadow stack <NUM>. The call stack <NUM> consists of frames <NUM>, one for each still-pending function invocation. Each frame <NUM> includes a return address <NUM>, as well as memory for local variables <NUM> and parameters <NUM>, as the case may be. The example of <FIG> includes functions main(), bar(), and foo(). The main() function is coded to call foo(), which is coded to call bar(). The call stack <NUM> shown in <FIG> reflects the executable program <NUM> when foo() has been called and is still executing. As shown in <FIG>, assuming that the call stack <NUM> has not been compromised, the shadow stack <NUM> should contain duplicates <NUM> of the return addresses in the respective frames in the call stack, and in the same order.

<FIG> shows how the call stack <NUM> and shadow stack <NUM> are maintained. As discussed in detail below, the steps for shadow stack management in <FIG> can be performed by special shadow stack hardware of the processor, by software, or both. When the executing program <NUM> calls a function at step <NUM>, at step <NUM> a new frame <NUM> is pushed to the call stack <NUM>. The new frame <NUM> includes a return address corresponding to the point where the called function was invoked. At step <NUM>, based on the same function invocation, a new return address <NUM> (the same return address that is in the new frame <NUM>) is pushed onto the shadow stack <NUM>. The steps for adding to the stacks can be performed in any order or in parallel.

At step <NUM> the executing program <NUM> is executing a called function and the function returns (e.g., exits). At step <NUM> the frame corresponding to the returned-from function is popped from the call stack, and at step <NUM> the top return address on the shadow stack is popped from the shadow stack. As with most steps described herein, the steps for removing from the stacks can be performed in any order or in parallel. Moreover, the removal/exit process includes some logic, whether in hardware or software, to compare the return address in the popped frame with the return address popped from the shadow stack, and to respond accordingly if they do not match. However, the comparison is not necessary for backtracing and if it is possible to implement a shadow stack without any comparison/remediating steps, the small backtrace overhead may be lowered further.

As noted above, the shadow stack can be implemented by hardware or software. <FIG> shows a software-based shadow stack implementation. In this embodiment, the compiler inserts into its translated output instrumentation code that implements the shadow stack. Generally, instrumentation code <NUM> will be placed near each function call <NUM>. The instrumentation code <NUM> may include entry logic <NUM> and exit logic <NUM>. The entry logic <NUM> may push the relevant return address onto the shadow stack, among other things. The exit logic <NUM> may pop the top of the shadow stack and check the popped return address against the return address in a corresponding call frame.

<FIG> shows a hardware-based shadow stack implementation. A processor <NUM> has circuitry that implements the various instructions in the processor's instruction set. In one embodiment, the processor's implementation of the call instruction includes both the ordinary call logic <NUM> as well as shadow stack logic <NUM>. The ordinary call logic <NUM> is the control flow logic of the call instruction that is found in a typical processor, regardless of whether the processor implements a shadow stack. The shadow stack logic <NUM> performs the shadow stack functionality discussed above, but in association with the call instruction <NUM>. Some processors may have a register for controlling whether the shadow stack logic is active, which may control implementation and/or backtrace-usage of the shadow stack logic <NUM>, among other things. The executable program may have no visibility of the shadow stack, which is implemented without instrumentation code. In other words, the same executable can run without modification on (i) one processor that lacks shadow stack support (and execution will have no shadows stack) and (ii) another processor that has shadow stack support (and will have shadow stacks).

In both hardware and software implementations, while a thread or program is executing, the shadow stack is assumed to be available for reading at any time, whether by the program/thread whose calls it is tracking, or by another component such as a debugger or runtime environment.

<FIG> shows how the shadow stack can be used to satisfy a backtrace request <NUM>. The compiled program <NUM> is executing. As discussed above, the program calls functions within functions, resulting in chains of function calls while corresponding state is reflected in the call stack and the shadow stack. The backtrace request <NUM> can be received during any arbitrary point of execution. The backtrace request <NUM> may be an application programming interface (API) call, an invocation of a backtrace function included in the executable program <NUM>, etc. The backtrace request might also be issued by an exception handler, a signal handler invoked by an external signal, and so forth.

Based on the backtrace request <NUM>, the shadow stack <NUM> is accessed and read, either by code in (or linked to) the program <NUM>, by a shadow stack instruction implemented by the processor, or a combination thereof. A full or partial copy <NUM> of the shadow stack <NUM> is captured (as used herein, a "copy" of the shadow stack refers to a full copy or a selective/partial copy). In some embodiments, the return addresses copied from the shadow stack may be limited to a top-N subset of the available addresses. Such a limit might come from a hardware constraint (e.g., a buffer size), a variable set in the program, a parameter passed into the backtrace request, etc..

Finally, the shadow stack copy <NUM> is incorporated into a trace or debug output <NUM>. Any of the many known means for capturing diagnostic information may be supplemented with shadow stack backtrace data in this fashion. In one embodiment, any known approach for requesting and storing a backtrace can be used by modifying such approach to obtain return address data from the shadow stack in addition to, or instead of, from the call stack. For example, a return address, which is just a number, can be converted into a function name, line number, file number etc. from symbol information. Such a full stack trace can then be stored in a log file, output to the screen, or sent over a network as telemetry for crash analysis etc..

<FIG> shows a process for using the shadow stack. At step <NUM>, a stack trace or backtrace is requested during execution of the program, either by the internal logic of the program or by a component communicating with the program. At step <NUM>, information about the shadow stack may be optionally acquired, for instance whether a shadow stack exists, its location, current size (or number of elements), the format of elements of the shadow stack, security information, attributes of the shadow stack (e.g., is it implemented in hardware or software), and so forth.

At step <NUM>, if shadow stack information was acquired, then the shadow stack information may be used to decide how or whether to carry out the backtrace request. For instance, unless a shadow stack is available (or has some specific trait), then the backtrace request may not generate a backtrace. Attributes of the shadow stack such as size, location, the size/number of elements, permissions, etc., may also be used to access and copy the shadow stack.

At step <NUM> the shadow stack is used to generate a backtrace. As discussed above, in some embodiments the backtrace is a verbatim copy of the shadow stack or a portion thereof. In other embodiments, the return addresses in the shadow stack are used to identify the relevant corresponding functions, the source code modules and line numbers where they are defined, the names of the called functions, the line numbers in functions where calls were made, information about the relevant functions (e.g., return type, parameter types/names), to name a few.

<FIG> shows shadow stack content <NUM> being used in combination with other information to generate a backtrace <NUM>. According to the invention, the return addresses in the shadow stack content <NUM> is used in combination with the call stack <NUM> and/or program/module metadata <NUM> to derive an enriched backtrace <NUM>. The program/module metadata <NUM> is metadata found in object files, symbol tables, source code files, and the like. A backtrace generating function <NUM> receives the shadow stack content <NUM>. The backtrace generating function <NUM> also receives or accesses the program/module metadata <NUM> and/or the call stack <NUM>. This information can be synthesized/combined in various ways. By correlating the return addresses in the shadow stack content with frames and return addresses in the call stack, the values of parameters and local variables can be obtained, the names of functions and modules can be found, line numbers of calls can be obtained, and so forth. Conceptually, the return addresses in the shadow stack content <NUM> can serve as an index to additional data; the shadow stack allows near zero-cost reconstruction of the call chain, and the call chain can be fleshed out with additional diagnostic information from other sources. In whatever form, the backtrace <NUM> is outputted, possibly through an existing diagnostic/debugging/telemetry scheme, which might involve using APIs for outputting diagnostic data, collecting formatted output in log files, sending the log files to a collection service, etc..

Another example is if there is data corruption on the regular stack. This is common because local variables are also stored on the regular stack, so a bug in the code can allow the return address on the regular stack to get overwritten. Because the shadow stack has no local variables, the program should need to touch any memory on the shadow stack, therefore it is much less likely to get corrupted. In the hardware case, the hardware can enforce the shadow stack to be read-only, and in the software case, the operating system can also make it read-only to everyone except for itself (for pushing and popping return addresses). In any case, if the return address is corrupted on the regular stack, the extra copy of the return address on the shadow stack could be substituted in to correct the problem. Then, the program could possibly continue to run if there are no other corruptions. If there are other corruptions, then at least we can still obtain a successful stack trace to catch the culprit of the corruption.

In addition, when unwinding the stack, if there is corruption in the middle of the stack, then unwinding will fail at the location of the corruption and further unwinding will be impossible since the data on the stack is wrong, therefore the location of the next return address is unknown. But if a shadow stack is available, even if the shadow stack itself has a few corrupted entries, the corrupted return address entries can be skipped and unwinding can proceed since the return addresses are known to be contiguous in memory.

<FIG> shows various contexts where backtrace logic can be implemented. In one implementation, shown at the top of <FIG>, the program <NUM> includes calls to an API for capturing backtraces. The API may be any known API for debugging or capturing backtraces. However, the logic implementing the API uses the shadow stack. The API may also have additional functions/methods that are relevant to using a shadow stack. For example, the API may define functions for setting prerequisites for honoring backtrace requests (e.g., shadow stack is available, shadow stack is available and supported by hardware, etc.). The API may also facilitate backward compatibility. The backtracing functionality can also be fully contained within the program, as shown in the middle of <FIG>. In another embodiment, an environment <NUM> such as a debugger, managed code environment, interpreter, or the like both executes the program and provides the backtracing functionality.

<FIG> shows a backtrace output example <NUM>. The example includes strings <NUM> generated based on shadow stack data. Any known style of backtrace formatting and content can be used. Settings, different backtrace functions (e.g., backtrace_raw() or backtrace_symbols()), or backtrace function parameters can be used to control the backtrace style and content, which can range from a list of bare memory addresses to detailed information and graphics about the functions that appear in the backtrace.

Although embodiments above involve compiled programs, most of the techniques can be readily applied to interpreted programs. In that case, the interpreter can be configured to use the shadow stack in similar fashion. The interpreter should be the same from the program's point of view; both compiled and interpreted programs have call/ret instructions that get executed by the processor.

Some call stack implementations use multiple call stacks that are linked together. In such cases, corresponding shadow stacks are linked, and unwinding the current call chain for a backtrace may involve unwinding the linked shadow stacks.

The term "program" as used herein is also considered to refer to threads. Usually, each thread has its own call stack and shadow stack.

<FIG> shows details of a computing device <NUM> on which embodiments described above may be implemented. The technical disclosures herein will suffice for programmers to write software, and/or configure reconfigurable processing hardware (e.g., field-programmable gate arrays (FPGAs)), and/or design application-specific integrated circuits (ASICs), etc., to run on the computing device or host <NUM> (possibly via cloud APIs) to implement the embodiments described herein.

The computing device or host <NUM> may have one or more displays <NUM>, a network interface <NUM> (or several), as well as storage hardware <NUM> and processing hardware <NUM>, which may be a combination of any one or more of: central processing units, graphics processing units, analog-to-digital converters, bus chips, FPGAs, ASICs, Application-specific Standard Products (ASSPs), or Complex Programmable Logic Devices (CPLDs), etc. The storage hardware <NUM> may be any combination of magnetic storage, static memory, volatile memory, non-volatile memory, optically or magnetically readable matter, etc. The meaning of the term "storage", as used herein does not refer to signals or energy per se, but rather refers to physical apparatuses and states of matter used thereby to read and/or store information. The hardware elements of the computing device or host <NUM> may cooperate in ways well understood in the art of machine computing. In addition, input devices may be integrated with or in communication with the computing device or host <NUM>. The computing device or host <NUM> may have any form-factor or may be used in any type of encompassing device. The computing device or host <NUM> may be in the form of a handheld device such as a smartphone, a tablet computer, a gaming device, a server, a rack-mounted or backplaned computer-on-a-board, a system-on-a-chip, or others.

Claim 1:
A method comprising:
executing, by a processor (<NUM>), a program (<NUM>), the program comprising functions, each function comprising respective code for respective functions and calls invoking the functions, the executing comprising:
maintaining a call stack (<NUM>) comprised of frames corresponding to respective invocations of the functions by the calls, each frame comprising a corresponding return address and memory for local variables and parameters;
maintaining a shadow stack (<NUM>), the maintaining comprising adding/removing return addresses to/from the shadow stack in correspondence with adding/removing the frames to/from the call stack, the shadow stack comprising return addresses that respectively correspond to the return addresses in the call stack; and
receiving a request (<NUM>) to capture a stack trace (<NUM>), and based thereon, using the return addresses in the shadow stack in combination with the call stack to capture and store the stack trace, wherein the return addresses in the shadow stack are correlated with frames and return addresses in the call stack to obtain local variables and parameters.