WIT 寫完整性測試

標(biāo)簽: DFI


Abstract

用于防止內(nèi)存錯誤  

WIT在編譯時刻用過程間指向性分析[23]計算CFG和程序中可能被每個指令寫的一系列objects客燕。然后裝備代碼防止不在靜態(tài)分析所得到的集合里的指令修改objects丁溅,并且保證間接控制跳轉(zhuǎn)符合CFG饼酿。為了提升精確度荆忍,WIT在原始的程序objects間插入小的guards秧廉。我們描述了一個實現(xiàn)生逸,降低了空間和時間損耗,并且可以實用因為它不用進行修改的編譯C和C++程序,沒有誤報艳狐。平均時間損耗7%(CPU密集型benchmarks)。

WIT使用指向性分析對每一個object和寫操作賦一個color坑傅,所有可以被一條指令進行寫操作的objects具有相同的color僵驰,裝備代碼在運行時記錄 object colors并且檢查指令寫到正確的color。存儲單元的color記錄在一個color表中唁毒,當(dāng)objects被分配和釋放時color表會相應(yīng)更新蒜茴。寫檢查在表中查閱正在被寫的存儲單元的color并且檢查是否和寫指令的color一致。如此確保了寫完整性浆西。

haha

WIT也會給間接跳轉(zhuǎn)指令和可能間接跳轉(zhuǎn)的函數(shù)的入口點一個color粉私,由此所有可能被同一條指令call的函數(shù)有相同的color。WIT裝備代碼在color表中記錄函數(shù)colors并且檢查間接跳轉(zhuǎn)指令近零。間接跳轉(zhuǎn)在表中查找目標(biāo)地址的color诺核,并且檢查是否與間接跳轉(zhuǎn)指令的color匹配。這些檢查和寫檢查確保了CFI久信。CFI防止攻擊者繞過我們的檢查并且提供了第二道防線來防御寫檢查沒有探測到的攻擊窖杀。
優(yōu)化:
1. 靜態(tài)分析找到不違反寫完整性的訪問和只有安全訪問的objects,我們只裝備不安全的寫操作裙士,并且對所有安全的objects賦給同樣的顏色入客。這會減少寫檢查的數(shù)目和color表的損耗。降低了需要用來表示color的bit數(shù)。我們所有的實驗中桌硫,一個字節(jié)足夠表示顏色夭咬。
2. color表使用緊湊表示,可以有效率的查找铆隘。color表用一個字節(jié)表示一個8字節(jié)的存儲卓舵,降低了12.5%的空間損耗。
3. Third, we reduce the cost of updating color table entries on function calls. Since most local variables are safe, we only update entries for guards and unsafe variables on function entry and we reset these entries to the color of safe objects on function exit.

2. Overview

WIT有compile-time和runtime兩個部件膀钠。
// Example vulnerable code: simplified Web server with a buffer overflow vulnerability.

1: char cgiCommand[1024];
2: char cgiDir[1024];
3:
4: void ProcessCGIRequest(char* msg, int sz) {
5: int i=0;
6: while (i < sz) {
7: cgiCommand[i] = msg[i];
8: i++;
9: }
10:
11: ExecuteRequest(cgiDir, cgiCommand);
12: }

lines 5-9 存在緩沖區(qū)溢出漏洞掏湾,如果message過長,攻擊者可以重寫cgiDir(里面是CGI commond調(diào)用的可執(zhí)行文件的路徑)來運行任意可執(zhí)行程序(例如shell)托修。這是一個non-control-data attack忘巧,不違反CFI。

使用指向性分析[23]計算程序中每條指令可能修改的obiects睦刃,上例中,分析計算出 the set {i} for the instructions at lines 5 and 8, and the set {cgiCommand} for the instruction at line 7.

為了降低運行時的損耗十酣,提出寫安全檢查來計算安全的指令和objects涩拙。不違反寫完整性的指令為安全指令,如果所有的能修改object的指令都是安全的耸采,那么這個object是安全的兴泥。上例中, instructions 5 and 8 are safe because they can only modify i and, therefore, i is safe. ProcessCGIRequest的參數(shù)也是安全的虾宇。instruction 7 is not safe because it may modify objects other than cgiCommand depending on i’s value.

對所有的安全指令和objects的color設(shè)為0搓彻。上例中, 變量 msg, sz, and i and instructions 5 and 8 are assigned color 0 because they are safe. We assign color 3 to variable cgiCommand and instruction 7, and color 4 to variable cgiDir.

為了降低漏報率(由于指向性分析的不精確導(dǎo)致),在原始程序的不安全的objects之間插入小得guards嘱朽。 Guard objects have color 0 or 1旭贬。這些color永遠(yuǎn)不能賦給不安全的指令以確保WIT能檢測到重寫guards或者安全的objects。

函數(shù)的color集合與objects搪泳,guards的color集合不相交稀轨。這可以防止不安全的指令重寫代碼并且防止代碼區(qū)域外的控制轉(zhuǎn)移。

WIT加了額外的編譯階段來裝備以實現(xiàn)寫完整性和控制流完整性岸军。有四種類型的instrumention:to insert guards, to maintain the color table, to check writes, and to check indirect calls. Guards 8個字節(jié)大小. 上例中,  add guards just before cgiCommand, between cgiCommand and cgiDir, and just after cgiDir奋刽。

當(dāng)一個object被分配時,把它存儲位置的color設(shè)為object的color艰赞。上例中, WIT adds instrumentation at the beginning of main to set the color of the storage locations occupied by cgiCommand to 3, the color of the storage for cgiDir to 4, and the color of the storage for the guards around them to 0佣谐。

為了降低更新color表的損耗,初始化所有存儲單元的color表為0方妖,當(dāng)安全的objects被分配時不更新color表狭魂。Instead, we only update the colors for locations corresponding to unsafe objects on function entry. On function exit, we reset color table entries that we updated on function entry to 0. Therefore, there is no instrumentation to update the color table on function entry or exit for ProcessCGIRequest.

上例中, WIT adds write checks only before instruction 7 to check if the location being written has color 3. It does not add write checks before lines 5 and 8 because these instructions are safe。

WIT的防御依賴于指向性分析的精度,比如兩個相同color的object可以相互賦值趁蕊。

WIT可以防御上述例子坞生。The write check before line 7 fails and raises an exception if an attacker attempts to overflow cgiCommand. When i is 1024, the color of the location being written is 0 (which is the color of the guard) rather than 3 (which is the color of cgiCommand). Even without guards, WIT would be able to detect this attack because the colors of cgiCommand and cgiDir are different。

3. Static analysis

We implemented the points-to and the write safety analysis using the Phoenix compiler framework [30]. These analysis operate on Phoenix’s medium level intermediate representation (MIR), which enables them to be applied to different languages and target architectures掷伙。

        i = ASSIGN 0 
$L6:    t273 = COMPARE(LT) _i, _sz 
        CONDITIONALBRANCH(True) t273, $L8, $L7 
$L8:    t278 = ADD _msg, _i 
        t276 = ADD &_cgiCommand, _i 
        [t276] = ASSIGN [t278] 
        _i = ADD _i, 1 
        GOTO $L6 
$L7:    CALL &_ExecuteRequest,&_cgiDir,&_cgiCommand

Figure 2. Example vulnerable code in mediumlevel intermediate representation (MIR).
  
  We use an inter-procedural points-to analysis due to Andersen [8] that is flow and context insensitive but scales to large programs. It computes a points-to set for each pointer,which is the set of logical objects the pointer may refer to. The logical objects are local and global variables and dynamically allocated objects (for example, allocated with malloc). We use a single logical object to represent all objects that are dynamically allocated at the same point in the program but we do cloning of simple allocation wrappers to improve analysis precision. Our implementation is similar to the one described in [23] but it is field-insensitive rather
than field-based (i.e., it does not distinguish between the different fields in a structure, union, or class). We use Phoenix to compile each source file to MIR and write points-to constraints to a file. The analysis reads the constraints file, computes the points-to sets, and stores them in a file.

In addition, the write safety analysis runs a simple intra-procedural pointer-range analysis to compute writes through pointers that are always in bounds. The instructions that perform these writes are marked safe.

While making the global pass over all source files to collect constraints for the points-to analysis, we also run the write safety analysis. We write unsafe pointers to a file. A pointer is unsafe if it is dereferenced for writing by an unsafe instruction.

We use an iterative process to compute color sets, which include objects and unsafe pointer dereferences that must be assigned the same color because they may alias each other.

WIT uses a similar algorithm to assign colors to functions that may be called indirectly.

4. Instrumentation

4.1 Color table

We implemented WIT for 32-bit x86 machines running Windows.We used several Phoenix plugins [30] to generate WIT’s instrumentation.

WIT maintains a color table that maps memory addresses to colors. The color table must cover the whole user virtual address space and it is accessed often by write and indirect call checks.

To keep the color table small, we divide the virtual memory of the instrumented program into aligned eight-byte slots. The color table is implemented as an array with an eight-bit color identifier for each of these slots.Therefore,it introduces a space overhead of only 12.5%.

We are able to record a single color for each eight-byte slot because we generate code such that no two objects with distinct colors ever share the same slot. It is easy to enforce this requirement for heap objects because they are eight-byte aligned and for functions because they are 16-byte aligned. But since the stack and data sections are only four-byte aligned in 32-bit x86 architectures,we cannot currently force eight byte alignment of objects in these sections without introducing runtime overhead.

Instead,we force unsafe objects and guard objects in the stack and data sections to be four-byte aligned and we insert a four-byte aligned pad after unsafe objects. For an unsafe object of size s, the pad is eight-bytes long if ?s/4? is even and four-bytes long if ?s/4? is odd. We set ?s/8? color table entries to the color of the unsafe object when the pad is four-bytes long and ?s/8?+1 when the pad is eight-bytes long. We should be able to reduce the space overhead when targeting 64-bit x86 architectures because the stack and data sections are eight-byte aligned in these architectures.

Since our points-to analysis does not distinguish between different fields in objects and between different elements in arrays,we always assign the same color to all the elements of an array and to all the fields of an object. Therefore, it is not necessary to change the layout of arrays and objects,which is important for backwards compatibility.

We only require eight bits to represent colors because the write safety analysis is very effective at reducing the number of objects that we must assign colors to. However,it is possible that more bits will be required to represent colors in very large programs. If this ever happens, there are several things we can do. For example, we can increase the size of color table entries to 16-bits and increase memory slot sizes to 16-bytes, or use 8-bit color identifiers at the expense of worse coverage.

The color table can be accessed efficiently. Since there are 2 GB of virtual address space available for the user in Windows XP and Windows Vista, we allocate 256 MB of virtual address space for the color table 2. We rely on the operating system to allocate physical pages for the color table on demand when they are first accessed. The base of the color table is currently at address 40000000h. So to compute the address of the color table entry for a storage location,we take the address of the storage location, shift it right by three, and add 40000000h.

To protect the color table from being overwritten by an attacker, we read-protect the pages in the table that contain the entries for the virtual address range occupied by the table itself. With the base of the table at 40000000h,we protect the pages in the address range 48000000h to 4A000000h (color table原地址:256MB,為40000000h-50000000h)to prevent reads and writes. Since we add checks before unsafe writes and control-flow integrity ensures that the attacker cannot bypass these checks, the attacker cannot overwrite the color table because the write check would trigger a read fault on the protected address range. This technique was first described in [44].

4.2 Inserting guards

