Skip to content

Commit

Permalink
Make UAF POI consider uses of all allocation subregions
Browse files Browse the repository at this point in the history
Fixes #52.
  • Loading branch information
thinkmoore committed Sep 2, 2022
1 parent 4ce8832 commit 14c0516
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 6 deletions.
6 changes: 3 additions & 3 deletions frontend/mate/mate/poi/analysis/UseAfterFree.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def compute_use_after_free(
)
.join(
AliasEdge,
(AliasEdge.source == BaseAllocation.uuid) & (AliasEdge.kind == EdgeKind.MAY_ALIAS),
(AliasEdge.source == BaseAllocation.uuid) & (AliasEdge.kind.in_([EdgeKind.MAY_ALIAS, EdgeKind.CONTAINS])),
)
.join(
LoadEdge,
Expand Down Expand Up @@ -311,7 +311,7 @@ def compute_use_after_free(
)
.join(
AliasEdge,
(AliasEdge.source == BaseAllocation.uuid) & (AliasEdge.kind == EdgeKind.MAY_ALIAS),
(AliasEdge.source == BaseAllocation.uuid) & (AliasEdge.kind.in_([EdgeKind.MAY_ALIAS, EdgeKind.CONTAINS])),
)
.join(
StoreEdge,
Expand Down Expand Up @@ -356,7 +356,7 @@ def compute_use_after_free(
)
.join(
AliasEdge,
(AliasEdge.source == BaseAllocation.uuid) & (AliasEdge.kind == EdgeKind.MAY_ALIAS),
(AliasEdge.source == BaseAllocation.uuid) & (AliasEdge.kind.in_([EdgeKind.MAY_ALIAS, EdgeKind.CONTAINS])),
)
.join(
DataflowEdge,
Expand Down
98 changes: 98 additions & 0 deletions frontend/test/programs/ntu-uaf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
MIT License
Copyright (c) 2019 yuawn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>



void init(){
setvbuf(stdout,0,2,0);
setvbuf(stdin,0,2,0);
setvbuf(stderr,0,2,0);
}

int read_int(){
char buf[0x10];
__read_chk( 0 , buf , 0xf , 0x10 );
return atoi( buf );
}

void welcome_func(){
puts( "Hello ~~~" );
}

void bye_func(){
puts( "Bye ~~~" );
}

void menu(){
puts( "1. add a box" );
puts( "2. exit" );
puts( ">" );
}

struct MessageBox{
void (*welcome)();
void (*bye)();
};

void backdoor(){
system("sh");
}

int main(){

init();

struct MessageBox* msgbox = (struct MessageBox*) malloc( sizeof( struct MessageBox ) );

msgbox->welcome = welcome_func;
msgbox->bye = bye_func;

msgbox->welcome();
free( msgbox );

int n = 3, size;
char *msg;

while( n-- ){
printf( "Size of your message: " );
size = read_int();

msg = (char*) malloc( size );

printf( "Message: " );
read( 0 , msg , size );

printf( "Saved message: %s\n" , msg );

free( msg );
}

msgbox->bye();

return 0;
}
20 changes: 17 additions & 3 deletions tests/postgres/poi_analysis/use_after_free_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_poi_use_after_free_read(cpg_db_v2):
for (free, usage, _, _) in compute_use_after_free(cpg.session, cpg)
}

expected = {(20, 24)}
expected = {(20, 24), (20, 25)}
assert results == expected


Expand All @@ -27,7 +27,7 @@ def test_poi_use_after_free_write(cpg_db_v2):
for (free, usage, _, _) in compute_use_after_free(cpg.session, cpg)
}

expected = {(23, 27)}
expected = {(23, 27), (23, 28)}
assert results == expected


Expand All @@ -42,5 +42,19 @@ def test_poi_use_after_free_cpp(cpg_db_v2):
for (free, usage, _, _) in compute_use_after_free(cpg.session, cpg)
}

expected = {(17, 21)}
expected = {(17, 21), (17,22)}
assert results == expected

def test_poi_use_after_free_ntu(cpg_db_v2):
cpg = cpg_db_v2("ntu-uaf.c")

results = {
(
free.location["line"],
usage.location["line"],
)
for (free, usage, _, _) in compute_use_after_free(cpg.session, cpg)
}

expected = {(76, 95)}
assert results == expected

0 comments on commit 14c0516

Please sign in to comment.