linux核心堆棧保護淺析__linux

來源:互聯網
上載者:User

一 啟用方式

開啟配置CONFIG_CC_STACKPROTECTOR,重新編譯核心即可。


二 工作原理

搜尋配置CONFIG_CC_STACKPROTECTOR產生的影響,可得到如下結果:

1.include/linux/stackprotector.h

#ifdef CONFIG_CC_STACKPROTECTOR
# include <asm/stackprotector.h>
#else
static inline void boot_init_stack_canary(void)
{
}
#endif

其中asm/stackprotector.h檔案內容:

extern unsigned long __stack_chk_guard;


/*
 * Initialize the stackprotector canary value.
 *
 * NOTE: this must only be called from functions that never return,
 * and it must always be inlined.
 */
static __always_inline void boot_init_stack_canary(void)
{
unsigned long canary;


/* Try to get a semi random initial value. */
get_random_bytes(&canary, sizeof(canary));
canary ^= LINUX_VERSION_CODE;


current->stack_canary = canary;
__stack_chk_guard = current->stack_canary;
}


2.kernel/fork.c

#ifdef CONFIG_CC_STACKPROTECTOR
tsk->stack_canary = pax_get_random_long();
#endif


3.kernel/panic.c

#ifdef CONFIG_CC_STACKPROTECTOR

/*
 * Called when gcc's -fstack-protector feature is used, and
 * gcc detects corruption of the on-stack canary value
 */
void __stack_chk_fail(void)
{
dump_stack();
panic("stack-protector: Kernel stack is corrupted in: %pA\n",
__builtin_return_address(0));
}
EXPORT_SYMBOL(__stack_chk_fail);
#endif


4.arch/mips/kernel/process.c

#ifdef CONFIG_CC_STACKPROTECTOR
#include <linux/stackprotector.h>
unsigned long __stack_chk_guard __read_mostly;
EXPORT_SYMBOL(__stack_chk_guard);
#endif


5.include/linux/sched.h

struct task_struct {

......

#ifdef CONFIG_CC_STACKPROTECTOR
/* Canary value for the -fstack-protector gcc feature */
unsigned long stack_canary;
#endif

....

}

6.arch/mips/kernel/r4k_switch.s

LEAF(resume)

....

#if defined(CONFIG_CC_STACKPROTECTOR) && !defined(CONFIG_SMP)
PTR_LA t8, __stack_chk_guard
LONG_L t9, TASK_STACK_CANARY(a1)
LONG_S t9, 0(t8)
#endif


7.arch/mips/makefile

ifdef CONFIG_CC_STACKPROTECTOR
  KBUILD_CFLAGS += -fstack-protector
endif

這些搜尋結果可以總結為:定義並設定了變數__stack_chk_guard,定義了函數__stack_chk_fail(),但是發現__stack_chk_guard設定後並沒有使用,函數__stack_chk_fail()也沒有被調用,頓時線索斷了。

但是他們既然被定義了,就一定會被使用到,並且核心不會依賴於其他的庫檔案,於是打算從obj檔案下手,隨便找了個init/mounts.o,查看了下重定位表,如下所示:

RELOCATION RECORDS FOR [.init.text]:
OFFSET   TYPE              VALUE 

.......

00000120 R_MIPS_HI16       __stack_chk_guard
00000124 R_MIPS_LO16       __stack_chk_guard

........

00000344 R_MIPS_HI16       __stack_chk_guard
0000034c R_MIPS_LO16       __stack_chk_guard
00000358 R_MIPS_26         __stack_chk_fail
00000388 R_MIPS_HI16       __stack_chk_guard
00000390 R_MIPS_LO16       __stack_chk_guard

從重定位表看__stack_chk_fail和__stack_chk_guard確實是使用到了,查看對應的反組譯碼結果:

Disassembly of section .init.text

.........

00000120 <mount_block_root>:
     120:       3c020000        lui     v0,0x0
     124:       8c420000        lw      v0,0(v0)
     128:       27bdff98        addiu   sp,sp,-104
     12c:       afa2003c        sw      v0,60(sp)
     130:       3c020000        lui     v0,0x0
     134:       afb70060        sw      s7,96(sp)
     138:       0080b821        move    s7,a0
     13c:       8c440000        lw      a0,0(v0)
     140:       afb2004c        sw      s2,76(sp)

..........

     33c:       0c000000        jal     0 <readonly>
     340:       02002821        move    a1,s0
     344:       3c020000        lui     v0,0x0
     348:       8fa3003c        lw      v1,60(sp)
     34c:       8c420000        lw      v0,0(v0)
     350:       10620003        beq     v1,v0,360 <mount_block_root+0x240>
     354:       8fbf0064        lw      ra,100(sp)
     358:       0c000000        jal     0 <readonly>
     35c:       00000000        nop
     360:       8fb70060        lw      s7,96(sp)
     364:       8fb6005c        lw      s6,92(sp)
     368:       8fb50058        lw      s5,88(sp)
     36c:       8fb40054        lw      s4,84(sp)

開啟init/mounts.c檔案,找到mount_block_root()函數,發現並沒有調用__stack_chk_fail和__stack_chk_guard,頓時就明白了,這個應該跟上面的搜尋結果7,編譯選項-fstack-protector相關,通過對比著看彙編代碼,在函數入口:

     120:       3c020000        lui     v0,0x0
     124:       8c420000        lw      v0,0(v0)
     128:       27bdff98        addiu   sp,sp,-104
     12c:       afa2003c        sw      v0,60(sp)

將__stack_chk_guard的值存入棧頂,然後在函數返回之前:

     344:       3c020000        lui     v0,0x0
     348:       8fa3003c        lw      v1,60(sp)
     34c:       8c420000        lw      v0,0(v0)
     350:       10620003        beq     v1,v0,360 <mount_block_root+0x240>

取出儲存在當前棧頂的值與__stack_chk_guard進行比較,如果相等,則跳轉到360,順利返回,否則繼續順序執行:

     354:       8fbf0064        lw      ra,100(sp)
     358:       0c000000        jal     0 <readonly>

通過上面查看到的重定位表,00000358 R_MIPS_26         __stack_chk_fail ,調用到的就是__stack_chk_fail ()這個函數,至此,整個過程應該非常明了了,編譯選項-fstack-protector在函數編譯時間插入一些代碼,函數入口在棧頂儲存__stack_chk_guard的值,在函數返回之前取出存放在棧頂的值和__stack_chk_guard進行比較,如果記憶體溢出修改了這個存放的值,則比較的結果不相等,調用__stack_chk_fail,對於核心__stack_chk_fail()函數直接宕機。


三 補充說明

通過上面的推斷,發現比較關鍵的是__stack_chk_guard這個值的安全性,這個值是在核心啟動的時候,調用boot_init_stack_canary()函數隨機產生的,所以理論上來說這個值是不可猜測的。另外每個進程都對應一個不同的值:tsk->stack_canary = pax_get_random_long();這樣進一步增加了安全性,在每次進程切換時設定將要啟動並執行進程的stack_canary到__stack_chk_guard,但是對於多核處理器,由於__stack_chk_guard只存了一份,並不是per cpu變數,所以__stack_chk_guard的值並不會在進程切換時改變,因為假設某個cpu核發生進程切換,重新設定改變了__stack_chk_guard的值,則此時正在核心中啟動並執行其他cpu核可能在函數返回時檢查不過二導致系統宕機。至於__stack_chk_guard為啥不搞成per cpu變數,這個就很好理解了,因為gcc只會引用__stack_chk_guard這個變數。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.