The guards are eight-bytes long to match the size of the slots that we record colors for in the color table. The instrumentation to insert these guards is different for the stack, heap, and global data sections.

To insert guards in the stack, we replace the compiler phase that lays out local variables in a stack frame by our implementation. We segregate safe local variables from unsafe ones to reduce the space overhead. First, we allocate contiguous storage for the safe local variables. Then we allocate storage for the guards, pads, and unsafe local variables. This allows us to insert only n+1 guards and pads for n unsafe local variables: the guard that prevents overflows of a variable prevents underflows of the next variable.

In the rare case where a function argument is written by an unsafe instruction, we cannot easily insert guards and pads around it. Therefore, we copy the argument to a local variable and rewrite the instructions to refer to the copy.This local variable is marked unsafe and we insert guards and pads around it.

We mark all heap-allocated objects as unsafe but we do not insert pads or guards around them. The standard heap allocator in Windows Vista, Windows XP SP2, and Windows 2003 inserts an eight-byte header before each allocated object. We use this header as a guard by simply setting its color to 1 in the color table.

We add guards and pads between all variables in the .data section and .bss sections but not in the read-only data section (.rdata).

We plan to implement an optimization that avoids the need for most guards by laying out stack and global objects such that adjacent objects have different colors.

4.3 Maintaining the color table

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末是己,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子任柜,更是在濱河造成了極大的恐慌卒废,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宙地,死亡現(xiàn)場離奇詭異摔认,居然都是意外死亡,警方通過查閱死者的電腦和手機宅粥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進店門参袱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人秽梅,你說我怎么就攤上這事抹蚀。” “怎么了企垦?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵环壤,是天一觀的道長。 經(jīng)常有香客問我钞诡,道長郑现,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任荧降,我火速辦了婚禮接箫,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘誊抛。我一直安慰自己列牺,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布拗窃。 她就那樣靜靜地躺著瞎领,像睡著了一般。 火紅的嫁衣襯著肌膚如雪随夸。 梳的紋絲不亂的頭發(fā)上九默,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天,我揣著相機與錄音宾毒,去河邊找鬼驼修。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的乙各。 我是一名探鬼主播墨礁,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼耳峦!你這毒婦竟也來了恩静?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤蹲坷,失蹤者是張志新(化名)和其女友劉穎驶乾,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體循签,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡级乐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了县匠。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片风科。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖聚唐,靈堂內(nèi)的尸體忽然破棺而出丐重,到底是詐尸還是另有隱情,我是刑警寧澤杆查,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布,位于F島的核電站臀蛛,受9級特大地震影響亲桦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜浊仆,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一客峭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧抡柿,春花似錦舔琅、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至囱稽,卻和暖如春郊尝,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背战惊。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工流昏, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓况凉,卻偏偏與公主長得像谚鄙,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子刁绒,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,389評論 0 23
  • 這紅塵這么美闷营,美得我想放下腳步,慢慢欣賞膛锭。怕錯過蝴蝶張開翅膀的聲音粮坞,怕錯過煙火升起時的一瞬間絢麗。
    玖瑤閱讀 228評論 0 1
  • 概述 SeekBar初狰,拖動條莫杈。主要的要點是自定義的樣式,拖動條的值奢入,拖動條值改變的監(jiān)聽器等等筝闹。 1.SeekBar...
    CokeNello閱讀 17,667評論 2 19
  • 八月就又要結(jié)束了,暑假也即將進入尾聲腥光。在陸續(xù)開學(xué)的日子里关顷,青子給大一的新生er們,寫了一個科普貼武福,希望對即將進入大...
    夢蘿青子ccc閱讀 564評論 1 9
  • 前些天去幼兒園接沛沛捉片,因為稍晚了一點平痰,沛沛看見我就大哭起來,埋怨我太晚來接他伍纫,又有一天我去接他宗雇,他因為老師沒有給他...
    梁琰閱讀 348評論 2 2