Skip to content

Commit f284a0b

Browse files
authored
Merge pull request #92 from struct/update_uaf_test
update the use after free test for when UAF_PTR_PAGE
2 parents dd1f0bb + cce83e7 commit f284a0b

3 files changed

Lines changed: 25 additions & 22 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ tests: clean library_debug_unit_tests
259259
@echo "make library_debug_unit_tests"
260260
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) tests/tagged_ptr_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/tagged_ptr_test $(LDFLAGS)
261261
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) tests/tests.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/tests $(LDFLAGS)
262-
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) tests/uaf.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/uaf
262+
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) tests/uaf.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/uaf $(LDFLAGS)
263263
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) tests/interfaces_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/interfaces_test $(LDFLAGS)
264264
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) tests/thread_tests.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/thread_tests $(LDFLAGS)
265265
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(UNIT_TESTING) tests/big_canary_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/big_canary_test $(LDFLAGS)

src/iso_alloc_search.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,29 @@
77
* value and return it or overwrite the first potentially
88
* dangling pointer with the address of an unmapped page */
99
INTERNAL_HIDDEN void *_iso_alloc_ptr_search(void *n, bool poison) {
10-
uint8_t *h = NULL;
10+
uint8_t *search = NULL;
11+
uint8_t *end = NULL;
1112

1213
for(int32_t i = 0; i < _root->zones_used; i++) {
1314
iso_alloc_zone_t *zone = &_root->zones[i];
1415

15-
UNMASK_ZONE_PTRS(zone);
16-
h = zone->user_pages_start;
16+
search = UNMASK_USER_PTR(zone);
17+
end = search + ZONE_USER_SIZE;
1718

18-
while(h <= (uint8_t *) (zone->user_pages_start + ZONE_USER_SIZE - sizeof(uint64_t))) {
19-
if(LIKELY((uint64_t) * (uint64_t *) h != (uint64_t) n)) {
20-
h++;
19+
while(search <= (uint8_t *) (end - sizeof(uint64_t))) {
20+
if(LIKELY((uint64_t) * (uint64_t *) search != (uint64_t) n)) {
21+
search++;
2122
} else {
2223
if(poison == false) {
23-
MASK_ZONE_PTRS(zone);
24-
return h;
24+
return search;
2525
} else {
2626
#if UAF_PTR_PAGE
27-
*(uint64_t *) h = UAF_PTR_PAGE_ADDR;
28-
MASK_ZONE_PTRS(zone);
29-
return h;
27+
*(uint64_t *) search = UAF_PTR_PAGE_ADDR;
28+
return search;
3029
#endif
3130
}
3231
}
3332
}
34-
35-
MASK_ZONE_PTRS(zone);
3633
}
3734

3835
return NULL;

tests/uaf.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
#include "iso_alloc.h"
55
#include "iso_alloc_internal.h"
66

7-
#if defined(UAF_PTR_PAGE) && !defined(ALLOC_SANITY)
8-
/* This test should be run manually. You need to enable UAF_PTR_PAGE
9-
* and then disable the sampling logic in iso_alloc. */
7+
#if UAF_PTR_PAGE && !ALLOC_SANITY
8+
/* This test should be run manually after enabling UAF_PTR_PAGE
9+
* and disabling the sampling mechanism before the call to
10+
* _iso_alloc_ptr_search in _iso_free_internal_unlocked */
1011
typedef struct test {
1112
char *str;
1213
} test_t;
@@ -15,14 +16,19 @@ int main(int argc, char *argv[]) {
1516
void *str = iso_alloc(32);
1617
test_t *test = (test_t *) iso_alloc(1024);
1718
test->str = str;
18-
memcpy(str, "a string!", 9);
19-
iso_free(str);
19+
20+
const char *s = "a string!";
21+
memcpy(str, s, strlen(s));
22+
23+
/* We free the chunk permanently because
24+
* it bypasses the quarantine */
25+
iso_free_permanently(str);
2026

2127
/* Dereference a pointer that should have been
2228
* detected and overwritten with UAF_PTR_PAGE */
23-
LOG("Attempting to dereference test->str.\nWe should fault on %x", UAF_PTR_PAGE_ADDR);
24-
LOG("%s", test->str);
25-
iso_free(test);
29+
fprintf(stdout, "Dereferencing test->str @ %p. Fault address will be %lx\n", test->str, UAF_PTR_PAGE_ADDR);
30+
fprintf(stdout, "[%s]\n", test->str);
31+
iso_free_permanently(test);
2632

2733
return OK;
2834
}

0 commit comments

Comments
 (0)