Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Stack&Heep
Search
Kotokaze
May 31, 2021
Education
0
84
Stack&Heep
スタック・ヒープオーバーラン勉強会の資料です
Kotokaze
May 31, 2021
Tweet
Share
More Decks by Kotokaze
See All by Kotokaze
Introduction to Git & GitHub
kotokaze
0
220
ファイルレスマルウェアの実態と対策
kotokaze
1
450
実は簡単!? AIを攻撃してみよう
kotokaze
0
360
Other Decks in Education
See All in Education
Use Cases and Course Review - Lecture 8 - Human-Computer Interaction (1023841ANR)
signer
PRO
0
1.3k
Library Prefects 2025-2026
cbtlibrary
0
150
Web Application Frameworks - Lecture 3 - Web Technologies (1019888BNR)
signer
PRO
0
3.1k
IKIGAI World Fes:program
tsutsumi
1
2.6k
東大1年生にJulia教えてみた
matsui_528
5
11k
1125
cbtlibrary
0
140
1202
cbtlibrary
0
140
1014
cbtlibrary
0
500
3Dプリンタでロボット作るよ#5_ロボット向け3Dプリンタ材料
shiba_8ro
0
130
Requirements Analysis and Prototyping - Lecture 3 - Human-Computer Interaction (1023841ANR)
signer
PRO
0
1.3k
Introduction - Lecture 1 - Human-Computer Interaction (1023841ANR)
signer
PRO
0
2.7k
Security, Privacy and Trust - Lecture 11 - Web Technologies (1019888BNR)
signer
PRO
0
3.1k
Featured
See All Featured
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.2k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.3k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
22k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.8k
A better future with KSS
kneath
240
18k
Raft: Consensus for Rubyists
vanstee
141
7.2k
Mobile First: as difficult as doing things right
swwweet
225
10k
Code Reviewing Like a Champion
maltzj
527
40k
Documentation Writing (for coders)
carmenintech
76
5.2k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.6k
Testing 201, or: Great Expectations
jmmastey
46
7.8k
Transcript
スタックとヒープ Kotokaze
1. メモリとは データの⼀時置き場 ( ⼤きな配列のようなもの) のこと プロセス実⾏時には、プログラムがメモリ上に展開される → 32bit コンピュータであれば、232
分だけ確保* される +----------------------------------------------------- ---------------+ | 0x00000000 | 0x00000001 | 0x00000002 | 0x00000003 | ≈≈≈≈≈ | 0xFFFFFFFF | +----------------------------------------------------- ---------------+ * ほとんどの場合では、必要とする分だけ OS が⽤意してくれる 2
environ argv share bss data text 0 番地 232 番地
heep stack 2. プロセスとメモリ 名称 主な配置データ 備考 text 機械語 Read Only data グローバル変数 初期値有り bss グローバル変数 初期値無し heep 動的なデータ malloc など stack ⾃動変数/ アドレス ⾃動的に拡張 3
data data 3. スタック 積み⽊ように、積み上げてデータ保存 アクセスできるのは最上位のみ スタック領域では、ローカル変数・ リターンアドレスなどを保存 命令語 内容
PUSH スタックの先頭に追加 POP スタック先頭の値を取り出す 4
使⽤中 data1 data0 使⽤中 使⽤中 使⽤中 4. ヒープ 使う分だけ確保して使う 未整列
( 空域に⾃動配置) → 場所指定でアクセス可 ⾃由度は⾼いが、 脆弱になりやすい int* data0 = new int[sizeof(buff)]; int* data1 = malloc(sizeof(buff)); 5
5. バッファ・オーバーラン (CWE-119) 実⾏中プロセスのメモリ内における、 意図したバッファ外の値を読み書きすること C / C++ で発⽣しやすい 名称
対象 スタック破壊攻撃 ローカル( ⾃動) 変数・リターンアドレス ヒープ破壊攻撃 配置したデータ 6
6. プログラム解析 サンプルコードの解析をしてみよう! サンプルコード: Kotokaze/stack-study 7
6. プログラム解析 (2) Key1: Stack のコンストラクタで、 data0 / data1 を初期化
→ ヒープ領域に連続して* 配置されていると考えられる +-----------------------------------------------------------------------+ | data0 | data1 | ≈≈≈≈≈ | +-----------------------------------------------------------------------+ * 複数の初期化を同時に⾏うと、連続して配置される場合が多い 8
6. プログラム解析 (3) Key2: コンストラクタに注⽬ for (int i = 0;
i <= this->tail; i++) this->data1[i] = 3; 名称 \ 位置 0 1 2 3 4 5 6 7 data0 - - - - - - - - data1 3 3 3 3 3 3 3 3 9
6. プログラム解析 (4) Key3: 値の変化に注⽬ → main のループで順に代⼊していくと... for (int
i = 0; i < num; i++) stk.push(i + 1); 名称 \ 位置 0 1 2 3 4 5 6 7 data0 1 2 3 - - - - - data1 3 3 3 3 3 3 3 3 10
7. 動かしてみる data0 に 12 回の値を⼊れ続けるとどうなる??? → README を参考に動かしてみよう ※
脆弱性を含んでいる事に注意しましょう 11
参考情報 https://jvndb.jvn.jp/ja/cwe/CWE-119.html https://www.ipa.go.jp/security/awareness/vendor/programmingv2/ contents/c901.html 12