1、什么是內(nèi)存越界引用(Out-of-Bounds Memery)
??啥叫內(nèi)存越界引用?按照CSAPP里面的說(shuō)法就是:
什么是緩存區(qū)溢出(Buffer Overflow)
1、什么是內(nèi)存越界引用(Out-of-Bounds Memery)
??啥叫內(nèi)存越界引用?按照CSAPP里面的說(shuō)法就是:
- C語(yǔ)言對(duì)于一個(gè)數(shù)組的引用,是不對(duì)該數(shù)組進(jìn)行任何邊界檢查的。通俗來(lái)講,你用C語(yǔ)言定義一個(gè)局部變量數(shù)組,大小為3
char a[3] = {'a', 'b', 'c'};,但你可以對(duì)數(shù)組下標(biāo)超過(guò)2的元素e.g. a[11]進(jìn)行讀寫操作。
1.1內(nèi)存越界引用的前提條件:
??一般局部數(shù)據(jù)會(huì)被存儲(chǔ)于寄存器中,但有時(shí)會(huì)發(fā)生,局部數(shù)據(jù)不得不存儲(chǔ)于內(nèi)存(棧)中的情況。主要是以下三種情況(At times,However, local data must be stored in memory rather than registers.Common cases of this include these):
- 1、沒(méi)有足夠的寄存器用于存儲(chǔ)局部數(shù)據(jù)。
- 2、取地址符
&用于局部變量,因此我們必須能夠?yàn)槠渖梢粋€(gè)地址。 - 3、局部變量如果是數(shù)組、結(jié)構(gòu)體,因此我們必須能夠?qū)?shù)組、結(jié)構(gòu)體進(jìn)行解引用。
??這里我們著重關(guān)注第三條:局部變量如果是數(shù)組、結(jié)構(gòu)體,會(huì)被procedure存儲(chǔ)于棧中。
??我們看下面這個(gè)有趣例子:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
char d = 'd';
char a[3] = {'a', 'b', 'c'};
int i;
for(i = 0; i <= 15; i++)
{
printf("a[%d] = %c ", i, a[i]);
}
printf("\n");
printf("d = %c\n", d);
printf("----------------------\n");
a[11] = 'e';
for(i = 0; i <= 15; i++)
{
printf("a[%d] = %c ", i, a[i]);
}
printf("\n");
printf("d = %c\n", d);
return;
}
輸出結(jié)果如下:
[root@study csapp]# ./a.out
a[0] = a a[1] = b a[2] = c a[3] = ? a[4] = ? a[5] = a[6] = a[7] = a[8] = a[9] = a[10] = a[11] = d a[12] =
a[13] = a[14] = a[15] =
d = d
----------------------
a[0] = a a[1] = b a[2] = c a[3] = ? a[4] = ? a[5] = a[6] = a[7] = a[8] = a[9] = a[10] = a[11] = e a[12] =
a[13] = a[14] = a[15] =
d = e