text
stringlengths
0
1.99k
582 */
583 errno = EINVAL;
584 return NULL;
585 }
586
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
594 /* Need a new one that's big enough */
595 pr_alarms_block();
596
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;
603
604 pr_alarms_unblock();
605 return (void *) first_avail;
606 }
ProFTPd uses both palloc() and pcalloc(), but when it needs a zeroed out
buffer, it prefers pcalloc() over palloc().
We'll see later that we control the p->last value. To keep control of the
pool blocks, we always need to return on line 591. If a new block is
retrieved, we lose control because another memory region will be returned
and overwrite the value we previously controlled.
There is also an important function called make_sub_pool(), which is mostly
called when ProFTPd needs a temporary pool. Its definition is as follows:
415 struct pool_rec *make_sub_pool(struct pool_rec *p) {
416 union block_hdr *blok;
417 pool *new_pool;
418
419 pr_alarms_block();
420
421 blok = new_block(0, FALSE);
422
423 new_pool = (pool *) blok->h.first_avail;
424 blok->h.first_avail = POOL_HDR_BYTES + (char *) blok->h.first_avail;
425
426 memset(new_pool, 0, sizeof(struct pool_rec));
427 new_pool->free_first_avail = blok->h.first_avail;
428 new_pool->first = new_pool->last = blok;
429
430 if (p) {
431 new_pool->parent = p;
432 new_pool->sub_next = p->sub_pools;
433
434 if (new_pool->sub_next)
435 new_pool->sub_next->sub_prev = new_pool;
436
437 p->sub_pools = new_pool;
438 }
439
440 pr_alarms_unblock();
441
442 return new_pool;
443 }
Basically, it retrieves a new block from block_freelist and inserts a new
pool in pool *p, updating its next, previous, and parent pointers. We'll
see later that we control the members of p. Also, some operations involve
its members. Note that a pointer to p will be stored in new_pool->parent;
this is a pointer to data that we control.
Push this information into your mind for now - we will need to pop it later.
We won't be interested in new_block(), but there's an explanation about it
in pool.c:184:
Get a new block, from the free list if possible, otherwise malloc a new
one. minsz is the requested size of the block to be allocated.
If exact is TRUE, then minsz is the exact size of the allocated block;
otherwise, the allocated size will be rounded up from minsz to the
nearest multiple of BLOCK_MINFREE.
Now that we understand some basic ProFTPd memory allocator internals, let's
dig into how to trigger the vulnerability.
--[ 5 - Vulnerability analysis
As we saw earlier, although this vulnerability could also be triggered
during the process of downloading a file from the server, the attacker
would hardly have control over the payload. So I think the best approach
is to use the upload functionality and trigger the flaw by shutting down
the FTP control connection while the upload data transfer is happening.
Triggering this condition is easy and can be forced by the attacker.
Now, start ProFTPd in a separate shell with some debugging options:
$ cd proftpd-1.3.7rc2/
$ sudo proftpd -d7 -n -c sample-configurations/basic.conf