text
stringlengths
0
1.99k
Since the filter_rule structs and associated strings are deallocated left
to right, and the tcache has a LIFO structure, the second filter_rule
chunk becomes the top of the 40-byte tcache bucket. Since only one 200
byte chunk is allocated and deallocated, it also becomes the top of its
tcache bucket. As a result, the next 200 byte and 40 byte allocations will
be placed in these slots.
We can cause this exact order of allocations to occur. In receive_sums(),
the struct sum_struct is allocated first, which consumes the top of the 40
byte tcache bucket. An allocation of 200 bytes occurs if s->count is 5:
struct sum_struct *s = new(struct sum_struct);
int lull_mod = protocol_version >= 31 ? 0 : allowed_lull * 5;
OFF_T offset = 0;
int32 i;
read_sum_head(f, s);
s->sums = NULL;
// ...
if (s->count == 0)
return(s);
s->sums = new_array(struct sum_buf, s->count);
As a result, we can achieve the desired heap layout, as depicted below:
+-----------------+------------------------------+----------------+------+
| 40 byte tcache | struct sum_buf array | struct | 8 |
| entry | (200 bytes) | sum_struct | byte |
| | | (40 bytes) | entry|
+-----------------+------------------------------+----------------+------+
With the heap layout achieved, the only thing left to finish the exploit
is to craft the final payload.
-- 2.5.0 - Achieving RIP control and RCE
With the Write-What-Where primitive achieved, we then had to look for a
way to achieve Remote-Code-Execution.
The Rsync codebase heavily relies on global variables to manage its state.
This state is unique to each connection because the Rsync daemon calls
fork() to handle a every connection. As a result of Rsync's reliance on
globals, we knew that gaining an Arbitrary-Write primitive would most
likely yield Remote-Code-Execution.
We found a global variable, ctx_evp that was a pointer to an OpenSSL
structure stored on the heap:
EVP_MD_CTX *ctx_evp = NULL;
This structure is used by Rsync to calculate digests for chunks in a file.
It gets allocated once and initialized for every new hash it produces:
if (emd && !(nni->flags & NNI_EVP_OK)) {
if (!ctx_evp && !(ctx_evp = EVP_MD_CTX_create()))
out_of_memory("csum_evp_md");
if (EVP_DigestInit_ex(ctx_evp, emd, NULL) == 0)
...
}
Under the hood, OpenSSL calls a function pointer within
EVP_DigestInit_ex() when certain fields are set. Of particular interest
are the following lines of code within OpenSSL:
if (ctx->algctx != NULL) {
if (ctx->digest != NULL && ctx->digest->freectx != NULL)
ctx->digest->freectx(ctx->algctx);
...
}
On the last line of the snippet above, the ctx->digest->freectx() function
pointer is called with a pointer to another object storedin ctx as its
argument, giving us full control of the first argument (the rsi register).
With this gadget, we could build a ROP chain with this gadget—or, if
system() or a similar function is available, simply call that instead.
It turns out, Rsync has a shell_exec() function that takes one argument to
a string, which is executed as a shell command:
int shell_exec(const char *cmd)
{
char *shell = getenv("RSYNC_SHELL");
int status;
pid_t pid;
if (!shell)
return system(cmd);
if ((pid = fork()) < 0)
return -1;
if (pid == 0) {
execlp(shell, shell, "-c", cmd, NULL);
...
}