Skip to content

Instantly share code, notes, and snippets.

@segfo
Created June 21, 2017 14:17
Show Gist options
  • Save segfo/060bfb20116fd1c01bb318226378ee87 to your computer and use it in GitHub Desktop.
Save segfo/060bfb20116fd1c01bb318226378ee87 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
typedef struct testVtbl{
int hoihoi[10];
void (*start)(void);
void (*end)(void);
}TestVTbl;
void start(void){
printf("default Start\n");
}
void end(void){
printf("default End\n");
}
void initTestVtbl(struct testVtbl *t){
t->start=start;
t->end=end;
}
void evil(){
printf("evil.\n");
}
int main(){
TestVTbl *t1 = malloc(sizeof(TestVTbl));
TestVTbl *t = malloc(sizeof(TestVTbl));
initTestVtbl(t);
// 通常のコード実行
t->end();
free(t);
free(t1);
// Use-after-Free!
t->start();
// mallocで適当な領域を取る、開放するみたいな
// 普通のオブジェクト指向のプログラムに
// よくありがちなシチュエーションを
// 以下のループで再現する。
void *p[100];
for(int i=0;i<100;i++){
p[i] = malloc(sizeof(TestVTbl));
}
t->end();
// こっちのループも上と同じだけど、
// 適当なデータを入れられて使われている
for(int j=0;j<100;j++){
for(int i=0;i<100;i++){
free(p[i]);
p[i] = malloc(sizeof(TestVTbl));
((TestVTbl*)p[i])->end=evil;
}
}
// 上書きされたデータによる任意コード実行
t->end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment