OSDN Git Service

Function Layout, Global Variable Layout and Pooled Constants Layout Reordering
authorQining Lu <qining@google.com>
Fri, 26 Jun 2015 16:36:00 +0000 (09:36 -0700)
committerQining Lu <qining@google.com>
Fri, 26 Jun 2015 16:36:00 +0000 (09:36 -0700)
commit7cd5351c07806a1bc7ba1c1aec3380f7b715bda8
tree8802877ed38c67f14b3e75c521083bb7a9d6c72b
parent67f8de9adf6439881a00d8e0f081918436c71f62
Function Layout, Global Variable Layout and Pooled Constants Layout Reordering

PURPOSE:
The purpose of function layout reordering is to defend against code-reuse attacks as the location of code blocks will be various among different binaries. The layout reordering for global variables and pooled constants can be considered as static data randomization. This is to stop memory corruption attacks by randomizing the locations of the static data. After function layout reordering, the order of function blocks in TEXT section will be randomized. Global variable reordering randomize the order of global variables, and pooled constant reordering randomize the order of pooled constants. Note the order of constant pools won’t be affected and all pooled constants will remain in their original constant pools.

USAGE:
-reorder-functions: bool type command line option, enables function layout shuffling in TEXT section. Note when -threads=0 is set, function reordering will be forced off.

-reorder-functions-window-size: uint32 type command line option, specify the length of the shuffling queue. Note -reorder-functions-window-size=0 or 1 means no shuffling applied to functions.

-reorder-global-variables: bool type command line option, enables global variables shuffling.

-reorder-pooled-constants: bool type command line option, enables pooled constants shuffling.

APPROACH:
Randomization is introduced at the code emission time. We use a shuffling method to randomize the emission of function code, global variables and pooled constants. For function code emission, we also introduce “window size” as a parameter to control the size of the function holding buffer for shuffling. Window size 1 and 0 mean no shuffling applied, and a value higher than the number of translated functions means holding all the functions and shuffling them before emitting any of them.

IMPLEMENTATION:
    Function reordering:
        GlobalContext::emitItems(): Call RandomShuffle() routine to shuffle a specific part of the Pending vector.

    Global variable reorder:
        GlobalContext::lowerGlobals(const IceString &SectionSuffix): Call RandomShuffle() routine upon declaration list: Globals.

    Pooled constant reordering:
        TargetDataX8632::emitConstantPool(GlobalContext *Ctx): Add call to RandomShuffle() to shuffle the constant pool to be emitted. This is for asm output.

        ELFObjectWriter::writeConstantPool(Type Tu): Add call to RandomShuffle() to shuffle the constant pool before emitting it. This is only for elf output.

ISSUES:
    The initialization of global variables are emitted along with function code, all of them are considered as EmitterWorkItem. However, we do need to first emit global variables to keep the block profiling workflow untouched. To fulfill this, a “kind” check is added in the while loop of GlobalContext::emitItems(). The “if” statement at line 480 shows the workaround of this issue.
BUG=
R=jpp@chromium.org, jvoung@chromium.org, stichnot@chromium.org

Review URL: https://codereview.chromium.org/1206723003.
src/IceClFlags.cpp
src/IceClFlags.h
src/IceConverter.cpp
src/IceELFObjectWriter.cpp
src/IceGlobalContext.cpp
src/IceGlobalInits.cpp
src/IceTargetLoweringX8632.cpp
src/IceTargetLoweringX86BaseImpl.h
tests_lit/llvm2ice_tests/reorder-functions.ll [new file with mode: 0644]
tests_lit/llvm2ice_tests/reorder-global-variables.ll [new file with mode: 0644]
tests_lit/llvm2ice_tests/reorder-pooled-constants.ll [new file with mode: 0644]