text
stringlengths
0
1.99k
The _dispatch() function is called several times, once for each phase, and
"dispatches" commands to the appropriate modules. The same happens for
logging phases: PRE_LOG, LOG, and POST_LOG.
The phases are processed by pr_module_call() in main.c:360 as function
pointers:
360 mr = pr_module_call(c->m, c->handler, cmd);
Each time a user connects to the daemon, it fork()s, and the child PID is
added to the child_list structure, and child_listlen is incremented. There
is a pipe between both parent and child, but it is closed as soon as
the fork happens.
ProFTPd uses its own internal memory allocator (please read previous
references for more details about it). The pool structure is defined as
follows:
struct pool_rec {
union block_hdr *first;
union block_hdr *last;
struct cleanup *cleanups;
struct pool_rec *sub_pools;
struct pool_rec *sub_next;
struct pool_rec *sub_prev;
struct pool_rec *parent;
char *free_first_avail;
const char *tag;
};
The definition of block_hdr union is as follows:
union align {
char *cp;
void (*f)(void);
long l;
FILE *fp;
double d;
};
union block_hdr {
union align a;
char pad[32]; /* Padding and aligning */
struct {
void *endp;
union block_hdr *next;
void *first_avail;
} h;
};
NOTE: Some comments and #defines were removed for simplicity.
ProFTPd allocates several pools for different purposes, but we'll focus on
resp_pool since it's the one that is corrupted. The resp_pool used to store
responses that are sent to the user (for example error messages).
The memory allocations happens via alloc_pool(), however alloc_pool() is
not called directly. There are wrappers to take care of some parameters:
616 void *pcalloc(struct pool_rec *p, size_t sz) {
617 void *res;
618
619 res = palloc(p, sz);
620 memset(res, '\0', sz);
621
622 return res;
623 }
As you can see, pcalloc() is a wrapper for palloc(), which is defined as:
608 void *palloc(struct pool_rec *p, size_t sz) {
609 return alloc_pool(p, sz, FALSE);
610 }
Finally let's see alloc_pool():
558 static void *alloc_pool(struct pool_rec *p, size_t reqsz, int exact) {
559 /* Round up requested size to an even number of aligned units */
560 size_t nclicks = 1 + ((reqsz - 1) / CLICK_SZ);
561 size_t sz = nclicks * CLICK_SZ;
562 union block_hdr *blok;
563 char *first_avail, *new_first_avail;
564
565 /* For performance, see if space is available in the most recently
566 * allocated block.
567 */
568
569 blok = p->last;
570 if (blok == NULL) {
571 errno = EINVAL;
572 return NULL;
573 }
574
575 first_avail = blok->h.first_avail;
576
577 if (reqsz == 0) {
578 /* Don't try to allocate memory of zero length.
579 *
580 * This should NOT happen normally; if it does, by returning NULL we
581 * almost guarantee a null pointer dereference.